summaryrefslogtreecommitdiffhomepage
path: root/vendor/golang.org/x/text/internal/language/compose.go
blob: 4ae78e0fa5fa984eb4141efec035c1b5ce264213 (plain) (blame)
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
// Copyright 2018 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 language

import (
	"sort"
	"strings"
)

// A Builder allows constructing a Tag from individual components.
// Its main user is Compose in the top-level language package.
type Builder struct {
	Tag Tag

	private    string // the x extension
	variants   []string
	extensions []string
}

// Make returns a new Tag from the current settings.
func (b *Builder) Make() Tag {
	t := b.Tag

	if len(b.extensions) > 0 || len(b.variants) > 0 {
		sort.Sort(sortVariants(b.variants))
		sort.Strings(b.extensions)

		if b.private != "" {
			b.extensions = append(b.extensions, b.private)
		}
		n := maxCoreSize + tokenLen(b.variants...) + tokenLen(b.extensions...)
		buf := make([]byte, n)
		p := t.genCoreBytes(buf)
		t.pVariant = byte(p)
		p += appendTokens(buf[p:], b.variants...)
		t.pExt = uint16(p)
		p += appendTokens(buf[p:], b.extensions...)
		t.str = string(buf[:p])
		// We may not always need to remake the string, but when or when not
		// to do so is rather tricky.
		scan := makeScanner(buf[:p])
		t, _ = parse(&scan, "")
		return t

	} else if b.private != "" {
		t.str = b.private
		t.RemakeString()
	}
	return t
}

// SetTag copies all the settings from a given Tag. Any previously set values
// are discarded.
func (b *Builder) SetTag(t Tag) {
	b.Tag.LangID = t.LangID
	b.Tag.RegionID = t.RegionID
	b.Tag.ScriptID = t.ScriptID
	// TODO: optimize
	b.variants = b.variants[:0]
	if variants := t.Variants(); variants != "" {
		for _, vr := range strings.Split(variants[1:], "-") {
			b.variants = append(b.variants, vr)
		}
	}
	b.extensions, b.private = b.extensions[:0], ""
	for _, e := range t.Extensions() {
		b.AddExt(e)
	}
}

// AddExt adds extension e to the tag. e must be a valid extension as returned
// by Tag.Extension. If the extension already exists, it will be discarded,
// except for a -u extension, where non-existing key-type pairs will added.
func (b *Builder) AddExt(e string) {
	if e[0] == 'x' {
		if b.private == "" {
			b.private = e
		}
		return
	}
	for i, s := range b.extensions {
		if s[0] == e[0] {
			if e[0] == 'u' {
				b.extensions[i] += e[1:]
			}
			return
		}
	}
	b.extensions = append(b.extensions, e)
}

// SetExt sets the extension e to the tag. e must be a valid extension as
// returned by Tag.Extension. If the extension already exists, it will be
// overwritten, except for a -u extension, where the individual key-type pairs
// will be set.
func (b *Builder) SetExt(e string) {
	if e[0] == 'x' {
		b.private = e
		return
	}
	for i, s := range b.extensions {
		if s[0] == e[0] {
			if e[0] == 'u' {
				b.extensions[i] = e + s[1:]
			} else {
				b.extensions[i] = e
			}
			return
		}
	}
	b.extensions = append(b.extensions, e)
}

// AddVariant adds any number of variants.
func (b *Builder) AddVariant(v ...string) {
	for _, v := range v {
		if v != "" {
			b.variants = append(b.variants, v)
		}
	}
}

// ClearVariants removes any variants previously added, including those
// copied from a Tag in SetTag.
func (b *Builder) ClearVariants() {
	b.variants = b.variants[:0]
}

// ClearExtensions removes any extensions previously added, including those
// copied from a Tag in SetTag.
func (b *Builder) ClearExtensions() {
	b.private = ""
	b.extensions = b.extensions[:0]
}

func tokenLen(token ...string) (n int) {
	for _, t := range token {
		n += len(t) + 1
	}
	return
}

func appendTokens(b []byte, token ...string) int {
	p := 0
	for _, t := range token {
		b[p] = '-'
		copy(b[p+1:], t)
		p += 1 + len(t)
	}
	return p
}

type sortVariants []string

func (s sortVariants) Len() int {
	return len(s)
}

func (s sortVariants) Swap(i, j int) {
	s[j], s[i] = s[i], s[j]
}

func (s sortVariants) Less(i, j int) bool {
	return variantIndex[s[i]] < variantIndex[s[j]]
}