FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
emscripten/src/library_formatString.js at universal-python · JavaScriptIOT/emscripten · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
JavaScriptIOT
/
emscripten
Public
forked from
emscripten-core/emscripten
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
emscripten
/
src
/
library_formatString.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
443 lines (424 loc) · 16.5 KB
Breadcrumbs
emscripten
/
src
/
library_formatString.js
Copy path
File metadata and controls
443 lines (424 loc) · 16.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
mergeInto
(
LibraryManager
.
library
,
{
// Performs printf-style formatting.
// format: A pointer to the format string.
// varargs: A pointer to the start of the arguments list.
// Returns the resulting string string as a character array.
_formatString__deps
:
[
'strlen'
,
'_reallyNegative'
]
,
_formatString
:
function
(
format
,
varargs
)
{
assert
(
(
varargs
&
3
)
===
0
)
;
var
textIndex
=
format
;
var
argIndex
=
varargs
;
function
getNextArg
(
type
)
{
// NOTE: Explicitly ignoring type safety. Otherwise this fails:
// int x = 4; printf("%c\n", (char)x);
var
ret
;
argIndex
=
Runtime
.
prepVararg
(
argIndex
,
type
)
;
if
(
type
===
'double'
)
{
ret
=
{
{
{
makeGetValue
(
'argIndex'
,
0
,
'double'
,
undefined
,
undefined
,
true
)
}
}
}
;
argIndex
+=
8
;
}
else
if
(
type
==
'i64'
)
{
ret
=
[
{
{
{
makeGetValue
(
'argIndex'
,
0
,
'i32'
,
undefined
,
undefined
,
true
,
4
)
}
}
}
,
{
{
{
makeGetValue
(
'argIndex'
,
4
,
'i32'
,
undefined
,
undefined
,
true
,
4
)
}
}
}
]
;
argIndex
+=
8
;
}
else
{
assert
(
(
argIndex
&
3
)
===
0
)
;
type
=
'i32'
;
// varargs are always i32, i64, or double
ret
=
{
{
{
makeGetValue
(
'argIndex'
,
0
,
'i32'
,
undefined
,
undefined
,
true
)
}
}
}
;
argIndex
+=
4
;
}
return
ret
;
}
var
ret
=
[
]
;
var
curr
,
next
,
currArg
;
while
(
1
)
{
var
startTextIndex
=
textIndex
;
curr
=
{
{
{
makeGetValue
(
0
,
'textIndex'
,
'i8'
)
}
}
}
;
if
(
curr
=
=
=
0
)
break
;
next
=
{
{
{
makeGetValue
(
0
,
'textIndex+1'
,
'i8'
)
}
}
}
;
if
(
curr
=
=
{
{
{
charCode
(
'%'
)
}
}
}
)
{
// Handle flags.
var
flagAlwaysSigned
=
false
;
var
flagLeftAlign
=
false
;
var
flagAlternative
=
false
;
var
flagZeroPad
=
false
;
var
flagPadSign
=
false
;
flagsLoop
:
while
(
1
)
{
switch
(
next
)
{
case
{
{
{
charCode
(
'+'
)
}
}
}
:
flagAlwaysSigned
=
true
;
break
;
case
{
{
{
charCode
(
'-'
)
}
}
}
:
flagLeftAlign
=
true
;
break
;
case
{
{
{
charCode
(
'#'
)
}
}
}
:
flagAlternative
=
true
;
break
;
case
{
{
{
charCode
(
'0'
)
}
}
}
:
if
(
flagZeroPad
)
{
break
flagsLoop
;
}
else
{
flagZeroPad
=
true
;
break
;
}
case
{
{
{
charCode
(
' '
)
}
}
}
:
flagPadSign
=
true
;
break
;
default:
break
flagsLoop
;
}
textIndex
++
;
next
=
{
{
{
makeGetValue
(
0
,
'textIndex+1'
,
'i8'
)
}
}
}
;
}
// Handle width.
var
width
=
0
;
if
(
next
==
{
{
{
charCode
(
'*'
)
}
}
}
)
{
width
=
getNextArg
(
'i32'
)
;
textIndex
++
;
next
=
{
{
{
makeGetValue
(
0
,
'textIndex+1'
,
'i8'
)
}
}
}
;
}
else
{
while
(
next
>=
{
{
{
charCode
(
'0'
)
}
}
}
&&
next
<=
{
{
{
charCode
(
'9'
)
}
}
}
)
{
width
=
width
*
10
+
(
next
-
{
{
{
charCode
(
'0'
)
}
}
}
)
;
textIndex
++
;
next
=
{
{
{
makeGetValue
(
0
,
'textIndex+1'
,
'i8'
)
}
}
}
;
}
}
// Handle precision.
var
precisionSet
=
false
,
precision
=
-
1
;
if
(
next
==
{
{
{
charCode
(
'.'
)
}
}
}
)
{
precision
=
0
;
precisionSet
=
true
;
textIndex
++
;
next
=
{
{
{
makeGetValue
(
0
,
'textIndex+1'
,
'i8'
)
}
}
}
;
if
(
next
=
=
{
{
{
charCode
(
'*'
)
}
}
}
)
{
precision
=
getNextArg
(
'i32'
)
;
textIndex
++
;
}
else
{
while
(
1
)
{
var
precisionChr
=
{
{
{
makeGetValue
(
0
,
'textIndex+1'
,
'i8'
)
}
}
}
;
if
(
precisionChr
<
{
{
{
charCode
(
'0'
)
}
}
}
||
precisionChr
>
{
{
{
charCode
(
'9'
)
}
}
}
)
break
;
precision
=
precision
*
10
+
(
precisionChr
-
{
{
{
charCode
(
'0'
)
}
}
}
)
;
textIndex
++
;
}
}
next
=
{
{
{
makeGetValue
(
0
,
'textIndex+1'
,
'i8'
)
}
}
}
;
}
if
(
precision
<
0
)
{
precision
=
6
;
// Standard default.
precisionSet
=
false
;
}
// Handle integer sizes. WARNING: These assume a 32-bit architecture!
var
argSize
;
switch
(
String
.
fromCharCode
(
next
)
)
{
case
'h'
:
var
nextNext
=
{
{
{
makeGetValue
(
0
,
'textIndex+2'
,
'i8'
)
}
}
}
;
if
(
nextNext
=
=
{
{
{
charCode
(
'h'
)
}
}
}
)
{
textIndex
++
;
argSize
=
1
;
// char (actually i32 in varargs)
}
else
{
argSize
=
2
;
// short (actually i32 in varargs)
}
break
;
case
'l'
:
var
nextNext
=
{
{
{
makeGetValue
(
0
,
'textIndex+2'
,
'i8'
)
}
}
}
;
if
(
nextNext
=
=
{
{
{
charCode
(
'l'
)
}
}
}
)
{
textIndex
++
;
argSize
=
8
;
// long long
}
else
{
argSize
=
4
;
// long
}
break
;
case
'L'
:
// long long
case
'q'
:
// int64_t
case
'j'
:
// intmax_t
argSize
=
8
;
break
;
case
'z'
:
// size_t
case
't'
:
// ptrdiff_t
case
'I'
:
// signed ptrdiff_t or unsigned size_t
argSize
=
4
;
break
;
default
:
argSize
=
null
;
}
if
(
argSize
)
textIndex
++
;
next
=
{
{
{
makeGetValue
(
0
,
'textIndex+1'
,
'i8'
)
}
}
}
;
// Handle type specifier.
switch
(
String
.
fromCharCode
(
next
)
)
{
case
'd'
:
case
'i'
:
case
'u'
:
case
'o'
:
case
'x'
:
case
'X'
:
case
'p'
:
{
// Integer.
var
signed
=
next
==
{
{
{
charCode
(
'd'
)
}
}
}
||
next
=
=
{
{
{
charCode
(
'i'
)
}
}
}
;
argSize
=
argSize
||
4
;
var
currArg
=
getNextArg
(
'i'
+
(
argSize
*
8
)
)
;
#if
PRECISE_I64_MATH
var
origArg
=
currArg
;
#endif
var
argText
;
// Flatten i64-1 [low, high] into a (slightly rounded) double
if
(
argSize
==
8
)
{
currArg
=
Runtime
.
makeBigInt
(
currArg
[
0
]
,
currArg
[
1
]
,
next
==
{
{
{
charCode
(
'u'
)
}
}
}
)
;
}
// Truncate to requested size.
if
(
argSize
<=
4
)
{
var
limit
=
Math
.
pow
(
256
,
argSize
)
-
1
;
currArg
=
(
signed
?
reSign
:
unSign
)
(
currArg
&
limit
,
argSize
*
8
)
;
}
// Format the number.
var
currAbsArg
=
Math
.
abs
(
currArg
)
;
var
prefix
=
''
;
if
(
next
==
{
{
{
charCode
(
'd'
)
}
}
}
||
next
==
{
{
{
charCode
(
'
i
'
)
}
}
}
)
{
#if
PRECISE_I64_MATH
if
(
argSize
==
8
&&
i64Math
)
argText
=
i64Math
.
stringify
(
origArg
[
0
]
,
origArg
[
1
]
,
null
)
;
else
#endif
argText
=
reSign
(
currArg
,
8
*
argSize
,
1
)
.
toString
(
10
)
;
}
else
if
(
next
==
{
{
{
charCode
(
'
u
'
)
}
}
}
)
{
#if
PRECISE_I64_MATH
if
(
argSize
==
8
&&
i64Math
)
argText
=
i64Math
.
stringify
(
origArg
[
0
]
,
origArg
[
1
]
,
true
)
;
else
#endif
argText
=
unSign
(
currArg
,
8
*
argSize
,
1
)
.
toString
(
10
)
;
currArg
=
Math
.
abs
(
currArg
)
;
}
else
if
(
next
==
{
{
{
charCode
(
'o'
)
}
}
}
)
{
argText
=
(
flagAlternative
?
'0'
:
''
)
+
currAbsArg
.
toString
(
8
)
;
}
else
if
(
next
==
{
{
{
charCode
(
'x'
)
}
}
}
||
next
==
{
{
{
charCode
(
'X'
)
}
}
}
)
{
prefix
=
(
flagAlternative
&&
currArg
!=
0
)
?
'0x'
:
''
;
#if
PRECISE_I64_MATH
if
(
argSize
==
8
&&
i64Math
)
{
if
(
origArg
[
1
]
)
{
argText
=
(
origArg
[
1
]
>>>
0
)
.
toString
(
16
)
;
var
lower
=
(
origArg
[
0
]
>>>
0
)
.
toString
(
16
)
;
while
(
lower
.
length
<
8
)
lower
=
'
0
'
+
lower
;
argText
+=
lower
;
}
else
{
argText
=
(
origArg
[
0
]
>>>
0
)
.
toString
(
16
)
;
}
}
else
#endif
if
(
currArg
<
0
)
{
// Represent negative numbers in hex as 2's complement.
currArg
=
-
currArg
;
argText
=
(
currAbsArg
-
1
)
.
toString
(
16
)
;
var
buffer
=
[
]
;
for
(
var
i
=
0
;
i
<
argText
.
length
;
i
++
)
{
buffer
.
push
(
(
0xF
-
parseInt
(
argText
[
i
]
,
16
)
)
.
toString
(
16
)
)
;
}
argText
=
buffer
.
join
(
''
)
;
while
(
argText
.
length
<
argSize
*
2
)
argText
=
'
f
'
+
argText
;
}
else
{
argText
=
currAbsArg
.
toString
(
16
)
;
}
if
(
next
==
{
{
{
charCode
(
'X'
)
}
}
}
)
{
prefix
=
prefix
.
toUpperCase
(
)
;
argText
=
argText
.
toUpperCase
(
)
;
}
}
else
if
(
next
==
{
{
{
charCode
(
'p'
)
}
}
}
)
{
if
(
currAbsArg
===
0
)
{
argText
=
'
(
nil
)
'
;
}
else
{
prefix
=
'
0
x
'
;
argText
=
currAbsArg
.
toString
(
16
)
;
}
}
if
(
precisionSet
)
{
while
(
argText
.
length
<
precision
)
{
argText
=
'
0
'
+
argText
;
}
}
// Add sign if needed
if
(
currArg
>=
0
)
{
if
(
flagAlwaysSigned
)
{
prefix
=
'
+
' + prefix;
}
else
if
(
flagPadSign
)
{
prefix
=
' '
+
prefix
;
}
}
// Move sign to prefix so we zero-pad after the sign
if
(
argText
.
charAt
(
0
)
==
'-'
)
{
prefix
=
'
-
' + prefix;
argText
=
argText
.
substr
(
1
)
;
}
// Add padding.
while
(
prefix
.
length
+
argText
.
length
<
width
)
{
if
(
flagLeftAlign
)
{
argText
+=
' '
;
}
else
{
if
(
flagZeroPad
)
{
argText
=
'
0
'
+
argText
;
}
else
{
prefix
=
' '
+
prefix
;
}
}
}
// Insert the result into the buffer.
argText
=
prefix
+
argText
;
argText
.
split
(
''
)
.
forEach
(
function
(
chr
)
{
ret
.
push
(
chr
.
charCodeAt
(
0
)
)
;
}
)
;
break
;
}
case
'f'
:
case
'
F
':
case
'
e
':
case
'
E
':
case
'
g
':
case
'
G
':
{
// Float.
var
currArg
=
getNextArg
(
'double'
)
;
var
argText
;
if
(
isNaN
(
currArg
)
)
{
argText
=
'
nan
'
;
flagZeroPad
=
false
;
}
else
if
(
!
isFinite
(
currArg
)
)
{
argText
=
(
currArg
<
0
?
'-'
:
''
)
+
'inf'
;
flagZeroPad
=
false
;
}
else
{
var
isGeneral
=
false
;
var
effectivePrecision
=
Math
.
min
(
precision
,
20
)
;
// Convert g/G to f/F or e/E, as per:
// http://pubs.opengroup.org/onlinepubs/9699919799/functions/printf.html
if
(
next
==
{
{
{
charCode
(
'g'
)
}
}
}
||
next
==
{
{
{
charCode
(
'G'
)
}
}
}
)
{
isGeneral
=
true
;
precision
=
precision
||
1
;
var
exponent
=
parseInt
(
currArg
.
toExponential
(
effectivePrecision
)
.
split
(
'e'
)
[
1
]
,
10
)
;
if
(
precision
>
exponent
&&
exponent
>=
-
4
)
{
next
=
(
(
next
==
{
{
{
charCode
(
'g'
)
}
}
}
)
?
'f'
:
'F'
)
.
charCodeAt
(
0
)
;
precision
-=
exponent
+
1
;
}
else
{
next
=
(
(
next
==
{
{
{
charCode
(
'g'
)
}
}
}
)
?
'e'
:
'E'
)
.
charCodeAt
(
0
)
;
precision
--
;
}
effectivePrecision
=
Math
.
min
(
precision
,
20
)
;
}
if
(
next
==
{
{
{
charCode
(
'e'
)
}
}
}
||
next
==
{
{
{
charCode
(
'E'
)
}
}
}
)
{
argText
=
currArg
.
toExponential
(
effectivePrecision
)
;
// Make sure the exponent has at least 2 digits.
if
(
/
[
e
E
]
[
-
+
]
\d
$
/
.
test
(
argText
)
)
{
argText
=
argText
.
slice
(
0
,
-
1
)
+
'0'
+
argText
.
slice
(
-
1
)
;
}
}
else
if
(
next
==
{
{
{
charCode
(
'f'
)
}
}
}
||
next
==
{
{
{
charCode
(
'F'
)
}
}
}
)
{
argText
=
currArg
.
toFixed
(
effectivePrecision
)
;
if
(
currArg
===
0
&&
__reallyNegative
(
currArg
)
)
{
argText
=
'
-
' + argText;
}
}
var
parts
=
argText
.
split
(
'e'
)
;
if
(
isGeneral
&&
!
flagAlternative
)
{
// Discard trailing zeros and periods.
while
(
parts
[
0
]
.
length
>
1
&&
parts
[
0
]
.
indexOf
(
'.'
)
!=
-
1
&&
(
parts
[
0
]
.
slice
(
-
1
)
==
'0'
||
parts
[
0
]
.
slice
(
-
1
)
==
'.'
)
)
{
parts
[
0
]
=
parts
[
0
]
.
slice
(
0
,
-
1
)
;
}
}
else
{
// Make sure we have a period in alternative mode.
if
(
flagAlternative
&&
argText
.
indexOf
(
'.'
)
==
-
1
)
parts
[
0
]
+=
'.'
;
// Zero pad until required precision.
while
(
precision
>
effectivePrecision
++
)
parts
[
0
]
+=
'0'
;
}
argText
=
parts
[
0
]
+
(
parts
.
length
>
1
?
'e'
+
parts
[
1
]
:
''
)
;
// Capitalize 'E' if needed.
if
(
next
==
{
{
{
charCode
(
'E'
)
}
}
}
)
argText
=
argText
.
toUpperCase
(
)
;
// Add sign.
if
(
currArg
>=
0
)
{
if
(
flagAlwaysSigned
)
{
argText
=
'
+
' + argText;
}
else
if
(
flagPadSign
)
{
argText
=
' '
+
argText
;
}
}
}
// Add padding.
while
(
argText
.
length
<
width
)
{
if
(
flagLeftAlign
)
{
argText
+=
' '
;
}
else
{
if
(
flagZeroPad
&&
(
argText
[
0
]
==
'-'
||
argText
[
0
]
==
'+'
)
)
{
argText
=
argText
[
0
]
+
'0'
+
argText
.
slice
(
1
)
;
}
else
{
argText
=
(
flagZeroPad
?
'0'
:
' '
)
+
argText
;
}
}
}
// Adjust case.
if
(
next
<
{
{
{
charCode
(
'a'
)
}
}
}
)
argText
=
argText
.
toUpperCase
(
)
;
// Insert the result into the buffer.
argText
.
split
(
''
)
.
forEach
(
function
(
chr
)
{
ret
.
push
(
chr
.
charCodeAt
(
0
)
)
;
}
)
;
break
;
}
case
's'
:
{
// String.
var
arg
=
getNextArg
(
'i8*'
)
;
var
argLength
=
arg
?
_strlen
(
arg
)
: '
(
null
)
'
.
length
;
if
(
precisionSet
)
argLength
=
Math
.
min
(
argLength
,
precision
)
;
if
(
!
flagLeftAlign
)
{
while
(
argLength
<
width
--
)
{
ret
.
push
(
{
{
{
charCode
(
' ') }}});
}
}
if
(
arg
)
{
for
(
var
i
=
0
;
i
<
argLength
;
i
++
)
{
ret
.
push
(
{
{
{
makeGetValue
(
'arg++'
,
0
,
'i8'
,
null
,
true
)
}
}
}
)
;
}
}
else
{
ret
=
ret
.
concat
(
intArrayFromString
(
'(null)'
.
substr
(
0
,
argLength
)
,
true
)
)
;
}
if
(
flagLeftAlign
)
{
while
(
argLength
<
width
--
)
{
ret
.
push
(
{
{
{
charCode
(
' ') }}});
}
}
break
;
}
case
'c'
:
{
// Character.
if
(
flagLeftAlign
)
ret
.
push
(
getNextArg
(
'i8'
)
)
;
while
(
--
width
>
0
)
{
ret
.
push
(
{
{
{
charCode
(
' ') }}});
}
if
(
!
flagLeftAlign
)
ret
.
push
(
getNextArg
(
'i8'
)
)
;
break
;
}
case
'n'
:
{
// Write the length written so far to the next parameter.
var
ptr
=
getNextArg
(
'i32*'
)
;
{
{
{
makeSetValue
(
'ptr'
,
'0'
,
'ret.length'
,
'i32'
)
}
}
}
;
break
;
}
case
'%'
:
{
// Literal percent sign.
ret
.
push
(
curr
)
;
break
;
}
default
:
{
// Unknown specifiers remain untouched.
for
(
var
i
=
startTextIndex
;
i
<
textIndex
+
2
;
i
++
)
{
ret
.
push
(
{
{
{
makeGetValue
(
0
,
'i'
,
'i8'
)
}
}
}
)
;
}
}
}
textIndex
+=
2
;
// TODO: Support a/A (hex float) and m (last error) specifiers.
// TODO: Support %1${specifier} for arg selection.
}
else
{
ret
.
push
(
curr
)
;
textIndex
+=
1
;
}
}
return
ret
;
}
,
// printf/puts implementations for when musl is not pulled in - very partial. useful for tests, and when bootstrapping structInfo
printf__deps
:
[
'_formatString'
]
,
printf
:
function
(
format
,
varargs
)
{
// int printf(const char *restrict format, ...);
// http://pubs.opengroup.org/onlinepubs/000095399/functions/printf.html
// extra effort to support printf, even without a filesystem. very partial, very hackish
var
result
=
__formatString
(
format
,
varargs
)
;
var
string
=
intArrayToString
(
result
)
;
if
(
string
[
string
.
length
-
1
]
===
'\n'
)
string
=
string
.
substr
(
0
,
string
.
length
-
1
)
;
// remove a final \n, as Module.print will do that
Module
.
print
(
string
)
;
return
result
.
length
;
}
,
puts
:
function
(
s
)
{
// extra effort to support puts, even without a filesystem. very partial, very hackish
var
result
=
Pointer_stringify
(
s
)
;
var
string
=
result
.
substr
(
0
)
;
if
(
string
[
string
.
length
-
1
]
===
'\n'
)
string
=
string
.
substr
(
0
,
string
.
length
-
1
)
;
// remove a final \n, as Module.print will do that
Module
.
print
(
string
)
;
return
result
.
length
;
}
,
}
)
;
Back
|
FazBrowse Home
|
New Git URL