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