--- a/Integer.st Sat Jul 09 21:20:04 2016 +0200
+++ b/Integer.st Sat Jul 09 21:20:19 2016 +0200
@@ -331,7 +331,7 @@
No whitespace-skipping is done.
Returns the value of exceptionBlock, if no number is available."
- |str nextChar nextChar2 nextChar3 nextChar4 value
+ |str nextChar value
r "{ Class: SmallInteger }"
r2 "{ Class: SmallInteger }"
r3 "{ Class: SmallInteger }"
@@ -341,14 +341,10 @@
str := aStringOrStream readStream.
nextChar := str peekOrNil.
- (nextChar notNil and:[nextChar isDigitRadix:radix]) ifFalse:[
+ (nextChar isNil or:[(value := nextChar digitValueRadix:radix) isNil]) ifTrue:[
^ exceptionBlock value
].
- value := nextChar digitValue.
- str next.
- nextChar := str peekOrNil.
-
"/ OLD code
"/ [nextChar notNil and:[nextChar isDigitRadix:radix]] whileTrue:[
"/ str next.
@@ -364,33 +360,31 @@
r := radix.
r2 := r * r.
- r3 := r2 * r.
r4 := r2 * r2.
- [nextChar notNil and:[ (digit1 := nextChar digitValueRadix:r) notNil]] whileTrue:[
+ [
+ nextChar := str nextPeekOrNil.
+ nextChar notNil and:[(digit1 := nextChar digitValueRadix:r) notNil]
+ ] whileTrue:[
"/ read 4 chars and pre-compute their value to avoid largeInt operations.
- str next.
- nextChar2 := str peekOrNil.
- (nextChar2 isNil or:[ (digit2 := nextChar2 digitValueRadix:r) isNil]) ifTrue:[
+ nextChar := str nextPeekOrNil.
+ (nextChar isNil or:[(digit2 := nextChar digitValueRadix:r) isNil]) ifTrue:[
^ (value * r) + digit1.
].
- str next.
- nextChar3 := str peekOrNil.
- (nextChar3 isNil or:[ (digit3 := nextChar3 digitValueRadix:r) isNil]) ifTrue:[
- ^ (value * r2) + ((digit1*r) + digit2).
+ nextChar := str nextPeekOrNil.
+ (nextChar isNil or:[(digit3 := nextChar digitValueRadix:r) isNil]) ifTrue:[
+ ^ (value * r2) + ((digit1*r) + digit2) .
].
- str next.
- nextChar4 := str peekOrNil.
- (nextChar4 isNil or:[ (digit4 := nextChar4 digitValueRadix:r) isNil]) ifTrue:[
+ nextChar := str nextPeekOrNil.
+ (nextChar isNil or:[ (digit4 := nextChar digitValueRadix:r) isNil]) ifTrue:[
+ r3 := r2 * r.
^ (value * r3) + ((((digit1*r) + digit2)*r) + digit3).
].
value := (value * r4) + ((((((digit1*r) + digit2)*r) + digit3)*r) + digit4).
- str next.
- nextChar := str peekOrNil.
].
^ value
@@ -401,10 +395,13 @@
Integer readFrom:(ReadStream on:'foobar') radix:10
Integer readFrom:(ReadStream on:'foobar') radix:10 onError:nil
Integer readFrom:'gg' radix:10 onError:0
-
+ Integer readFrom:'' radix:10 onError:'wrong'
+
+ |s|
+ s := String new:1000 withAll:$1.
Time millisecondsToRun:[
1000 timesRepeat:[
- (String new:1000 withAll:$1) asInteger
+ s asInteger
]
]
"