...
1
2
3
4
5
6
7
8 package number
9
10 import (
11 "unicode/utf8"
12
13 "golang.org/x/text/internal/language/compact"
14 "golang.org/x/text/language"
15 )
16
17
18 type Info struct {
19 system systemData
20 symIndex symOffset
21 }
22
23
24
25
26 func InfoFromLangID(compactIndex compact.ID, numberSystem string) Info {
27 p := langToDefaults[compactIndex]
28
29 pSymIndex := symOffset(0)
30 system, ok := systemMap[numberSystem]
31 if !ok {
32
33
34 if p&hasNonLatnMask == 0 {
35 pSymIndex = p
36 } else {
37
38 data := langToAlt[p&^hasNonLatnMask]
39 pSymIndex = data.symIndex
40 system = data.system
41 }
42 } else {
43 langIndex := compactIndex
44 ns := system
45 outerLoop:
46 for ; ; p = langToDefaults[langIndex] {
47 if p&hasNonLatnMask == 0 {
48 if ns == 0 {
49
50 pSymIndex = p
51 break
52 }
53
54 langIndex = langIndex.Parent()
55 } else {
56
57 for _, e := range langToAlt[p&^hasNonLatnMask:] {
58 if e.compactTag != langIndex {
59 if langIndex == 0 {
60
61
62
63
64
65 if ns == 0 {
66 break
67 }
68
69
70
71 ns = numLatn
72 langIndex = compactIndex
73 continue outerLoop
74 }
75
76 langIndex = langIndex.Parent()
77 } else if e.system == ns {
78 pSymIndex = e.symIndex
79 break outerLoop
80 }
81 }
82 }
83 }
84 }
85 if int(system) >= len(numSysData) {
86
87
88 d := numSysData[0]
89 d.id = system
90 return Info{
91 system: d,
92 symIndex: pSymIndex,
93 }
94 }
95 return Info{
96 system: numSysData[system],
97 symIndex: pSymIndex,
98 }
99 }
100
101
102 func InfoFromTag(t language.Tag) Info {
103 return InfoFromLangID(tagToID(t), t.TypeForKey("nu"))
104 }
105
106
107
108 func (n Info) IsDecimal() bool {
109 return int(n.system.id) < len(numSysData)
110 }
111
112
113
114
115 func (n Info) WriteDigit(dst []byte, asciiDigit rune) int {
116 copy(dst, n.system.zero[:n.system.digitSize])
117 dst[n.system.digitSize-1] += byte(asciiDigit - '0')
118 return int(n.system.digitSize)
119 }
120
121
122
123
124 func (n Info) AppendDigit(dst []byte, digit byte) []byte {
125 dst = append(dst, n.system.zero[:n.system.digitSize]...)
126 dst[len(dst)-1] += digit
127 return dst
128 }
129
130
131
132
133 func (n Info) Digit(asciiDigit rune) rune {
134 var x [utf8.UTFMax]byte
135 n.WriteDigit(x[:], asciiDigit)
136 r, _ := utf8.DecodeRune(x[:])
137 return r
138 }
139
140
141 func (n Info) Symbol(t SymbolType) string {
142 return symData.Elem(int(symIndex[n.symIndex][t]))
143 }
144
145 func formatForLang(t language.Tag, index []byte) *Pattern {
146 return &formats[index[tagToID(t)]]
147 }
148
149 func tagToID(t language.Tag) compact.ID {
150 id, _ := compact.RegionalID(compact.Tag(t))
151 return id
152 }
153
View as plain text