Stream.st
changeset 50 71f3b9444905
parent 5 67342904af11
child 62 e1b4369c61fb
equal deleted inserted replaced
49:f1c2d75f2eb6 50:71f3b9444905
    20 Stream comment:'
    20 Stream comment:'
    21 
    21 
    22 COPYRIGHT (c) 1989 by Claus Gittinger
    22 COPYRIGHT (c) 1989 by Claus Gittinger
    23               All Rights Reserved
    23               All Rights Reserved
    24 
    24 
    25 $Header: /cvs/stx/stx/libbasic/Stream.st,v 1.4 1993-10-13 02:14:01 claus Exp $
    25 $Header: /cvs/stx/stx/libbasic/Stream.st,v 1.5 1994-02-05 12:24:58 claus Exp $
    26 '!
    26 '!
    27 
    27 
    28 !Stream class methodsFor:'instance creation'!
    28 !Stream class methodsFor:'instance creation'!
    29 
    29 
    30 new
    30 new
   202 ! !
   202 ! !
   203 
   203 
   204 !Stream methodsFor: 'nonhomogeneous accessing'!
   204 !Stream methodsFor: 'nonhomogeneous accessing'!
   205 
   205 
   206 nextNumber: n 
   206 nextNumber: n 
   207         "Answer the next n bytes as a positive Integer or LargePositiveInteger."
   207         "Answer the next n bytes as a positive Integer."
   208 
   208 
   209         | s i |
   209         | s i |
       
   210 
   210         n <= 4 ifTrue:[
   211         n <= 4 ifTrue:[
   211             s _ 0.
   212             s := 0.
   212             i _ 0.
   213             i := 0.
   213             [(i _ i + 1) <= n] whileTrue: [s _ ((s bitShift: 8) bitOr: self next)].
   214             [(i := i + 1) <= n] whileTrue: [s := ((s bitShift: 8) bitOr: self next)].
   214             ^s
   215             ^ s
   215         ].
   216         ].
   216         s _ 0.
   217         s := 0.
   217         1 to: n do: [:j | s := s * 256 + self next].
   218         1 to: n do: [:j | s := s * 256 + self next].
   218         "reverse order of significance"
   219         "reverse order of significance"
   219         ^s truncated
   220         ^s truncated
   220 !
   221 !
   221 
   222 
   222 nextNumber: n put: v 
   223 nextNumber: n put: v 
   223         "Append to the receiver the argument, v, which is a positive SmallInteger or
   224         "Append to the receiver the argument, v, which is a positive Integer,
   224         a LargePositiveInteger, as the next n bytes.  Possibly pad with leading zeros."
   225          as the next n bytes.  Possibly pad with leading zeros."
   225 
       
   226 
   226 
   227         | vlen i |
   227         | vlen i |
   228         n < (vlen _ v digitLength) ifTrue: [self error: 'number too big'].
   228 
       
   229         n < (vlen := v digitLength) ifTrue: [self error: 'number too big'].
   229 
   230 
   230         "pad with leading zeros"
   231         "pad with leading zeros"
   231         i _ n.
   232         i := n.
   232         [i > vlen] whileTrue: [self nextPut: 0. i _ i - 1].
   233         [i > vlen] whileTrue: [self nextPut: 0. i := i - 1].
   233         i = 1 ifTrue: [^self nextPut: v].
   234         i = 1 ifTrue: [^self nextPut: v].
   234         [i > 0] whileTrue: [self nextPut: (v digitAt: i). i _ i - 1]
   235         [i > 0] whileTrue: [self nextPut: (v digitAt: i). i := i - 1]
   235 ! !
   236 ! !