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
|
// -*-go-*-
//
// Convert a string to an integer.
//
// To compile:
//
// ragel -Z -T0 -o atoi.go atoi.rl
// go build -o atoi atoi.go
// ./atoi
//
// To show a diagram of your state machine:
//
// ragel -V -Z -p -o atoi.dot atoi.rl
// xdot atoi.dot
//
package main
import (
"os"
"fmt"
)
%%{
machine atoi;
write data;
}%%
func atoi(data string) (val int) {
cs, p, pe := 0, 0, len(data)
neg := false
%%{
action see_neg { neg = true }
action add_digit { val = val * 10 + (int(fc) - '0') }
main :=
( '-'@see_neg | '+' )? ( digit @add_digit )+
'\n'?
;
write init;
write exec;
}%%
if neg {
val = -1 * val;
}
if cs < atoi_first_final {
fmt.Println("atoi: there was an error:", cs, "<", atoi_first_final)
fmt.Println(data)
for i := 0; i < p; i++ {
fmt.Print(" ")
}
fmt.Println("^")
}
return val
}
//////////////////////////////////////////////////////////////////////
type atoiTest struct {
s string
v int
}
var atoiTests = []atoiTest{
atoiTest{"7", 7},
atoiTest{"666", 666},
atoiTest{"-666", -666},
atoiTest{"+666", 666},
atoiTest{"1234567890", 1234567890},
atoiTest{"+1234567890\n", 1234567890},
// atoiTest{"+ 1234567890", 1234567890}, // i will fail
}
func main() {
res := 0
for _, test := range atoiTests {
res := atoi(test.s)
if res != test.v {
fmt.Fprintf(os.Stderr, "FAIL atoi(%#v) != %#v\n", test.s, test.v)
res = 1
}
}
os.Exit(res)
}
|