80 readFrom:aStringOrStream onError:exceptionBlock |
80 readFrom:aStringOrStream onError:exceptionBlock |
81 "return the next Number from the (character-)stream aStream; |
81 "return the next Number from the (character-)stream aStream; |
82 skipping all whitespace first; return the value of exceptionBlock, |
82 skipping all whitespace first; return the value of exceptionBlock, |
83 if no number can be read." |
83 if no number can be read." |
84 |
84 |
85 |value| |
85 ^ [ |
86 |
86 |value str nextChar radix negative signExp| |
87 ErrorSignal handle:[:ex | |
87 |
88 ^ exceptionBlock value |
88 str := aStringOrStream readStream. |
89 ] do:[ |
89 |
90 |str nextChar radix negative signExp| |
90 nextChar := str skipSeparators. |
91 |
91 nextChar isNil ifTrue:[^ exceptionBlock value]. |
92 str := aStringOrStream readStream. |
92 |
93 |
93 (nextChar == $-) ifTrue:[ |
94 nextChar := str skipSeparators. |
94 negative := true. |
95 nextChar isNil ifTrue:[^ exceptionBlock value]. |
95 str next. |
96 |
96 nextChar := str peekOrNil |
97 (nextChar == $-) ifTrue:[ |
97 ] ifFalse:[ |
98 negative := true. |
98 negative := false. |
99 str next. |
99 (nextChar == $+) ifTrue:[ |
100 nextChar := str peekOrNil |
100 str next. |
101 ] ifFalse:[ |
101 nextChar := str peekOrNil |
102 negative := false. |
102 ] |
103 (nextChar == $+) ifTrue:[ |
103 ]. |
104 str next. |
104 (nextChar isDigit or:[nextChar == $.]) ifFalse:[ |
105 nextChar := str peekOrNil |
105 ^ exceptionBlock value. |
106 ] |
|
107 ]. |
|
108 (nextChar isDigit or:[nextChar == $.]) ifFalse:[ |
|
109 ^ exceptionBlock value. |
|
110 "/ value := super readFrom:str. |
106 "/ value := super readFrom:str. |
111 "/ negative ifTrue:[value := value negated]. |
107 "/ negative ifTrue:[value := value negated]. |
112 "/ ^ value |
108 "/ ^ value |
113 ]. |
109 ]. |
114 nextChar == $. ifTrue:[ |
110 nextChar == $. ifTrue:[ |
115 radix := 10. |
111 radix := 10. |
116 value := 0.0. |
112 value := 0.0. |
117 ] ifFalse:[ |
113 ] ifFalse:[ |
118 value := Integer readFrom:str radix:10. |
114 value := Integer readFrom:str radix:10. |
119 nextChar := str peekOrNil. |
115 nextChar := str peekOrNil. |
120 ((nextChar == $r) or:[ nextChar == $R]) ifTrue:[ |
116 ((nextChar == $r) or:[ nextChar == $R]) ifTrue:[ |
121 str next. |
117 str next. |
122 radix := value. |
118 radix := value. |
123 value := Integer readFrom:str radix:radix. |
119 value := Integer readFrom:str radix:radix. |
124 ] ifFalse:[ |
120 ] ifFalse:[ |
125 radix := 10 |
121 radix := 10 |
126 ]. |
122 ]. |
127 ]. |
123 ]. |
128 |
124 |
129 (nextChar == $.) ifTrue:[ |
125 (nextChar == $.) ifTrue:[ |
130 str next. |
126 str next. |
131 nextChar := str peekOrNil. |
127 nextChar := str peekOrNil. |
132 (nextChar notNil and:[nextChar isDigitRadix:radix]) ifTrue:[ |
128 (nextChar notNil and:[nextChar isDigitRadix:radix]) ifTrue:[ |
133 value := value asFloat |
129 value := value asFloat |
134 + (Number readMantissaFrom:str radix:radix). |
130 + (Number readMantissaFrom:str radix:radix). |
135 nextChar := str peekOrNil |
131 nextChar := str peekOrNil |
136 ] |
132 ] |
137 ]. |
133 ]. |
138 ((nextChar == $e) or:[nextChar == $E]) ifTrue:[ |
134 ((nextChar == $e) or:[nextChar == $E]) ifTrue:[ |
139 str next. |
135 str next. |
140 nextChar := str peekOrNil. |
136 nextChar := str peekOrNil. |
141 signExp := 1. |
137 signExp := 1. |
142 (nextChar == $+) ifTrue:[ |
138 (nextChar == $+) ifTrue:[ |
143 str next. |
139 str next. |
144 nextChar := str peekOrNil. |
140 nextChar := str peekOrNil. |
145 ] ifFalse:[ |
141 ] ifFalse:[ |
146 (nextChar == $-) ifTrue:[ |
142 (nextChar == $-) ifTrue:[ |
147 str next. |
143 str next. |
148 nextChar := str peekOrNil. |
144 nextChar := str peekOrNil. |
149 signExp := -1 |
145 signExp := -1 |
150 ] |
146 ] |
151 ]. |
147 ]. |
152 (nextChar notNil and:[(nextChar isDigitRadix:radix)]) ifTrue:[ |
148 (nextChar notNil and:[(nextChar isDigitRadix:radix)]) ifTrue:[ |
153 value := value asFloat |
149 value := value asFloat |
154 * (10.0 raisedToInteger: |
150 * (10.0 raisedToInteger: |
155 ((Integer readFrom:str radix:radix) * signExp)) |
151 ((Integer readFrom:str radix:radix) * signExp)) |
156 ] |
152 ] |
157 ]. |
153 ]. |
158 negative ifTrue:[ |
154 negative ifTrue:[ |
159 value := value negated |
155 value := value negated |
160 ]. |
156 ]. |
161 ]. |
157 value. |
162 ^ value. |
158 ] on:Error do:exceptionBlock |
163 |
159 |
164 " |
160 " |
165 Number readFrom:(ReadStream on:'54.32e-01') |
161 Number readFrom:(ReadStream on:'54.32e-01') |
166 Number readFrom:(ReadStream on:'12345678901234567890') |
162 Number readFrom:(ReadStream on:'12345678901234567890') |
167 Number readFrom:(ReadStream on:'16rAAAAFFFFAAAAFFFF') |
163 Number readFrom:(ReadStream on:'16rAAAAFFFFAAAAFFFF') |