...
1
2
3
4
5 package number
6
7 import (
8 "fmt"
9
10 "golang.org/x/text/internal/number"
11 "golang.org/x/text/language"
12 )
13
14
15 type Option option
16
17 type option func(tag language.Tag, f *number.Formatter)
18
19
20
21
22
23 func NoSeparator() Option {
24 return func(t language.Tag, f *number.Formatter) {
25 f.GroupingSize = [2]uint8{}
26 }
27 }
28
29
30
31 func MaxIntegerDigits(max int) Option {
32 return func(t language.Tag, f *number.Formatter) {
33 if max >= 1<<8 {
34 max = (1 << 8) - 1
35 }
36 f.MaxIntegerDigits = uint8(max)
37 }
38 }
39
40
41
42 func MinIntegerDigits(min int) Option {
43 return func(t language.Tag, f *number.Formatter) {
44 if min >= 1<<8 {
45 min = (1 << 8) - 1
46 }
47 f.MinIntegerDigits = uint8(min)
48 }
49 }
50
51
52 func MaxFractionDigits(max int) Option {
53 return func(t language.Tag, f *number.Formatter) {
54 if max >= 1<<15 {
55 max = (1 << 15) - 1
56 }
57 f.MaxFractionDigits = int16(max)
58 }
59 }
60
61
62 func MinFractionDigits(min int) Option {
63 return func(t language.Tag, f *number.Formatter) {
64 if min >= 1<<8 {
65 min = (1 << 8) - 1
66 }
67 f.MinFractionDigits = uint8(min)
68 }
69 }
70
71
72
73 func Precision(prec int) Option {
74 return func(t language.Tag, f *number.Formatter) {
75 f.SetPrecision(prec)
76 }
77 }
78
79
80
81 func Scale(decimals int) Option {
82 return func(t language.Tag, f *number.Formatter) {
83 f.SetScale(decimals)
84 }
85 }
86
87
88
89
90 func IncrementString(decimal string) Option {
91 increment := 0
92 scale := 0
93 d := decimal
94 p := 0
95 for ; p < len(d) && '0' <= d[p] && d[p] <= '9'; p++ {
96 increment *= 10
97 increment += int(d[p]) - '0'
98 }
99 if p < len(d) && d[p] == '.' {
100 for p++; p < len(d) && '0' <= d[p] && d[p] <= '9'; p++ {
101 increment *= 10
102 increment += int(d[p]) - '0'
103 scale++
104 }
105 }
106 if p < len(d) {
107 increment = 0
108 scale = 0
109 }
110 return func(t language.Tag, f *number.Formatter) {
111 f.Increment = uint32(increment)
112 f.IncrementScale = uint8(scale)
113 f.SetScale(scale)
114 }
115 }
116
117 func noop(language.Tag, *number.Formatter) {}
118
119
120
121
122
123
124 func PatternOverrides(patterns map[string]string) Option {
125
126
127 m := map[language.Tag]*number.Pattern{}
128 for k, v := range patterns {
129 tag := language.MustParse(k)
130 p, err := number.ParsePattern(v)
131 if err != nil {
132 panic(fmt.Errorf("number: PatternOverrides: %v", err))
133 }
134 m[tag] = p
135 }
136 return func(t language.Tag, f *number.Formatter) {
137
138
139 for lang := t; ; lang = t.Parent() {
140 if p, ok := m[lang]; ok {
141 f.Pattern = *p
142 break
143 }
144 if lang == language.Und {
145 break
146 }
147 }
148 }
149 }
150
151
152 func FormatWidth(n int) Option {
153 if n <= 0 {
154 return noop
155 }
156 return func(t language.Tag, f *number.Formatter) {
157 f.FormatWidth = uint16(n)
158 if f.PadRune == 0 {
159 f.PadRune = ' '
160 }
161 }
162 }
163
164
165 func Pad(r rune) Option {
166 return func(t language.Tag, f *number.Formatter) {
167 f.PadRune = r
168 }
169 }
170
171
172
173
174
175
176
177
178
View as plain text