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