-
Notifications
You must be signed in to change notification settings - Fork 9
/
NSString+JXCodePointEnumeration.m
167 lines (124 loc) · 4.71 KB
/
NSString+JXCodePointEnumeration.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//
// NSString+JXCodePointEnumeration.m
// JXNumberStringProcessing
//
// Created by Jan on 07.06.14.
// Copyright (c) 2014-2015 Jan Weiß. All rights reserved.
//
// MIT License. See NSString+JXCodePointEnumeration.h for details.
#import "NSString+JXCodePointEnumeration.h"
#if 0
NS_INLINE NSRange NSMakeRangeFromCFRange(CFRange cfr) {
return NSMakeRange(cfr.location == kCFNotFound ? NSNotFound : cfr.location, cfr.length);
}
#endif
NS_INLINE CFRange CFRangeMakeFromNSRange(NSRange range) {
return CFRangeMake((range.location == NSNotFound ? kCFNotFound : range.location), range.length);
}
@implementation NSString (JXCodePointEnumeration)
- (void)enumerateCodePointsWithOptionsJX:(JXCodePointEnumerationOptions)opts
usingBlock:(void (^)(UTF32Char codePoint, NSRange range, BOOL *stop))block;
{
NSRange fullRange = NSMakeRange(0, self.length);
[self enumerateCodePointsInRange:fullRange
optionsJX:opts
usingBlock:block];
}
- (void)enumerateCodePointsInRange:(NSRange)range
optionsJX:(JXCodePointEnumerationOptions)opts
usingBlock:(void (^)(UTF32Char codePoint, NSRange range, BOOL *stop))block;
{
if (range.length == 0) return;
if (self.length == 0) return;
CFStringRef string = (__bridge CFStringRef)(self);
const CFRange subRange = CFRangeMakeFromNSRange(range);
const bool wantRange = !(opts & JXCodePointEnumerationOptionsRangeNotRequired);
const NSRange dummyRange = NSMakeRange(NSNotFound, 0);
const bool forwardEnumeration = !(opts & JXCodePointEnumerationReverse);
CFStringInlineBuffer stringInlineBuffer;
CFStringInitInlineBuffer(string, &stringInlineBuffer, subRange);
CFIndex i; // Index relative to subRange.
NSRange currentRange = dummyRange;
if (forwardEnumeration) {
for (i = 0;
i < subRange.length;
i++) {
BOOL stop = NO;
const UniChar char1 = CFStringGetCharacterFromInlineBuffer(&stringInlineBuffer, i);
UniChar char2 = 0;
const Boolean isPair = CFStringIsSurrogateHighCharacter(char1);
if (isPair) {
const CFIndex char2Index = i + 1;
NSAssert(char2Index < subRange.length, @"The range must neither start nor end withing a surrogate pair.");
char2 = CFStringGetCharacterFromInlineBuffer(&stringInlineBuffer, char2Index);
NSAssert(CFStringIsSurrogateLowCharacter(char2), @"The string must be valid Unicode.");
const UTF32Char codePoint = CFStringGetLongCharacterForSurrogatePair(char1, char2);
if (wantRange) {
currentRange.location = subRange.location + i;
currentRange.length = 2;
}
block(codePoint, currentRange, &stop);
i++; // Continue, stepping an additional time, so we don’t process the UTF-16 low character again on the next loop.
}
else {
if (wantRange) {
currentRange.location = subRange.location + i;
currentRange.length = 1;
}
block((UTF32Char)char1, currentRange, &stop);
}
if (stop) break;
}
}
else {
CFIndex last = subRange.length - 1;
for (i = last;
YES;
--i) {
BOOL stop = NO;
UniChar char0 = 0;
const UniChar char1 = CFStringGetCharacterFromInlineBuffer(&stringInlineBuffer, i);
const Boolean isPair = CFStringIsSurrogateLowCharacter(char1);
if (isPair) {
i -= 1; // The current range actually starts one code unit earlier.
const CFIndex char0Index = i;
NSAssert(char0Index >= 0, @"The range must neither start nor end withing a surrogate pair.");
char0 = CFStringGetCharacterFromInlineBuffer(&stringInlineBuffer, char0Index);
NSAssert(CFStringIsSurrogateHighCharacter(char0), @"The string must be valid Unicode.");
const UTF32Char codePoint = CFStringGetLongCharacterForSurrogatePair(char0, char1);
if (wantRange) {
currentRange.location = subRange.location + i;
currentRange.length = 2;
}
block(codePoint, currentRange, &stop);
}
else {
if (wantRange) {
currentRange.location = subRange.location + i;
currentRange.length = 1;
}
block((UTF32Char)char1, currentRange, &stop);
}
if ((i == 0) || stop) break;
}
}
}
- (NSUInteger)countOccurancesOfCharactersInSetJX:(NSCharacterSet *)characterSet;
{
if (self.length == 0) return 0;
CFCharacterSetRef characterSetCF = (__bridge CFCharacterSetRef)characterSet;
__block NSUInteger count = 0;
[self enumerateCodePointsWithOptionsJX:JXCodePointEnumerationOptionsRangeNotRequired
usingBlock:
^(UTF32Char codePoint, NSRange range, BOOL *stop) {
if (CFCharacterSetIsLongCharacterMember(characterSetCF, codePoint)) {
count++;
}
}];
return count;
}
- (NSUInteger)countOccurancesOfDecimalDigitsJX;
{
return [self countOccurancesOfCharactersInSetJX:[NSCharacterSet decimalDigitCharacterSet]];
}
@end