internal/number: fix some Pattern bugs
Change-Id: I8eb62939e8a26f1ba39c8a240e676c43eac0de8e
Reviewed-on: https://go-review.googlesource.com/45095
Run-TryBot: Marcel van Lohuizen <mpvl@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Nigel Tao <nigeltao@golang.org>
diff --git a/internal/number/pattern.go b/internal/number/pattern.go
index 018cf02..ad8f729 100644
--- a/internal/number/pattern.go
+++ b/internal/number/pattern.go
@@ -104,7 +104,8 @@
}
func (p *parser) updateGrouping() {
- if p.hasGroup && p.groupingCount < 255 {
+ if p.hasGroup &&
+ 0 < p.groupingCount && p.groupingCount < 255 {
p.GroupingSize[1] = p.GroupingSize[0]
p.GroupingSize[0] = uint8(p.groupingCount)
}
@@ -163,6 +164,7 @@
s = p.parsePad(s, PadAfterPrefix)
s = p.parse(p.number, s)
+ p.updateGrouping()
s = p.parsePad(s, PadBeforeSuffix)
s = p.parseAffix(s)
@@ -294,6 +296,8 @@
next = p.exponent
case '.':
next = p.fraction
+ case ',':
+ next = p.integer
}
p.updateGrouping()
return next
diff --git a/internal/number/pattern_test.go b/internal/number/pattern_test.go
index 810b5a8..6adeaf7 100644
--- a/internal/number/pattern_test.go
+++ b/internal/number/pattern_test.go
@@ -26,6 +26,20 @@
MinIntegerDigits: 1,
},
}, {
+ "+0",
+ &Pattern{
+ Affix: "\x01+\x00",
+ FormatWidth: 2,
+ MinIntegerDigits: 1,
+ },
+}, {
+ "0+",
+ &Pattern{
+ Affix: "\x00\x01+",
+ FormatWidth: 2,
+ MinIntegerDigits: 1,
+ },
+}, {
"0000",
&Pattern{
FormatWidth: 4,
@@ -52,6 +66,22 @@
MaxFractionDigits: 6,
},
}, {
+ "#,0",
+ &Pattern{
+ FormatWidth: 3,
+ GroupingSize: [2]uint8{1, 0},
+ MinIntegerDigits: 1,
+ },
+}, {
+ "#,0.00",
+ &Pattern{
+ FormatWidth: 6,
+ GroupingSize: [2]uint8{1, 0},
+ MinIntegerDigits: 1,
+ MinFractionDigits: 2,
+ MaxFractionDigits: 2,
+ },
+}, {
"#,##0.###",
&Pattern{
FormatWidth: 9,
@@ -176,7 +206,7 @@
MaxFractionDigits: 3,
},
}, {
- // Rounding increments
+ // Rounding increment
"1.05",
&Pattern{
RoundIncrement: 105,
@@ -186,6 +216,17 @@
MaxFractionDigits: 2,
},
}, {
+ // Rounding increment with grouping
+ "1,05",
+ &Pattern{
+ RoundIncrement: 105,
+ FormatWidth: 4,
+ GroupingSize: [2]uint8{2, 0},
+ MinIntegerDigits: 3,
+ MinFractionDigits: 0,
+ MaxFractionDigits: 0,
+ },
+}, {
"0.0%",
&Pattern{
Affix: "\x00\x01%",
@@ -282,19 +323,21 @@
func TestParsePattern(t *testing.T) {
for i, tc := range testCases {
- f, err := ParsePattern(tc.pat)
- if !reflect.DeepEqual(f, tc.want) {
- t.Errorf("%d:%s:\ngot %#v;\nwant %#v", i, tc.pat, f, tc.want)
- }
- if got, want := err != nil, tc.want == nil; got != want {
- t.Errorf("%d:%s:error: got %v; want %v", i, tc.pat, err, want)
- }
+ t.Run(tc.pat, func(t *testing.T) {
+ f, err := ParsePattern(tc.pat)
+ if !reflect.DeepEqual(f, tc.want) {
+ t.Errorf("%d:%s:\ngot %#v;\nwant %#v", i, tc.pat, f, tc.want)
+ }
+ if got, want := err != nil, tc.want == nil; got != want {
+ t.Errorf("%d:%s:error: got %v; want %v", i, tc.pat, err, want)
+ }
+ })
}
}
func TestPatternSize(t *testing.T) {
if sz := unsafe.Sizeof(Pattern{}); sz > 48 {
- t.Errorf("got %d; want 48", sz)
+ t.Errorf("got %d; want <= 48", sz)
}
}