Oolite
Loading...
Searching...
No Matches
OOIsNumberLiteral.m
Go to the documentation of this file.
1/*
2 OOISNumberLiteral.m
3
4 Copyright (C) 2008-2013 Jens Ayton
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23*/
24
25#import "OOIsNumberLiteral.h"
26
27#if 0
28#define FAIL(s) \
29 do { \
30 NSLog(@"OOIsNumberLiteral failed for \"%@\": %@.", string, @s); \
31 return NO; \
32 } while (0)
33#else
34#define FAIL(s) \
35 do { \
36 return NO; \
37 } while (0)
38#endif
39
40BOOL OOIsNumberLiteral(NSString* string, BOOL allowSpaces)
41{
42 BOOL leadingSpace = allowSpaces,
43 trailingSpace = NO,
44 allowSign = YES,
45 allowE = NO,
46 hadE = NO,
47 hadExp = NO,
48 allowDec = YES,
49 hadNumber = NO;
50 NSUInteger i, count;
51
52 if (string == nil)
53 return NO;
54
55 count = [string length];
56 for (i = 0; i != count; ++i) {
57 switch ([string characterAtIndex:i]) {
58 // <digit>
59 case '0':
60 case '1':
61 case '2':
62 case '3':
63 case '4':
64 case '5':
65 case '6':
66 case '7':
67 case '8':
68 case '9':
69 leadingSpace = NO;
70 if (trailingSpace)
71 FAIL("Digit after trailing whitespace");
72 if (!hadE)
73 allowE = YES;
74 else
75 hadExp = YES;
76 allowSign = NO;
77 hadNumber = YES;
78 break;
79
80 // <whitespaceChar>
81 case ' ':
82 case '\t':
83 if (leadingSpace || trailingSpace)
84 break;
85 if (hadNumber && allowSpaces) {
86 trailingSpace = YES;
87 allowSign = allowE = allowDec = NO;
88 break;
89 }
90 FAIL("Space in unpermitted position");
91
92 // <sign>
93 case '-':
94 case '+':
95 leadingSpace = NO;
96 if (allowSign) {
97 allowSign = NO;
98 break;
99 }
100 FAIL("Sign (+ or -) in unpermitted position");
101
102 // <decimalPoint>
103 case '.':
104 leadingSpace = NO;
105 if (allowDec) {
106 allowDec = NO;
107 continue;
108 }
109 FAIL("Sign (+ or -) in unpermitted position");
110
111 // <e>
112 case 'e':
113 case 'E':
114 leadingSpace = NO;
115 if (allowE) {
116 allowE = NO;
117 allowSign = YES;
118 allowDec = NO;
119 hadE = YES;
120 continue;
121 }
122 FAIL("E in unpermitted position");
123
124 default:
125 FAIL("Unpermitted character");
126 }
127 }
128
129 if (hadE && !hadExp)
130 FAIL("E with no exponent");
131 if (!hadNumber)
132 FAIL("No digits in string");
133
134 return YES;
135}
BOOL OOIsNumberLiteral(NSString *string, BOOL allowSpaces)
#define FAIL(s)
unsigned count
return nil