-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathStringExtension.mm
More file actions
66 lines (51 loc) · 1.65 KB
/
Copy pathStringExtension.mm
File metadata and controls
66 lines (51 loc) · 1.65 KB
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
#import "StringExtension.h"
@implementation NSString (StringExtension)
- (std::string)toCppString {
return std::string([self UTF8String]);
}
+ (NSString *)fromCppString:(std::string)string {
return [NSString stringWithUTF8String:string.c_str()];
}
+ (NSString *)stringByEscapingHtml:(NSString *)html {
NSMutableString *escaped = [html mutableCopy];
NSDictionary *escapeMap = @{
@"&": @"&",
@"<": @"<",
@">": @">",
};
for(NSString *key in escapeMap) {
[escaped replaceOccurrencesOfString:key withString:escapeMap[key] options:NSLiteralSearch range:NSMakeRange(0, escaped.length)];
}
return escaped;
}
+ (NSDictionary *)getEscapedCharactersInfoFrom:(NSString *)text {
NSDictionary *unescapeMap = @{
@"&": @"&",
@"<": @"<",
@">": @">",
};
NSMutableDictionary *results = [[NSMutableDictionary alloc] init];
for(NSString *key in unescapeMap) {
NSRange searchRange = NSMakeRange(0, text.length);
NSRange foundRange;
while(searchRange.location < text.length) {
foundRange = [text rangeOfString:key options:0 range:searchRange];
if(foundRange.location == NSNotFound) {
break;
}
results[@(foundRange.location)] = @[key, unescapeMap[key]];
searchRange.location = foundRange.location + foundRange.length;
searchRange.length = text.length - searchRange.location;
}
}
return results;
}
@end
@implementation NSMutableString (StringExtension)
- (std::string)toCppString {
return std::string([self UTF8String]);
}
+ (NSMutableString *)fromCppString:(std::string)string {
return [NSMutableString stringWithUTF8String:string.c_str()];
}
@end