-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathNSString+InflectionSupport.m
130 lines (99 loc) · 3.21 KB
/
NSString+InflectionSupport.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//
// NSString+InflectionSupport.m
//
//
// Created by Ryan Daigle on 7/31/08.
// Copyright 2008 yFactorial, LLC.
//
#import "NSString+InflectionSupport.h"
@implementation NSString (InflectionSupport)
static NSMutableDictionary *cachedCamelized;
- (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]]) {
[underscored appendFormat:@"%@%@", delimiter, [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*)camelizeCached {
if (cachedCamelized == nil)
cachedCamelized = [[NSMutableDictionary dictionary] retain];
else {
NSString* cached = [cachedCamelized objectForKey:self];
if (cached != nil)
return cached;
}
NSString* camelized = [self camelize];
[cachedCamelized setObject:camelized forKey:self];
return camelized;
}
- (NSString *)titleize {
if([self length] == 0)
return self;
NSArray *words = [self componentsSeparatedByString:@" "];
NSMutableArray *output = [NSMutableArray array];
for(NSString *word in words){
if([word stringIsEmptyOrWhitespace])
continue;
[output addObject:[[word lowercaseString] capitalizedString]];
}
return [output componentsJoinedByString:@" "];
}
- (NSString *)decapitalize {
return [[[self substringToIndex:1] lowercaseString] stringByAppendingString:[self substringFromIndex:1]];
}
- (NSString *)toClassName {
NSString *result = [self camelize];
return [result stringByReplacingCharactersInRange:NSMakeRange(0,1)
withString:[[result substringWithRange:NSMakeRange(0,1)] uppercaseString]];
}
//// From three20, renamed to avoid any issues
///////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)stringIsEmptyOrWhitespace {
return !self.length ||
![self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]].length;
}
@end