mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-01-13 09:30:46 +08:00
99 lines
2.5 KiB
Objective-C
99 lines
2.5 KiB
Objective-C
//
|
|
// NSString+InflectionSupport.m
|
|
//
|
|
//
|
|
// Created by Ryan Daigle on 7/31/08.
|
|
// Copyright 2008 yFactorial, LLC. All rights reserved.
|
|
//
|
|
|
|
#import "NSString+InflectionSupport.h"
|
|
|
|
@implementation NSString (InflectionSupport)
|
|
|
|
- (NSCharacterSet *)capitals {
|
|
return [NSCharacterSet uppercaseLetterCharacterSet];
|
|
}
|
|
|
|
- (NSString *)deCamelizeWith:(NSString *)delimiter {
|
|
|
|
unichar *buffer = calloc([self length], sizeof(unichar));
|
|
[self getCharacters:buffer ];
|
|
NSMutableString *underscored = [NSMutableString string];
|
|
|
|
NSString *currChar;
|
|
for (int i = 0; i < [self length]; i++) {
|
|
currChar = [NSString stringWithCharacters:buffer+i length:1];
|
|
if([[self capitals] characterIsMember:buffer[i]]) {
|
|
if (0 != i) {
|
|
[underscored appendFormat:@"%@%@", delimiter, [currChar lowercaseString]];
|
|
} else {
|
|
[underscored appendFormat:@"%@", [currChar lowercaseString]];
|
|
}
|
|
} else {
|
|
[underscored appendString:currChar];
|
|
}
|
|
}
|
|
|
|
free(buffer);
|
|
return underscored;
|
|
}
|
|
|
|
|
|
- (NSString *)dasherize {
|
|
return [self deCamelizeWith:@"-"];
|
|
}
|
|
|
|
- (NSString *)underscore {
|
|
return [self deCamelizeWith:@"_"];
|
|
}
|
|
|
|
- (NSCharacterSet *)camelcaseDelimiters {
|
|
return [NSCharacterSet characterSetWithCharactersInString:@"-_"];
|
|
}
|
|
|
|
- (NSString *)camelize {
|
|
|
|
unichar *buffer = calloc([self length], sizeof(unichar));
|
|
[self getCharacters:buffer ];
|
|
NSMutableString *underscored = [NSMutableString string];
|
|
|
|
BOOL capitalizeNext = NO;
|
|
NSCharacterSet *delimiters = [self camelcaseDelimiters];
|
|
for (int i = 0; i < [self length]; i++) {
|
|
NSString *currChar = [NSString stringWithCharacters:buffer+i length:1];
|
|
if([delimiters characterIsMember:buffer[i]]) {
|
|
capitalizeNext = YES;
|
|
} else {
|
|
if(capitalizeNext) {
|
|
[underscored appendString:[currChar uppercaseString]];
|
|
capitalizeNext = NO;
|
|
} else {
|
|
[underscored appendString:currChar];
|
|
}
|
|
}
|
|
}
|
|
|
|
free(buffer);
|
|
return underscored;
|
|
|
|
}
|
|
|
|
- (NSString *)titleize {
|
|
NSArray *words = [self componentsSeparatedByString:@" "];
|
|
NSMutableString *output = [NSMutableString string];
|
|
for (NSString *word in words) {
|
|
[output appendString:[[word substringToIndex:1] uppercaseString]];
|
|
[output appendString:[[word substringFromIndex:1] lowercaseString]];
|
|
[output appendString:@" "];
|
|
}
|
|
return [output substringToIndex:[self length]];
|
|
}
|
|
|
|
- (NSString *)toClassName {
|
|
NSString *result = [self camelize];
|
|
return [result stringByReplacingCharactersInRange:NSMakeRange(0,1)
|
|
withString:[[result substringWithRange:NSMakeRange(0,1)] uppercaseString]];
|
|
}
|
|
|
|
@end
|