~mwhudson/goyaml/fix-tests-gccgo

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
package goyaml

import (
	"strconv"
	"strings"
	"math"
)


// TODO: merge, timestamps, base 60 floats, omap.


type resolveMapItem struct {
	value interface{}
	tag   string
}

var resolveTable = make([]byte, 256)
var resolveMap = make(map[string]resolveMapItem)


func init() {
	t := resolveTable
	t[int('+')] = 'S' // Sign
	t[int('-')] = 'S'
	for _, c := range "0123456789" {
		t[int(c)] = 'D' // Digit
	}
	for _, c := range "yYnNtTfFoO~" {
		t[int(c)] = 'M' // In map
	}
	t[int('.')] = '.' // Float (potentially in map)
	t[int('<')] = '<' // Merge

	var resolveMapList = []struct {
		v   interface{}
		tag string
		l   []string
	}{
		{true, "!!bool", []string{"y", "Y", "yes", "Yes", "YES"}},
		{true, "!!bool", []string{"true", "True", "TRUE"}},
		{true, "!!bool", []string{"on", "On", "ON"}},
		{false, "!!bool", []string{"n", "N", "no", "No", "NO"}},
		{false, "!!bool", []string{"false", "False", "FALSE"}},
		{false, "!!bool", []string{"off", "Off", "OFF"}},
		{nil, "!!null", []string{"~", "null", "Null", "NULL"}},
		{math.NaN(), "!!float", []string{".nan", ".NaN", ".NAN"}},
		{math.Inf(+1), "!!float", []string{".inf", ".Inf", ".INF"}},
		{math.Inf(+1), "!!float", []string{"+.inf", "+.Inf", "+.INF"}},
		{math.Inf(-1), "!!float", []string{"-.inf", "-.Inf", "-.INF"}},
	}

	m := resolveMap
	for _, item := range resolveMapList {
		for _, s := range item.l {
			m[s] = resolveMapItem{item.v, item.tag}
		}
	}
}

const longTagPrefix = "tag:yaml.org,2002:"

func shortTag(tag string) string {
	if strings.HasPrefix(tag, longTagPrefix) {
		return "!!" + tag[len(longTagPrefix):]
	}
	return tag
}

func resolvableTag(tag string) bool {
	switch tag {
	case "", "!!str", "!!bool", "!!int", "!!float", "!!null":
		return true
	}
	return false
}

func resolve(tag string, in string) (rtag string, out interface{}) {
	tag = shortTag(tag)
	if !resolvableTag(tag) {
		return tag, in
	}

	defer func() {
		if tag != "" && tag != rtag {
			panic("Can't decode " + rtag + " '" + in + "' as a " + tag)
		}
	}()

	if in == "" {
		return "!!null", nil
	}

	c := resolveTable[in[0]]
	if c == 0 {
		// It's a string for sure. Nothing to do.
		return "!!str", in
	}

	// Handle things we can lookup in a map.
	if item, ok := resolveMap[in]; ok {
		return item.tag, item.value
	}

	switch c {
	case 'M':
		// We've already checked the map above.

	case '.':
		// Not in the map, so maybe a normal float.
		floatv, err := strconv.Atof64(in)
		if err == nil {
			return "!!float", floatv
		}
		// XXX Handle base 60 floats here (WTF!)

	case 'D', 'S':
		// Int, float, or timestamp.
		for i := 0; i != len(in); i++ {
			if in[i] == '_' {
				in = strings.Replace(in, "_", "", -1)
				break
			}
		}
		intv, err := strconv.Btoi64(in, 0)
		if err == nil {
			if intv == int64(int(intv)) {
				return "!!int", int(intv)
			} else {
				return "!!int", intv
			}
		}
		floatv, err := strconv.Atof64(in)
		if err == nil {
			return "!!float", floatv
		}
		if strings.HasPrefix(in, "0b") {
			intv, err := strconv.Btoi64(in[2:], 2)
			if err == nil {
				return "!!int", int(intv)
			}
		} else if strings.HasPrefix(in, "-0b") {
			intv, err := strconv.Btoi64(in[3:], 2)
			if err == nil {
				return "!!int", -int(intv)
			}
		}
		// XXX Handle timestamps here.

	case '<':
		// XXX Handle merge (<<) here.

	default:
		panic("resolveTable item not yet handled: " +
			string([]byte{c}) + " (with " + in + ")")
	}
	return "!!str", in
}