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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
|
// Copyright 2020 The Libc Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build !(linux && (amd64 || arm64 || loong64))
package libc // import "modernc.org/libc"
import (
"io"
"strconv"
"strings"
"unsafe"
)
// The format string consists of a sequence of directives which describe how to
// process the sequence of input characters. If processing of a directive
// fails, no further input is read, and scanf() returns. A "failure" can
// be either of the following: input failure, meaning that input characters
// were unavailable, or matching failure, meaning that the input was
// inappropriate.
func scanf(r io.ByteScanner, format, args uintptr) (nvalues int32) {
// var src []byte //TODO-
var ok bool
out:
for {
c := *(*byte)(unsafe.Pointer(format))
// src = append(src, c) //TODO-
switch c {
case '%':
var n int
var match bool
format, n, match = scanfConversion(r, format, &args)
if !match {
break out
}
nvalues += int32(n)
ok = true
case 0:
break out
case ' ', '\t', '\n', '\r', '\v', '\f':
format = skipWhiteSpace(format)
ok = true
next:
for {
c, err := r.ReadByte()
if err != nil {
break out
}
switch c {
case ' ', '\t', '\n', '\r', '\v', '\f':
// nop
default:
r.UnreadByte()
break next
}
}
default:
c2, err := r.ReadByte()
if err != nil {
break out
}
if c2 != c {
r.UnreadByte()
break out
}
format++
ok = true
}
}
if ok {
return nvalues
}
return -1 // stdio.EOF but not defined for windows
}
func scanfConversion(r io.ByteScanner, format uintptr, args *uintptr) (_ uintptr, nvalues int, match bool) {
format++ // '%'
// Each conversion specification in format begins with either the character '%'
// or the character sequence "%n$" (see below for the distinction) followed by:
mod := 0
width := -1
discard := false
flags:
for {
switch c := *(*byte)(unsafe.Pointer(format)); c {
case '*':
// An optional '*' assignment-suppression character: scanf() reads input as
// directed by the conversion specification, but discards the input. No
// corresponding pointer argument is re‐ quired, and this specification is not
// included in the count of successful assignments returned by scanf().
format++
discard = true
case '\'':
// For decimal conversions, an optional quote character ('). This specifies
// that the input number may include thousands' separators as defined by the
// LC_NUMERIC category of the current locale. (See setlocale(3).) The quote
// character may precede or follow the '*' assignment-suppression character.
format++
panic(todo(""))
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
// An optional decimal integer which specifies the maximum field width.
// Reading of characters stops either when this maximum is reached or when a
// nonmatching character is found, whichever happens first. Most conversions
// discard initial white space characters (the exceptions are noted below), and
// these discarded characters don't count toward the maximum field width.
// String input conversions store a terminating null byte ('\0') to mark the
// end of the input; the maximum field width does not include this terminator.
width = 0
num:
for {
var digit int
switch c := *(*byte)(unsafe.Pointer(format)); {
default:
break num
case c >= '0' && c <= '9':
format++
digit = int(c) - '0'
}
width0 := width
width = 10*width + digit
if width < width0 {
panic(todo(""))
}
}
case 'h', 'j', 'l', 'L', 'q', 't', 'z':
format, mod = parseLengthModifier(format)
default:
break flags
}
}
// A conversion specifier that specifies the type of input conversion to be
// performed.
switch c := *(*byte)(unsafe.Pointer(format)); c {
case '%':
// Matches a literal '%'. That is, %% in the format string matches a single
// input '%' character. No conversion is done (but initial white space
// characters are discarded), and assign‐ ment does not occur.
format++
skipReaderWhiteSpace(r)
c, err := r.ReadByte()
if err != nil {
return format, -1, false
}
if c == '%' {
return format, 1, true
}
r.UnreadByte()
return format, 0, false
case 'd':
// Matches an optionally signed decimal integer; the next pointer must be a
// pointer to int.
format++
skipReaderWhiteSpace(r)
var digit, n uint64
allowSign := true
neg := false
dec:
for ; width != 0; width-- {
c, err := r.ReadByte()
if err != nil {
if match {
break dec
}
return 0, 0, false
}
if allowSign {
switch c {
case '-':
allowSign = false
neg = true
continue
case '+':
allowSign = false
continue
}
}
switch {
case c >= '0' && c <= '9':
digit = uint64(c) - '0'
default:
r.UnreadByte()
break dec
}
match = true
n0 := n
n = n*10 + digit
if n < n0 {
panic(todo(""))
}
}
if !match {
break
}
if !discard {
arg := VaUintptr(args)
v := int64(n)
if neg {
v = -v
}
switch mod {
case modNone:
*(*int32)(unsafe.Pointer(arg)) = int32(v)
case modH:
*(*int16)(unsafe.Pointer(arg)) = int16(v)
case modHH:
*(*int8)(unsafe.Pointer(arg)) = int8(v)
case modL:
*(*long)(unsafe.Pointer(arg)) = long(v)
case modLL:
*(*int64)(unsafe.Pointer(arg)) = int64(v)
default:
panic(todo("", mod))
}
}
nvalues = 1
case 'D':
// Equivalent to ld; this exists only for backward compatibility. (Note:
// thus only in libc4. In libc5 and glibc the %D is silently ignored, causing
// old programs to fail mysteriously.)
format++
panic(todo(""))
case 'i':
// Matches an optionally signed integer; the next pointer must be a pointer to
// int. The integer is read in base 16 if it begins with 0x or 0X, in base 8
// if it begins with 0, and in base 10 otherwise. Only characters that
// correspond to the base are used.
format++
panic(todo(""))
case 'o':
// Matches an unsigned octal integer; the next pointer must be a pointer to
// unsigned int.
format++
panic(todo(""))
case 'u':
// Matches an unsigned decimal integer; the next pointer must be a pointer to
// unsigned int.
format++
panic(todo(""))
case 'x', 'X':
// Matches an unsigned hexadecimal integer; the next pointer must be a pointer
// to unsigned int.
format++
skipReaderWhiteSpace(r)
var digit, n uint64
allowPrefix := true
var b []byte
hex:
for ; width != 0; width-- {
c, err := r.ReadByte()
if err != nil {
if match || err == io.EOF {
break hex
}
panic(todo("", err))
}
if allowPrefix {
if len(b) == 1 && b[0] == '0' && (c == 'x' || c == 'X') {
allowPrefix = false
match = false
b = nil
continue
}
b = append(b, c)
}
switch {
case c >= '0' && c <= '9':
digit = uint64(c) - '0'
case c >= 'a' && c <= 'f':
digit = uint64(c) - 'a' + 10
case c >= 'A' && c <= 'F':
digit = uint64(c) - 'A' + 10
default:
r.UnreadByte()
break hex
}
match = true
n0 := n
n = n<<4 + digit
if n < n0 {
panic(todo(""))
}
}
if !match {
break
}
if !discard {
arg := VaUintptr(args)
switch mod {
case modNone:
*(*uint32)(unsafe.Pointer(arg)) = uint32(n)
case modH:
*(*uint16)(unsafe.Pointer(arg)) = uint16(n)
case modHH:
*(*byte)(unsafe.Pointer(arg)) = byte(n)
case modL:
*(*ulong)(unsafe.Pointer(arg)) = ulong(n)
default:
panic(todo(""))
}
}
nvalues = 1
case 'f', 'e', 'g', 'E', 'a':
// Matches an optionally signed floating-point number; the next pointer must be
// a pointer to float.
format++
skipReaderWhiteSpace(r)
seq := fpLiteral(r)
if len(seq) == 0 {
return 0, 0, false
}
var neg bool
switch seq[0] {
case '+':
seq = seq[1:]
case '-':
neg = true
seq = seq[1:]
}
n, err := strconv.ParseFloat(string(seq), 64)
if err != nil {
panic(todo("", err))
}
if !discard {
arg := VaUintptr(args)
if neg {
n = -n
}
switch mod {
case modNone:
*(*float32)(unsafe.Pointer(arg)) = float32(n)
case modL:
*(*float64)(unsafe.Pointer(arg)) = n
default:
panic(todo("", mod, neg, n))
}
}
return format, 1, true
case 's':
// Matches a sequence of non-white-space characters; the next pointer must be
// a pointer to the initial element of a character array that is long enough to
// hold the input sequence and the terminating null byte ('\0'), which is added
// automatically. The input string stops at white space or at the maximum
// field width, whichever occurs first.
var c byte
var err error
var arg uintptr
if !discard {
arg = VaUintptr(args)
}
scans:
for ; width != 0; width-- {
if c, err = r.ReadByte(); err != nil {
if err != io.EOF {
nvalues = -1
}
break scans
}
switch c {
case ' ', '\t', '\n', '\r', '\v', '\f':
break scans
}
nvalues = 1
match = true
if !discard {
*(*byte)(unsafe.Pointer(arg)) = c
arg++
}
}
if match {
switch {
case width == 0:
r.UnreadByte()
fallthrough
default:
if !discard {
*(*byte)(unsafe.Pointer(arg)) = 0
}
}
}
case 'c':
// Matches a sequence of characters whose length is specified by the maximum
// field width (default 1); the next pointer must be a pointer to char, and
// there must be enough room for all the characters (no terminating null byte
// is added). The usual skip of leading white space is suppressed. To skip
// white space first, use an explicit space in the format.
format++
panic(todo(""))
case '[':
// Matches a nonempty sequence of characters from the specified set of
// accepted characters; the next pointer must be a pointer to char, and there
// must be enough room for all the char‐ acters in the string, plus a
// terminating null byte. The usual skip of leading white space is suppressed.
// The string is to be made up of characters in (or not in) a particular set;
// the set is defined by the characters between the open bracket [ character
// and a close bracket ] character. The set excludes those characters if the
// first character after the open bracket is a circumflex (^). To include a
// close bracket in the set, make it the first character after the open bracket
// or the circumflex; any other position will end the set. The hyphen
// character - is also special; when placed between two other characters, it
// adds all intervening characters to the set. To include a hyphen, make it
// the last character before the final close bracket. For instance, [^]0-9-]
// means the set "everything except close bracket, zero through nine, and
// hyphen". The string ends with the appearance of a character not in the
// (or, with a circumflex, in) set or when the field width runs out.
format++
var re0 []byte
bracket:
for i := 0; ; i++ {
c := *(*byte)(unsafe.Pointer(format))
format++
if c == ']' && i != 0 {
break bracket
}
re0 = append(re0, c)
}
set := map[byte]struct{}{}
re := string(re0)
neg := strings.HasPrefix(re, "^")
if neg {
re = re[1:]
}
for len(re) != 0 {
switch {
case len(re) >= 3 && re[1] == '-':
for c := re[0]; c <= re[2]; c++ {
set[c] = struct{}{}
}
re = re[3:]
default:
set[c] = struct{}{}
re = re[1:]
}
}
var arg uintptr
if !discard {
arg = VaUintptr(args)
}
for ; width != 0; width-- {
c, err := r.ReadByte()
if err != nil {
if err == io.EOF {
return format, nvalues, match
}
return format, -1, match
}
if _, ok := set[c]; ok == !neg {
match = true
nvalues = 1
if !discard {
*(*byte)(unsafe.Pointer(arg)) = c
arg++
}
}
}
if match {
switch {
case width == 0:
r.UnreadByte()
fallthrough
default:
if !discard {
*(*byte)(unsafe.Pointer(arg)) = 0
}
}
}
case 'p':
// Matches a pointer value (as printed by %p in printf(3); the next pointer
// must be a pointer to a pointer to void.
format++
skipReaderWhiteSpace(r)
c, err := r.ReadByte()
if err != nil {
panic(todo("", err))
}
if c == '0' {
if c, err = r.ReadByte(); err != nil {
panic(todo("", err))
}
if c != 'x' && c != 'X' {
r.UnreadByte()
}
}
var digit, n uint64
ptr:
for ; width != 0; width-- {
c, err := r.ReadByte()
if err != nil {
if match {
break ptr
}
panic(todo(""))
}
switch {
case c >= '0' && c <= '9':
digit = uint64(c) - '0'
case c >= 'a' && c <= 'f':
digit = uint64(c) - 'a' + 10
case c >= 'A' && c <= 'F':
digit = uint64(c) - 'A' + 10
default:
r.UnreadByte()
break ptr
}
match = true
n0 := n
n = n<<4 + digit
if n < n0 {
panic(todo(""))
}
}
if !match {
break
}
if !discard {
arg := VaUintptr(args)
*(*uintptr)(unsafe.Pointer(arg)) = uintptr(n)
}
nvalues = 1
case 'n':
// Nothing is expected; instead, the number of characters consumed thus far
// from the input is stored through the next pointer, which must be a pointer
// to int. This is not a conversion and does not increase the count returned
// by the function. The assignment can be suppressed with the *
// assignment-suppression character, but the effect on the return value is
// undefined. Therefore %*n conversions should not be used.
format++
panic(todo(""))
default:
panic(todo("%#U", c))
}
return format, nvalues, match
}
func skipReaderWhiteSpace(r io.ByteScanner) error {
for {
c, err := r.ReadByte()
if err != nil {
return err
}
switch c {
case ' ', '\t', '\n', '\r', '\v', '\f':
// ok
default:
r.UnreadByte()
return nil
}
}
}
func skipWhiteSpace(s uintptr) uintptr {
for {
switch c := *(*byte)(unsafe.Pointer(s)); c {
case ' ', '\t', '\n', '\r', '\v', '\f':
s++
default:
return s
}
}
}
// [-+]?([0-9]*[.])?[0-9]+([eE][-+]?\d+)?
func fpLiteral(rd io.ByteScanner) (seq []byte) {
const endOfText = 0x110000
var pos, width, length int
defer func() {
if len(seq) > length {
rd.UnreadByte()
seq = seq[:len(seq)-1]
}
}()
var r rune
step := func(pos int) (rune, int) {
b, err := rd.ReadByte()
if err != nil {
return endOfText, 0
}
seq = append(seq, b)
return rune(b), 1
}
move := func() {
pos += width
if r != endOfText {
r, width = step(pos + width)
}
}
accept := func(x rune) bool {
if r == x {
move()
return true
}
return false
}
accept2 := func(x rune) bool {
if r <= x {
move()
return true
}
return false
}
r = endOfText
width = 0
r, width = step(pos)
if accept('.') {
goto l7
}
if accept('+') {
goto l30
}
if accept('-') {
goto l30
}
if r < '0' {
goto l4out
}
if accept2('9') {
goto l35
}
l4out:
return seq
l7:
if r < '0' {
goto l7out
}
if accept2('9') {
goto l10
}
l7out:
return seq
l10:
length = pos
if accept('E') {
goto l18
}
if accept('e') {
goto l18
}
if r < '0' {
goto l15out
}
if accept2('9') {
goto l10
}
l15out:
return seq
l18:
if accept('+') {
goto l23
}
if accept('-') {
goto l23
}
if r < '0' {
goto l20out
}
if accept2('9') {
goto l26
}
l20out:
return seq
l23:
if r < '0' {
goto l23out
}
if accept2('9') {
goto l26
}
l23out:
return seq
l26:
length = pos
if r < '0' {
goto l27out
}
if accept2('9') {
goto l26
}
l27out:
return seq
l30:
if accept('.') {
goto l7
}
if r < '0' {
goto l32out
}
if accept2('9') {
goto l35
}
l32out:
return seq
l35:
length = pos
if accept('.') {
goto l7
}
if accept('E') {
goto l18
}
if accept('e') {
goto l18
}
if r < '0' {
goto l42out
}
if accept2('9') {
goto l35
}
l42out:
return seq
}
|