forked from gotnospirit/messageformat
-
Notifications
You must be signed in to change notification settings - Fork 2
/
plural.go
204 lines (164 loc) · 4.28 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
198
199
200
201
202
203
204
package messageformat
import (
"bytes"
"fmt"
"strconv"
)
type PluralExpr struct {
Select SelectExpr `json:"select"`
Offset int `json:"offset"`
}
func (p *parser) parsePlural(varname string, char rune, start, end int, ptr_input *[]rune) (Expression, int, error) {
if char != PartChar {
return nil, start, fmt.Errorf("MalformedOption")
}
hasOtherChoice := false
result := &PluralExpr{
Select: SelectExpr{
Key: varname,
Choices: make(map[string]*ParseTree),
},
}
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 || k == "" {
return nil, j, fmt.Errorf("MissingChoiceName")
}
key, char, i = k, c, j
}
choice, c, i, err := p.readChoice(char, i, end, ptr_input)
if err != nil {
return nil, i, err
}
if key == "other" {
hasOtherChoice = true
}
result.Select.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 (f *formatter) formatPlural(expr Expression, ptr_output *bytes.Buffer, data map[string]any) error {
o, ok := expr.(*PluralExpr)
if !ok {
return fmt.Errorf("InvalidExprType: want PluralExpr, got %T", expr)
}
key := o.Select.Key
offset := o.Offset
value, err := toString(data, key)
if err != nil {
return err
}
var choice *ParseTree
if v, ok := data[key]; ok {
switch val := v.(type) {
case int:
key = fmt.Sprintf("=%d", val)
case float64:
key = "=" + strconv.FormatFloat(val, 'f', -1, 64)
case string:
key = "=" + val
default:
return fmt.Errorf("UnsupportedPluralKeyType: %T", v)
}
if choice = o.Select.Choices[key]; nil == choice {
switch val := v.(type) {
case int:
if offset != 0 {
offset_value := val - offset
value = fmt.Sprintf("%d", offset_value)
key, err = f.getNamedKey(offset_value, false)
} else {
key, err = f.getNamedKey(v.(int), false)
}
case float64:
if offset != 0 {
offset_value := val - float64(offset)
value = strconv.FormatFloat(offset_value, 'f', -1, 64)
key, err = f.getNamedKey(offset_value, false)
} else {
key, err = f.getNamedKey(v.(float64), 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 = f.getNamedKey(offset_value, false)
} else {
key, err = f.getNamedKey(value, false)
}
}
if err != nil {
return err
}
choice = o.Select.Choices[key]
}
}
if choice == nil {
choice = o.Select.Choices["other"]
}
return f.format(choice, ptr_output, data, 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 nil != err {
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")
}