-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathplural.go
197 lines (160 loc) · 4.19 KB
/
plural.go
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
package messageformat
import (
"bytes"
"fmt"
"strconv"
)
type pluralExpr struct {
selectExpr
offset int
}
func parsePlural(varname string, ptr_compiler *Parser, char rune, start, end int, ptr_input *[]rune) (Expression, int, error) {
if char != PartChar {
return nil, start, fmt.Errorf("MalformedOption")
}
hasOtherChoice := false
result := new(pluralExpr)
result.key = varname
result.choices = make(map[string]*node)
pos := start + 1
for pos < end {
key, char, i, err := readKey(pos, end, ptr_input)
if err != nil {
return nil, i, err
}
if char == ':' {
if key != "offset" {
return nil, i, fmt.Errorf("UnsupportedExtension: `%s`", key)
}
offset, c, j, err := readOffset(i+1, end, ptr_input)
if err != nil {
return nil, j, err
}
result.offset = offset
if isWhitespace(c) {
j++
}
k, c, j, err := readKey(j, end, ptr_input)
if err != nil {
return nil, j, err
} else if k == "" {
return nil, j, fmt.Errorf("MissingChoiceName")
}
key, char, i = k, c, j
}
if key == "other" {
hasOtherChoice = true
}
choice, c, i, err := readChoice(ptr_compiler, char, i, end, ptr_input)
if err != nil {
return nil, i, err
}
result.choices[key] = choice
pos, char = i, c
if char == CloseChar {
break
}
}
if !hasOtherChoice {
return nil, pos, fmt.Errorf("MissingMandatoryChoice")
}
return result, pos, nil
}
// formatPlural is the format function associated with the "plural" type.
//
// It will returns an error if :
// - the associated value can't be convert to string or to an int (i.e. bool, ...)
// - the pluralFunc is not defined (MessageFormat.getNamedKey)
//
// It will falls back to the "other" choice if :
// - its key can't be found in the given map
// - the computed named key (MessageFormat.getNamedKey) is not a key of the given map
func formatPlural(expr Expression, ptr_output *bytes.Buffer, data *map[string]interface{}, ptr_mf *MessageFormat, _ string) error {
o := expr.(*pluralExpr)
key := o.key
offset := o.offset
value, err := toString(*data, key)
if err != nil {
return err
}
var choice *node
if v, ok := (*data)[key]; ok {
switch t := v.(type) {
default:
return fmt.Errorf("Plural: Unsupported type for named key: %T", v)
case int:
key = fmt.Sprintf("=%d", t)
case float64:
key = "=" + strconv.FormatFloat(t, 'f', -1, 64)
case string:
key = "=" + t
}
if choice = o.choices[key]; choice == nil {
switch t := v.(type) {
case int:
if offset != 0 {
offset_value := t - offset
value = fmt.Sprintf("%d", offset_value)
key, err = ptr_mf.getNamedKey(offset_value, false)
} else {
key, err = ptr_mf.getNamedKey(t, false)
}
case float64:
if offset != 0 {
offset_value := t - float64(offset)
value = strconv.FormatFloat(offset_value, 'f', -1, 64)
key, err = ptr_mf.getNamedKey(offset_value, false)
} else {
key, err = ptr_mf.getNamedKey(t, false)
}
case string:
if offset != 0 {
offset_value, fError := strconv.ParseFloat(value, 64)
if fError != nil {
return fError
}
offset_value -= float64(offset)
value = strconv.FormatFloat(offset_value, 'f', -1, 64)
key, err = ptr_mf.getNamedKey(offset_value, false)
} else {
key, err = ptr_mf.getNamedKey(value, false)
}
}
if err != nil {
return err
}
choice = o.choices[key]
}
}
if choice == nil {
choice = o.choices["other"]
}
return choice.format(ptr_output, data, ptr_mf, value)
}
func readOffset(start, end int, ptr_input *[]rune) (int, rune, int, error) {
var buf bytes.Buffer
char, pos := whitespace(start, end, ptr_input)
input := *ptr_input
for pos < end {
switch char {
default:
buf.WriteRune(char)
pos++
if pos < end {
char = input[pos]
}
case ' ', '\r', '\n', '\t', OpenChar, CloseChar:
if buf.Len() != 0 {
result, err := strconv.Atoi(buf.String())
if err != nil {
return 0, char, pos, fmt.Errorf("BadCast")
} else if result < 0 {
return 0, char, pos, fmt.Errorf("InvalidOffsetValue")
}
return result, char, pos, nil
}
return 0, char, pos, fmt.Errorf("MissingOffsetValue")
}
}
return 0, char, pos, fmt.Errorf("UnbalancedBraces")
}