author | Claus Gittinger <cg@exept.de> |
Mon, 19 Jun 2017 18:12:29 +0200 | |
changeset 4422 | 92f08dfe59af |
parent 4417 | 01b4de1f0b01 |
child 4428 | f44ab76503a6 |
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" |
|
4422 | 502 |
i := x truncated. |
503 |
i := i asInteger. |
|
4417 | 504 |
aStream nextPut:(48 + i) asCharacter. |
505 |
x := (x - i) * 10.0. |
|
506 |
fuzz := fuzz * 10.0. |
|
507 |
exp := exp - 1. |
|
508 |
exp = -1 ifTrue:[ |
|
509 |
aStream nextPut:$. |
|
510 |
] |
|
511 |
]. |
|
512 |
[ exp >= -1 ] whileTrue:[ |
|
513 |
aStream nextPut:$0. |
|
514 |
exp := exp - 1. |
|
515 |
exp = -1 ifTrue:[ |
|
516 |
aStream nextPut:$. |
|
517 |
] |
|
518 |
] |
|
4416 | 519 |
|
4422 | 520 |
"Modified: / 19-06-2017 / 18:06:51 / cg" |
4416 | 521 |
! |
522 |
||
4417 | 523 |
absPrintFloat:aFloat on:aStream digits:digits |
524 |
"Place a string representation of the receiver on <aStream>, |
|
525 |
using <digits> significant digits." |
|
526 |
||
527 |
(aFloat < 1.0e6 and:[ aFloat > 1.0e-4 ]) ifTrue:[ |
|
528 |
self absDecimalPrintFloat:aFloat on:aStream digits:digits |
|
529 |
] ifFalse:[ |
|
530 |
self absScientificPrintFloat:aFloat on:aStream digits:digits |
|
531 |
] |
|
4416 | 532 |
|
4417 | 533 |
"Modified: / 19-06-2017 / 15:03:40 / cg" |
4416 | 534 |
! |
535 |
||
4417 | 536 |
absScientificPrintFloat:aFloat on:aStream digits:digits |
537 |
"Place a string representation of the receiver on <aStream>, |
|
538 |
using <digits> significant digits, using scientific notation." |
|
539 |
||
540 |
|absVal exp fuzz x q i| |
|
541 |
||
542 |
absVal := aFloat abs. |
|
4416 | 543 |
|
4417 | 544 |
"x is myself normalized to [1.0, 10.0), exp is my exponent" |
545 |
exp := absVal < 1.0 |
|
546 |
ifTrue:[ |
|
547 |
(10.0 / absVal) log10 floor asInteger negated] |
|
548 |
ifFalse:[ |
|
549 |
absVal log10 floor asInteger]. |
|
550 |
x := absVal / (10.0 raisedTo:exp). |
|
551 |
"round the last digit to be printed" |
|
552 |
fuzz := 10.0 raisedTo:1 - digits. |
|
553 |
x := 0.5 * fuzz + x. |
|
554 |
x >= 10.0 ifTrue:[ "check if rounding has unnormalized x" |
|
555 |
x := x / 10.0. |
|
556 |
exp := exp + 1 |
|
557 |
]. |
|
558 |
q := exp. |
|
559 |
exp := 0. |
|
560 |
[ x >= fuzz ] whileTrue:[ |
|
561 |
"use fuzz to track significance" |
|
562 |
i := x truncated. |
|
563 |
i := i asInteger. |
|
564 |
aStream nextPut:(48 + i) asCharacter. |
|
565 |
x := (x - i) * 10.0. |
|
566 |
fuzz := fuzz * 10.0. |
|
567 |
exp := exp - 1. |
|
568 |
exp = -1 ifTrue:[ |
|
569 |
aStream nextPut:$. |
|
570 |
] |
|
571 |
]. |
|
572 |
[ exp >= -1 ] whileTrue:[ |
|
573 |
aStream nextPut:$0. |
|
574 |
exp := exp - 1. |
|
575 |
exp = -1 ifTrue:[ |
|
576 |
aStream nextPut:$. |
|
577 |
] |
|
578 |
]. |
|
579 |
aStream nextPut:$e. |
|
580 |
q printOn:aStream |
|
4416 | 581 |
|
4417 | 582 |
"Modified: / 19-06-2017 / 15:57:09 / cg" |
4416 | 583 |
! |
584 |
||
4417 | 585 |
formatArgCountFor:aFormatString |
586 |
"Return the number of arguments required/produced, |
|
587 |
if the argument is interpreted as a printf/scanf format control string." |
|
588 |
||
589 |
|nonConsecutive count| |
|
4416 | 590 |
|
4417 | 591 |
nonConsecutive := true. |
592 |
count := 0. |
|
593 |
aFormatString do:[:c | |
|
594 |
c == $% ifTrue:[ |
|
595 |
nonConsecutive ifTrue:[ |
|
596 |
count := count + 1. |
|
597 |
nonConsecutive := false |
|
598 |
] ifFalse:[ |
|
599 |
count := count - 1. |
|
600 |
nonConsecutive := true |
|
601 |
] |
|
602 |
] ifFalse:[ |
|
603 |
nonConsecutive := true |
|
604 |
] |
|
605 |
]. |
|
606 |
^ count |
|
607 |
! |
|
1463 | 608 |
|
609 |
scanArgFrom:dataStream to:collection format:format |
|
610 |
"Add to <collection> an object who's representation is found |
|
611 |
in <dataStream> interpreted according to the conversion |
|
612 |
control string in the Stream <format>. <format> is assumed to |
|
613 |
be positioned just past a $%, and a complete control string is |
|
614 |
assumed available. |
|
615 |
||
616 |
Return when the conversion control string is consumed. Leave |
|
617 |
<format> pointing past the last character in the conversion |
|
618 |
control string, leave <dataStream> pointing past any width |
|
619 |
specified in <format>, or at the first character that doesn't |
|
620 |
make sense for the <format>." |
|
4417 | 621 |
|
1463 | 622 |
|final width char pos data scanset exclusive return last| |
623 |
||
624 |
final := [:retval | |
|
625 |
collection add:retval. |
|
626 |
data == dataStream ifFalse:[ |
|
627 |
dataStream position:dataStream position + data position |
|
628 |
]. |
|
629 |
^ self |
|
630 |
]. |
|
1464 | 631 |
|
1463 | 632 |
char := format peek. |
633 |
char == $% ifTrue:[ |
|
634 |
^ dataStream peekFor:char |
|
635 |
]. |
|
636 |
char == $* ifTrue:[ |
|
637 |
format next. |
|
638 |
char := format peek. |
|
639 |
final := [:retval | |
|
640 |
data == dataStream ifFalse:[ |
|
641 |
dataStream position:dataStream position + data position |
|
642 |
]. |
|
643 |
^ self |
|
644 |
] |
|
645 |
]. |
|
1464 | 646 |
width := 0. |
1463 | 647 |
char isDigit ifTrue:[ |
648 |
width := Integer readFrom:format. |
|
649 |
char := format peek |
|
650 |
]. |
|
651 |
('slhduoxfeg' includes:char) ifTrue:[ |
|
652 |
dataStream skipSeparators |
|
653 |
]. |
|
654 |
width = 0 ifTrue:[ |
|
655 |
data := dataStream |
|
656 |
] ifFalse:[ |
|
657 |
pos := dataStream position. |
|
658 |
data := ReadStream on:(dataStream next:width). |
|
659 |
dataStream position:pos |
|
660 |
]. |
|
661 |
char == $s ifTrue:[ |
|
662 |
final value:(data upToSeparator) |
|
663 |
]. |
|
664 |
char == $c ifTrue:[ |
|
665 |
width = 0 ifTrue:[ |
|
666 |
final value:(String with:data next) |
|
667 |
] ifFalse:[ |
|
668 |
final value:data contents |
|
669 |
] |
|
670 |
]. |
|
671 |
char == $[ ifTrue:[ |
|
672 |
"What a mess!!!!" |
|
3539 | 673 |
return := CharacterWriteStream on:(String new:8). |
1463 | 674 |
scanset := IdentitySet new. |
675 |
format next. |
|
676 |
width = 0 ifTrue:[ |
|
677 |
width := SmallInteger maxVal |
|
678 |
]. |
|
679 |
exclusive := format peekFor:$^. |
|
680 |
[ |
|
681 |
last := char. |
|
682 |
char := format next. |
|
683 |
char == $] |
|
684 |
] whileFalse:[ |
|
685 |
char == $- ifFalse:[ |
|
686 |
scanset add:char |
|
687 |
] ifTrue:[ |
|
2678
a2f6f1e865e6
changed: #scanArgFrom:to:format:
Claus Gittinger <cg@exept.de>
parents:
2581
diff
changeset
|
688 |
last to:format next do:[:c | |
1463 | 689 |
scanset add:c |
690 |
] |
|
691 |
] |
|
692 |
]. |
|
693 |
[ |
|
694 |
data atEnd not and:[ (scanset includes:data peek) xor:exclusive ] |
|
695 |
] whileTrue:[ return nextPut:data next ]. |
|
696 |
final value:return contents |
|
697 |
]. |
|
698 |
('lh' includes:char) ifTrue:[ |
|
699 |
format next. |
|
700 |
char := format peek |
|
701 |
]. |
|
702 |
('DUdu' includes:char) ifTrue:[ |
|
703 |
final value:(Integer readFrom:data) |
|
704 |
]. |
|
705 |
('FEGfeg' includes:char) ifTrue:[ |
|
706 |
final value:(Float readFrom:data) |
|
707 |
]. |
|
708 |
('Oo' includes:char) ifTrue:[ |
|
709 |
final value:(Integer readFrom:data radix:8) |
|
710 |
]. |
|
711 |
('Xx' includes:char) ifTrue:[ |
|
712 |
final value:(Integer readFrom:data radix:16) |
|
2889 | 713 |
]. |
714 |
('Bb' includes:char) ifTrue:[ |
|
715 |
final value:(Integer readFrom:data radix:2) |
|
716 |
]. |
|
2678
a2f6f1e865e6
changed: #scanArgFrom:to:format:
Claus Gittinger <cg@exept.de>
parents:
2581
diff
changeset
|
717 |
|
4417 | 718 |
"Created: / 19-06-2017 / 15:53:28 / cg" |
719 |
! ! |
|
720 |
||
721 |
!PrintfScanf class methodsFor:'scanning'! |
|
1463 | 722 |
|
723 |
scanf:formatString fromStream:dataStream |
|
724 |
"Return a Collection of objects found in the Character Stream |
|
725 |
<dataStream> as interpreted according to the receiver. The |
|
726 |
receiver is assumed to be a conversion control string as |
|
727 |
specified in the UTek manual page for scanf(3)." |
|
4417 | 728 |
|
1463 | 729 |
|results format char| |
730 |
||
731 |
results := OrderedCollection new. |
|
732 |
format := ReadStream on:formatString. |
|
733 |
[ format atEnd ] whileFalse:[ |
|
734 |
char := format next. |
|
735 |
(char == Character space or:[ char == Character tab ]) ifTrue:[ |
|
736 |
dataStream skipSeparators. |
|
737 |
format skipSeparators |
|
738 |
]. |
|
739 |
char == $% ifTrue:[ |
|
4416 | 740 |
self scanArgFrom:dataStream to:results format:format |
1463 | 741 |
] ifFalse:[ |
742 |
dataStream peekFor:char |
|
743 |
] |
|
744 |
]. |
|
745 |
^ results |
|
4416 | 746 |
|
4417 | 747 |
"Modified: / 19-06-2017 / 15:53:04 / cg" |
1463 | 748 |
! |
749 |
||
750 |
sscanf:formatString fromString:aString |
|
751 |
"Return a Collection of objects found in <string> as |
|
752 |
interpreted according to the receiver. The receiver is |
|
753 |
assumed to be a conversion control string as specified in the |
|
754 |
UTek manual page for scanf(3)." |
|
4417 | 755 |
|
756 |
^ self scanf:formatString fromStream:(ReadStream on:aString) |
|
757 |
||
758 |
"Modified: / 19-06-2017 / 15:53:59 / cg" |
|
759 |
! ! |
|
760 |
||
761 |
!PrintfScanf methodsFor:'helpers'! |
|
762 |
||
763 |
absDecimalPrintFloat:arg1 on:arg2 digits:arg3 |
|
764 |
<resource: #obsolete> |
|
765 |
^ self class absDecimalPrintFloat:arg1 on:arg2 digits:arg3 |
|
766 |
! |
|
767 |
||
768 |
absPrintFloat:arg1 on:arg2 digits:arg3 |
|
769 |
<resource: #obsolete> |
|
770 |
^ self class absPrintFloat:arg1 on:arg2 digits:arg3 |
|
771 |
! |
|
772 |
||
773 |
absScientificPrintFloat:arg1 on:arg2 digits:arg3 |
|
774 |
<resource: #obsolete> |
|
775 |
^ self class absScientificPrintFloat:arg1 on:arg2 digits:arg3 |
|
776 |
! |
|
777 |
||
778 |
formatArgCountFor:arg |
|
779 |
<resource: #obsolete> |
|
780 |
^ self class formatArgCountFor:arg |
|
781 |
! ! |
|
782 |
||
783 |
!PrintfScanf methodsFor:'printing'! |
|
784 |
||
785 |
printArgFrom:arg1 to:arg2 arguments:arg3 |
|
786 |
<resource: #obsolete> |
|
787 |
^ self class printArgFrom:arg1 to:arg2 arguments:arg3 |
|
788 |
! |
|
789 |
||
790 |
printf:aString arguments:args |
|
791 |
<resource: #obsolete> |
|
792 |
||
793 |
"Format and print the receiver with <args> formatted in C style, |
|
794 |
as described in the UTek manual page for printf(3). |
|
795 |
Returns the formatted printString." |
|
1463 | 796 |
|
4417 | 797 |
^ self class printf:aString arguments:args |
798 |
||
799 |
" |
|
800 |
self new printf:'%d %x' arguments:#(1234 45054) |
|
801 |
" |
|
802 |
||
803 |
"Modified: / 19-06-2017 / 15:21:56 / cg" |
|
804 |
! |
|
805 |
||
806 |
printf:aFormatString on:outStream arguments: args |
|
807 |
<resource: #obsolete> |
|
808 |
"Format and print aFormatString on <outStream> with <args> |
|
809 |
formatted in C style, as described in the UTek manual page for |
|
810 |
printf(3)." |
|
811 |
||
812 |
self class printf:aFormatString on:outStream arguments: args |
|
813 |
||
814 |
"Modified (comment): / 19-06-2017 / 15:22:15 / cg" |
|
815 |
! ! |
|
816 |
||
817 |
!PrintfScanf methodsFor:'scanning'! |
|
818 |
||
819 |
scanArgFrom:dataStream to:collection format:format |
|
820 |
<resource: #obsolete> |
|
821 |
||
822 |
"Add to <collection> an object who's representation is found |
|
823 |
in <dataStream> interpreted according to the conversion |
|
824 |
control string in the Stream <format>. <format> is assumed to |
|
825 |
be positioned just past a $%, and a complete control string is |
|
826 |
assumed available. |
|
827 |
||
828 |
Return when the conversion control string is consumed. Leave |
|
829 |
<format> pointing past the last character in the conversion |
|
830 |
control string, leave <dataStream> pointing past any width |
|
831 |
specified in <format>, or at the first character that doesn't |
|
832 |
make sense for the <format>." |
|
833 |
||
834 |
^ self class scanArgFrom:dataStream to:collection format:format |
|
835 |
||
836 |
"Modified: / 19-06-2017 / 15:54:43 / cg" |
|
837 |
! |
|
838 |
||
839 |
scanf:formatString fromStream:dataStream |
|
840 |
<resource: #obsolete> |
|
841 |
||
842 |
"Return a Collection of objects found in the Character Stream |
|
843 |
<dataStream> as interpreted according to the receiver. The |
|
844 |
receiver is assumed to be a conversion control string as |
|
845 |
specified in the UTek manual page for scanf(3)." |
|
846 |
||
847 |
^ self class scanf:formatString fromStream:dataStream |
|
848 |
||
849 |
"Modified: / 19-06-2017 / 15:54:27 / cg" |
|
850 |
! |
|
851 |
||
852 |
sscanf:formatString fromString:aString |
|
853 |
<resource: #obsolete> |
|
854 |
||
855 |
"Return a Collection of objects found in <string> as |
|
856 |
interpreted according to the receiver. The receiver is |
|
857 |
assumed to be a conversion control string as specified in the |
|
858 |
UTek manual page for scanf(3)." |
|
859 |
||
860 |
^ self class sscanf:formatString fromString:aString |
|
1463 | 861 |
|
862 |
" |
|
863 |
self new sscanf:'%d %x' fromString:'1234 affe' |
|
864 |
" |
|
4417 | 865 |
|
866 |
"Modified: / 19-06-2017 / 15:54:14 / cg" |
|
1463 | 867 |
! ! |
868 |
||
869 |
!PrintfScanf class methodsFor:'documentation'! |
|
870 |
||
871 |
version |
|
3719 | 872 |
^ '$Header$' |
3115 | 873 |
! |
874 |
||
875 |
version_CVS |
|
3719 | 876 |
^ '$Header$' |
1463 | 877 |
! ! |
2875 | 878 |