-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber.go
More file actions
388 lines (353 loc) · 9.38 KB
/
number.go
File metadata and controls
388 lines (353 loc) · 9.38 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package goxslt
import (
"fmt"
"math"
"strings"
"unicode"
"github.com/speedata/goxml"
"github.com/speedata/goxpath"
)
// --------------------------------------------------------------------------
// Counting algorithms for xsl:number
// --------------------------------------------------------------------------
// numberValue evaluates the value expression and returns a single number.
func numberValue(tc *TransformContext, expr string) ([]int, error) {
result, err := tc.evalXPath(expr)
if err != nil {
return nil, fmt.Errorf("xsl:number value='%s': %w", expr, err)
}
f, err := goxpath.NumberValue(result)
if err != nil {
return nil, fmt.Errorf("xsl:number value='%s': %w", expr, err)
}
n := int(math.Round(f))
if n < 1 {
n = 0
}
return []int{n}, nil
}
// defaultCountPattern creates a pattern matching nodes with the same name and
// type as the given node.
func defaultCountPattern(node goxml.XMLNode) Pattern {
switch n := node.(type) {
case *goxml.Element:
return &NameTest{LocalName: n.Name, Kind: NodeElement}
case goxml.CharData:
return &NodeKindTest{Kind: NodeText}
case goxml.Comment:
return &NodeKindTest{Kind: NodeComment}
case goxml.ProcInst:
return &NodeKindTest{Kind: NodeProcessingInstruction}
default:
return &AnyNodeTest{}
}
}
// numberSingle implements level="single": find the nearest ancestor-or-self
// matching count, then count preceding siblings matching count.
func numberSingle(node goxml.XMLNode, count, from Pattern, mc *MatchContext) []int {
target := findAncestorOrSelf(node, count, from, mc)
if target == nil {
return nil
}
return []int{countPrecedingSiblings(target, count, mc) + 1}
}
// numberMultiple implements level="multiple": collect all ancestors-or-self
// matching count (stopping at from), then count preceding siblings for each.
func numberMultiple(node goxml.XMLNode, count, from Pattern, mc *MatchContext) []int {
var ancestors []goxml.XMLNode
cur := node
for cur != nil {
if from != nil && from.Matches(cur, mc) {
break
}
if count.Matches(cur, mc) {
ancestors = append(ancestors, cur)
}
cur = elementParent(cur)
}
if len(ancestors) == 0 {
return nil
}
// Reverse to get outermost first.
for i, j := 0, len(ancestors)-1; i < j; i, j = i+1, j-1 {
ancestors[i], ancestors[j] = ancestors[j], ancestors[i]
}
numbers := make([]int, len(ancestors))
for i, anc := range ancestors {
numbers[i] = countPrecedingSiblings(anc, count, mc) + 1
}
return numbers
}
// numberAny implements level="any": count all nodes matching count that
// precede the current node in document order (stopping at from).
func numberAny(node goxml.XMLNode, count, from Pattern, mc *MatchContext) []int {
n := countPrecedingAny(node, count, from, mc)
if n == 0 {
return nil
}
return []int{n}
}
// findAncestorOrSelf walks ancestor-or-self to find the first node matching
// count. Stops if from is matched (ancestor above the match is ignored).
func findAncestorOrSelf(node goxml.XMLNode, count, from Pattern, mc *MatchContext) goxml.XMLNode {
cur := node
for cur != nil {
if count.Matches(cur, mc) {
return cur
}
if from != nil && from.Matches(cur, mc) {
return nil
}
cur = elementParent(cur)
}
return nil
}
// countPrecedingSiblings counts how many preceding siblings match count.
func countPrecedingSiblings(node goxml.XMLNode, count Pattern, mc *MatchContext) int {
parent := elementParent(node)
if parent == nil {
return 0
}
siblings := parent.Children()
n := 0
for _, sib := range siblings {
if nodeID(sib) == nodeID(node) {
break
}
if count.Matches(sib, mc) {
n++
}
}
return n
}
// countPrecedingAny counts all nodes matching count that precede node in
// document order. Stops at from (if set).
func countPrecedingAny(node goxml.XMLNode, count, from Pattern, mc *MatchContext) int {
// Collect all nodes in document order up to and including node.
var all []goxml.XMLNode
targetID := nodeID(node)
found := false
collectDocOrder(documentRoot(node), &all, targetID, &found)
// Count backwards from the node, stopping at from.
n := 0
// Include the target node itself if it matches.
for i := len(all) - 1; i >= 0; i-- {
nd := all[i]
if from != nil && from.Matches(nd, mc) && nodeID(nd) != targetID {
break
}
if count.Matches(nd, mc) {
n++
}
}
return n
}
// collectDocOrder collects nodes in document order, stopping after targetID.
func collectDocOrder(node goxml.XMLNode, result *[]goxml.XMLNode, targetID int, found *bool) {
if *found {
return
}
*result = append(*result, node)
if nodeID(node) == targetID {
*found = true
return
}
for _, child := range node.Children() {
collectDocOrder(child, result, targetID, found)
if *found {
return
}
}
}
// elementParent returns the parent of a node (only works for *Element).
func elementParent(node goxml.XMLNode) goxml.XMLNode {
if elt, ok := node.(*goxml.Element); ok {
return elt.Parent
}
return nil
}
// documentRoot walks up to the root document node.
func documentRoot(node goxml.XMLNode) goxml.XMLNode {
for {
switch n := node.(type) {
case *goxml.XMLDocument:
return n
case *goxml.Element:
if n.Parent != nil {
node = n.Parent
continue
}
return n
case *goxml.Attribute:
if n.Parent != nil {
node = n.Parent
continue
}
return n
default:
return node
}
}
}
// nodeID returns the ID of a node for identity comparison.
func nodeID(node goxml.XMLNode) int {
switch n := node.(type) {
case *goxml.XMLDocument:
return n.ID
case *goxml.Element:
return n.ID
case goxml.CharData:
return n.ID
case goxml.Comment:
return n.ID
case goxml.ProcInst:
return n.ID
default:
return -1
}
}
// --------------------------------------------------------------------------
// Format tokenizer and number formatting
// --------------------------------------------------------------------------
// tokenizeFormat splits a format string into alternating punctuation and
// format tokens. Alphanumeric runs are tokens, everything else is punctuation.
// Returns punctuation (len = tokens+1) and tokens.
func tokenizeFormat(format string) (punctuation []string, tokens []string) {
runes := []rune(format)
i := 0
var punc strings.Builder
// Leading punctuation.
for i < len(runes) && !isFormatToken(runes[i]) {
punc.WriteRune(runes[i])
i++
}
punctuation = append(punctuation, punc.String())
for i < len(runes) {
// Token.
var tok strings.Builder
for i < len(runes) && isFormatToken(runes[i]) {
tok.WriteRune(runes[i])
i++
}
tokens = append(tokens, tok.String())
// Separator.
punc.Reset()
for i < len(runes) && !isFormatToken(runes[i]) {
punc.WriteRune(runes[i])
i++
}
punctuation = append(punctuation, punc.String())
}
return
}
// isFormatToken returns true for characters that form format tokens.
func isFormatToken(r rune) bool {
return unicode.IsLetter(r) || unicode.IsDigit(r)
}
// formatNumber formats a single integer according to a format token.
func formatNumber(n int, token string) string {
if n < 1 {
return "0"
}
switch {
case token == "1":
return fmt.Sprintf("%d", n)
case len(token) > 1 && allDigits(token):
// Zero-padded: "01" means width 2, "001" means width 3.
return fmt.Sprintf("%0*d", len(token), n)
case token == "a":
return alphaNumber(n, 'a')
case token == "A":
return alphaNumber(n, 'A')
case token == "i":
return toRomanLower(n)
case token == "I":
return toRomanUpper(n)
default:
return fmt.Sprintf("%d", n)
}
}
func allDigits(s string) bool {
for _, r := range s {
if !unicode.IsDigit(r) {
return false
}
}
return len(s) > 0
}
// alphaNumber converts 1→a, 2→b, ..., 26→z, 27→aa, etc.
func alphaNumber(n int, base rune) string {
if n < 1 {
return string(base)
}
var result []rune
for n > 0 {
n--
result = append([]rune{rune(int(base) + n%26)}, result...)
n /= 26
}
return string(result)
}
// toRomanUpper converts n to uppercase Roman numerals.
func toRomanUpper(n int) string {
if n <= 0 || n >= 4000 {
return fmt.Sprintf("%d", n)
}
values := []int{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}
symbols := []string{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}
var sb strings.Builder
for i, v := range values {
for n >= v {
sb.WriteString(symbols[i])
n -= v
}
}
return sb.String()
}
// toRomanLower converts n to lowercase Roman numerals.
func toRomanLower(n int) string {
return strings.ToLower(toRomanUpper(n))
}
// formatNumbers assembles numbers with a format string.
func formatNumbers(numbers []int, format string) string {
if len(numbers) == 0 {
return ""
}
punctuation, tokens := tokenizeFormat(format)
// If no tokens found, use "1" as default.
if len(tokens) == 0 {
tokens = []string{"1"}
punctuation = []string{"", ""}
}
var sb strings.Builder
// Leading punctuation.
sb.WriteString(punctuation[0])
for i, n := range numbers {
if i > 0 {
// Separator between numbers.
sepIdx := i
if sepIdx >= len(punctuation) {
sepIdx = len(punctuation) - 2 // reuse last separator
}
if sepIdx < 1 {
sepIdx = 1
}
if sepIdx < len(punctuation) {
sb.WriteString(punctuation[sepIdx])
} else {
sb.WriteString(".")
}
}
// Pick token: use last available if index exceeds tokens.
tokIdx := i
if tokIdx >= len(tokens) {
tokIdx = len(tokens) - 1
}
sb.WriteString(formatNumber(n, tokens[tokIdx]))
}
// Trailing punctuation.
if len(punctuation) > 0 {
sb.WriteString(punctuation[len(punctuation)-1])
}
return sb.String()
}