Number.st
changeset 6632 65014fd967d9
parent 6584 4aa036c5fdcb
child 6637 b6ee031f3096
equal deleted inserted replaced
6631:5ffa1ce7b2cc 6632:65014fd967d9
    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') 
   288         errorString:'foo bar test'
   284         errorString:'foo bar test'
   289     "
   285     "
   290 
   286 
   291     "Modified: / 16.11.2001 / 14:13:16 / cg"
   287     "Modified: / 16.11.2001 / 14:13:16 / cg"
   292 ! !
   288 ! !
   293 
       
   294 
   289 
   295 !Number class methodsFor:'private'!
   290 !Number class methodsFor:'private'!
   296 
   291 
   297 readMantissaFrom:aStream radix:radix
   292 readMantissaFrom:aStream radix:radix
   298     "helper for readFrom: -
   293     "helper for readFrom: -
   500     "interpreting the receiver as degrees, return the radians"
   495     "interpreting the receiver as degrees, return the radians"
   501 
   496 
   502     ^ (self * 180.0) / (Float pi)
   497     ^ (self * 180.0) / (Float pi)
   503 ! !
   498 ! !
   504 
   499 
   505 
       
   506 !Number methodsFor:'intervals'!
   500 !Number methodsFor:'intervals'!
   507 
   501 
   508 downTo:stop
   502 downTo:stop
   509     "return an interval from receiver down to the argument, incrementing by -1"
   503     "return an interval from receiver down to the argument, incrementing by -1"
   510 
   504 
   590      123 timesTwoPower:1  
   584      123 timesTwoPower:1  
   591      123 timesTwoPower:2  
   585      123 timesTwoPower:2  
   592      123 timesTwoPower:3  
   586      123 timesTwoPower:3  
   593     "
   587     "
   594 ! !
   588 ! !
   595 
       
   596 
   589 
   597 !Number methodsFor:'printing & storing'!
   590 !Number methodsFor:'printing & storing'!
   598 
   591 
   599 printOn:aStream paddedWith:padCharacter to:size base:radix
   592 printOn:aStream paddedWith:padCharacter to:size base:radix
   600     |s|
   593     |s|
   753 ! !
   746 ! !
   754 
   747 
   755 !Number class methodsFor:'documentation'!
   748 !Number class methodsFor:'documentation'!
   756 
   749 
   757 version
   750 version
   758     ^ '$Header: /cvs/stx/stx/libbasic/Number.st,v 1.74 2002-06-19 12:17:06 cg Exp $'
   751     ^ '$Header: /cvs/stx/stx/libbasic/Number.st,v 1.75 2002-07-15 09:46:51 stefan Exp $'
   759 ! !
   752 ! !