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