]> git.saurik.com Git - apple/cf.git/blob - String.subproj/CFStringScanner.c
CF-368.27.tar.gz
[apple/cf.git] / String.subproj / CFStringScanner.c
1 /*
2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /* CFStringScanner.c
24 Copyright 1999-2002, Apple, Inc. All rights reserved.
25 Responsibility: Ali Ozer
26 */
27
28 #include "CFInternal.h"
29 #include <CoreFoundation/CFString.h>
30 #if !defined(__MACOS8__)
31 #include <sys/types.h>
32 #endif
33 #include <limits.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 CF_INLINE Boolean __CFCharacterIsADigit(UniChar ch) {
38 return (ch >= '0' && ch <= '9') ? true : false;
39 }
40
41 /* Returns -1 on illegal value */
42 CF_INLINE SInt32 __CFCharacterNumericOrHexValue (UniChar ch) {
43 if (ch >= '0' && ch <= '9') {
44 return ch - '0';
45 } else if (ch >= 'A' && ch <= 'F') {
46 return ch + 10 - 'A';
47 } else if (ch >= 'a' && ch <= 'f') {
48 return ch + 10 - 'a';
49 } else {
50 return -1;
51 }
52 }
53
54 /* Returns -1 on illegal value */
55 CF_INLINE SInt32 __CFCharacterNumericValue(UniChar ch) {
56 return (ch >= '0' && ch <= '9') ? (ch - '0') : -1;
57 }
58
59 CF_INLINE UniChar __CFStringGetFirstNonSpaceCharacterFromInlineBuffer(CFStringInlineBuffer *buf, SInt32 *indexPtr) {
60 UniChar ch;
61 while (__CFIsWhitespace(ch = __CFStringGetCharacterFromInlineBufferAux(buf, *indexPtr))) (*indexPtr)++;
62 return ch;
63 }
64
65 /* result is int64_t or int, depending on doLonglong
66 */
67 __private_extern__ Boolean __CFStringScanInteger(CFStringInlineBuffer *buf, CFDictionaryRef locale, SInt32 *indexPtr, Boolean doLonglong, void *result) {
68 Boolean doingLonglong = false; /* Set to true if doLonglong, and we overflow an int... */
69 Boolean neg = false;
70 int intResult = 0;
71 register int64_t longlongResult = 0; /* ??? int64_t is slow when not in regs; I hope this does the right thing. */
72 UniChar ch;
73
74 ch = __CFStringGetFirstNonSpaceCharacterFromInlineBuffer(buf, indexPtr);
75
76 if (ch == '-' || ch == '+') {
77 neg = (ch == '-');
78 (*indexPtr)++;
79 ch = __CFStringGetFirstNonSpaceCharacterFromInlineBuffer(buf, indexPtr);
80 }
81
82 if (! __CFCharacterIsADigit(ch)) return false; /* No digits, bail out... */
83 do {
84 if (doingLonglong) {
85 if ((longlongResult >= LLONG_MAX / 10) && ((longlongResult > LLONG_MAX / 10) || (__CFCharacterNumericValue(ch) - (neg ? 1 : 0) >= LLONG_MAX - longlongResult * 10))) {
86 /* ??? This might not handle LLONG_MIN correctly... */
87 longlongResult = neg ? LLONG_MIN : LLONG_MAX;
88 neg = false;
89 while (__CFCharacterIsADigit(ch = __CFStringGetCharacterFromInlineBufferAux(buf, ++(*indexPtr)))); /* Skip remaining digits */
90 } else {
91 longlongResult = longlongResult * 10 + __CFCharacterNumericValue(ch);
92 ch = __CFStringGetCharacterFromInlineBufferAux(buf, ++(*indexPtr));
93 }
94 } else {
95 if ((intResult >= INT_MAX / 10) && ((intResult > INT_MAX / 10) || (__CFCharacterNumericValue(ch) - (neg ? 1 : 0) >= INT_MAX - intResult * 10))) {
96 // Overflow, check for int64_t...
97 if (doLonglong) {
98 longlongResult = intResult;
99 doingLonglong = true;
100 } else {
101 /* ??? This might not handle INT_MIN correctly... */
102 intResult = neg ? INT_MIN : INT_MAX;
103 neg = false;
104 while (__CFCharacterIsADigit(ch = __CFStringGetCharacterFromInlineBufferAux(buf, ++(*indexPtr)))); /* Skip remaining digits */
105 }
106 } else {
107 intResult = intResult * 10 + __CFCharacterNumericValue(ch);
108 ch = __CFStringGetCharacterFromInlineBufferAux(buf, ++(*indexPtr));
109 }
110 }
111 } while (__CFCharacterIsADigit(ch));
112
113 if (result) {
114 if (doLonglong) {
115 if (!doingLonglong) longlongResult = intResult;
116 *(int64_t *)result = neg ? -longlongResult : longlongResult;
117 } else {
118 *(int *)result = neg ? -intResult : intResult;
119 }
120 }
121
122 return true;
123 }
124
125 __private_extern__ Boolean __CFStringScanHex(CFStringInlineBuffer *buf, SInt32 *indexPtr, unsigned *result) {
126 UInt32 value = 0;
127 SInt32 curDigit;
128 UniChar ch;
129
130 ch = __CFStringGetFirstNonSpaceCharacterFromInlineBuffer(buf, indexPtr);
131 /* Ignore the optional "0x" or "0X"; if it's followed by a non-hex, just parse the "0" and leave pointer at "x" */
132 if (ch == '0') {
133 ch = __CFStringGetCharacterFromInlineBufferAux(buf, ++(*indexPtr));
134 if (ch == 'x' || ch == 'X') ch = __CFStringGetCharacterFromInlineBufferAux(buf, ++(*indexPtr));
135 curDigit = __CFCharacterNumericOrHexValue(ch);
136 if (curDigit == -1) {
137 (*indexPtr)--; /* Go back over the "x" or "X" */
138 if (result) *result = 0;
139 return true; /* We just saw "0" */
140 }
141 } else {
142 curDigit = __CFCharacterNumericOrHexValue(ch);
143 if (curDigit == -1) return false;
144 }
145
146 do {
147 if (value > (UINT_MAX >> 4)) {
148 value = UINT_MAX; /* We do this over and over again, but it's an error case anyway */
149 } else {
150 value = (value << 4) + curDigit;
151 }
152 curDigit = __CFCharacterNumericOrHexValue(__CFStringGetCharacterFromInlineBufferAux(buf, ++(*indexPtr)));
153 } while (curDigit != -1);
154
155 if (result) *result = value;
156 return true;
157 }
158
159 // Packed array of Boolean
160 static const char __CFNumberSet[16] = {
161 0X00, // 0, 0, 0, 0, 0, 0, 0, 0, // nul soh stx etx eot enq ack bel
162 0X00, // 0, 0, 0, 0, 0, 0, 0, 0, // bs ht nl vt np cr so si
163 0X00, // 0, 0, 0, 0, 0, 0, 0, 0, // dle dc1 dc2 dc3 dc4 nak syn etb
164 0X00, // 0, 0, 0, 0, 0, 0, 0, 0, // can em sub esc fs gs rs us
165 0X00, // 0, 0, 0, 0, 0, 0, 0, 0, // sp ! " # $ % & '
166 0X28, // 0, 0, 0, 1, 0, 1, 0, 0, // ( ) * + , - . /
167 0XFF, // 1, 1, 1, 1, 1, 1, 1, 1, // 0 1 2 3 4 5 6 7
168 0X03, // 1, 1, 0, 0, 0, 0, 0, 0, // 8 9 : ; < = > ?
169 0X20, // 0, 0, 0, 0, 0, 1, 0, 0, // @ A B C D E F G
170 0X00, // 0, 0, 0, 0, 0, 0, 0, 0, // H I J K L M N O
171 0X00, // 0, 0, 0, 0, 0, 0, 0, 0, // P Q R S T U V W
172 0X00, // 0, 0, 0, 0, 0, 0, 0, 0, // X Y Z [ \ ] ^ _
173 0X20, // 0, 0, 0, 0, 0, 1, 0, 0, // ` a b c d e f g
174 0X00, // 0, 0, 0, 0, 0, 0, 0, 0, // h i j k l m n o
175 0X00, // 0, 0, 0, 0, 0, 0, 0, 0, // p q r s t u v w
176 0X00, // 0, 0, 0, 0, 0, 0, 0, 0 // x y z { | } ~ del
177 };
178
179 __private_extern__ Boolean __CFStringScanDouble(CFStringInlineBuffer *buf, CFDictionaryRef locale, SInt32 *indexPtr, double *resultPtr) {
180 #define STACK_BUFFER_SIZE 256
181 #define ALLOC_CHUNK_SIZE 256 // first and subsequent malloc size. Should be greater than STACK_BUFFER_SIZE
182 char localCharBuffer[STACK_BUFFER_SIZE];
183 char *charPtr = localCharBuffer;
184 char *endCharPtr;
185 UniChar decimalChar = '.';
186 SInt32 numChars = 0;
187 SInt32 capacity = STACK_BUFFER_SIZE; // in chars
188 double result;
189 UniChar ch;
190 CFAllocatorRef tmpAlloc = NULL;
191
192 #if 0
193 if (locale != NULL) {
194 CFStringRef decimalSeparator = [locale objectForKey: NSDecimalSeparator];
195 if (decimalSeparator != nil) decimalChar = [decimalSeparator characterAtIndex:0];
196 }
197 #endif
198 ch = __CFStringGetFirstNonSpaceCharacterFromInlineBuffer(buf, indexPtr);
199 // At this point indexPtr points at the first non-space char
200 #if 0
201 #warning need to allow, case insensitively, all of: "nan", "inf", "-inf", "+inf", "-infinity", "+infinity", "infinity";
202 #warning -- strtod() will actually do most or all of that for us
203 #define BITSFORDOUBLENAN ((uint64_t)0x7ff8000000000000)
204 #define BITSFORDOUBLEPOSINF ((uint64_t)0x7ff0000000000000)
205 #define BITSFORDOUBLENEGINF ((uint64_t)0xfff0000000000000)
206 if ('N' == ch || 'n' == ch) { // check for "NaN", case insensitively
207 UniChar next1 = __CFStringGetCharacterFromInlineBufferAux(buf, *indexPtr + 1);
208 UniChar next2 = __CFStringGetCharacterFromInlineBufferAux(buf, *indexPtr + 2);
209 if (('a' == next1 || 'A' == next1) &&
210 ('N' == next2 || 'n' == next2)) {
211 *indexPtr += 3;
212 if (resultPtr) *(uint64_t *)resultPtr = BITSFORDOUBLENAN;
213 return true;
214 }
215 }
216 if ('I' == ch || 'i' == ch) { // check for "Inf", case insensitively
217 UniChar next1 = __CFStringGetCharacterFromInlineBufferAux(buf, *indexPtr + 1);
218 UniChar next2 = __CFStringGetCharacterFromInlineBufferAux(buf, *indexPtr + 2);
219 if (('n' == next1 || 'N' == next1) &&
220 ('f' == next2 || 'F' == next2)) {
221 *indexPtr += 3;
222 if (resultPtr) *(uint64_t *)resultPtr = BITSFORDOUBLEPOSINF;
223 return true;
224 }
225 }
226 if ('+' == ch || '-' == ch) { // check for "+/-Inf", case insensitively
227 UniChar next1 = __CFStringGetCharacterFromInlineBufferAux(buf, *indexPtr + 1);
228 UniChar next2 = __CFStringGetCharacterFromInlineBufferAux(buf, *indexPtr + 2);
229 UniChar next3 = __CFStringGetCharacterFromInlineBufferAux(buf, *indexPtr + 3);
230 if (('I' == next1 || 'i' == next1) &&
231 ('n' == next2 || 'N' == next2) &&
232 ('f' == next3 || 'F' == next3)) {
233 *indexPtr += 4;
234 if (resultPtr) *(uint64_t *)resultPtr = ('-' == ch) ? BITSFORDOUBLENEGINF : BITSFORDOUBLEPOSINF;
235 return true;
236 }
237 }
238 #endif 0
239 do {
240 if (ch >= 128 || (__CFNumberSet[ch >> 3] & (1 << (ch & 7))) == 0) {
241 // Not in __CFNumberSet
242 if (ch != decimalChar) break;
243 ch = '.'; // Replace the decimal character with something strtod will understand
244 }
245 if (numChars >= capacity - 1) {
246 capacity += ALLOC_CHUNK_SIZE;
247 if (tmpAlloc == NULL) tmpAlloc = __CFGetDefaultAllocator();
248 if (charPtr == localCharBuffer) {
249 charPtr = CFAllocatorAllocate(tmpAlloc, capacity * sizeof(char), 0);
250 memmove(charPtr, localCharBuffer, numChars * sizeof(char));
251 } else {
252 charPtr = CFAllocatorReallocate(tmpAlloc, charPtr, capacity * sizeof(char), 0);
253 }
254 }
255 charPtr[numChars++] = (char)ch;
256 ch = __CFStringGetCharacterFromInlineBufferAux(buf, *indexPtr + numChars);
257 } while (true);
258 charPtr[numChars] = 0; // Null byte for strtod
259
260 result = strtod_l(charPtr, &endCharPtr, NULL);
261
262 if (tmpAlloc) CFAllocatorDeallocate(tmpAlloc, charPtr);
263 if (charPtr == endCharPtr) return false;
264 *indexPtr += (endCharPtr - charPtr);
265 if (resultPtr) *resultPtr = result; // only store result if we succeed
266
267 return true;
268 }
269
270
271 #undef STACK_BUFFER_SIZE
272 #undef ALLOC_CHUNK_SIZE
273
274