forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCFStringScanner.c
244 lines (218 loc) · 10.2 KB
/
CFStringScanner.c
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* CFStringScanner.c
Copyright (c) 1999-2018, Apple Inc. and the Swift project authors
Portions Copyright (c) 2014-2018, Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
Responsibility: Ali Ozer
*/
#include "CFInternal.h"
#include <CoreFoundation/CFString.h>
#include <sys/types.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
CF_INLINE Boolean __CFCharacterIsADigit(UniChar ch) {
return (ch >= '0' && ch <= '9') ? true : false;
}
/* Returns -1 on illegal value */
CF_INLINE SInt32 __CFCharacterNumericOrHexValue (UniChar ch) {
if (ch >= '0' && ch <= '9') {
return ch - '0';
} else if (ch >= 'A' && ch <= 'F') {
return ch + 10 - 'A';
} else if (ch >= 'a' && ch <= 'f') {
return ch + 10 - 'a';
} else {
return -1;
}
}
/* Returns -1 on illegal value */
CF_INLINE SInt32 __CFCharacterNumericValue(UniChar ch) {
return (ch >= '0' && ch <= '9') ? (ch - '0') : -1;
}
CF_INLINE UniChar __CFStringGetFirstNonSpaceCharacterFromInlineBuffer(CFStringInlineBuffer *buf, SInt32 *indexPtr) {
UniChar ch;
while (__CFIsWhitespace(ch = __CFStringGetCharacterFromInlineBufferAux(buf, *indexPtr))) (*indexPtr)++;
return ch;
}
/* result is int64_t or int, depending on doLonglong
*/
CF_PRIVATE Boolean __CFStringScanInteger(CFStringInlineBuffer *buf, CFTypeRef locale, SInt32 *indexPtr, Boolean doLonglong, void *result) {
Boolean doingLonglong = false; /* Set to true if doLonglong, and we overflow an int... */
Boolean neg = false;
int intResult = 0;
register int64_t longlongResult = 0; /* ??? int64_t is slow when not in regs; I hope this does the right thing. */
UniChar ch;
ch = __CFStringGetFirstNonSpaceCharacterFromInlineBuffer(buf, indexPtr);
if (ch == '-' || ch == '+') {
neg = (ch == '-');
(*indexPtr)++;
ch = __CFStringGetFirstNonSpaceCharacterFromInlineBuffer(buf, indexPtr);
}
if (! __CFCharacterIsADigit(ch)) return false; /* No digits, bail out... */
do {
if (doingLonglong) {
if ((longlongResult >= LLONG_MAX / 10) && ((longlongResult > LLONG_MAX / 10) || (__CFCharacterNumericValue(ch) - (neg ? 1 : 0) >= LLONG_MAX - longlongResult * 10))) {
/* ??? This might not handle LLONG_MIN correctly... */
longlongResult = neg ? LLONG_MIN : LLONG_MAX;
neg = false;
while (__CFCharacterIsADigit(ch = __CFStringGetCharacterFromInlineBufferAux(buf, ++(*indexPtr)))); /* Skip remaining digits */
} else {
longlongResult = longlongResult * 10 + __CFCharacterNumericValue(ch);
ch = __CFStringGetCharacterFromInlineBufferAux(buf, ++(*indexPtr));
}
} else {
if ((intResult >= INT_MAX / 10) && ((intResult > INT_MAX / 10) || (__CFCharacterNumericValue(ch) - (neg ? 1 : 0) >= INT_MAX - intResult * 10))) {
// Overflow, check for int64_t...
if (doLonglong) {
longlongResult = intResult;
doingLonglong = true;
} else {
/* ??? This might not handle INT_MIN correctly... */
intResult = neg ? INT_MIN : INT_MAX;
neg = false;
while (__CFCharacterIsADigit(ch = __CFStringGetCharacterFromInlineBufferAux(buf, ++(*indexPtr)))); /* Skip remaining digits */
}
} else {
intResult = intResult * 10 + __CFCharacterNumericValue(ch);
ch = __CFStringGetCharacterFromInlineBufferAux(buf, ++(*indexPtr));
}
}
} while (__CFCharacterIsADigit(ch));
if (result) {
if (doLonglong) {
if (!doingLonglong) longlongResult = intResult;
*(int64_t *)result = neg ? -longlongResult : longlongResult;
} else {
*(int *)result = neg ? -intResult : intResult;
}
}
return true;
}
CF_PRIVATE Boolean __CFStringScanHex(CFStringInlineBuffer *buf, SInt32 *indexPtr, unsigned *result) {
UInt32 value = 0;
SInt32 curDigit;
UniChar ch;
ch = __CFStringGetFirstNonSpaceCharacterFromInlineBuffer(buf, indexPtr);
/* Ignore the optional "0x" or "0X"; if it's followed by a non-hex, just parse the "0" and leave pointer at "x" */
if (ch == '0') {
ch = __CFStringGetCharacterFromInlineBufferAux(buf, ++(*indexPtr));
if (ch == 'x' || ch == 'X') ch = __CFStringGetCharacterFromInlineBufferAux(buf, ++(*indexPtr));
curDigit = __CFCharacterNumericOrHexValue(ch);
if (curDigit == -1) {
(*indexPtr)--; /* Go back over the "x" or "X" */
if (result) *result = 0;
return true; /* We just saw "0" */
}
} else {
curDigit = __CFCharacterNumericOrHexValue(ch);
if (curDigit == -1) return false;
}
do {
if (value > (UINT_MAX >> 4)) {
value = UINT_MAX; /* We do this over and over again, but it's an error case anyway */
} else {
value = (value << 4) + curDigit;
}
curDigit = __CFCharacterNumericOrHexValue(__CFStringGetCharacterFromInlineBufferAux(buf, ++(*indexPtr)));
} while (curDigit != -1);
if (result) *result = value;
return true;
}
// Packed array of Boolean
static const unsigned char __CFNumberSet[16] = {
0X00, // 0, 0, 0, 0, 0, 0, 0, 0, // nul soh stx etx eot enq ack bel
0X00, // 0, 0, 0, 0, 0, 0, 0, 0, // bs ht nl vt np cr so si
0X00, // 0, 0, 0, 0, 0, 0, 0, 0, // dle dc1 dc2 dc3 dc4 nak syn etb
0X00, // 0, 0, 0, 0, 0, 0, 0, 0, // can em sub esc fs gs rs us
0X00, // 0, 0, 0, 0, 0, 0, 0, 0, // sp ! " # $ % & '
0X68, // 0, 0, 0, 1, 0, 1, 1, 0, // ( ) * + , - . /
0xFF, // 1, 1, 1, 1, 1, 1, 1, 1, // 0 1 2 3 4 5 6 7
0X03, // 1, 1, 0, 0, 0, 0, 0, 0, // 8 9 : ; < = > ?
0X20, // 0, 0, 0, 0, 0, 1, 0, 0, // @ A B C D E F G
0X00, // 0, 0, 0, 0, 0, 0, 0, 0, // H I J K L M N O
0X00, // 0, 0, 0, 0, 0, 0, 0, 0, // P Q R S T U V W
0X00, // 0, 0, 0, 0, 0, 0, 0, 0, // X Y Z [ \ ] ^ _
0X20, // 0, 0, 0, 0, 0, 1, 0, 0, // ` a b c d e f g
0X00, // 0, 0, 0, 0, 0, 0, 0, 0, // h i j k l m n o
0X00, // 0, 0, 0, 0, 0, 0, 0, 0, // p q r s t u v w
0X00, // 0, 0, 0, 0, 0, 0, 0, 0 // x y z { | } ~ del
};
CF_PRIVATE Boolean __CFStringScanDouble(CFStringInlineBuffer *buf, CFTypeRef locale, SInt32 *indexPtr, double *resultPtr) {
#define STACK_BUFFER_SIZE 256
#define ALLOC_CHUNK_SIZE 256 // first and subsequent malloc size. Should be greater than STACK_BUFFER_SIZE
char localCharBuffer[STACK_BUFFER_SIZE];
char *charPtr = localCharBuffer;
char *endCharPtr;
SInt32 numChars = 0;
SInt32 capacity = STACK_BUFFER_SIZE; // in chars
double result;
UniChar ch;
CFAllocatorRef tmpAlloc = NULL;
ch = __CFStringGetFirstNonSpaceCharacterFromInlineBuffer(buf, indexPtr);
// At this point indexPtr points at the first non-space char
#if 0
#warning need to allow, case insensitively, all of: "nan", "inf", "-inf", "+inf", "-infinity", "+infinity", "infinity";
#warning -- strtod() will actually do most or all of that for us
#define BITSFORDOUBLENAN ((uint64_t)0x7ff8000000000000ULL)
#define BITSFORDOUBLEPOSINF ((uint64_t)0x7ff0000000000000ULL)
#define BITSFORDOUBLENEGINF ((uint64_t)0xfff0000000000000ULL)
if ('N' == ch || 'n' == ch) { // check for "NaN", case insensitively
UniChar next1 = __CFStringGetCharacterFromInlineBufferAux(buf, *indexPtr + 1);
UniChar next2 = __CFStringGetCharacterFromInlineBufferAux(buf, *indexPtr + 2);
if (('a' == next1 || 'A' == next1) &&
('N' == next2 || 'n' == next2)) {
*indexPtr += 3;
if (resultPtr) *(uint64_t *)resultPtr = BITSFORDOUBLENAN;
return true;
}
}
if ('I' == ch || 'i' == ch) { // check for "Inf", case insensitively
UniChar next1 = __CFStringGetCharacterFromInlineBufferAux(buf, *indexPtr + 1);
UniChar next2 = __CFStringGetCharacterFromInlineBufferAux(buf, *indexPtr + 2);
if (('n' == next1 || 'N' == next1) &&
('f' == next2 || 'F' == next2)) {
*indexPtr += 3;
if (resultPtr) *(uint64_t *)resultPtr = BITSFORDOUBLEPOSINF;
return true;
}
}
if ('+' == ch || '-' == ch) { // check for "+/-Inf", case insensitively
UniChar next1 = __CFStringGetCharacterFromInlineBufferAux(buf, *indexPtr + 1);
UniChar next2 = __CFStringGetCharacterFromInlineBufferAux(buf, *indexPtr + 2);
UniChar next3 = __CFStringGetCharacterFromInlineBufferAux(buf, *indexPtr + 3);
if (('I' == next1 || 'i' == next1) &&
('n' == next2 || 'N' == next2) &&
('f' == next3 || 'F' == next3)) {
*indexPtr += 4;
if (resultPtr) *(uint64_t *)resultPtr = ('-' == ch) ? BITSFORDOUBLENEGINF : BITSFORDOUBLEPOSINF;
return true;
}
}
#endif // 0
// Get characters until one not in __CFNumberSet[] is encountered
while ((ch < 128) && (__CFNumberSet[ch >> 3] & (1 << (ch & 7)))) {
if (numChars >= capacity - 1) {
capacity += ALLOC_CHUNK_SIZE;
if (tmpAlloc == NULL) tmpAlloc = __CFGetDefaultAllocator();
if (charPtr == localCharBuffer) {
charPtr = (char *)CFAllocatorAllocate(tmpAlloc, capacity * sizeof(char), 0);
memmove(charPtr, localCharBuffer, numChars * sizeof(char));
} else {
charPtr = __CFSafelyReallocateWithAllocator(tmpAlloc, charPtr, capacity * sizeof(char), 0, NULL);
}
}
charPtr[numChars++] = (char)ch;
ch = __CFStringGetCharacterFromInlineBufferAux(buf, *indexPtr + numChars);
};
charPtr[numChars] = 0; // Null byte for strtod
result = strtod_l(charPtr, &endCharPtr, NULL);
if (tmpAlloc) CFAllocatorDeallocate(tmpAlloc, charPtr);
if (charPtr == endCharPtr) return false;
*indexPtr += (endCharPtr - charPtr);
if (resultPtr) *resultPtr = result; // only store result if we succeed
return true;
}
#undef STACK_BUFFER_SIZE
#undef ALLOC_CHUNK_SIZE