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
| package input
// #include <stdlib.h>
// #include "input.h"
//
// #cgo CFLAGS: -O3 -Wno-error=format-security -Wno-format-nonliteral -Wno-format-extra-args -Wno-format-security
//
import "C"
import "unsafe"
func Scanf(format string, a ...interface{}) (n int) {
var buf []byte
i, arg := 0, 0
for {
for ; i < len(format) && format[i] != '%'; i++ {
buf = append(buf, format[i])
}
if i == len(format) {
if len(buf) > 0 {
fmt := C.CString(string(buf))
n += int(C.scanverbatim(fmt))
C.free(unsafe.Pointer(fmt))
}
break
}
buf = append(buf, '%')
if i++; i == len(format) {
panic("input.Scanf: format string ends with '%'")
} else if format[i] == '%' {
buf = append(buf, '%')
} else {
spec := format[i]
if arg == len(a) {
panic("input.Scanf: the number of format specifiers exeeds the number of arguments")
}
if a[arg] == nil {
panic("input.Scanf: nil pointer passed as argument")
}
if spec == 'd' {
switch p := a[arg].(type) {
case *uint: {
buf = append(buf, ([]byte)("llu")...)
fmt := C.CString(string(buf))
var res C.ulonglong
n += int(C.scanuint(fmt, &res))
C.free(unsafe.Pointer(fmt))
*p = uint(res)
}
case *uint8: {
buf = append(buf, 'l', 'l' ,'u')
fmt := C.CString(string(buf))
var res C.ulonglong
n += int(C.scanuint(fmt, &res))
C.free(unsafe.Pointer(fmt))
*p = uint8(res)
}
case *uint16: {
buf = append(buf, 'l', 'l' ,'u')
fmt := C.CString(string(buf))
var res C.ulonglong
n += int(C.scanuint(fmt, &res))
C.free(unsafe.Pointer(fmt))
*p = uint16(res)
}
case *uint32: {
buf = append(buf, 'l', 'l' ,'u')
fmt := C.CString(string(buf))
var res C.ulonglong
n += int(C.scanuint(fmt, &res))
C.free(unsafe.Pointer(fmt))
*p = uint32(res)
}
case *uint64: {
buf = append(buf, 'l', 'l' ,'u')
fmt := C.CString(string(buf))
var res C.ulonglong
n += int(C.scanuint(fmt, &res))
C.free(unsafe.Pointer(fmt))
*p = uint64(res)
}
case *int: {
buf = append(buf, 'l', 'l' ,'d')
fmt := C.CString(string(buf))
var res C.longlong
n += int(C.scanint(fmt, &res))
C.free(unsafe.Pointer(fmt))
*p = int(res)
}
case *int8: {
buf = append(buf, 'l', 'l' ,'d')
fmt := C.CString(string(buf))
var res C.longlong
n += int(C.scanint(fmt, &res))
C.free(unsafe.Pointer(fmt))
*p = int8(res)
}
case *int16: {
buf = append(buf, 'l', 'l' ,'d')
fmt := C.CString(string(buf))
var res C.longlong
n += int(C.scanint(fmt, &res))
C.free(unsafe.Pointer(fmt))
*p = int16(res)
}
case *int32: {
buf = append(buf, 'l', 'l' ,'d')
fmt := C.CString(string(buf))
var res C.longlong
n += int(C.scanint(fmt, &res))
C.free(unsafe.Pointer(fmt))
*p = int32(res)
}
case *int64: {
buf = append(buf, 'l', 'l' ,'d')
fmt := C.CString(string(buf))
var res C.longlong
n += int(C.scanint(fmt, &res))
C.free(unsafe.Pointer(fmt))
*p = int64(res)
}
default:
panic("input.Scanf: argument must be pointer to some integer variable")
}
buf = buf[:0]
} else if spec == 'c' {
buf = append(buf, 'l', 'c')
fmt := C.CString(string(buf))
var res C.wchar_t
n += int(C.scanchar(fmt, &res))
C.free(unsafe.Pointer(fmt))
switch p := a[arg].(type) {
case *uint: *p = uint(res)
case *uint8: *p = uint8(res)
case *uint16: *p = uint16(res)
case *uint32: *p = uint32(res)
case *uint64: *p = uint64(res)
case *int: *p = int(res)
case *int8: *p = int8(res)
case *int16: *p = int16(res)
case *int32: *p = int32(res)
case *int64: *p = int64(res)
default:
panic("input.Scanf: argument must be pointer to some integer variable")
}
buf = buf[:0]
} else if spec == 'f' {
buf = append(buf, 'l', 'f')
fmt := C.CString(string(buf))
var res C.double
n += int(C.scandouble(fmt, &res))
C.free(unsafe.Pointer(fmt))
switch p := a[arg].(type) {
case *float32: *p = float32(res)
case *float64: *p = float64(res)
default:
panic("input.Scanf: argument must be pointer to some floating-point variable")
}
buf = buf[:0]
} else if spec == 's' {
buf = append(buf, 'm', 's')
fmt := C.CString(string(buf))
buf = buf[:0]
var res *C.char
count := int(C.scanstring(fmt, &res))
C.free(unsafe.Pointer(fmt))
if count == 1 {
n++
if p, ok := a[arg].(*string); ok {
*p = C.GoString(res)
C.free(unsafe.Pointer(res))
} else {
panic("input.Scanf: argument must be pointer to string variable")
}
}
} else {
panic("input.Scanf: only '%d', '%c' and 's' format specifiers allowed")
}
arg++
}
i++
}
return
}
func Gets() (s string) {
cs := C.getstring()
if cs == nil {
panic("input.Gets: I/O error or out of memory")
}
s = C.GoString(cs)
C.free(unsafe.Pointer(cs))
return
} |