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