'From Squeak3.7alpha of ''11 September 2003'' [latest update: #5548] on 26 November 2003 at 10:43:12 am'!
"Change Set: IntegerPrintOpt-gk
Date: 25 November 2003
Author: Gšran Krampe
Improvement of Integer/SmallInteger>>printOn:base: spurred by results comparing SharpSmallalk with Squeak. I have borrowed John's implementation and merged it with the old one.
It is up to 3 times faster than the old code. In the Smopstone benchmark it improves the 'generate strings' benchmark by about 20%, but the seemingly small improvement is because it only tests SmallIntegers. The real benefits come when doing LargeIntegers.
The code in SmallInteger is a bit different - since I rewrote the old version. I am not sure if we really need two slightly different implementations, they might be mergable - I did try the Integer version for SmallIntegers and the difference is almost none.
The code might be improved - especially if we could calculate the number of letters, for example digits is set on each invocation, but I assume that is negligible.
Of course, a plugin might make this even faster. :)"!
!Integer methodsFor: 'printing' stamp: 'gk 11/26/2003 10:26'!
printOn: aStream base: base
"Print a representation of the receiver on the stream
in base where
2 <= <= 16. If is other than 10
it is written first separated by $r followed by the number
like for example: 16rFCE2"
| current letters digits quo i |
self < 0 ifTrue: [
aStream nextPut: $-.
^self negated printOn: aStream base: base].
digits _ '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'.
base = 10 ifFalse: [aStream print: base; nextPut: $r].
current _ self.
i _ self digitLength * 8.
letters _ String new: i.
[current < base] whileFalse:
[quo _ current quo: base.
letters at: i put: (digits at: (current - (quo * base)) + 1).
i _ i - 1.
current _ quo].
letters at: i put: (digits at: current + 1).
aStream nextPutAll: (letters copyFrom: i to: letters size)! !
!SmallInteger methodsFor: 'printing' stamp: 'gk 11/26/2003 10:27'!
printOn: aStream base: base
"Refer to the comment in Integer|printOn:base:."
"SmallInteger maxVal printStringBase: 2"
| x i digits letters |
self < 0 ifTrue: [
aStream nextPut: $-.
^ self negated printOn: aStream base: base].
digits _ '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'.
base = 10 ifFalse: [aStream print: base; nextPut: $r].
letters _ String new: 32.
x _ self.
i _ 33.
[x >= base] whileTrue: [
letters at: (i _ i - 1) put: (digits at: (x \\ base) + 1).
x _ x // base].
letters at: (i _ i - 1) put: (digits at: x + 1).
aStream nextPutAll: (letters copyFrom: i to: letters size)! !