#BUGFIX by Stefan Reise
authorsr
Tue, 17 Dec 2019 14:30:36 +0100
changeset 25145 888d6ef294c3
parent 25144 03ca574d45c2
child 25146 880c7f41b369
#BUGFIX by Stefan Reise class: Integer class changed: #readFrom:allowRadix:onError: 0xXXX reading
Integer.st
--- a/Integer.st	Tue Dec 17 14:30:23 2019 +0100
+++ b/Integer.st	Tue Dec 17 14:30:36 2019 +0100
@@ -1,5 +1,3 @@
-"{ Encoding: utf8 }"
-
 "
  COPYRIGHT (c) 1988 by Claus Gittinger
               All Rights Reserved
@@ -268,7 +266,7 @@
     Error handle:[:ex |
         ^ exceptionBlock value
     ] do:[
-        |str nextChar negative|
+        |str nextChar negative radix|
 
         str := aStringOrStream readStream.
 
@@ -286,25 +284,50 @@
             "
             ^ exceptionBlock value
         ].
-        value := self readFrom:str radix:10.
-        nextChar := str peekOrNil.
-        (allowRadix and:[((nextChar == $r) or:[ nextChar == $R])]) ifTrue:[
-            "-xxr<number> is invalid; should be xxr-<val>"
-
-            negative ifTrue:[
-                'Integer [warning]: invalid (negative) radix; please use xxr-<val> instead of -xxr<number>' errorPrintCR.
-                "/ negative := false
+        (allowRadix and:[nextChar == $0]) ifTrue:[
+            nextChar := str nextPeekOrNil.
+            ((nextChar == $x) or:[ nextChar == $X]) ifTrue:[
+                radix := 16.
+                nextChar := str nextPeekOrNil.
+            ] ifFalse:[
+                ((nextChar == $b) or:[ nextChar == $B]) ifTrue:[ 
+                    radix := 2.
+                    nextChar := str nextPeekOrNil.
+                ] ifFalse:[
+                    ((nextChar == $o) or:[ nextChar == $O]) ifTrue:[ 
+                        radix := 8.
+                        nextChar := str nextPeekOrNil.
+                    ] ifFalse:[
+                        nextChar isDigit ifFalse:[
+                            ^ 0
+                        ].
+                        value := self readFrom:str radix:10
+                    ].
+                ]
             ].
-            str next.
+        ] ifFalse:[
+            value := self readFrom:str radix:10.
             nextChar := str peekOrNil.
+            (allowRadix and:[((nextChar == $r) or:[ nextChar == $R])]) ifTrue:[
+                "-xxr<number> is invalid; should be xxr-<val>"
+
+                negative ifTrue:[
+                    Logger info:'Integer [warning]: invalid (negative) radix; please use xxr-<val> instead of -xxr<number>'.
+                    "/ negative := false
+                ].
+                nextChar := str nextPeekOrNil.
+                radix := value.
+            ].
+        ].
+        radix notNil ifTrue:[
             nextChar == $- ifTrue:[
                 negative ifTrue:[
-                    'Integer [warning]: invalid double sign; please use xxr-<val> instead of -xxr<number>' errorPrintCR.
+                    Logger info:'Integer [warning]: invalid double sign; please use xxr-<val> instead of -xxr<number>'.
                 ].
                 negative := true.
                 str next.
             ].
-            value := self readFrom:str radix:value
+            value := self readFrom:str radix:radix
         ].
         negative ifTrue:[
             value := value negated
@@ -313,16 +336,18 @@
     ^ value
 
     "
-     Integer readFrom:'12345'      onError:['wrong']
-     Integer readFrom:'-12345'     onError:['wrong']
-     Integer readFrom:'+12345'     onError:['wrong']
-     Integer readFrom:'16rFFFF'    onError:['wrong']
-     Integer readFrom:'12345.1234' onError:['wrong']
-     Integer readFrom:'foo'        onError:['wrong']
-     Integer readFrom:'foo'
+     Integer readFrom:'12345'      onError:['wrong']   
+     Integer readFrom:'-12345'     onError:['wrong']  
+     Integer readFrom:'+12345'     onError:['wrong']  
+     Integer readFrom:'16rFFFF'    onError:['wrong']  
+     Integer readFrom:'12345.1234' onError:['wrong']  
+     Integer readFrom:'foo'        onError:['wrong']  
+     Integer readFrom:'foo'                           
+     Integer readFrom:'0x123'                           
     "
 
     "Created: / 09-03-2017 / 16:26:28 / cg"
+    "Modified: / 17-12-2019 / 14:24:18 / Stefan Reise"
 !
 
 readFrom:aStringOrStream onError:exceptionBlock
@@ -803,6 +828,8 @@
     "Modified: / 15.11.1999 / 20:35:20 / cg"
 ! !
 
+
+
 !Integer class methodsFor:'class initialization'!
 
 initialize
@@ -859,6 +886,7 @@
     "Modified: 18.7.1996 / 12:26:38 / cg"
 ! !
 
+
 !Integer class methodsFor:'prime numbers'!
 
 flushPrimeCache
@@ -1234,6 +1262,7 @@
     ^ self == Integer
 ! !
 
+
 !Integer methodsFor:'Compatibility-Dolphin'!
 
 highWord
@@ -4118,7 +4147,7 @@
     ^ fibUsingDict value:self
 
     "the running time is mostly dictated by the LargeInteger multiplication performance...
-     (therefore, we get O(n²) execution times, even for a linear number of multiplications)
+     (therefore, we get O(n²) execution times, even for a linear number of multiplications)
 
      Time millisecondsToRun:[50000 fib_iterative]  312    (DUO 1.7Ghz CPU)
      Time millisecondsToRun:[50000 fib_helper]     109
@@ -5713,6 +5742,7 @@
     "Created: / 09-01-2012 / 17:18:06 / cg"
 ! !
 
+
 !Integer methodsFor:'special modulo arithmetic'!
 
 add_32:anInteger