author | Claus Gittinger <cg@exept.de> |
Tue, 20 Jun 2017 18:06:12 +0200 | |
changeset 4435 | 96ba42c85b3e |
parent 4434 | 92084009a524 |
child 4441 | 592237602835 |
permissions | -rw-r--r-- |
1463 | 1 |
"{ Package: 'stx:libbasic2' }" |
2 |
||
3539 | 3 |
"{ NameSpace: Smalltalk }" |
4 |
||
1463 | 5 |
Object subclass:#PrintfScanf |
6 |
instanceVariableNames:'' |
|
4084 | 7 |
classVariableNames:'Singleton' |
1463 | 8 |
poolDictionaries:'' |
9 |
category:'System-Support' |
|
10 |
! |
|
11 |
||
12 |
!PrintfScanf class methodsFor:'documentation'! |
|
13 |
||
1465 | 14 |
documentation |
15 |
" |
|
4416 | 16 |
Contributed by Jan Steinman donated to the community in 1989. |
1465 | 17 |
|
18 |
Provided AS-IS - no warranty, use at your own risk. |
|
19 |
||
20 |
Original comment: |
|
21 |
||
22 |
NAME printf-scanf |
|
23 |
AUTHOR Jan Steinman <jans@tekgvs.labs.tek.com> |
|
24 |
FUNCTION printf and scanf for Smalltalk |
|
25 |
ST-VERSIONS Tek 2.2.2a 4.0 |
|
26 |
PREREQUISITES CharacterComparing |
|
27 |
CONFLICTS |
|
28 |
DISTRIBUTION world |
|
29 |
VERSION 1.1 |
|
30 |
DATE Apr 1989? |
|
31 |
SUMMARY |
|
32 |
||
33 |
The following methods implement printf and scanf functionality. They |
|
34 |
are intended to be used to ease porting between Smalltalk and C, and |
|
35 |
for facilitating machine-machine communication. They are not at all |
|
36 |
intended as replacements for Smalltalk's printOn: functionality. |
|
37 |
||
38 |
Jan Steinman - N7JDB |
|
39 |
Tektronix Electronic Systems Laboratory |
|
40 |
Box 500, MS 50-370, Beaverton, OR 97077 |
|
41 |
(w)503/627-5881 (h)503/657-7703 |
|
4416 | 42 |
|
43 |
changes: |
|
44 |
slight changes to make it work with higher precision real numbers |
|
45 |
(i.e. asking for the precision instead of hard-coding it) |
|
4417 | 46 |
|
47 |
no need for the singleton - classes are already singletons ready to use exactly for that. |
|
48 |
(no need to use C++/Java patterns here in Smalltalk) |
|
1465 | 49 |
" |
50 |
! |
|
51 |
||
1463 | 52 |
examples |
53 |
" |
|
4416 | 54 |
self printf:'%#x %#X %03o%*.*s' arguments: #(16rABCD 16rEF 5 9 5 'ghijklmn') |
55 |
-> '0xabcd 0xEF 005 ghijk' |
|
1463 | 56 |
|
4416 | 57 |
self printf:'%- 10.4s%.2e' arguments: { 'abcdefghijkl' . Float pi } |
58 |
-> ' abcd 3.14e0' |
|
59 |
||
60 |
self printf:'%8.3f' arguments: { 200 sqrt negated } |
|
61 |
-> ' -14.142' |
|
1463 | 62 |
|
4394 | 63 |
self printf:'%8.3f' on:Transcript arguments: { 200 sqrt negated } |
64 |
self printf:'%10.4f' on:Transcript arguments: { 200 sqrt negated } |
|
65 |
self printf:'%20.10f' on:Transcript arguments: { 200 sqrt negated } |
|
66 |
||
67 |
self printf:'%20.10f' on:Transcript arguments: { 1.234567890123456789f } |
|
68 |
self printf:'%20.10f' on:Transcript arguments: { 1.234567890123456789q } |
|
1463 | 69 |
|
4416 | 70 |
self printf:'%x' arguments: #(16r41) -> '41' |
71 |
self printf:'%#x' arguments: #(16r41) -> '0x41' |
|
72 |
self printf:'%d' arguments: #(16r41) -> '65' |
|
73 |
self printf:'%b' arguments: #(16r41) -> '1000001' |
|
74 |
self printf:'%c' arguments: #(16r41) -> 'A' |
|
75 |
self printf:'%c' arguments: #( $A ) -> 'A' |
|
76 |
self printf:'%s' arguments: #( $A ) -> 'A' |
|
77 |
self printf:'%s' arguments: #( 'hello' ) -> 'hello' |
|
78 |
self printf:'%4s' arguments: #( 'hello' ) -> 'hello' |
|
79 |
self printf:'%7s' arguments: #( 'hello' ) -> ' hello' |
|
1463 | 80 |
|
4416 | 81 |
self sscanf:'%f%2s%s%s%s' fromString: '237.0 this is a test' |
82 |
-> OrderedCollection(237.0 'th' 'is' 'is' 'a') |
|
1463 | 83 |
|
4416 | 84 |
self sscanf:'%d%f%s' fromString: '25 54.32e-01 monday' |
85 |
-> OrderedCollection(25 5.432 'monday') |
|
1464 | 86 |
|
4416 | 87 |
self sscanf:'%f%*f %8[A-F0-9]%c%d 0x%x%f' fromString: '12.45 1048.73 AE40Z527 0x75BCD15 34' |
88 |
-> OrderedCollection(12.45 'AE40' 'Z' 527 123456789 34.0) |
|
1463 | 89 |
|
4416 | 90 |
'%#x %#X %03o%*.*s' printf: #(16rABCD 16rEF 5 9 5 'ghijklmn') |
91 |
-> '0xabcd 0xEF 005 ghijk' |
|
1463 | 92 |
|
3115 | 93 |
'%- 10.4s%.2e' printf: { 'abcdefghijkl' . Float pi } |
4416 | 94 |
-> ' abcd 3.14e0' |
1463 | 95 |
|
3115 | 96 |
'%8.3f' printf: { 200 sqrt negated } |
4416 | 97 |
-> ' -14.142' |
1463 | 98 |
|
99 |
'%c' printf: #(16r41) |
|
4416 | 100 |
-> 'A' |
1463 | 101 |
|
102 |
'%f%2s%s%s%s' sscanf: '237.0 this is a test' |
|
4416 | 103 |
-> OrderedCollection(237.0 'th' 'is' 'is' 'a') |
1463 | 104 |
|
105 |
'%d%f%s' sscanf: '25 54.32e-01 monday' |
|
4416 | 106 |
-> OrderedCollection(25 5.432 'monday') |
1463 | 107 |
|
108 |
'%f%*f %8[A-F0-9]%c%d 0x%x%f' sscanf: '12.45 1048.73 AE40Z527 0x75BCD15 34' |
|
4416 | 109 |
-> OrderedCollection(12.45 'AE40' 'Z' 527 123456789 34.0) |
4417 | 110 |
|
111 |
'%d\n' printf:{ 1234 } on:Transcript |
|
112 |
||
113 |
'%f\n' printf:{ 1234.0 } on:Transcript |
|
114 |
'%f\n' printf:{ 1234.0 asShortFloat } on:Transcript |
|
115 |
'%f\n' printf:{ 1234.0 asLongFloat } on:Transcript |
|
116 |
'%f\n' printf:{ 1234.0 asQDouble } on:Transcript |
|
1463 | 117 |
" |
4434 | 118 |
! |
119 |
||
120 |
format |
|
121 |
" |
|
122 |
Format specifier: |
|
123 |
||
124 |
required: leading '%' |
|
125 |
optional: '-' (POSIX refers to this as the <<flags>>) |
|
126 |
optional: positive number or '*' (POSIX <<width>>) |
|
127 |
optional: period followed by positive number or * (POSIX <<precision>>) |
|
128 |
optional: an h or l to indicate size of data (POSIX <<length>>) |
|
129 |
required: character describing output behavior (POSIX <<conversion specifier>>) |
|
130 |
||
131 |
Various implementations of printf have added different functionality. |
|
132 |
||
133 |
ANSI standards up through C99: |
|
134 |
||
135 |
more flags '+' ' ' '0' '#' |
|
136 |
more lengths 'L' 'hh' 'll' 'j' 'z' 't' |
|
137 |
more conversions 'F' 'a' 'A' 'n' |
|
138 |
||
139 |
The POSIX specification of printf added: |
|
140 |
||
141 |
positional parameters to identify argument indices |
|
142 |
more flags ''' (single quote) |
|
143 |
more conversions 'C' 'S' |
|
144 |
clarifications regarding corner cases and 'undefined behavior' |
|
145 |
||
146 |
BSD implementations added: |
|
147 |
||
148 |
more lengths 'q' |
|
149 |
more conversions 'D' 'U' 'O' |
|
150 |
||
151 |
glibc (GNU) added: |
|
152 |
||
153 |
more lengths 'Z' |
|
154 |
more conversions 'm' |
|
155 |
||
156 |
Windows C Runtime (CRT) added: |
|
157 |
||
158 |
more lengths 'I' 'I32' 'I64' 'w' |
|
159 |
||
160 |
glibc and CRT both added 'Z'. |
|
161 |
glibc uses 'Z' for the length size_t. |
|
162 |
CRT uses Z as a conversion for length-prefixed strings. |
|
163 |
This implementation takes the former approach, handling 'Z' in the same way as 'z'. |
|
164 |
||
165 |
BSD and IBM C library both added 'D'. |
|
166 |
BSD uses D as a conversion, namely as an alias of 'ld'. |
|
167 |
IBM uses 'D' for the length for _Decimal64, a decimal floating point type, in accordance with ISO/IEC TR 24732. |
|
168 |
This implementation takes the former approach. |
|
169 |
||
170 |
This implementation also adds new conversions: |
|
171 |
||
172 |
'b' and 'B' for binary (base-2) integer renderings |
|
173 |
'y' and 'Y' for true/false and yes/no Boolean conversions |
|
174 |
'J' for JSON |
|
175 |
'T' and 'V' for JS typeof and valueOf inspection |
|
176 |
||
177 |
Conversions (upper case same as lower case): |
|
178 |
'a' (not implemented) base-2 floating point exp form |
|
179 |
'b' binary (base 2) |
|
180 |
'c' character or (first char of string) |
|
181 |
'd' character or (first char of string) |
|
182 |
'e' base-10 floating point exp form (scientific) |
|
183 |
'f' base-10 floating point decimal form (non-scientific) |
|
184 |
'g' 'e' or 'f', whichever looks more appropriate (based on value) |
|
185 |
'i' integer (alias for 'd') |
|
186 |
'j' (not implemented) JSON format |
|
187 |
'n' (not implemented) stores number of characters written into arg |
|
188 |
'o' base-8 octal |
|
189 |
'p' (not implemented) pointer |
|
190 |
's' string |
|
191 |
't' type (i.e. class name) |
|
192 |
'u' (not implemented) unsigned (negative values are converted) |
|
193 |
'v' (not implemented) store string |
|
194 |
'x' base-16 hex |
|
195 |
||
196 |
Parameter selection (not implemented): |
|
197 |
||
198 |
<n>$ take n'th parameter |
|
199 |
||
200 |
Dynamic width/precision (consumed in order as presented): |
|
201 |
||
202 |
* take width/parameter from next argument |
|
203 |
||
204 |
PrintfScanf printf:'|%s|' arguments:{ 'abc' } -> '|abc|' |
|
205 |
PrintfScanf printf:'|%5s|' arguments:{ 'abc' } -> '| abc|' |
|
206 |
PrintfScanf printf:'|%*s|' arguments:{ 5 . 'abc' } -> '| abc|' |
|
207 |
||
208 |
PrintfScanf printf:'|%8f|' arguments:{ 1.234 } -> '| 1.234|' |
|
209 |
PrintfScanf printf:'|%*f|' arguments:{ 8 . 1.234 } -> '| 1.234|' |
|
210 |
||
211 |
||
212 |
Negative width will fill at the right: |
|
213 |
||
214 |
PrintfScanf printf:'|%5s|' arguments:{ 'abc' } -> '| abc|' |
|
215 |
PrintfScanf printf:'|%-5s|' arguments:{ 'abc' } -> '|abc |' |
|
216 |
PrintfScanf printf:'|%-*s|' arguments:{ 5 . 'abc' } -> '|abc |' |
|
217 |
PrintfScanf printf:'|%*s|' arguments:{ -5 . 'abc' } -> '|abc |' |
|
218 |
||
219 |
PrintfScanf printf:'|%*f|' arguments:{ -8 . 1.234 } -> '|1.234 |' |
|
220 |
PrintfScanf printf:'|%-8f|' arguments:{ 1.234 } -> '|1.234 |' |
|
221 |
PrintfScanf printf:'|%-*f|' arguments:{ 8 . 1.234 } -> '|1.234 |' |
|
222 |
PrintfScanf printf:'|%-*f|' arguments:{ -8 . 1.234 } -> '|1.234 |' |
|
223 |
||
4435 | 224 |
A Zero as fill character: |
225 |
||
226 |
PrintfScanf printf:'|%05s|' arguments:{ 'abc' } -> '|00abc|' |
|
227 |
PrintfScanf printf:'|%-05s|' arguments:{ 'abc' } -> '|abc00|' |
|
228 |
||
229 |
PrintfScanf printf:'|%0*f|' arguments:{ -8 . 1.234 } -> '|1.234 |' |
|
230 |
PrintfScanf printf:'|%-8f|' arguments:{ 1.234 } -> '|1.234 |' |
|
231 |
PrintfScanf printf:'|%-*f|' arguments:{ 8 . 1.234 } -> '|1.234 |' |
|
232 |
PrintfScanf printf:'|%-*f|' arguments:{ -8 . 1.234 } -> '|1.234 |' |
|
4434 | 233 |
" |
1463 | 234 |
! ! |
235 |
||
4084 | 236 |
!PrintfScanf class methodsFor:'instance creation'! |
237 |
||
238 |
new |
|
239 |
Singleton isNil ifTrue:[ |
|
240 |
Singleton := self basicNew |
|
241 |
]. |
|
242 |
^ Singleton |
|
243 |
! ! |
|
244 |
||
4416 | 245 |
!PrintfScanf class methodsFor:'printing'! |
2875 | 246 |
|
4416 | 247 |
printArgFrom:formatStream to:outStream arguments:argStream |
2875 | 248 |
"Interpret the required number of arguments from <argStream> |
4417 | 249 |
according to the formatting information in <formatStream>. |
250 |
Place the interpretation on <outStream>. |
|
251 |
The interpretation is C printf(3) style, as described in the UTek manual page for printf(3). |
|
252 |
<formatStream> is assumed to be positioned just past |
|
2875 | 253 |
$%, and a complete control string is assumed available. |
254 |
||
255 |
Return when the conversion control string is consumed. |
|
4416 | 256 |
Leave <formatStream> pointing past the last character in the conversion control string. |
2875 | 257 |
|
4416 | 258 |
This code assumes that <formatStream> is formatted according to |
2875 | 259 |
specification, and error checking is minimal. Unexpected |
260 |
results will be obtained by illegal control strings, or when |
|
261 |
argument types do not match conversion codes, but it probably |
|
262 |
won't dump core, like C does in such cases!!!!" |
|
263 |
||
4417 | 264 |
|nextArg ljust plus pound width precision pad char arg argString | |
265 |
||
266 |
nextArg := [ |
|
267 |
argStream atEnd ifTrue:[ |
|
268 |
self error:'not enough arguments for format string' |
|
269 |
]. |
|
270 |
argStream next |
|
271 |
]. |
|
2875 | 272 |
|
273 |
ljust := plus := pound := false. |
|
274 |
width := 0. |
|
4401 | 275 |
"/ precision := SmallInteger maxVal. |
276 |
precision := nil. |
|
2875 | 277 |
pad := $ . |
4416 | 278 |
char := formatStream peek. |
2875 | 279 |
|
280 |
char == $% ifTrue:[ |
|
4416 | 281 |
^ outStream nextPut: formatStream next |
2875 | 282 |
]. |
283 |
||
4434 | 284 |
"/ flags: |
285 |
||
2875 | 286 |
char == $- ifTrue:[ |
287 |
ljust := true. |
|
4416 | 288 |
formatStream next. |
289 |
char := formatStream peek |
|
2875 | 290 |
]. |
291 |
||
292 |
char == $ ifTrue:[ |
|
293 |
outStream space. |
|
4416 | 294 |
formatStream next. |
295 |
char := formatStream peek |
|
2875 | 296 |
]. |
297 |
||
298 |
char == $+ ifTrue:[ |
|
299 |
plus := true. |
|
4416 | 300 |
formatStream next. |
301 |
char := formatStream peek |
|
2875 | 302 |
]. |
303 |
||
304 |
char == $# ifTrue:[ |
|
305 |
pound := true. |
|
4416 | 306 |
formatStream next. |
307 |
char := formatStream peek |
|
2875 | 308 |
]. |
309 |
||
4434 | 310 |
"/ possibly a width |
2875 | 311 |
char == $* ifTrue:[ |
4434 | 312 |
width := nextArg value. |
313 |
width isInteger ifFalse:[ |
|
314 |
self error:'non integer width argument in printf' |
|
315 |
]. |
|
316 |
width < 0 ifTrue:[ |
|
317 |
ljust := true. |
|
318 |
width := width negated |
|
319 |
]. |
|
4416 | 320 |
formatStream next. |
321 |
char := formatStream peek |
|
2875 | 322 |
]. |
323 |
||
324 |
char isDigit ifTrue:[ |
|
2889 | 325 |
char == $0 ifTrue: [pad := $0]. |
4434 | 326 |
width := Integer readFrom: formatStream onError:0. |
4416 | 327 |
char := formatStream peek |
2875 | 328 |
]. |
329 |
||
4434 | 330 |
"/ precision separator |
331 |
||
2875 | 332 |
char == $. ifTrue:[ |
4416 | 333 |
formatStream next. char := formatStream peek. |
4434 | 334 |
char == $* ifTrue: [ |
335 |
precision := nextArg value. |
|
336 |
precision isInteger ifFalse:[ |
|
337 |
self error:'non integer precision argument in printf' |
|
338 |
]. |
|
339 |
formatStream next. |
|
340 |
] ifFalse: [ |
|
341 |
precision := Integer readFrom: formatStream. |
|
342 |
]. |
|
4416 | 343 |
char := formatStream peek |
2875 | 344 |
]. |
345 |
||
346 |
char == $l ifTrue:[ |
|
347 |
"Ignore long specifier." |
|
4416 | 348 |
formatStream next. char := formatStream peek |
2875 | 349 |
]. |
350 |
||
351 |
('feg' includes: char) ifTrue:[ |
|
4417 | 352 |
arg := nextArg value. |
4394 | 353 |
arg isLimitedPrecisionReal ifTrue:[ |
4401 | 354 |
precision := precision ? (arg class defaultPrintPrecision). |
355 |
] ifFalse:[ |
|
356 |
arg := arg asFloat. |
|
357 |
precision := precision ? (Float defaultPrintPrecision). |
|
4394 | 358 |
]. |
359 |
argString := WriteStream on:''. |
|
360 |
char == $g ifTrue: [ |
|
361 |
self absPrintFloat:arg on: argString digits: precision + 1 ]. |
|
362 |
char == $f ifTrue: [ |
|
4416 | 363 |
self absDecimalPrintFloat:arg on: argString digits: precision + arg abs log10 + 1]. |
4394 | 364 |
char == $e ifTrue: [ |
365 |
self absScientificPrintFloat:arg on: argString digits: precision + 1]. |
|
366 |
argString := argString contents. |
|
367 |
arg < 0 |
|
368 |
ifTrue: [argString := '-', argString] |
|
369 |
ifFalse: [plus ifTrue: [argString := '+', argString]]. |
|
370 |
(precision = 0 and: [pound not]) ifTrue:[ |
|
371 |
(argString includes: $e) |
|
372 |
ifTrue: ["self halt"] |
|
373 |
ifFalse: [ |
|
374 |
argString := arg truncated printString]]. |
|
375 |
pound ifTrue:[ |
|
376 |
(argString includes: $e) |
|
377 |
ifTrue: ["self halt"] |
|
378 |
ifFalse:[ |
|
379 |
precision - (argString size - (argString indexOf: $.)) |
|
380 |
timesRepeat:[ |
|
381 |
argString := argString, '0']]]. |
|
382 |
ljust ifTrue: [outStream nextPutAll: argString]. |
|
383 |
width - argString size timesRepeat: [outStream nextPut: pad]. |
|
384 |
ljust ifFalse: [outStream nextPutAll: argString]. |
|
4416 | 385 |
^ formatStream next |
2875 | 386 |
]. |
387 |
||
388 |
char == $c ifTrue:[ |
|
4417 | 389 |
arg := nextArg value asCharacter asString |
2875 | 390 |
]. |
391 |
||
392 |
char == $s ifTrue:[ |
|
393 |
"Assume the arg is a String or Symbol." |
|
4417 | 394 |
arg := nextArg value asString |
2875 | 395 |
]. |
396 |
||
397 |
char == $d ifTrue:[ |
|
4417 | 398 |
arg := nextArg value asInteger printString. |
2889 | 399 |
plus ifTrue: [arg := '+', arg] |
2875 | 400 |
]. |
401 |
||
402 |
char == $u ifTrue:[ |
|
4417 | 403 |
arg := nextArg value asInteger abs printString |
2875 | 404 |
]. |
405 |
||
406 |
char == $o ifTrue:[ |
|
4417 | 407 |
arg := nextArg value asInteger abs printStringRadix: 8. |
4394 | 408 |
pound ifTrue: [arg := '0', arg] |
2875 | 409 |
]. |
410 |
||
411 |
('xX' includes: char) ifTrue:[ |
|
4417 | 412 |
arg := nextArg value asInteger abs printStringRadix: 16. |
2889 | 413 |
pound ifTrue: [arg := '0x', arg] |
414 |
]. |
|
415 |
('bB' includes: char) ifTrue:[ |
|
4417 | 416 |
arg := nextArg value asInteger abs printStringRadix: 2. |
2889 | 417 |
pound ifTrue: [arg := '0b', arg] |
2875 | 418 |
]. |
419 |
||
420 |
char == $x ifTrue:[ |
|
421 |
1 to: arg size do: [:i | |
|
4394 | 422 |
('ABCDEF' includes: (arg at: i)) ifTrue:[ |
423 |
arg at: i put: (arg at: i) asLowercase |
|
424 |
] |
|
3719 | 425 |
] |
2875 | 426 |
]. |
427 |
||
4401 | 428 |
precision := precision ? arg size. |
2875 | 429 |
ljust ifTrue: [outStream nextPutAll: (arg copyFrom: 1 to: precision)]. |
430 |
width - precision timesRepeat: [outStream nextPut: pad]. |
|
431 |
ljust ifFalse: [outStream nextPutAll: (arg copyFrom: 1 to: precision)]. |
|
4416 | 432 |
^ formatStream next |
2875 | 433 |
|
4434 | 434 |
"Modified: / 20-06-2017 / 15:26:02 / cg" |
2875 | 435 |
! |
436 |
||
4416 | 437 |
printf:formatString argument:arg |
438 |
"Format and print the receiver with <arg> formatted in C style, |
|
439 |
as described in the UTek manual page for printf(3)." |
|
440 |
||
441 |
^ self printf:formatString arguments:{ arg } |
|
442 |
||
443 |
" |
|
444 |
self printf:'%e' on:Transcript argument:(1.234 asShortFloat) |
|
445 |
self printf:'%e' on:Transcript argument:(1.234 asFloat) |
|
446 |
self printf:'%e' on:Transcript argument:(1.234 asLongFloat) |
|
447 |
self printf:'%e' on:Transcript argument:(1.234 asQDouble) |
|
448 |
self printf:'%e' on:Transcript argument:(1.234 asInteger) |
|
449 |
||
450 |
self printf:'%10e' on:Transcript argument:(1.234 asShortFloat) |
|
451 |
self printf:'%10e' on:Transcript argument:(1.234 asFloat) |
|
452 |
self printf:'%10e' on:Transcript argument:(1.234 asLongFloat) |
|
453 |
self printf:'%10e' on:Transcript argument:(1.234 asQDouble) |
|
454 |
self printf:'%10e' on:Transcript argument:(1.234 asInteger) |
|
455 |
||
456 |
self printf:'%010e' on:Transcript argument:(1.234 asInteger) |
|
457 |
self printf:'%-10e' on:Transcript argument:(1.234 asInteger) |
|
458 |
||
459 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asShortFloat) |
|
460 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asFloat) |
|
461 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asLongFloat) |
|
462 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asQDouble) |
|
463 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asInteger) |
|
464 |
" |
|
465 |
||
466 |
"Created: / 16-06-2017 / 14:50:08 / cg" |
|
467 |
! |
|
468 |
||
469 |
printf:formatString arguments:args |
|
2875 | 470 |
"Format and print the receiver with <args> formatted in C style, |
4416 | 471 |
as described in the UTek manual page for printf(3)." |
472 |
||
2875 | 473 |
|aStream| |
474 |
||
4416 | 475 |
aStream := WriteStream on:(formatString species new:100). |
476 |
self printf:formatString on:aStream arguments:args. |
|
2875 | 477 |
^ aStream contents |
478 |
||
479 |
" |
|
4416 | 480 |
self printf:'%e' on:Transcript arguments:{ (1.234 asShortFloat) } |
481 |
self printf:'%e' on:Transcript arguments:{ (1.234 asFloat) } |
|
482 |
self printf:'%e' on:Transcript arguments:{ (1.234 asLongFloat) } |
|
483 |
self printf:'%e' on:Transcript arguments:{ (1.234 asQDouble) } |
|
484 |
self printf:'%e' on:Transcript arguments:{ (1.234 asInteger) } |
|
485 |
||
486 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asShortFloat) } |
|
487 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asFloat) } |
|
488 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asLongFloat) } |
|
489 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asQDouble) } |
|
490 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asInteger) } |
|
491 |
||
492 |
self printf:'%010e' on:Transcript arguments:{ (1.234 asInteger) } |
|
493 |
self printf:'%-10e' on:Transcript arguments:{ (1.234 asInteger) } |
|
494 |
||
495 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asShortFloat) } |
|
496 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asFloat) } |
|
497 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asLongFloat) } |
|
498 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asQDouble) } |
|
499 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asInteger) } |
|
2875 | 500 |
" |
4394 | 501 |
|
4416 | 502 |
"Modified: / 19-06-2017 / 15:21:34 / cg" |
503 |
! |
|
504 |
||
505 |
printf:formatString on:outStream argument: arg |
|
506 |
"Format and print formatString on <outStream> with <arg> |
|
507 |
formatted in C style, as described in the UTek manual page for |
|
508 |
printf(3)." |
|
509 |
||
510 |
^ self printf:formatString on:outStream arguments:{ arg } |
|
511 |
||
512 |
" |
|
513 |
self printf:'%e' on:Transcript argument:(1.234 asShortFloat). Transcript cr. |
|
514 |
self printf:'%e' on:Transcript argument:(1.234 asFloat) . Transcript cr. |
|
515 |
self printf:'%e' on:Transcript argument:(1.234 asLongFloat) . Transcript cr. |
|
516 |
self printf:'%e' on:Transcript argument:(1.234 asQDouble) . Transcript cr. |
|
517 |
self printf:'%e' on:Transcript argument:(1.234 asInteger) . Transcript cr. |
|
518 |
||
519 |
self printf:'%10e' on:Transcript argument:(1.234 asShortFloat). Transcript cr. |
|
520 |
self printf:'%10e' on:Transcript argument:(1.234 asFloat) . Transcript cr. |
|
521 |
self printf:'%10e' on:Transcript argument:(1.234 asLongFloat) . Transcript cr. |
|
522 |
self printf:'%10e' on:Transcript argument:(1.234 asQDouble) . Transcript cr. |
|
523 |
self printf:'%10e' on:Transcript argument:(1.234 asInteger) . Transcript cr. |
|
524 |
||
525 |
self printf:'%010e' on:Transcript argument:(1.234 asInteger) . Transcript cr. |
|
526 |
self printf:'%-10e' on:Transcript argument:(1.234 asInteger) . Transcript cr. |
|
527 |
||
528 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asShortFloat). Transcript cr. |
|
529 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asFloat) . Transcript cr. |
|
530 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asLongFloat) . Transcript cr. |
|
531 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asQDouble) . Transcript cr. |
|
532 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asInteger) . Transcript cr. |
|
533 |
" |
|
534 |
||
535 |
"Created: / 16-06-2017 / 14:50:40 / cg" |
|
536 |
"Modified (comment): / 19-06-2017 / 15:07:22 / cg" |
|
2875 | 537 |
! |
538 |
||
539 |
printf:aFormatString on:outStream arguments: args |
|
540 |
"Format and print aFormatString on <outStream> with <args> |
|
541 |
formatted in C style, as described in the UTek manual page for |
|
4416 | 542 |
printf(3)." |
2875 | 543 |
|
4417 | 544 |
| argStream formatStream char escChar | |
2875 | 545 |
|
546 |
argStream := ReadStream on: args. |
|
4417 | 547 |
formatStream := ReadStream on: aFormatString. |
548 |
[formatStream atEnd] whileFalse:[ |
|
549 |
(char := formatStream next) == $% ifFalse: [ |
|
550 |
char == $\ ifTrue:[ |
|
551 |
escChar := formatStream next. |
|
552 |
escChar == $n ifTrue:[ char := Character nl ] |
|
553 |
ifFalse:[ escChar == $t ifTrue:[ char := Character tab ] |
|
554 |
ifFalse:[ escChar == $r ifTrue:[ char := Character return ] |
|
555 |
ifFalse:[ char := escChar ]]] |
|
556 |
]. |
|
557 |
outStream nextPut:char |
|
2875 | 558 |
] ifTrue: [ |
4417 | 559 |
self printArgFrom:formatStream to:outStream arguments:argStream |
2875 | 560 |
] |
561 |
] |
|
4394 | 562 |
|
4416 | 563 |
" |
4417 | 564 |
self printf:'%e\n' on:Transcript arguments:{ (1.234 asShortFloat) } |
565 |
self printf:'%f\n' on:Transcript arguments:{ (1.234 asShortFloat) } |
|
566 |
self printf:'%g\n' on:Transcript arguments:{ (1.234 asShortFloat) } |
|
567 |
||
568 |
self printf:'%e\n' on:Transcript arguments:{ (1.234 asFloat) } |
|
569 |
self printf:'%f\n' on:Transcript arguments:{ (1.234 asFloat) } |
|
570 |
self printf:'%g\n' on:Transcript arguments:{ (1.234 asFloat) } |
|
571 |
||
572 |
self printf:'%e\n' on:Transcript arguments:{ (1.234 asLongFloat) } |
|
573 |
self printf:'%e\n' on:Transcript arguments:{ (1.234 asQDouble) } |
|
574 |
self printf:'%e\n' on:Transcript arguments:{ (1.234 asInteger) } |
|
4416 | 575 |
|
576 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asShortFloat) } |
|
577 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asFloat) } |
|
578 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asLongFloat) } |
|
579 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asQDouble) } |
|
580 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asInteger) } |
|
581 |
||
582 |
self printf:'%010e' on:Transcript arguments:{ (1.234 asInteger) } |
|
583 |
self printf:'%-10e' on:Transcript arguments:{ (1.234 asInteger) } |
|
584 |
||
585 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asShortFloat) } |
|
586 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asFloat) } |
|
587 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asLongFloat) } |
|
588 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asQDouble) } |
|
589 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asInteger) } |
|
590 |
" |
|
591 |
||
4417 | 592 |
"Modified (comment): / 19-06-2017 / 15:49:10 / cg" |
4416 | 593 |
! ! |
594 |
||
4417 | 595 |
!PrintfScanf class methodsFor:'private helpers'! |
4416 | 596 |
|
4417 | 597 |
absDecimalPrintFloat:aFloat on:aStream digits:digits |
598 |
"Place a string representation of the receiver on <aStream>, |
|
599 |
using <digits> significant digits, using decimal notation." |
|
600 |
||
601 |
" |
|
4428 | 602 |
self printf:'%20.10f\n' on:Transcript arguments: { 1.234567890123456789f } |
603 |
self printf:'%20.10f\n' on:Transcript arguments: { 1.234567890123456789q } |
|
604 |
||
605 |
self printf:'%20.10f\n' on:Transcript arguments: { 1e1000 } |
|
606 |
self printf:'%20.10f\n' on:Transcript arguments: { 1e1000 asShortFloat } |
|
607 |
self printf:'%20.10f\n' on:Transcript arguments: { 1e1000 asLongFloat } |
|
608 |
self printf:'%20.10f\n' on:Transcript arguments: { 1e1000 asQDouble } |
|
609 |
||
4432 | 610 |
self printf:'%20.10f\n' on:Transcript arguments: { 0.0 log10 } |
611 |
self printf:'%20.10f\n' on:Transcript arguments: { 0.0 asShortFloat log10 } |
|
612 |
self printf:'%20.10f\n' on:Transcript arguments: { 0.0 asLongFloat log10 } |
|
613 |
self printf:'%20.10f\n' on:Transcript arguments: { 0.0 asQDouble log10 } |
|
614 |
||
4434 | 615 |
self printf:'%20.10f\n' on:Transcript arguments: { DomainError ignoreIn:[ -1.0 log10 ] } |
616 |
self printf:'%20.10f\n' on:Transcript arguments: { DomainError ignoreIn:[ -1.0 asShortFloat log10 ] } |
|
617 |
self printf:'%20.10f\n' on:Transcript arguments: { DomainError ignoreIn:[ -1.0 asLongFloat log10] } |
|
618 |
self printf:'%20.10f\n' on:Transcript arguments: { DomainError ignoreIn:[ -1.0 asQDouble log10] } |
|
619 |
||
620 |
self printf:'%10s\n' on:Transcript arguments:{ 'hello' } |
|
621 |
self printf:'%*s\n' on:Transcript arguments:{ 10 . 'hello' } |
|
4417 | 622 |
" |
4416 | 623 |
|
4417 | 624 |
|absVal exp x fuzz i| |
625 |
||
4428 | 626 |
aFloat isNaN ifTrue:[ |
627 |
aStream nextPutAll:'NAN'. |
|
628 |
^ self. |
|
629 |
]. |
|
630 |
aFloat isInfinite ifTrue:[ |
|
631 |
aStream nextPutAll:'INF'. |
|
632 |
^ self. |
|
633 |
]. |
|
634 |
aFloat = 0.0 ifTrue:[ |
|
635 |
aStream nextPutAll:'0'. |
|
636 |
^ self. |
|
637 |
]. |
|
638 |
||
4417 | 639 |
absVal := aFloat abs. |
4416 | 640 |
|
4417 | 641 |
"x is myself normalized to (1.0, 10.0), exp is my exponent" |
642 |
exp := absVal < 1.0 |
|
4428 | 643 |
ifTrue:[ (10.0 / absVal) log10 floor negated ] |
644 |
ifFalse:[ absVal log10 floor]. |
|
645 |
||
646 |
exp := exp asInteger. |
|
4417 | 647 |
x := absVal / (10.0 raisedTo:exp). |
4428 | 648 |
fuzz := 10.0 raisedTo:(1 - digits). |
4417 | 649 |
"round the last digit to be printed" |
650 |
x := 0.5 * fuzz + x. |
|
651 |
x >= 10.0 ifTrue:[ |
|
652 |
"check if rounding has unnormalized x" |
|
653 |
x := x / 10.0. |
|
654 |
exp := exp + 1 |
|
655 |
]. |
|
656 |
||
657 |
exp < 0 ifTrue:[ |
|
658 |
1 to:(1 - exp) do:[:j | |
|
659 |
"/ cg: huh - what sort of code is that???? |
|
660 |
"/ aStream nextPut:('0.000000000000' at:j) |
|
661 |
aStream nextPut:(j == 2 ifTrue:[$.] ifFalse:[$0]) |
|
662 |
]. |
|
663 |
||
664 |
]. |
|
665 |
[ x >= fuzz ] whileTrue:[ |
|
666 |
"use fuzz to track significance" |
|
4422 | 667 |
i := x truncated. |
668 |
i := i asInteger. |
|
4417 | 669 |
aStream nextPut:(48 + i) asCharacter. |
670 |
x := (x - i) * 10.0. |
|
671 |
fuzz := fuzz * 10.0. |
|
672 |
exp := exp - 1. |
|
4428 | 673 |
exp == -1 ifTrue:[ |
4417 | 674 |
aStream nextPut:$. |
675 |
] |
|
676 |
]. |
|
4428 | 677 |
|
4417 | 678 |
[ exp >= -1 ] whileTrue:[ |
679 |
aStream nextPut:$0. |
|
680 |
exp := exp - 1. |
|
4428 | 681 |
exp == -1 ifTrue:[ |
4417 | 682 |
aStream nextPut:$. |
683 |
] |
|
684 |
] |
|
4416 | 685 |
|
4434 | 686 |
"Modified (comment): / 20-06-2017 / 15:00:22 / cg" |
4416 | 687 |
! |
688 |
||
4417 | 689 |
absPrintFloat:aFloat on:aStream digits:digits |
690 |
"Place a string representation of the receiver on <aStream>, |
|
691 |
using <digits> significant digits." |
|
692 |
||
693 |
(aFloat < 1.0e6 and:[ aFloat > 1.0e-4 ]) ifTrue:[ |
|
694 |
self absDecimalPrintFloat:aFloat on:aStream digits:digits |
|
695 |
] ifFalse:[ |
|
696 |
self absScientificPrintFloat:aFloat on:aStream digits:digits |
|
697 |
] |
|
4416 | 698 |
|
4417 | 699 |
"Modified: / 19-06-2017 / 15:03:40 / cg" |
4416 | 700 |
! |
701 |
||
4417 | 702 |
absScientificPrintFloat:aFloat on:aStream digits:digits |
703 |
"Place a string representation of the receiver on <aStream>, |
|
704 |
using <digits> significant digits, using scientific notation." |
|
705 |
||
706 |
|absVal exp fuzz x q i| |
|
707 |
||
4428 | 708 |
aFloat isNaN ifTrue:[ |
709 |
aStream nextPutAll:'NAN'. |
|
710 |
^ self. |
|
711 |
]. |
|
712 |
aFloat isInfinite ifTrue:[ |
|
713 |
aStream nextPutAll:'INF'. |
|
714 |
^ self. |
|
715 |
]. |
|
716 |
aFloat = 0.0 ifTrue:[ |
|
717 |
aStream nextPutAll:'0'. |
|
718 |
^ self. |
|
719 |
]. |
|
720 |
||
4417 | 721 |
absVal := aFloat abs. |
4416 | 722 |
|
4417 | 723 |
"x is myself normalized to [1.0, 10.0), exp is my exponent" |
724 |
exp := absVal < 1.0 |
|
725 |
ifTrue:[ |
|
726 |
(10.0 / absVal) log10 floor asInteger negated] |
|
727 |
ifFalse:[ |
|
728 |
absVal log10 floor asInteger]. |
|
729 |
x := absVal / (10.0 raisedTo:exp). |
|
730 |
"round the last digit to be printed" |
|
731 |
fuzz := 10.0 raisedTo:1 - digits. |
|
732 |
x := 0.5 * fuzz + x. |
|
733 |
x >= 10.0 ifTrue:[ "check if rounding has unnormalized x" |
|
734 |
x := x / 10.0. |
|
735 |
exp := exp + 1 |
|
736 |
]. |
|
737 |
q := exp. |
|
738 |
exp := 0. |
|
739 |
[ x >= fuzz ] whileTrue:[ |
|
740 |
"use fuzz to track significance" |
|
741 |
i := x truncated. |
|
742 |
i := i asInteger. |
|
743 |
aStream nextPut:(48 + i) asCharacter. |
|
744 |
x := (x - i) * 10.0. |
|
745 |
fuzz := fuzz * 10.0. |
|
746 |
exp := exp - 1. |
|
747 |
exp = -1 ifTrue:[ |
|
748 |
aStream nextPut:$. |
|
749 |
] |
|
750 |
]. |
|
751 |
[ exp >= -1 ] whileTrue:[ |
|
752 |
aStream nextPut:$0. |
|
753 |
exp := exp - 1. |
|
754 |
exp = -1 ifTrue:[ |
|
755 |
aStream nextPut:$. |
|
756 |
] |
|
757 |
]. |
|
758 |
aStream nextPut:$e. |
|
759 |
q printOn:aStream |
|
4416 | 760 |
|
4428 | 761 |
"Modified: / 20-06-2017 / 11:45:11 / cg" |
4416 | 762 |
! |
763 |
||
4417 | 764 |
formatArgCountFor:aFormatString |
765 |
"Return the number of arguments required/produced, |
|
766 |
if the argument is interpreted as a printf/scanf format control string." |
|
767 |
||
768 |
|nonConsecutive count| |
|
4416 | 769 |
|
4417 | 770 |
nonConsecutive := true. |
771 |
count := 0. |
|
772 |
aFormatString do:[:c | |
|
773 |
c == $% ifTrue:[ |
|
774 |
nonConsecutive ifTrue:[ |
|
775 |
count := count + 1. |
|
776 |
nonConsecutive := false |
|
777 |
] ifFalse:[ |
|
778 |
count := count - 1. |
|
779 |
nonConsecutive := true |
|
780 |
] |
|
781 |
] ifFalse:[ |
|
782 |
nonConsecutive := true |
|
783 |
] |
|
784 |
]. |
|
785 |
^ count |
|
786 |
! |
|
1463 | 787 |
|
788 |
scanArgFrom:dataStream to:collection format:format |
|
789 |
"Add to <collection> an object who's representation is found |
|
790 |
in <dataStream> interpreted according to the conversion |
|
791 |
control string in the Stream <format>. <format> is assumed to |
|
792 |
be positioned just past a $%, and a complete control string is |
|
793 |
assumed available. |
|
794 |
||
795 |
Return when the conversion control string is consumed. Leave |
|
796 |
<format> pointing past the last character in the conversion |
|
797 |
control string, leave <dataStream> pointing past any width |
|
798 |
specified in <format>, or at the first character that doesn't |
|
799 |
make sense for the <format>." |
|
4417 | 800 |
|
1463 | 801 |
|final width char pos data scanset exclusive return last| |
802 |
||
803 |
final := [:retval | |
|
804 |
collection add:retval. |
|
805 |
data == dataStream ifFalse:[ |
|
806 |
dataStream position:dataStream position + data position |
|
807 |
]. |
|
808 |
^ self |
|
809 |
]. |
|
1464 | 810 |
|
1463 | 811 |
char := format peek. |
812 |
char == $% ifTrue:[ |
|
813 |
^ dataStream peekFor:char |
|
814 |
]. |
|
815 |
char == $* ifTrue:[ |
|
816 |
format next. |
|
817 |
char := format peek. |
|
818 |
final := [:retval | |
|
819 |
data == dataStream ifFalse:[ |
|
820 |
dataStream position:dataStream position + data position |
|
821 |
]. |
|
822 |
^ self |
|
823 |
] |
|
824 |
]. |
|
1464 | 825 |
width := 0. |
1463 | 826 |
char isDigit ifTrue:[ |
827 |
width := Integer readFrom:format. |
|
828 |
char := format peek |
|
829 |
]. |
|
830 |
('slhduoxfeg' includes:char) ifTrue:[ |
|
831 |
dataStream skipSeparators |
|
832 |
]. |
|
833 |
width = 0 ifTrue:[ |
|
834 |
data := dataStream |
|
835 |
] ifFalse:[ |
|
836 |
pos := dataStream position. |
|
837 |
data := ReadStream on:(dataStream next:width). |
|
838 |
dataStream position:pos |
|
839 |
]. |
|
840 |
char == $s ifTrue:[ |
|
841 |
final value:(data upToSeparator) |
|
842 |
]. |
|
843 |
char == $c ifTrue:[ |
|
844 |
width = 0 ifTrue:[ |
|
845 |
final value:(String with:data next) |
|
846 |
] ifFalse:[ |
|
847 |
final value:data contents |
|
848 |
] |
|
849 |
]. |
|
850 |
char == $[ ifTrue:[ |
|
851 |
"What a mess!!!!" |
|
3539 | 852 |
return := CharacterWriteStream on:(String new:8). |
1463 | 853 |
scanset := IdentitySet new. |
854 |
format next. |
|
855 |
width = 0 ifTrue:[ |
|
856 |
width := SmallInteger maxVal |
|
857 |
]. |
|
858 |
exclusive := format peekFor:$^. |
|
859 |
[ |
|
860 |
last := char. |
|
861 |
char := format next. |
|
862 |
char == $] |
|
863 |
] whileFalse:[ |
|
864 |
char == $- ifFalse:[ |
|
865 |
scanset add:char |
|
866 |
] ifTrue:[ |
|
2678
a2f6f1e865e6
changed: #scanArgFrom:to:format:
Claus Gittinger <cg@exept.de>
parents:
2581
diff
changeset
|
867 |
last to:format next do:[:c | |
1463 | 868 |
scanset add:c |
869 |
] |
|
870 |
] |
|
871 |
]. |
|
872 |
[ |
|
873 |
data atEnd not and:[ (scanset includes:data peek) xor:exclusive ] |
|
874 |
] whileTrue:[ return nextPut:data next ]. |
|
875 |
final value:return contents |
|
876 |
]. |
|
877 |
('lh' includes:char) ifTrue:[ |
|
878 |
format next. |
|
879 |
char := format peek |
|
880 |
]. |
|
881 |
('DUdu' includes:char) ifTrue:[ |
|
882 |
final value:(Integer readFrom:data) |
|
883 |
]. |
|
884 |
('FEGfeg' includes:char) ifTrue:[ |
|
885 |
final value:(Float readFrom:data) |
|
886 |
]. |
|
887 |
('Oo' includes:char) ifTrue:[ |
|
888 |
final value:(Integer readFrom:data radix:8) |
|
889 |
]. |
|
890 |
('Xx' includes:char) ifTrue:[ |
|
891 |
final value:(Integer readFrom:data radix:16) |
|
2889 | 892 |
]. |
893 |
('Bb' includes:char) ifTrue:[ |
|
894 |
final value:(Integer readFrom:data radix:2) |
|
895 |
]. |
|
2678
a2f6f1e865e6
changed: #scanArgFrom:to:format:
Claus Gittinger <cg@exept.de>
parents:
2581
diff
changeset
|
896 |
|
4417 | 897 |
"Created: / 19-06-2017 / 15:53:28 / cg" |
898 |
! ! |
|
899 |
||
900 |
!PrintfScanf class methodsFor:'scanning'! |
|
1463 | 901 |
|
902 |
scanf:formatString fromStream:dataStream |
|
903 |
"Return a Collection of objects found in the Character Stream |
|
904 |
<dataStream> as interpreted according to the receiver. The |
|
905 |
receiver is assumed to be a conversion control string as |
|
906 |
specified in the UTek manual page for scanf(3)." |
|
4417 | 907 |
|
1463 | 908 |
|results format char| |
909 |
||
910 |
results := OrderedCollection new. |
|
911 |
format := ReadStream on:formatString. |
|
912 |
[ format atEnd ] whileFalse:[ |
|
913 |
char := format next. |
|
914 |
(char == Character space or:[ char == Character tab ]) ifTrue:[ |
|
915 |
dataStream skipSeparators. |
|
916 |
format skipSeparators |
|
917 |
]. |
|
918 |
char == $% ifTrue:[ |
|
4416 | 919 |
self scanArgFrom:dataStream to:results format:format |
1463 | 920 |
] ifFalse:[ |
921 |
dataStream peekFor:char |
|
922 |
] |
|
923 |
]. |
|
924 |
^ results |
|
4416 | 925 |
|
4417 | 926 |
"Modified: / 19-06-2017 / 15:53:04 / cg" |
1463 | 927 |
! |
928 |
||
929 |
sscanf:formatString fromString:aString |
|
930 |
"Return a Collection of objects found in <string> as |
|
931 |
interpreted according to the receiver. The receiver is |
|
932 |
assumed to be a conversion control string as specified in the |
|
933 |
UTek manual page for scanf(3)." |
|
4417 | 934 |
|
935 |
^ self scanf:formatString fromStream:(ReadStream on:aString) |
|
936 |
||
937 |
"Modified: / 19-06-2017 / 15:53:59 / cg" |
|
938 |
! ! |
|
939 |
||
940 |
!PrintfScanf methodsFor:'helpers'! |
|
941 |
||
942 |
absDecimalPrintFloat:arg1 on:arg2 digits:arg3 |
|
943 |
<resource: #obsolete> |
|
944 |
^ self class absDecimalPrintFloat:arg1 on:arg2 digits:arg3 |
|
945 |
! |
|
946 |
||
947 |
absPrintFloat:arg1 on:arg2 digits:arg3 |
|
948 |
<resource: #obsolete> |
|
949 |
^ self class absPrintFloat:arg1 on:arg2 digits:arg3 |
|
950 |
! |
|
951 |
||
952 |
absScientificPrintFloat:arg1 on:arg2 digits:arg3 |
|
953 |
<resource: #obsolete> |
|
954 |
^ self class absScientificPrintFloat:arg1 on:arg2 digits:arg3 |
|
955 |
! |
|
956 |
||
957 |
formatArgCountFor:arg |
|
958 |
<resource: #obsolete> |
|
959 |
^ self class formatArgCountFor:arg |
|
960 |
! ! |
|
961 |
||
962 |
!PrintfScanf methodsFor:'printing'! |
|
963 |
||
964 |
printArgFrom:arg1 to:arg2 arguments:arg3 |
|
965 |
<resource: #obsolete> |
|
966 |
^ self class printArgFrom:arg1 to:arg2 arguments:arg3 |
|
967 |
! |
|
968 |
||
969 |
printf:aString arguments:args |
|
970 |
<resource: #obsolete> |
|
971 |
||
972 |
"Format and print the receiver with <args> formatted in C style, |
|
973 |
as described in the UTek manual page for printf(3). |
|
974 |
Returns the formatted printString." |
|
1463 | 975 |
|
4417 | 976 |
^ self class printf:aString arguments:args |
977 |
||
978 |
" |
|
979 |
self new printf:'%d %x' arguments:#(1234 45054) |
|
980 |
" |
|
981 |
||
982 |
"Modified: / 19-06-2017 / 15:21:56 / cg" |
|
983 |
! |
|
984 |
||
985 |
printf:aFormatString on:outStream arguments: args |
|
986 |
<resource: #obsolete> |
|
987 |
"Format and print aFormatString on <outStream> with <args> |
|
988 |
formatted in C style, as described in the UTek manual page for |
|
989 |
printf(3)." |
|
990 |
||
991 |
self class printf:aFormatString on:outStream arguments: args |
|
992 |
||
993 |
"Modified (comment): / 19-06-2017 / 15:22:15 / cg" |
|
994 |
! ! |
|
995 |
||
996 |
!PrintfScanf methodsFor:'scanning'! |
|
997 |
||
998 |
scanArgFrom:dataStream to:collection format:format |
|
999 |
<resource: #obsolete> |
|
1000 |
||
1001 |
"Add to <collection> an object who's representation is found |
|
1002 |
in <dataStream> interpreted according to the conversion |
|
1003 |
control string in the Stream <format>. <format> is assumed to |
|
1004 |
be positioned just past a $%, and a complete control string is |
|
1005 |
assumed available. |
|
1006 |
||
1007 |
Return when the conversion control string is consumed. Leave |
|
1008 |
<format> pointing past the last character in the conversion |
|
1009 |
control string, leave <dataStream> pointing past any width |
|
1010 |
specified in <format>, or at the first character that doesn't |
|
1011 |
make sense for the <format>." |
|
1012 |
||
1013 |
^ self class scanArgFrom:dataStream to:collection format:format |
|
1014 |
||
1015 |
"Modified: / 19-06-2017 / 15:54:43 / cg" |
|
1016 |
! |
|
1017 |
||
1018 |
scanf:formatString fromStream:dataStream |
|
1019 |
<resource: #obsolete> |
|
1020 |
||
1021 |
"Return a Collection of objects found in the Character Stream |
|
1022 |
<dataStream> as interpreted according to the receiver. The |
|
1023 |
receiver is assumed to be a conversion control string as |
|
1024 |
specified in the UTek manual page for scanf(3)." |
|
1025 |
||
1026 |
^ self class scanf:formatString fromStream:dataStream |
|
1027 |
||
1028 |
"Modified: / 19-06-2017 / 15:54:27 / cg" |
|
1029 |
! |
|
1030 |
||
1031 |
sscanf:formatString fromString:aString |
|
1032 |
<resource: #obsolete> |
|
1033 |
||
1034 |
"Return a Collection of objects found in <string> as |
|
1035 |
interpreted according to the receiver. The receiver is |
|
1036 |
assumed to be a conversion control string as specified in the |
|
1037 |
UTek manual page for scanf(3)." |
|
1038 |
||
1039 |
^ self class sscanf:formatString fromString:aString |
|
1463 | 1040 |
|
1041 |
" |
|
1042 |
self new sscanf:'%d %x' fromString:'1234 affe' |
|
1043 |
" |
|
4417 | 1044 |
|
1045 |
"Modified: / 19-06-2017 / 15:54:14 / cg" |
|
1463 | 1046 |
! ! |
1047 |
||
1048 |
!PrintfScanf class methodsFor:'documentation'! |
|
1049 |
||
1050 |
version |
|
3719 | 1051 |
^ '$Header$' |
3115 | 1052 |
! |
1053 |
||
1054 |
version_CVS |
|
3719 | 1055 |
^ '$Header$' |
1463 | 1056 |
! ! |
2875 | 1057 |