author | Claus Gittinger <cg@exept.de> |
Sat, 18 May 2019 13:09:58 +0200 | |
changeset 4946 | ccd655ac6fc1 |
parent 4945 | f98cb762d159 |
child 4966 | cc232f4d7d73 |
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 |
||
342 |
nextArg := [ |
|
343 |
argStream atEnd ifTrue:[ |
|
344 |
self error:'not enough arguments for format string' |
|
345 |
]. |
|
346 |
argStream next |
|
347 |
]. |
|
2875 | 348 |
|
349 |
ljust := plus := pound := false. |
|
350 |
width := 0. |
|
4401 | 351 |
"/ precision := SmallInteger maxVal. |
352 |
precision := nil. |
|
2875 | 353 |
pad := $ . |
4416 | 354 |
char := formatStream peek. |
2875 | 355 |
|
356 |
char == $% ifTrue:[ |
|
4416 | 357 |
^ outStream nextPut: formatStream next |
2875 | 358 |
]. |
359 |
||
4434 | 360 |
"/ flags: |
361 |
||
2875 | 362 |
char == $- ifTrue:[ |
363 |
ljust := true. |
|
4416 | 364 |
formatStream next. |
365 |
char := formatStream peek |
|
2875 | 366 |
]. |
367 |
||
368 |
char == $ ifTrue:[ |
|
369 |
outStream space. |
|
4416 | 370 |
formatStream next. |
371 |
char := formatStream peek |
|
2875 | 372 |
]. |
373 |
||
374 |
char == $+ ifTrue:[ |
|
375 |
plus := true. |
|
4416 | 376 |
formatStream next. |
377 |
char := formatStream peek |
|
2875 | 378 |
]. |
379 |
||
380 |
char == $# ifTrue:[ |
|
381 |
pound := true. |
|
4416 | 382 |
formatStream next. |
383 |
char := formatStream peek |
|
2875 | 384 |
]. |
385 |
||
4434 | 386 |
"/ possibly a width |
2875 | 387 |
char == $* ifTrue:[ |
4434 | 388 |
width := nextArg value. |
389 |
width isInteger ifFalse:[ |
|
390 |
self error:'non integer width argument in printf' |
|
391 |
]. |
|
392 |
width < 0 ifTrue:[ |
|
393 |
ljust := true. |
|
394 |
width := width negated |
|
395 |
]. |
|
4416 | 396 |
formatStream next. |
397 |
char := formatStream peek |
|
2875 | 398 |
]. |
399 |
||
400 |
char isDigit ifTrue:[ |
|
2889 | 401 |
char == $0 ifTrue: [pad := $0]. |
4434 | 402 |
width := Integer readFrom: formatStream onError:0. |
4416 | 403 |
char := formatStream peek |
2875 | 404 |
]. |
405 |
||
4434 | 406 |
"/ precision separator |
407 |
||
2875 | 408 |
char == $. ifTrue:[ |
4416 | 409 |
formatStream next. char := formatStream peek. |
4434 | 410 |
char == $* ifTrue: [ |
411 |
precision := nextArg value. |
|
412 |
precision isInteger ifFalse:[ |
|
413 |
self error:'non integer precision argument in printf' |
|
414 |
]. |
|
415 |
formatStream next. |
|
416 |
] ifFalse: [ |
|
417 |
precision := Integer readFrom: formatStream. |
|
418 |
]. |
|
4416 | 419 |
char := formatStream peek |
2875 | 420 |
]. |
421 |
||
4452 | 422 |
((char == $l) or:[char == $L]) ifTrue:[ |
423 |
"Ignore long specifier (l, L and ll)." |
|
4441 | 424 |
formatStream next. |
425 |
char := formatStream peek. |
|
426 |
char == $l ifTrue:[ |
|
427 |
"Ignore long long specifier." |
|
428 |
formatStream next. |
|
429 |
char := formatStream peek |
|
430 |
]. |
|
431 |
]. |
|
432 |
char == $h ifTrue:[ |
|
433 |
"Ignore half-length (short) specifier." |
|
434 |
formatStream next. |
|
435 |
char := formatStream peek. |
|
2875 | 436 |
]. |
437 |
||
4472 | 438 |
('fegFEG' includes: char) ifTrue:[ |
4417 | 439 |
arg := nextArg value. |
4394 | 440 |
arg isLimitedPrecisionReal ifTrue:[ |
4401 | 441 |
precision := precision ? (arg class defaultPrintPrecision). |
442 |
] ifFalse:[ |
|
443 |
arg := arg asFloat. |
|
444 |
precision := precision ? (Float defaultPrintPrecision). |
|
4394 | 445 |
]. |
4472 | 446 |
(arg isNaN or:[arg isFinite not]) ifTrue:[ |
447 |
argString := arg printString. |
|
448 |
(char isUppercase) |
|
449 |
ifTrue:[ argString := argString asUppercase ] |
|
450 |
ifFalse:[ argString := argString asLowercase ]. |
|
451 |
] ifFalse:[ |
|
452 |
argString := WriteStream on:''. |
|
453 |
char asLowercase == $g ifTrue: [ |
|
4588 | 454 |
self absPrintFloat:arg on:argString digits:(precision + 1) ]. |
455 |
char asLowercase == $f ifTrue: [ |
|
456 |
|absArg nDigits| |
|
457 |
absArg := arg asFloat abs. |
|
458 |
nDigits := absArg = 0 ifTrue:[1] ifFalse:[absArg log10 ceiling asInteger]. |
|
459 |
self absDecimalPrintFloat:arg on:argString digits:(precision + nDigits "+ 1")]. |
|
4472 | 460 |
char asLowercase == $e ifTrue: [ |
4588 | 461 |
self absScientificPrintFloat:arg on:argString digits:(precision + 1)]. |
4472 | 462 |
argString := argString contents. |
463 |
arg < 0 |
|
464 |
ifTrue: [argString := '-', argString] |
|
465 |
ifFalse: [plus ifTrue: [argString := '+', argString]]. |
|
466 |
||
467 |
(precision = 0 and: [pound not]) ifTrue:[ |
|
468 |
(argString includes: $e) |
|
469 |
ifTrue: ["self halt"] |
|
470 |
ifFalse: [ argString := arg truncated printString] |
|
471 |
]. |
|
472 |
pound ifTrue:[ |
|
473 |
(argString includes: $e) |
|
474 |
ifTrue: ["self halt"] |
|
475 |
ifFalse:[ |
|
476 |
precision - (argString size - (argString indexOf: $.)) |
|
477 |
timesRepeat:[ argString := argString, '0'] |
|
478 |
] |
|
479 |
]. |
|
480 |
]. |
|
4394 | 481 |
ljust ifTrue: [outStream nextPutAll: argString]. |
482 |
width - argString size timesRepeat: [outStream nextPut: pad]. |
|
483 |
ljust ifFalse: [outStream nextPutAll: argString]. |
|
4416 | 484 |
^ formatStream next |
2875 | 485 |
]. |
486 |
||
487 |
char == $c ifTrue:[ |
|
4441 | 488 |
arg := nextArg value. |
489 |
arg isString ifTrue:[ |
|
490 |
arg := arg first |
|
491 |
] ifFalse:[ |
|
492 |
arg := arg asCharacter asString |
|
493 |
]. |
|
2875 | 494 |
]. |
495 |
||
496 |
char == $s ifTrue:[ |
|
497 |
"Assume the arg is a String or Symbol." |
|
4417 | 498 |
arg := nextArg value asString |
2875 | 499 |
]. |
500 |
||
4441 | 501 |
((char == $d) or:[char == $i]) ifTrue:[ |
4417 | 502 |
arg := nextArg value asInteger printString. |
2889 | 503 |
plus ifTrue: [arg := '+', arg] |
2875 | 504 |
]. |
505 |
||
506 |
char == $u ifTrue:[ |
|
4441 | 507 |
"/ should we convert unsigned numbers here??? |
508 |
"/ (negatives: maybe to the next power-of-2) |
|
4417 | 509 |
arg := nextArg value asInteger abs printString |
2875 | 510 |
]. |
511 |
||
512 |
char == $o ifTrue:[ |
|
4417 | 513 |
arg := nextArg value asInteger abs printStringRadix: 8. |
4394 | 514 |
pound ifTrue: [arg := '0', arg] |
2875 | 515 |
]. |
516 |
||
4441 | 517 |
((char == $x) or:[char == $X]) ifTrue:[ |
4417 | 518 |
arg := nextArg value asInteger abs printStringRadix: 16. |
4441 | 519 |
pound ifTrue: [arg := '0x', arg]. |
520 |
||
521 |
char == $x ifTrue:[ |
|
522 |
"/ make it lowercase |
|
523 |
1 to: arg size do: [:i | |
|
524 |
('ABCDEF' includes: (arg at: i)) ifTrue:[ |
|
525 |
arg at: i put: (arg at: i) asLowercase |
|
526 |
] |
|
527 |
] |
|
528 |
]. |
|
529 |
||
2889 | 530 |
]. |
4441 | 531 |
((char == $b) or:[char == $B]) ifTrue:[ |
4417 | 532 |
arg := nextArg value asInteger abs printStringRadix: 2. |
2889 | 533 |
pound ifTrue: [arg := '0b', arg] |
2875 | 534 |
]. |
4622 | 535 |
(char == $p) ifTrue:[ |
536 |
arg := nextArg value identityHash printString. |
|
537 |
]. |
|
538 |
(char == $P) ifTrue:[ |
|
539 |
arg := nextArg value printString. |
|
540 |
]. |
|
541 |
(char == $S) ifTrue:[ |
|
542 |
arg := nextArg value storeString. |
|
543 |
]. |
|
2875 | 544 |
|
4452 | 545 |
precision := (precision ? arg size) min: arg size. |
546 |
ljust ifTrue: [precision > 0 ifTrue:[ outStream nextPutAll: (arg copyFrom: 1 to: precision)]]. |
|
2875 | 547 |
width - precision timesRepeat: [outStream nextPut: pad]. |
4452 | 548 |
ljust ifFalse: [precision > 0 ifTrue:[ outStream nextPutAll: (arg copyFrom: 1 to: precision)]]. |
4416 | 549 |
^ formatStream next |
2875 | 550 |
|
4472 | 551 |
"Modified: / 14-07-2017 / 11:42:01 / cg" |
2875 | 552 |
! |
553 |
||
4416 | 554 |
printf:formatString argument:arg |
555 |
"Format and print the receiver with <arg> formatted in C style, |
|
556 |
as described in the UTek manual page for printf(3)." |
|
557 |
||
558 |
^ self printf:formatString arguments:{ arg } |
|
559 |
||
560 |
" |
|
561 |
self printf:'%e' on:Transcript argument:(1.234 asShortFloat) |
|
562 |
self printf:'%e' on:Transcript argument:(1.234 asFloat) |
|
563 |
self printf:'%e' on:Transcript argument:(1.234 asLongFloat) |
|
564 |
self printf:'%e' on:Transcript argument:(1.234 asQDouble) |
|
565 |
self printf:'%e' on:Transcript argument:(1.234 asInteger) |
|
566 |
||
567 |
self printf:'%10e' on:Transcript argument:(1.234 asShortFloat) |
|
568 |
self printf:'%10e' on:Transcript argument:(1.234 asFloat) |
|
569 |
self printf:'%10e' on:Transcript argument:(1.234 asLongFloat) |
|
570 |
self printf:'%10e' on:Transcript argument:(1.234 asQDouble) |
|
571 |
self printf:'%10e' on:Transcript argument:(1.234 asInteger) |
|
572 |
||
573 |
self printf:'%010e' on:Transcript argument:(1.234 asInteger) |
|
574 |
self printf:'%-10e' on:Transcript argument:(1.234 asInteger) |
|
575 |
||
576 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asShortFloat) |
|
577 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asFloat) |
|
578 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asLongFloat) |
|
579 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asQDouble) |
|
580 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asInteger) |
|
581 |
" |
|
582 |
||
583 |
"Created: / 16-06-2017 / 14:50:08 / cg" |
|
584 |
! |
|
585 |
||
586 |
printf:formatString arguments:args |
|
2875 | 587 |
"Format and print the receiver with <args> formatted in C style, |
4416 | 588 |
as described in the UTek manual page for printf(3)." |
589 |
||
2875 | 590 |
|aStream| |
591 |
||
4416 | 592 |
aStream := WriteStream on:(formatString species new:100). |
593 |
self printf:formatString on:aStream arguments:args. |
|
2875 | 594 |
^ aStream contents |
595 |
||
596 |
" |
|
4416 | 597 |
self printf:'%e' on:Transcript arguments:{ (1.234 asShortFloat) } |
598 |
self printf:'%e' on:Transcript arguments:{ (1.234 asFloat) } |
|
599 |
self printf:'%e' on:Transcript arguments:{ (1.234 asLongFloat) } |
|
600 |
self printf:'%e' on:Transcript arguments:{ (1.234 asQDouble) } |
|
601 |
self printf:'%e' on:Transcript arguments:{ (1.234 asInteger) } |
|
602 |
||
603 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asShortFloat) } |
|
604 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asFloat) } |
|
605 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asLongFloat) } |
|
606 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asQDouble) } |
|
607 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asInteger) } |
|
608 |
||
609 |
self printf:'%010e' on:Transcript arguments:{ (1.234 asInteger) } |
|
610 |
self printf:'%-10e' on:Transcript arguments:{ (1.234 asInteger) } |
|
611 |
||
612 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asShortFloat) } |
|
613 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asFloat) } |
|
614 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asLongFloat) } |
|
615 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asQDouble) } |
|
616 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asInteger) } |
|
2875 | 617 |
" |
4394 | 618 |
|
4416 | 619 |
"Modified: / 19-06-2017 / 15:21:34 / cg" |
620 |
! |
|
621 |
||
622 |
printf:formatString on:outStream argument: arg |
|
623 |
"Format and print formatString on <outStream> with <arg> |
|
624 |
formatted in C style, as described in the UTek manual page for |
|
625 |
printf(3)." |
|
626 |
||
627 |
^ self printf:formatString on:outStream arguments:{ arg } |
|
628 |
||
629 |
" |
|
630 |
self printf:'%e' on:Transcript argument:(1.234 asShortFloat). Transcript cr. |
|
631 |
self printf:'%e' on:Transcript argument:(1.234 asFloat) . Transcript cr. |
|
632 |
self printf:'%e' on:Transcript argument:(1.234 asLongFloat) . Transcript cr. |
|
633 |
self printf:'%e' on:Transcript argument:(1.234 asQDouble) . Transcript cr. |
|
634 |
self printf:'%e' on:Transcript argument:(1.234 asInteger) . Transcript cr. |
|
635 |
||
636 |
self printf:'%10e' on:Transcript argument:(1.234 asShortFloat). Transcript cr. |
|
637 |
self printf:'%10e' on:Transcript argument:(1.234 asFloat) . Transcript cr. |
|
638 |
self printf:'%10e' on:Transcript argument:(1.234 asLongFloat) . Transcript cr. |
|
639 |
self printf:'%10e' on:Transcript argument:(1.234 asQDouble) . Transcript cr. |
|
640 |
self printf:'%10e' on:Transcript argument:(1.234 asInteger) . Transcript cr. |
|
641 |
||
642 |
self printf:'%010e' on:Transcript argument:(1.234 asInteger) . Transcript cr. |
|
643 |
self printf:'%-10e' on:Transcript argument:(1.234 asInteger) . Transcript cr. |
|
644 |
||
645 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asShortFloat). Transcript cr. |
|
646 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asFloat) . Transcript cr. |
|
647 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asLongFloat) . Transcript cr. |
|
648 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asQDouble) . Transcript cr. |
|
649 |
self printf:'%10.9f' on:Transcript argument:(1.2345 asInteger) . Transcript cr. |
|
650 |
" |
|
651 |
||
652 |
"Created: / 16-06-2017 / 14:50:40 / cg" |
|
653 |
"Modified (comment): / 19-06-2017 / 15:07:22 / cg" |
|
2875 | 654 |
! |
655 |
||
656 |
printf:aFormatString on:outStream arguments: args |
|
657 |
"Format and print aFormatString on <outStream> with <args> |
|
658 |
formatted in C style, as described in the UTek manual page for |
|
4416 | 659 |
printf(3)." |
2875 | 660 |
|
4417 | 661 |
| argStream formatStream char escChar | |
2875 | 662 |
|
663 |
argStream := ReadStream on: args. |
|
4417 | 664 |
formatStream := ReadStream on: aFormatString. |
665 |
[formatStream atEnd] whileFalse:[ |
|
666 |
(char := formatStream next) == $% ifFalse: [ |
|
667 |
char == $\ ifTrue:[ |
|
668 |
escChar := formatStream next. |
|
669 |
escChar == $n ifTrue:[ char := Character nl ] |
|
670 |
ifFalse:[ escChar == $t ifTrue:[ char := Character tab ] |
|
671 |
ifFalse:[ escChar == $r ifTrue:[ char := Character return ] |
|
672 |
ifFalse:[ char := escChar ]]] |
|
673 |
]. |
|
674 |
outStream nextPut:char |
|
2875 | 675 |
] ifTrue: [ |
4417 | 676 |
self printArgFrom:formatStream to:outStream arguments:argStream |
2875 | 677 |
] |
678 |
] |
|
4394 | 679 |
|
4416 | 680 |
" |
4417 | 681 |
self printf:'%e\n' on:Transcript arguments:{ (1.234 asShortFloat) } |
682 |
self printf:'%f\n' on:Transcript arguments:{ (1.234 asShortFloat) } |
|
683 |
self printf:'%g\n' on:Transcript arguments:{ (1.234 asShortFloat) } |
|
684 |
||
685 |
self printf:'%e\n' on:Transcript arguments:{ (1.234 asFloat) } |
|
686 |
self printf:'%f\n' on:Transcript arguments:{ (1.234 asFloat) } |
|
687 |
self printf:'%g\n' on:Transcript arguments:{ (1.234 asFloat) } |
|
688 |
||
689 |
self printf:'%e\n' on:Transcript arguments:{ (1.234 asLongFloat) } |
|
690 |
self printf:'%e\n' on:Transcript arguments:{ (1.234 asQDouble) } |
|
691 |
self printf:'%e\n' on:Transcript arguments:{ (1.234 asInteger) } |
|
4416 | 692 |
|
693 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asShortFloat) } |
|
694 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asFloat) } |
|
695 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asLongFloat) } |
|
696 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asQDouble) } |
|
697 |
self printf:'%10e' on:Transcript arguments:{ (1.234 asInteger) } |
|
698 |
||
699 |
self printf:'%010e' on:Transcript arguments:{ (1.234 asInteger) } |
|
700 |
self printf:'%-10e' on:Transcript arguments:{ (1.234 asInteger) } |
|
701 |
||
702 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asShortFloat) } |
|
703 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asFloat) } |
|
704 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asLongFloat) } |
|
705 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asQDouble) } |
|
706 |
self printf:'%10.9f' on:Transcript arguments:{ (1.2345 asInteger) } |
|
707 |
" |
|
708 |
||
4417 | 709 |
"Modified (comment): / 19-06-2017 / 15:49:10 / cg" |
4416 | 710 |
! ! |
711 |
||
4417 | 712 |
!PrintfScanf class methodsFor:'private helpers'! |
4416 | 713 |
|
4417 | 714 |
absDecimalPrintFloat:aFloat on:aStream digits:digits |
715 |
"Place a string representation of the receiver on <aStream>, |
|
716 |
using <digits> significant digits, using decimal notation." |
|
717 |
||
718 |
" |
|
4428 | 719 |
self printf:'%20.10f\n' on:Transcript arguments: { 1.234567890123456789f } |
720 |
self printf:'%20.10f\n' on:Transcript arguments: { 1.234567890123456789q } |
|
721 |
||
722 |
self printf:'%20.10f\n' on:Transcript arguments: { 1e1000 } |
|
723 |
self printf:'%20.10f\n' on:Transcript arguments: { 1e1000 asShortFloat } |
|
724 |
self printf:'%20.10f\n' on:Transcript arguments: { 1e1000 asLongFloat } |
|
725 |
self printf:'%20.10f\n' on:Transcript arguments: { 1e1000 asQDouble } |
|
726 |
||
4432 | 727 |
self printf:'%20.10f\n' on:Transcript arguments: { 0.0 log10 } |
728 |
self printf:'%20.10f\n' on:Transcript arguments: { 0.0 asShortFloat log10 } |
|
729 |
self printf:'%20.10f\n' on:Transcript arguments: { 0.0 asLongFloat log10 } |
|
730 |
self printf:'%20.10f\n' on:Transcript arguments: { 0.0 asQDouble log10 } |
|
731 |
||
4452 | 732 |
self printf:'%20.10f\n' on:Transcript arguments: { Float NaN } |
4434 | 733 |
self printf:'%20.10f\n' on:Transcript arguments: { DomainError ignoreIn:[ -1.0 log10 ] } |
734 |
self printf:'%20.10f\n' on:Transcript arguments: { DomainError ignoreIn:[ -1.0 asShortFloat log10 ] } |
|
735 |
self printf:'%20.10f\n' on:Transcript arguments: { DomainError ignoreIn:[ -1.0 asLongFloat log10] } |
|
736 |
self printf:'%20.10f\n' on:Transcript arguments: { DomainError ignoreIn:[ -1.0 asQDouble log10] } |
|
737 |
||
738 |
self printf:'%10s\n' on:Transcript arguments:{ 'hello' } |
|
739 |
self printf:'%*s\n' on:Transcript arguments:{ 10 . 'hello' } |
|
4417 | 740 |
" |
4416 | 741 |
|
4417 | 742 |
|absVal exp x fuzz i| |
743 |
||
4428 | 744 |
aFloat isNaN ifTrue:[ |
745 |
aStream nextPutAll:'NAN'. |
|
746 |
^ self. |
|
747 |
]. |
|
748 |
aFloat isInfinite ifTrue:[ |
|
749 |
aStream nextPutAll:'INF'. |
|
750 |
^ self. |
|
751 |
]. |
|
752 |
aFloat = 0.0 ifTrue:[ |
|
753 |
aStream nextPutAll:'0'. |
|
754 |
^ self. |
|
755 |
]. |
|
756 |
||
4417 | 757 |
absVal := aFloat abs. |
4416 | 758 |
|
4417 | 759 |
"x is myself normalized to (1.0, 10.0), exp is my exponent" |
760 |
exp := absVal < 1.0 |
|
4452 | 761 |
ifTrue:[ (10.0 / absVal asFloat) log10 floor negated ] |
762 |
ifFalse:[ absVal asFloat log10 floor]. |
|
4428 | 763 |
|
764 |
exp := exp asInteger. |
|
4417 | 765 |
x := absVal / (10.0 raisedTo:exp). |
4428 | 766 |
fuzz := 10.0 raisedTo:(1 - digits). |
4417 | 767 |
"round the last digit to be printed" |
768 |
x := 0.5 * fuzz + x. |
|
769 |
x >= 10.0 ifTrue:[ |
|
770 |
"check if rounding has unnormalized x" |
|
771 |
x := x / 10.0. |
|
772 |
exp := exp + 1 |
|
773 |
]. |
|
774 |
||
775 |
exp < 0 ifTrue:[ |
|
776 |
1 to:(1 - exp) do:[:j | |
|
777 |
"/ cg: huh - what sort of code is that???? |
|
778 |
"/ aStream nextPut:('0.000000000000' at:j) |
|
779 |
aStream nextPut:(j == 2 ifTrue:[$.] ifFalse:[$0]) |
|
780 |
]. |
|
781 |
||
782 |
]. |
|
783 |
[ x >= fuzz ] whileTrue:[ |
|
784 |
"use fuzz to track significance" |
|
4422 | 785 |
i := x truncated. |
786 |
i := i asInteger. |
|
4441 | 787 |
aStream nextPut:($0 + i). |
4417 | 788 |
x := (x - i) * 10.0. |
789 |
fuzz := fuzz * 10.0. |
|
790 |
exp := exp - 1. |
|
4428 | 791 |
exp == -1 ifTrue:[ |
4417 | 792 |
aStream nextPut:$. |
793 |
] |
|
794 |
]. |
|
4428 | 795 |
|
4417 | 796 |
[ exp >= -1 ] whileTrue:[ |
797 |
aStream nextPut:$0. |
|
798 |
exp := exp - 1. |
|
4428 | 799 |
exp == -1 ifTrue:[ |
4417 | 800 |
aStream nextPut:$. |
801 |
] |
|
802 |
] |
|
4416 | 803 |
|
4452 | 804 |
"Modified: / 03-07-2017 / 14:13:31 / cg" |
4416 | 805 |
! |
806 |
||
4417 | 807 |
absPrintFloat:aFloat on:aStream digits:digits |
808 |
"Place a string representation of the receiver on <aStream>, |
|
809 |
using <digits> significant digits." |
|
810 |
||
811 |
(aFloat < 1.0e6 and:[ aFloat > 1.0e-4 ]) ifTrue:[ |
|
812 |
self absDecimalPrintFloat:aFloat on:aStream digits:digits |
|
813 |
] ifFalse:[ |
|
814 |
self absScientificPrintFloat:aFloat on:aStream digits:digits |
|
815 |
] |
|
4416 | 816 |
|
4417 | 817 |
"Modified: / 19-06-2017 / 15:03:40 / cg" |
4416 | 818 |
! |
819 |
||
4417 | 820 |
absScientificPrintFloat:aFloat on:aStream digits:digits |
821 |
"Place a string representation of the receiver on <aStream>, |
|
822 |
using <digits> significant digits, using scientific notation." |
|
823 |
||
824 |
|absVal exp fuzz x q i| |
|
825 |
||
4428 | 826 |
aFloat isNaN ifTrue:[ |
827 |
aStream nextPutAll:'NAN'. |
|
828 |
^ self. |
|
829 |
]. |
|
830 |
aFloat isInfinite ifTrue:[ |
|
831 |
aStream nextPutAll:'INF'. |
|
832 |
^ self. |
|
833 |
]. |
|
834 |
aFloat = 0.0 ifTrue:[ |
|
835 |
aStream nextPutAll:'0'. |
|
836 |
^ self. |
|
837 |
]. |
|
838 |
||
4417 | 839 |
absVal := aFloat abs. |
4416 | 840 |
|
4417 | 841 |
"x is myself normalized to [1.0, 10.0), exp is my exponent" |
842 |
exp := absVal < 1.0 |
|
843 |
ifTrue:[ |
|
844 |
(10.0 / absVal) log10 floor asInteger negated] |
|
845 |
ifFalse:[ |
|
846 |
absVal log10 floor asInteger]. |
|
847 |
x := absVal / (10.0 raisedTo:exp). |
|
848 |
"round the last digit to be printed" |
|
849 |
fuzz := 10.0 raisedTo:1 - digits. |
|
850 |
x := 0.5 * fuzz + x. |
|
851 |
x >= 10.0 ifTrue:[ "check if rounding has unnormalized x" |
|
852 |
x := x / 10.0. |
|
853 |
exp := exp + 1 |
|
854 |
]. |
|
855 |
q := exp. |
|
856 |
exp := 0. |
|
857 |
[ x >= fuzz ] whileTrue:[ |
|
858 |
"use fuzz to track significance" |
|
859 |
i := x truncated. |
|
860 |
i := i asInteger. |
|
4441 | 861 |
aStream nextPut:($0 + i). |
4417 | 862 |
x := (x - i) * 10.0. |
863 |
fuzz := fuzz * 10.0. |
|
864 |
exp := exp - 1. |
|
865 |
exp = -1 ifTrue:[ |
|
866 |
aStream nextPut:$. |
|
867 |
] |
|
868 |
]. |
|
869 |
[ exp >= -1 ] whileTrue:[ |
|
870 |
aStream nextPut:$0. |
|
871 |
exp := exp - 1. |
|
872 |
exp = -1 ifTrue:[ |
|
873 |
aStream nextPut:$. |
|
874 |
] |
|
875 |
]. |
|
876 |
aStream nextPut:$e. |
|
877 |
q printOn:aStream |
|
4416 | 878 |
|
4441 | 879 |
"Modified: / 21-06-2017 / 13:29:37 / cg" |
4416 | 880 |
! |
881 |
||
4417 | 882 |
formatArgCountFor:aFormatString |
883 |
"Return the number of arguments required/produced, |
|
884 |
if the argument is interpreted as a printf/scanf format control string." |
|
885 |
||
886 |
|nonConsecutive count| |
|
4416 | 887 |
|
4417 | 888 |
nonConsecutive := true. |
889 |
count := 0. |
|
890 |
aFormatString do:[:c | |
|
891 |
c == $% ifTrue:[ |
|
892 |
nonConsecutive ifTrue:[ |
|
893 |
count := count + 1. |
|
894 |
nonConsecutive := false |
|
895 |
] ifFalse:[ |
|
896 |
count := count - 1. |
|
897 |
nonConsecutive := true |
|
898 |
] |
|
899 |
] ifFalse:[ |
|
900 |
nonConsecutive := true |
|
901 |
] |
|
902 |
]. |
|
903 |
^ count |
|
904 |
! |
|
1463 | 905 |
|
906 |
scanArgFrom:dataStream to:collection format:format |
|
907 |
"Add to <collection> an object who's representation is found |
|
908 |
in <dataStream> interpreted according to the conversion |
|
909 |
control string in the Stream <format>. <format> is assumed to |
|
910 |
be positioned just past a $%, and a complete control string is |
|
911 |
assumed available. |
|
912 |
||
913 |
Return when the conversion control string is consumed. Leave |
|
914 |
<format> pointing past the last character in the conversion |
|
915 |
control string, leave <dataStream> pointing past any width |
|
916 |
specified in <format>, or at the first character that doesn't |
|
917 |
make sense for the <format>." |
|
4417 | 918 |
|
4471 | 919 |
|final width char pos data scanset exclusive return last |
920 |
isShort isLong isLongLong isLongDouble isLongLongDouble| |
|
1463 | 921 |
|
922 |
final := [:retval | |
|
923 |
collection add:retval. |
|
924 |
data == dataStream ifFalse:[ |
|
925 |
dataStream position:dataStream position + data position |
|
926 |
]. |
|
927 |
^ self |
|
928 |
]. |
|
1464 | 929 |
|
1463 | 930 |
char := format peek. |
931 |
char == $% ifTrue:[ |
|
932 |
^ dataStream peekFor:char |
|
933 |
]. |
|
934 |
char == $* ifTrue:[ |
|
935 |
format next. |
|
936 |
char := format peek. |
|
937 |
final := [:retval | |
|
938 |
data == dataStream ifFalse:[ |
|
939 |
dataStream position:dataStream position + data position |
|
940 |
]. |
|
941 |
^ self |
|
942 |
] |
|
943 |
]. |
|
4471 | 944 |
|
1464 | 945 |
width := 0. |
4471 | 946 |
isShort := isLong := isLongLong := false. |
947 |
isLongDouble := isLongLongDouble := false. |
|
948 |
||
1463 | 949 |
char isDigit ifTrue:[ |
950 |
width := Integer readFrom:format. |
|
951 |
char := format peek |
|
952 |
]. |
|
953 |
('slhduoxfeg' includes:char) ifTrue:[ |
|
954 |
dataStream skipSeparators |
|
955 |
]. |
|
956 |
width = 0 ifTrue:[ |
|
957 |
data := dataStream |
|
958 |
] ifFalse:[ |
|
959 |
pos := dataStream position. |
|
960 |
data := ReadStream on:(dataStream next:width). |
|
961 |
dataStream position:pos |
|
962 |
]. |
|
963 |
char == $s ifTrue:[ |
|
964 |
final value:(data upToSeparator) |
|
965 |
]. |
|
966 |
char == $c ifTrue:[ |
|
967 |
width = 0 ifTrue:[ |
|
968 |
final value:(String with:data next) |
|
969 |
] ifFalse:[ |
|
970 |
final value:data contents |
|
971 |
] |
|
972 |
]. |
|
973 |
char == $[ ifTrue:[ |
|
974 |
"What a mess!!!!" |
|
3539 | 975 |
return := CharacterWriteStream on:(String new:8). |
1463 | 976 |
scanset := IdentitySet new. |
977 |
format next. |
|
978 |
width = 0 ifTrue:[ |
|
979 |
width := SmallInteger maxVal |
|
980 |
]. |
|
981 |
exclusive := format peekFor:$^. |
|
982 |
[ |
|
983 |
last := char. |
|
984 |
char := format next. |
|
985 |
char == $] |
|
986 |
] whileFalse:[ |
|
987 |
char == $- ifFalse:[ |
|
988 |
scanset add:char |
|
989 |
] ifTrue:[ |
|
2678
a2f6f1e865e6
changed: #scanArgFrom:to:format:
Claus Gittinger <cg@exept.de>
parents:
2581
diff
changeset
|
990 |
last to:format next do:[:c | |
1463 | 991 |
scanset add:c |
992 |
] |
|
993 |
] |
|
994 |
]. |
|
995 |
[ |
|
996 |
data atEnd not and:[ (scanset includes:data peek) xor:exclusive ] |
|
997 |
] whileTrue:[ return nextPut:data next ]. |
|
998 |
final value:return contents |
|
999 |
]. |
|
4471 | 1000 |
(char == $l) ifTrue:[ |
1001 |
isLong := true. |
|
1002 |
format next. |
|
1003 |
char := format peek. |
|
1004 |
||
1005 |
(char == $l) ifTrue:[ |
|
1006 |
isLongLong := true. |
|
1007 |
format next. |
|
1008 |
char := format peek. |
|
1009 |
]. |
|
1010 |
]. |
|
1011 |
(char == $h) ifTrue:[ |
|
1012 |
isShort := true. |
|
1463 | 1013 |
format next. |
1014 |
char := format peek |
|
1015 |
]. |
|
4471 | 1016 |
(char == $L) ifTrue:[ |
1017 |
isLongDouble := true. |
|
1018 |
format next. |
|
1019 |
char := format peek. |
|
1020 |
||
1021 |
(char == $L) ifTrue:[ |
|
1022 |
isLongLongDouble := true. |
|
1023 |
format next. |
|
1024 |
char := format peek |
|
1025 |
]. |
|
1026 |
]. |
|
1463 | 1027 |
('DUdu' includes:char) ifTrue:[ |
1028 |
final value:(Integer readFrom:data) |
|
1029 |
]. |
|
1030 |
('FEGfeg' includes:char) ifTrue:[ |
|
4471 | 1031 |
(isLongLongDouble) ifTrue:[ |
1032 |
final value:(QDouble readFrom:data) |
|
1033 |
] ifFalse:[ |
|
1034 |
(isLongDouble) ifTrue:[ |
|
1035 |
final value:(LongFloat readFrom:data) |
|
1036 |
] ifFalse:[ |
|
1037 |
isShort ifTrue:[ |
|
1038 |
final value:(ShortFloat readFrom:data) |
|
1039 |
] ifFalse:[ |
|
1040 |
final value:(Float readFrom:data) |
|
1041 |
]. |
|
1042 |
]. |
|
1043 |
]. |
|
1463 | 1044 |
]. |
1045 |
('Oo' includes:char) ifTrue:[ |
|
1046 |
final value:(Integer readFrom:data radix:8) |
|
1047 |
]. |
|
1048 |
('Xx' includes:char) ifTrue:[ |
|
1049 |
final value:(Integer readFrom:data radix:16) |
|
2889 | 1050 |
]. |
1051 |
('Bb' includes:char) ifTrue:[ |
|
1052 |
final value:(Integer readFrom:data radix:2) |
|
1053 |
]. |
|
4945 | 1054 |
('Nn' includes:char) ifTrue:[ |
1055 |
final value:(Number readFrom:data) |
|
1056 |
]. |
|
2678
a2f6f1e865e6
changed: #scanArgFrom:to:format:
Claus Gittinger <cg@exept.de>
parents:
2581
diff
changeset
|
1057 |
|
4471 | 1058 |
" |
1059 |
'%d %x' sscanf:'1234 ff00' |
|
1060 |
'%d %x %b' sscanf:'1234 ff00 1001' |
|
1061 |
||
1062 |
('%f' sscanf:'1234') first |
|
1063 |
('%lf' sscanf:'1234') first |
|
1064 |
('%llf' sscanf:'1234') first |
|
1065 |
('%hf' sscanf:'1234') first |
|
1066 |
('%Lf' sscanf:'1234') first |
|
1067 |
('%LLf' sscanf:'1234') first |
|
4945 | 1068 |
('%n' sscanf:'1234') first |
1069 |
('%n' sscanf:'1234.456') first |
|
1070 |
('%n %n' sscanf:'1234.456 123') |
|
4471 | 1071 |
" |
1072 |
||
4417 | 1073 |
"Created: / 19-06-2017 / 15:53:28 / cg" |
4471 | 1074 |
"Modified (comment): / 14-07-2017 / 11:31:01 / cg" |
4945 | 1075 |
"Modified (comment): / 18-05-2019 / 13:08:30 / Claus Gittinger" |
4417 | 1076 |
! ! |
1077 |
||
1078 |
!PrintfScanf class methodsFor:'scanning'! |
|
1463 | 1079 |
|
1080 |
scanf:formatString fromStream:dataStream |
|
1081 |
"Return a Collection of objects found in the Character Stream |
|
1082 |
<dataStream> as interpreted according to the receiver. The |
|
1083 |
receiver is assumed to be a conversion control string as |
|
1084 |
specified in the UTek manual page for scanf(3)." |
|
4417 | 1085 |
|
1463 | 1086 |
|results format char| |
1087 |
||
1088 |
results := OrderedCollection new. |
|
1089 |
format := ReadStream on:formatString. |
|
1090 |
[ format atEnd ] whileFalse:[ |
|
1091 |
char := format next. |
|
1092 |
(char == Character space or:[ char == Character tab ]) ifTrue:[ |
|
1093 |
dataStream skipSeparators. |
|
1094 |
format skipSeparators |
|
1095 |
]. |
|
1096 |
char == $% ifTrue:[ |
|
4416 | 1097 |
self scanArgFrom:dataStream to:results format:format |
1463 | 1098 |
] ifFalse:[ |
1099 |
dataStream peekFor:char |
|
1100 |
] |
|
1101 |
]. |
|
1102 |
^ results |
|
4416 | 1103 |
|
4417 | 1104 |
"Modified: / 19-06-2017 / 15:53:04 / cg" |
1463 | 1105 |
! |
1106 |
||
1107 |
sscanf:formatString fromString:aString |
|
1108 |
"Return a Collection of objects found in <string> as |
|
1109 |
interpreted according to the receiver. The receiver is |
|
1110 |
assumed to be a conversion control string as specified in the |
|
1111 |
UTek manual page for scanf(3)." |
|
4417 | 1112 |
|
1113 |
^ self scanf:formatString fromStream:(ReadStream on:aString) |
|
1114 |
||
4945 | 1115 |
" |
1116 |
self sscanf:'%d %d %d' fromString:'123 45 999' |
|
4946 | 1117 |
'%d %d %d' scanf:'123 45 999' |
4945 | 1118 |
" |
1119 |
||
4417 | 1120 |
"Modified: / 19-06-2017 / 15:53:59 / cg" |
4946 | 1121 |
"Modified (comment): / 18-05-2019 / 13:09:48 / Claus Gittinger" |
4417 | 1122 |
! ! |
1123 |
||
1124 |
!PrintfScanf methodsFor:'helpers'! |
|
1125 |
||
1126 |
absDecimalPrintFloat:arg1 on:arg2 digits:arg3 |
|
1127 |
<resource: #obsolete> |
|
1128 |
^ self class absDecimalPrintFloat:arg1 on:arg2 digits:arg3 |
|
1129 |
! |
|
1130 |
||
1131 |
absPrintFloat:arg1 on:arg2 digits:arg3 |
|
1132 |
<resource: #obsolete> |
|
1133 |
^ self class absPrintFloat:arg1 on:arg2 digits:arg3 |
|
1134 |
! |
|
1135 |
||
1136 |
absScientificPrintFloat:arg1 on:arg2 digits:arg3 |
|
1137 |
<resource: #obsolete> |
|
1138 |
^ self class absScientificPrintFloat:arg1 on:arg2 digits:arg3 |
|
1139 |
! |
|
1140 |
||
1141 |
formatArgCountFor:arg |
|
1142 |
<resource: #obsolete> |
|
1143 |
^ self class formatArgCountFor:arg |
|
1144 |
! ! |
|
1145 |
||
1146 |
!PrintfScanf methodsFor:'printing'! |
|
1147 |
||
1148 |
printArgFrom:arg1 to:arg2 arguments:arg3 |
|
1149 |
<resource: #obsolete> |
|
1150 |
^ self class printArgFrom:arg1 to:arg2 arguments:arg3 |
|
1151 |
! |
|
1152 |
||
1153 |
printf:aString arguments:args |
|
1154 |
<resource: #obsolete> |
|
1155 |
||
1156 |
"Format and print the receiver with <args> formatted in C style, |
|
1157 |
as described in the UTek manual page for printf(3). |
|
1158 |
Returns the formatted printString." |
|
1463 | 1159 |
|
4417 | 1160 |
^ self class printf:aString arguments:args |
1161 |
||
1162 |
" |
|
1163 |
self new printf:'%d %x' arguments:#(1234 45054) |
|
1164 |
" |
|
1165 |
||
1166 |
"Modified: / 19-06-2017 / 15:21:56 / cg" |
|
1167 |
! |
|
1168 |
||
1169 |
printf:aFormatString on:outStream arguments: args |
|
1170 |
<resource: #obsolete> |
|
1171 |
"Format and print aFormatString on <outStream> with <args> |
|
1172 |
formatted in C style, as described in the UTek manual page for |
|
1173 |
printf(3)." |
|
1174 |
||
1175 |
self class printf:aFormatString on:outStream arguments: args |
|
1176 |
||
1177 |
"Modified (comment): / 19-06-2017 / 15:22:15 / cg" |
|
1178 |
! ! |
|
1179 |
||
1180 |
!PrintfScanf methodsFor:'scanning'! |
|
1181 |
||
1182 |
scanArgFrom:dataStream to:collection format:format |
|
1183 |
<resource: #obsolete> |
|
1184 |
||
1185 |
"Add to <collection> an object who's representation is found |
|
1186 |
in <dataStream> interpreted according to the conversion |
|
1187 |
control string in the Stream <format>. <format> is assumed to |
|
1188 |
be positioned just past a $%, and a complete control string is |
|
1189 |
assumed available. |
|
1190 |
||
1191 |
Return when the conversion control string is consumed. Leave |
|
1192 |
<format> pointing past the last character in the conversion |
|
1193 |
control string, leave <dataStream> pointing past any width |
|
1194 |
specified in <format>, or at the first character that doesn't |
|
1195 |
make sense for the <format>." |
|
1196 |
||
1197 |
^ self class scanArgFrom:dataStream to:collection format:format |
|
1198 |
||
1199 |
"Modified: / 19-06-2017 / 15:54:43 / cg" |
|
1200 |
! |
|
1201 |
||
1202 |
scanf:formatString fromStream:dataStream |
|
1203 |
<resource: #obsolete> |
|
1204 |
||
1205 |
"Return a Collection of objects found in the Character Stream |
|
1206 |
<dataStream> as interpreted according to the receiver. The |
|
1207 |
receiver is assumed to be a conversion control string as |
|
1208 |
specified in the UTek manual page for scanf(3)." |
|
1209 |
||
1210 |
^ self class scanf:formatString fromStream:dataStream |
|
1211 |
||
1212 |
"Modified: / 19-06-2017 / 15:54:27 / cg" |
|
1213 |
! |
|
1214 |
||
1215 |
sscanf:formatString fromString:aString |
|
1216 |
<resource: #obsolete> |
|
1217 |
||
1218 |
"Return a Collection of objects found in <string> as |
|
1219 |
interpreted according to the receiver. The receiver is |
|
1220 |
assumed to be a conversion control string as specified in the |
|
1221 |
UTek manual page for scanf(3)." |
|
1222 |
||
1223 |
^ self class sscanf:formatString fromString:aString |
|
1463 | 1224 |
|
1225 |
" |
|
1226 |
self new sscanf:'%d %x' fromString:'1234 affe' |
|
1227 |
" |
|
4417 | 1228 |
|
1229 |
"Modified: / 19-06-2017 / 15:54:14 / cg" |
|
1463 | 1230 |
! ! |
1231 |
||
1232 |
!PrintfScanf class methodsFor:'documentation'! |
|
1233 |
||
1234 |
version |
|
3719 | 1235 |
^ '$Header$' |
3115 | 1236 |
! |
1237 |
||
1238 |
version_CVS |
|
3719 | 1239 |
^ '$Header$' |
1463 | 1240 |
! ! |
2875 | 1241 |