extensions.st
changeset 2256 10583ebc7f64
parent 2241 81f10d4ad4bf
child 2260 fd7e5634d0d0
--- a/extensions.st	Wed Sep 16 17:17:37 2009 +0200
+++ b/extensions.st	Wed Sep 16 22:00:50 2009 +0200
@@ -107,6 +107,10 @@
     ^ aStream contents
 
     "
+     Transcript showCR:('%05x %d %f %o' printf:{ 123. 234*5. 1.234. 8r377 } ) 
+    "
+
+    "
      Transcript showCR: 'Some examples:'!!
 
      Transcript show:'''%#x %#X %03o%*.*s'' printf: #(16rABCD 16rEF 5 9 5 ''ghijklmn'') = .'.
@@ -138,6 +142,58 @@
 
 !CharacterArray methodsFor:'printing & storing'!
 
+printfWith:arg1 
+    "Format and print the receiver with <arg1> formatted in C style, as 
+     specified in the Unix C-language manual page for printf(3)."
+    
+    ^ self printf:(Array with:arg1)
+
+    "
+     Transcript showCR:('%05x' printfWith:123) 
+    "
+! !
+
+!CharacterArray methodsFor:'printing & storing'!
+
+printfWith:arg1 with:arg2 
+    "Format and print the receiver with <argI> formatted in C style, as 
+     specified in the Unix C-language manual page for printf(3)."
+    
+    ^ self printf:(Array with:arg1 with:arg2)
+
+    "
+     Transcript showCR:('%d %05x' printfWith:123 with:234) 
+    "
+! !
+
+!CharacterArray methodsFor:'printing & storing'!
+
+printfWith:arg1 with:arg2 with:arg3
+    "Format and print the receiver with <argI> formatted in C style, as 
+     specified in the Unix C-language manual page for printf(3)."
+    
+    ^ self printf:(Array with:arg1 with:arg2 with:arg3)
+
+    "
+     Transcript showCR:('%d %05x %08o' printfWith:123 with:234 with:345) 
+    "
+! !
+
+!CharacterArray methodsFor:'printing & storing'!
+
+printfWith:arg1 with:arg2 with:arg3 with:arg4
+    "Format and print the receiver with <argI> formatted in C style, as 
+     specified in the Unix C-language manual page for printf(3)."
+    
+    ^ self printf:(Array with:arg1 with:arg2 with:arg3 with:arg4)
+
+    "
+     Transcript showCR:('%d %05x %08o %b' printfWith:123 with:234 with:345 with:123) 
+    "
+! !
+
+!CharacterArray methodsFor:'printing & storing'!
+
 printf_formatArgCount
     "Return the number of arguments required/produced if the receiver is interpreted
      as a printf/scanf format control string.
@@ -185,7 +241,14 @@
 
      For copyright information, see goodies/String-printf_scanf.chg"
     
-    |ljust plus pound width precision pad char arg argString|
+    |nextArg ljust plus pound width precision pad char arg argString|
+
+    nextArg := [ 
+                    argStream atEnd ifTrue:[
+                        self error:'not enough arguments for format string'
+                    ].
+                    argStream next
+               ].
 
     ljust := plus := pound := false.
     width := 0.
@@ -216,7 +279,7 @@
         char := inStream peek
     ].
     char == $* ifTrue:[
-        width := argStream next.
+        width := nextArg value.
         inStream next.
         char := inStream peek
     ].
@@ -231,7 +294,7 @@
         inStream next.
         char := inStream peek.
         char == $* ifTrue:[
-            precision := argStream next.
+            precision := nextArg value.
             inStream next.
             char := inStream peek
         ] ifFalse:[
@@ -244,7 +307,7 @@
         char := inStream peek
     ].
     ('feg' includes:char) ifTrue:[
-        arg := argStream next asFloat.
+        arg := nextArg value asFloat.
         precision := precision min:6.
         argString := WriteStream on:VariableString "String" new.
         char == $g ifTrue:[
@@ -292,28 +355,34 @@
         ^ inStream next
     ].
     char == $c ifTrue:[
-        arg := String with:argStream next asCharacter
+        arg := String with:nextArg value asCharacter
     ].
     char == $s "Assume the arg is a String or Symbol." ifTrue:[
-        arg := argStream next asString
+        arg := nextArg value asString
     ].
     char == $d ifTrue:[
-        arg := argStream next asInteger printString.
+        arg := nextArg value asInteger printString.
         plus ifTrue:[
             arg := '+' , arg
         ]
     ].
     char == $u ifTrue:[
-        arg := argStream next asInteger abs printString
+        arg := nextArg value asInteger abs printString
     ].
     char == $o ifTrue:[
-        arg := argStream next asInteger abs printStringRadix:8.
+        arg := nextArg value asInteger abs printStringRadix:8.
+        pound ifTrue:[
+            arg := '0' , arg
+        ]
+    ].
+    char == $b ifTrue:[
+        arg := nextArg value asInteger abs printStringRadix:2.
         pound ifTrue:[
             arg := '0' , arg
         ]
     ].
     ('xX' includes:char) ifTrue:[
-        arg := argStream next asInteger abs printStringRadix:16.
+        arg := nextArg value asInteger abs printStringRadix:16.
         pound ifTrue:[
             arg := '0x' , arg
         ]
@@ -498,12 +567,20 @@
     ('FEGfeg' includes:char) ifTrue:[
         final value:(Float readFrom:data)
     ].
+    ('b' includes:char) ifTrue:[
+        final value:(Integer readFrom:data radix:2)
+    ].
     ('Oo' includes:char) ifTrue:[
         final value:(Integer readFrom:data radix:8)
     ].
     ('Xx' includes:char) ifTrue:[
         final value:(Integer readFrom:data radix:16)
     ]
+
+    "
+     '%d %x' sscanf:'1234 ff00'    
+     '%d %x %b' sscanf:'1234 ff00 1001'    
+    "
 ! !
 
 !CharacterArray methodsFor:'converting'!
@@ -519,6 +596,7 @@
 
     "
      '%d %x' sscanf:'1234 ff00'    
+     '%d %x %b' sscanf:'1234 ff00 1001'    
     "
 ! !