forked from nolanw/HTMLReader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHTMLPreprocessedInputStream.m
184 lines (168 loc) · 4.96 KB
/
HTMLPreprocessedInputStream.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// HTMLPreprocessedInputStream.m
//
// Public domain. https://github.com/nolanw/HTMLReader
#import "HTMLPreprocessedInputStream.h"
#import "HTMLString.h"
@implementation HTMLPreprocessedInputStream
{
NSUInteger _scanLocation;
CFStringInlineBuffer _buffer;
BOOL _reconsume;
UTF32Char _currentInputCharacter;
}
- (instancetype)initWithString:(NSString *)string
{
if ((self = [super init])) {
_string = [string copy];
CFStringInitInlineBuffer((__bridge CFStringRef)_string, &_buffer, CFRangeMake(0, _string.length));
}
return self;
}
- (instancetype)init
{
return [self initWithString:@""];
}
- (BOOL)consumeString:(NSString *)string matchingCase:(BOOL)caseSensitive
{
NSScanner *scanner = [self unprocessedScanner];
scanner.caseSensitive = caseSensitive;
BOOL ok = [scanner scanString:string intoString:nil];
if (ok) {
_scanLocation = scanner.scanLocation;
}
return ok;
}
- (NSString *)consumeCharactersUpToFirstPassingTest:(BOOL(^)(UTF32Char character))test
{
NSMutableString *consumed = [NSMutableString new];
for (;;) {
UTF32Char c = [self consumeNextInputCharacter];
if (c == (UTF32Char)EOF) break;
if (test(c)) {
[self reconsumeCurrentInputCharacter];
break;
}
AppendLongCharacter(consumed, c);
}
if (consumed.length > 0) {
return consumed;
} else {
return nil;
}
}
- (BOOL)consumeHexInt:(out unsigned int *)number
{
// NSScanner's -scanHexInt: allows for a leading "0x" or "0X", while the HTML spec does not.
NSCharacterSet *hexSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789ABCDEFabcdef"];
BOOL justHexDigits = [[self unprocessedScanner] scanCharactersFromSet:hexSet intoString:nil];
if (!justHexDigits) return NO;
NSScanner *scanner = [self unprocessedScanner];
BOOL ok = [scanner scanHexInt:number];
if (ok) {
_scanLocation = scanner.scanLocation;
}
return ok;
}
- (BOOL)consumeUnsignedInt:(out unsigned int *)outNumber
{
NSScanner *scanner = [self unprocessedScanner];
long long number;
BOOL ok = [scanner scanLongLong:&number];
if (!ok || number < 0) return NO;
_scanLocation = scanner.scanLocation;
if (outNumber) {
if (number > (long long)UINT_MAX) {
*outNumber = UINT_MAX;
} else {
*outNumber = (unsigned int)number;
}
}
return ok;
}
- (NSScanner *)unprocessedScanner
{
NSScanner *scanner = [NSScanner scannerWithString:_string];
scanner.charactersToBeSkipped = nil;
scanner.scanLocation = _scanLocation;
return scanner;
}
- (UTF32Char)nextInputCharacter
{
return [self nextInputCharacterAndConsume:NO];
}
- (UTF32Char)consumeNextInputCharacter
{
return [self nextInputCharacterAndConsume:YES];
}
- (UTF32Char)nextInputCharacterAndConsume:(BOOL)consume
{
if (_reconsume) {
if (consume) {
_reconsume = NO;
}
return _currentInputCharacter;
}
NSUInteger advance = 0;
UTF32Char c = CFStringGetCharacterFromInlineBuffer(&_buffer, _scanLocation + advance);
if (c == 0 && _scanLocation + advance >= _string.length) {
c = EOF;
} else {
advance++;
}
if (CFStringIsSurrogateHighCharacter(c)) {
unichar low = CFStringGetCharacterFromInlineBuffer(&_buffer, _scanLocation + advance);
if (CFStringIsSurrogateLowCharacter(low)) {
advance++;
unichar high = c;
c = CFStringGetLongCharacterForSurrogatePair(high, low);
} else {
if (self.errorBlock) {
self.errorBlock(@"Isolated lead surrogate");
}
}
} else if (CFStringIsSurrogateLowCharacter(c)) {
if (self.errorBlock) {
self.errorBlock(@"Isloated trail surrogate");
}
} else if (c == '\r') {
c = '\n';
if (CFStringGetCharacterFromInlineBuffer(&_buffer, _scanLocation + advance) == '\n') {
advance++;
}
}
if (is_undefined_or_disallowed(c)) {
if (self.errorBlock) {
self.errorBlock(@"Noncharacter or disallowed control character");
}
}
if (consume) {
_scanLocation += advance;
_currentInputCharacter = c;
}
return c;
}
- (NSString *)nextUnprocessedCharactersWithMaximumLength:(NSUInteger)length
{
NSRange range = NSMakeRange(_scanLocation, length);
if (NSMaxRange(range) > _string.length) {
range.length = _string.length - range.location;
}
if (range.length > 0) {
return [_string substringWithRange:range];
} else {
return nil;
}
}
- (void)reconsumeCurrentInputCharacter
{
_reconsume = YES;
}
- (void)unconsumeInputCharacters:(NSUInteger)numberOfCharactersToUnconsume
{
// TODO skip over ignored carriage returns
// TODO skip over surrogate second halves
// TODO bounds checking
// TODO consider reconsume
_scanLocation -= numberOfCharactersToUnconsume;
}
@end