summaryrefslogtreecommitdiff
path: root/libgo/go/exp/locale/collate/export_test.go
blob: 3782144d83164c650dce8414dc51cec98ab15988 (plain)
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
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package collate

// Export for testing.

import (
	"fmt"
)

type Weights struct {
	Primary, Secondary, Tertiary, Quaternary int
}

func W(ce ...int) Weights {
	w := Weights{ce[0], defaultSecondary, defaultTertiary, 0}
	if len(ce) > 1 {
		w.Secondary = ce[1]
	}
	if len(ce) > 2 {
		w.Tertiary = ce[2]
	}
	if len(ce) > 3 {
		w.Quaternary = ce[3]
	} else if w.Tertiary != 0 {
		w.Quaternary = MaxQuaternary
	}
	return w
}
func (w Weights) String() string {
	return fmt.Sprintf("[%X.%X.%X.%X]", w.Primary, w.Secondary, w.Tertiary, w.Quaternary)
}

type Table struct {
	t Weigher
}

func GetTable(c *Collator) *Table {
	return &Table{c.t}
}

func convertToWeights(ws []Elem) []Weights {
	out := make([]Weights, len(ws))
	for i, w := range ws {
		out[i] = Weights{int(w.Primary()), int(w.Secondary()), int(w.Tertiary()), int(w.Quaternary())}
	}
	return out
}

func convertFromWeights(ws []Weights) []Elem {
	out := make([]Elem, len(ws))
	for i, w := range ws {
		out[i] = makeCE([]int{w.Primary, w.Secondary, w.Tertiary, 0})
		if out[i] == ceIgnore && w.Quaternary > 0 {
			out[i] = MakeQuaternary(w.Quaternary)
		}
	}
	return out
}

func (t *Table) AppendNext(s []byte) ([]Weights, int) {
	w, n := t.t.AppendNext(nil, s)
	return convertToWeights(w), n
}

func SetTop(c *Collator, top int) {
	if c.t == nil {
		c.t = &table{}
	}
	c.variableTop = uint32(top)
}

func GetColElems(c *Collator, str []byte) []Weights {
	ce := c.getColElems(str)
	return convertToWeights(ce)
}

func ProcessWeights(h AlternateHandling, top int, w []Weights) []Weights {
	in := convertFromWeights(w)
	processWeights(h, uint32(top), in)
	return convertToWeights(in)
}

func KeyFromElems(c *Collator, buf *Buffer, w []Weights) []byte {
	k := len(buf.key)
	c.keyFromElems(buf, convertFromWeights(w))
	return buf.key[k:]
}