Skip to content

Commit 3193b7d

Browse files
committed
Update converter for converting camel case string to under score string.
1 parent 9056d1b commit 3193b7d

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

STNetTaskQueue/STHTTPNetTaskParametersPacker.m

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,35 @@ - (NSString *)parameterNameOfProperty:(NSString *)propertyName withSeparator:(NS
9898
return propertyName;
9999
}
100100

101-
static NSRegularExpression *parameterNameRegex;
102-
static dispatch_once_t onceToken;
103-
dispatch_once(&onceToken, ^{
104-
parameterNameRegex = [NSRegularExpression regularExpressionWithPattern:@"([a-z])([A-Z])" options:kNilOptions error:NULL];
105-
});
101+
NSMutableString *parameterName = [NSMutableString new];
102+
const char *chars = propertyName.UTF8String;
103+
for (NSUInteger i = 0; i < propertyName.length; i++) {
104+
BOOL hasPrevious = i != 0;
105+
BOOL hasNext = i + 1 < propertyName.length;
106+
BOOL prependUnderscore = NO;
107+
char ch = chars[i];
108+
if (isupper(ch)) {
109+
if (hasPrevious) {
110+
if (!isupper(chars[i - 1])) {
111+
prependUnderscore = YES;
112+
}
113+
}
114+
if(hasNext && !prependUnderscore) {
115+
if (!isupper(chars[i + 1])) {
116+
prependUnderscore = YES;
117+
}
118+
}
119+
ch = tolower(ch);
120+
}
121+
if (prependUnderscore) {
122+
[parameterName appendFormat:@"_%c", ch];
123+
}
124+
else {
125+
[parameterName appendFormat:@"%c", ch];
126+
}
127+
}
106128

107-
NSString *parameterName = [parameterNameRegex stringByReplacingMatchesInString:propertyName options:kNilOptions range:NSMakeRange(0, propertyName.length) withTemplate:[NSString stringWithFormat:@"$1%@$2", separator]];
108-
parameterName = parameterName.lowercaseString;
109-
return parameterName;
129+
return [NSString stringWithString:parameterName];
110130
}
111131

112132
@end

0 commit comments

Comments
 (0)