Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 100 lines (82 sloc) 2.434 kb
05eedb5 @itod initial add
authored
1 // Copyright 2010 Todd Ditchendorf
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #import "PKScientificNumberState.h"
16 #import <ParseKit/PKReader.h>
17 #import <ParseKit/PKTypes.h>
18
19 @interface PKTokenizerState ()
20 - (void)append:(PKUniChar)c;
21 @end
22
23 @interface PKNumberState ()
24 - (CGFloat)absorbDigitsFromReader:(PKReader *)r isFraction:(BOOL)isFraction;
25 - (void)parseRightSideFromReader:(PKReader *)r;
26 - (void)reset:(PKUniChar)cin;
27 - (CGFloat)value;
28 @end
29
30 @implementation PKScientificNumberState
31
32 - (id)init {
33 if (self = [super init]) {
34 self.allowsScientificNotation = YES;
35 }
36 return self;
37 }
38
39
40 - (void)parseRightSideFromReader:(PKReader *)r {
41 NSParameterAssert(r);
42 [super parseRightSideFromReader:r];
43 if (!allowsScientificNotation) {
44 return;
45 }
46
47 if ('e' == c || 'E' == c) {
48 PKUniChar e = c;
49 c = [r read];
50
51 BOOL hasExp = isdigit(c);
52 negativeExp = ('-' == c);
53 BOOL positiveExp = ('+' == c);
54
55 if (!hasExp && (negativeExp || positiveExp)) {
56 c = [r read];
57 hasExp = isdigit(c);
58 }
59 if (PKEOF != c) {
60 [r unread];
61 }
62 if (hasExp) {
63 [self append:e];
64 if (negativeExp) {
65 [self append:'-'];
66 } else if (positiveExp) {
67 [self append:'+'];
68 }
69 c = [r read];
70 exp = [super absorbDigitsFromReader:r isFraction:NO];
71 }
72 }
73 }
74
75
76 - (void)reset:(PKUniChar)cin {
77 [super reset:cin];
78 exp = (CGFloat)0.0;
79 negativeExp = NO;
80 }
81
82
83 - (CGFloat)value {
84 CGFloat result = (CGFloat)floatValue;
85
86 NSUInteger i = 0;
87 for ( ; i < exp; i++) {
88 if (negativeExp) {
89 result /= (CGFloat)10.0;
90 } else {
91 result *= (CGFloat)10.0;
92 }
93 }
94
95 return (CGFloat)result;
96 }
97
98 @synthesize allowsScientificNotation;
99 @end
Something went wrong with that request. Please try again.