mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-22 11:46:05 +08:00
Finished split into more logical components. Library now copies Headers to Build/RestKit and you can selectively link against the components your app needs. libRestKit.a contains everything from the core library.
This commit is contained in:
98
Code/Support/NSString+InflectionSupport.m
Normal file
98
Code/Support/NSString+InflectionSupport.m
Normal file
@@ -0,0 +1,98 @@
|
||||
//
|
||||
// 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
|
||||
Reference in New Issue
Block a user