--- a/extensions.st Mon Sep 14 11:47:44 2009 +0200
+++ b/extensions.st Mon Sep 14 11:54:45 2009 +0200
@@ -611,7 +611,51 @@
!String methodsFor:'printing'!
-formatArgCount
+printf:args
+ "Format and print the receiver with <args> formatted in C style, as
+ specified in the Unix C-language manual page for printf(3).
+
+ For copyright information, see goodies/String-printf_scanf.chg"
+
+ |aStream|
+
+ aStream := WriteStream on:String new.
+ self printf_printOn:aStream withData:args.
+ ^ aStream contents
+
+ "
+ Transcript showCR: 'Some examples:'!!
+
+ Transcript show:'''%#x %#X %03o%*.*s'' printf: #(16rABCD 16rEF 5 9 5 ''ghijklmn'') = .'.
+ Transcript show: ('%#x %#X %03o%*.*s' printf: #(16rABCD 16rEF 5 9 5 'ghijklmn')).
+ Transcript showCR: '.'
+
+ Transcript show: '''%- 10.4s%.2e'' printf: (Array with: ''abcdefghijkl'' with: Float pi) = .'.
+ Transcript show: ('%- 10.4s%.2e' printf: (Array with: 'abcdefghijkl' with: Float pi)).
+ Transcript showCR: '.'
+
+ Transcript show: '''%8.3f'' printf: (Array with: 200 sqrt negated) = .'.
+ Transcript show: ('%8.3f' printf: (Array with: 200 sqrt negated)).
+ Transcript showCR: '.'
+
+ Transcript show: '''%c'' printf: #(16r41) = .'.
+ Transcript show: ('%c' printf: #(16r41)).
+ Transcript showCR: '.'
+
+ Transcript show: '''%f%2s%s%s%s'' sscanf: ''237.0 this is a test'' = '.
+ Transcript showCR: ('%f%2s%s%s%s' sscanf: '237.0 this is a test') printString.
+
+ Transcript show: '''%d%f%s'' sscanf: ''25 54.32e-01 monday'' = '.
+ Transcript showCR: ('%d%f%s' sscanf: '25 54.32e-01 monday') printString.
+
+ Transcript show: '''%f%*f %8[A-F0-9]%c%d 0x%x%f'' sscanf: ''12.45 1048.73 AE40Z527 0x75BCD15 34'' = '.
+ Transcript showCR: ('%f%*f %8[A-F0-9]%c%d 0x%x%f' sscanf: '12.45 1048.73 AE40Z527 0x75BCD15 34') printString.
+ "
+! !
+
+!String methodsFor:'printing'!
+
+printf_formatArgCount
"Return the number of arguments required/produced if the receiver is interpreted
as a printf/scanf format control string.
For copyright information, see goodies/String-printf_scanf.chg"
@@ -638,16 +682,17 @@
!String methodsFor:'private'!
-printArgFrom:inStream to:outStream withData:argStream
+printf_printArgFrom:inStream to:outStream withData:argStream
"Interpret the required number of arguments from <argStream>
- according to the formatting information in <inStream>. Place
- the interpretation on <outStream>. The interpretation is C
- printf(3) style, as described in the UTek manual page for
- printf(3). <inStream> is assumed to be positioned just past
+ according to the formatting information in <inStream>.
+ Place the interpretation on <outStream>.
+ The interpretation is C printf(3) style, as
+ specified in the Unix C-language manual page for printf(3).
+ <inStream> is assumed to be positioned just past
$%, and a complete control string is assumed available.
- Return when the conversion control string is consumed. Leave <inStream> pointing
- past the last character in the conversion control string.
+ Return when the conversion control string is consumed.
+ Leave <inStream> pointing past the last character in the conversion control string.
This code assumes that <inStream> is formatted according to
specification, and error checking is minimal. Unexpected
@@ -657,7 +702,7 @@
For copyright information, see goodies/String-printf_scanf.chg"
- |ljust plus pound width precision pad char arg argString sci|
+ |ljust plus pound width precision pad char arg argString|
ljust := plus := pound := false.
width := 0.
@@ -812,10 +857,10 @@
!String methodsFor:'printing'!
-printOn:outStream withData:args
+printf_printOn:outStream withData:args
"Format and print the receiver on <outStream> with <args>
- formatted in C style, as described in the UTek manual page for
- printf(3).
+ formatted in C style, as specified in the Unix C-language manual page for printf(3).
+
For copyright information, see goodies/String-printf_scanf.chg"
|argStream inStream char|
@@ -827,64 +872,56 @@
outStream nextPut:char
] ifTrue:[
self
- printArgFrom:inStream
+ printf_printArgFrom:inStream
to:outStream
withData:argStream
]
]
! !
-!String methodsFor:'printing'!
+!String methodsFor:'converting'!
-printf:args
- "Format and print the receiver with <args> formatted in C style, as described
- in the UTek manual page for printf(3).
+scanf:dataStream
+ "Return a Collection of objects found in the Character Stream
+ <dataStream> as interpreted according to the receiver.
+ The receiver is assumed to be a conversion control string as
+ specified in the Unix C-language manual page for scanf(3).
For copyright information, see goodies/String-printf_scanf.chg"
- |aStream|
+ |results format char|
- aStream := WriteStream on:String new.
- self printOn:aStream withData:args.
- ^ aStream contents
+ results := OrderedCollection new.
+ format := ReadStream on:self.
+ [ format atEnd ] whileFalse:[
+ char := format next.
+ (char == Character space or:[ char == Character tab ]) ifTrue:[
+ dataStream skipSeparators.
+ format skipSeparators
+ ].
+ char == $% ifTrue:[
+ self
+ scanf_scanArgFrom:dataStream
+ to:results
+ format:format
+ ] ifFalse:[
+ dataStream peekFor:char
+ ]
+ ].
+ ^ results
"
- Transcript showCR: 'Some examples:'!!
-
- Transcript show:'''%#x %#X %03o%*.*s'' printf: #(16rABCD 16rEF 5 9 5 ''ghijklmn'') = .'.
- Transcript show: ('%#x %#X %03o%*.*s' printf: #(16rABCD 16rEF 5 9 5 'ghijklmn')).
- Transcript showCR: '.'
-
- Transcript show: '''%- 10.4s%.2e'' printf: (Array with: ''abcdefghijkl'' with: Float pi) = .'.
- Transcript show: ('%- 10.4s%.2e' printf: (Array with: 'abcdefghijkl' with: Float pi)).
- Transcript showCR: '.'
-
- Transcript show: '''%8.3f'' printf: (Array with: 200 sqrt negated) = .'.
- Transcript show: ('%8.3f' printf: (Array with: 200 sqrt negated)).
- Transcript showCR: '.'
-
- Transcript show: '''%c'' printf: #(16r41) = .'.
- Transcript show: ('%c' printf: #(16r41)).
- Transcript showCR: '.'
-
- Transcript show: '''%f%2s%s%s%s'' sscanf: ''237.0 this is a test'' = '.
- Transcript showCR: ('%f%2s%s%s%s' sscanf: '237.0 this is a test') printString.
-
- Transcript show: '''%d%f%s'' sscanf: ''25 54.32e-01 monday'' = '.
- Transcript showCR: ('%d%f%s' sscanf: '25 54.32e-01 monday') printString.
-
- Transcript show: '''%f%*f %8[A-F0-9]%c%d 0x%x%f'' sscanf: ''12.45 1048.73 AE40Z527 0x75BCD15 34'' = '.
- Transcript showCR: ('%f%*f %8[A-F0-9]%c%d 0x%x%f' sscanf: '12.45 1048.73 AE40Z527 0x75BCD15 34') printString.
+ '%d %x' scanf:(ReadStream on:'1234 ff00')
"
! !
!String methodsFor:'private'!
-scanArgFrom:dataStream to:collection format:format
+scanf_scanArgFrom:dataStream to:collection format:format
"Add to <collection> an object who's representation is found
in <dataStream> interpreted according to the conversion
- control string in the Stream <format>. <format> is assumed to
- be positioned just past a $%, and a complete control string is
- assumed available.
+ control string in the Stream <format>.
+ <format> is assumed to be positioned just past a $%, and a complete control
+ string is assumed available.
Return when the conversion control string is consumed. Leave
<format> pointing past the last character in the conversion
@@ -988,46 +1025,11 @@
!String methodsFor:'converting'!
-scanf:dataStream
- "Return a Collection of objects found in the Character Stream
- <dataStream> as interpreted according to the receiver. The
- receiver is assumed to be a conversion control string as
- specified in the UTek manual page for scanf(3).
- For copyright information, see goodies/String-printf_scanf.chg"
-
- |results format char|
-
- results := OrderedCollection new.
- format := ReadStream on:self.
- [ format atEnd ] whileFalse:[
- char := format next.
- (char == Character space or:[ char == Character tab ]) ifTrue:[
- dataStream skipSeparators.
- format skipSeparators
- ].
- char == $% ifTrue:[
- self
- scanArgFrom:dataStream
- to:results
- format:format
- ] ifFalse:[
- dataStream peekFor:char
- ]
- ].
- ^ results
-
- "
- '%d %x' scanf:(ReadStream on:'1234 ff00')
- "
-! !
-
-!String methodsFor:'converting'!
-
sscanf:string
"Return a Collection of objects found in <string> as
- interpreted according to the receiver. The receiver is
- assumed to be a conversion control string as specified in the
- UTek manual page for scanf(3).
+ interpreted according to the receiver.
+ The receiver is assumed to be a conversion control string as
+ specified in the Unix C-language manual page for scanf(3).
For copyright information, see goodies/String-printf_scanf.chg"
^ self scanf:(ReadStream on:string)