| 1 | // Copyright 2018 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package languagescip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/ |
| 6 | |
| 7 | import ( |
| 8 | "sortscip-go gomod github.com/golang/go/src go1.25.0 sort/" |
| 9 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 10 | ) |
| 11 | |
| 12 | // A Builder allows constructing a Tag from individual components. |
| 13 | // Its main user is Compose in the top-level language package. |
| 14 | type Builderscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder# struct { |
| 15 | Tagscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#Tag. Tagscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Tag# |
| 16 | |
| 17 | privatescip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#private. string // the x extension |
| 18 | variantsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#variants. []string |
| 19 | extensionsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#extensions. []string |
| 20 | } |
| 21 | |
| 22 | // Make returns a new Tag from the current settings. |
| 23 | func (b *Builderscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#) Makescip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#Make().() Tagscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Tag# { |
| 24 | t := b.Tagscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#Tag. |
| 25 | |
| 26 | if len(b.extensionsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#extensions.) > 0 || len(b.variantsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#variants.) > 0 { |
| 27 | sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Sortscip-go gomod github.com/golang/go/src go1.25.0 sort/Sort().(sortVariantsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/sortVariants#(b.variantsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#variants.)) |
| 28 | sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Stringsscip-go gomod github.com/golang/go/src go1.25.0 sort/Strings().(b.extensionsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#extensions.) |
| 29 | |
| 30 | if b.privatescip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#private. != "" { |
| 31 | b.extensionsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#extensions. = append(b.extensionsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#extensions., b.privatescip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#private.) |
| 32 | } |
| 33 | n := maxCoreSizescip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/maxCoreSize. + tokenLenscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/tokenLen().(b.variantsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#variants....) + tokenLenscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/tokenLen().(b.extensionsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#extensions....) |
| 34 | buf := make([]byte, n) |
| 35 | p := t.genCoreBytesscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Tag#genCoreBytes().(buf) |
| 36 | t.pVariantscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Tag#pVariant. = byte(p) |
| 37 | p += appendTokensscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/appendTokens().(buf[p:], b.variantsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#variants....) |
| 38 | t.pExtscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Tag#pExt. = uint16(p) |
| 39 | p += appendTokensscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/appendTokens().(buf[p:], b.extensionsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#extensions....) |
| 40 | t.strscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Tag#str. = string(buf[:p]) |
| 41 | // We may not always need to remake the string, but when or when not |
| 42 | // to do so is rather tricky. |
| 43 | scan := makeScannerscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/makeScanner().(buf[:p]) |
| 44 | t, _ = parsescip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/parse().(&scan, "") |
| 45 | return t |
| 46 | |
| 47 | } else if b.privatescip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#private. != "" { |
| 48 | t.strscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Tag#str. = b.privatescip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#private. |
| 49 | t.RemakeStringscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Tag#RemakeString().() |
| 50 | } |
| 51 | return t |
| 52 | } |
| 53 | |
| 54 | // SetTag copies all the settings from a given Tag. Any previously set values |
| 55 | // are discarded. |
| 56 | func (b *Builderscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#) SetTagscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#SetTag().(t Tagscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Tag#) { |
| 57 | b.Tagscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#Tag..LangIDscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Tag#LangID. = t.LangIDscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Tag#LangID. |
| 58 | b.Tagscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#Tag..RegionIDscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Tag#RegionID. = t.RegionIDscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Tag#RegionID. |
| 59 | b.Tagscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#Tag..ScriptIDscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Tag#ScriptID. = t.ScriptIDscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Tag#ScriptID. |
| 60 | // TODO: optimize |
| 61 | b.variantsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#variants. = b.variantsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#variants.[:0] |
| 62 | if variants := t.Variantsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Tag#Variants().(); variants != "" { |
| 63 | for _, vr := range stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Splitscip-go gomod github.com/golang/go/src go1.25.0 strings/Split().(variants[1:], "-") { |
| 64 | b.variantsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#variants. = append(b.variantsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#variants., vr) |
| 65 | } |
| 66 | } |
| 67 | b.extensionsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#extensions., b.privatescip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#private. = b.extensionsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#extensions.[:0], "" |
| 68 | for _, e := range t.Extensionsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Tag#Extensions().() { |
| 69 | b.AddExtscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#AddExt().(e) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // AddExt adds extension e to the tag. e must be a valid extension as returned |
| 74 | // by Tag.Extension. If the extension already exists, it will be discarded, |
| 75 | // except for a -u extension, where non-existing key-type pairs will added. |
| 76 | func (b *Builderscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#) AddExtscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#AddExt().(e string) { |
| 77 | if e[0] == 'x' { |
| 78 | if b.privatescip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#private. == "" { |
| 79 | b.privatescip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#private. = e |
| 80 | } |
| 81 | return |
| 82 | } |
| 83 | for i, s := range b.extensionsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#extensions. { |
| 84 | if s[0] == e[0] { |
| 85 | if e[0] == 'u' { |
| 86 | b.extensionsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#extensions.[i] += e[1:] |
| 87 | } |
| 88 | return |
| 89 | } |
| 90 | } |
| 91 | b.extensionsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#extensions. = append(b.extensionsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#extensions., e) |
| 92 | } |
| 93 | |
| 94 | // SetExt sets the extension e to the tag. e must be a valid extension as |
| 95 | // returned by Tag.Extension. If the extension already exists, it will be |
| 96 | // overwritten, except for a -u extension, where the individual key-type pairs |
| 97 | // will be set. |
| 98 | func (b *Builderscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#) SetExtscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#SetExt().(e string) { |
| 99 | if e[0] == 'x' { |
| 100 | b.privatescip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#private. = e |
| 101 | return |
| 102 | } |
| 103 | for i, s := range b.extensionsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#extensions. { |
| 104 | if s[0] == e[0] { |
| 105 | if e[0] == 'u' { |
| 106 | b.extensionsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#extensions.[i] = e + s[1:] |
| 107 | } else { |
| 108 | b.extensionsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#extensions.[i] = e |
| 109 | } |
| 110 | return |
| 111 | } |
| 112 | } |
| 113 | b.extensionsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#extensions. = append(b.extensionsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#extensions., e) |
| 114 | } |
| 115 | |
| 116 | // AddVariant adds any number of variants. |
| 117 | func (b *Builderscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#) AddVariantscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#AddVariant().(v ...string) { |
| 118 | for _, v := range v { |
| 119 | if v != "" { |
| 120 | b.variantsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#variants. = append(b.variantsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#variants., v) |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // ClearVariants removes any variants previously added, including those |
| 126 | // copied from a Tag in SetTag. |
| 127 | func (b *Builderscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#) ClearVariantsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#ClearVariants().() { |
| 128 | b.variantsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#variants. = b.variantsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#variants.[:0] |
| 129 | } |
| 130 | |
| 131 | // ClearExtensions removes any extensions previously added, including those |
| 132 | // copied from a Tag in SetTag. |
| 133 | func (b *Builderscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#) ClearExtensionsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#ClearExtensions().() { |
| 134 | b.privatescip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#private. = "" |
| 135 | b.extensionsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#extensions. = b.extensionsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Builder#extensions.[:0] |
| 136 | } |
| 137 | |
| 138 | func tokenLenscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/tokenLen().(token ...string) (n int) { |
| 139 | for _, t := range token { |
| 140 | n += len(t) + 1 |
| 141 | } |
| 142 | return |
| 143 | } |
| 144 | |
| 145 | func appendTokensscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/appendTokens().(b []byte, token ...string) int { |
| 146 | p := 0 |
| 147 | for _, t := range token { |
| 148 | b[p] = '-' |
| 149 | copy(b[p+1:], t) |
| 150 | p += 1 + len(t) |
| 151 | } |
| 152 | return p |
| 153 | } |
| 154 | |
| 155 | type sortVariantsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/sortVariants# []string |
| 156 | |
| 157 | func (s sortVariantsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/sortVariants#) Lenscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/sortVariants#Len().() int { |
| 158 | return len(s) |
| 159 | } |
| 160 | |
| 161 | func (s sortVariantsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/sortVariants#) Swapscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/sortVariants#Swap().(i, j int) { |
| 162 | s[j], s[i] = s[i], s[j] |
| 163 | } |
| 164 | |
| 165 | func (s sortVariantsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/sortVariants#) Lessscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/sortVariants#Less().(i, j int) bool { |
| 166 | return variantIndexscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/variantIndex.[s[i]] < variantIndexscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/variantIndex.[s[j]] |
| 167 | } |