-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathot_language.go
72 lines (62 loc) · 1.31 KB
/
ot_language.go
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
package harfbuzz
import (
"strings"
tt "github.com/benoitkugler/textlayout/fonts/truetype"
"github.com/benoitkugler/textlayout/language"
)
type langTag struct {
language string
tag tt.Tag
}
// return -1 if `a` < `l`
func (l *langTag) compare(a string) int {
b := l.language
p := strings.IndexByte(a, '-')
// da := len(a)
if p != -1 {
// da = p
a = a[:p]
}
p = strings.IndexByte(b, '-')
// db := len(b)
if p != -1 {
// db = p
b = b[:p]
}
// L := min(min(len(a), len(b)), max(da, db))
return strings.Compare(a, b)
}
func bfindLanguage(lang string) int {
low, high := 0, len(otLanguages)
for low <= high {
mid := (low + high) / 2
p := &otLanguages[mid]
cmp := p.compare(lang)
if cmp < 0 {
high = mid - 1
} else if cmp > 0 {
low = mid + 1
} else {
return mid
}
}
return -1
}
func subtagMatches(langStr string, limit int, subtag string) bool {
LS := len(subtag)
for {
s := strings.Index(langStr, subtag)
if s == -1 || s >= limit {
return false
}
if s+LS >= len(langStr) || !isAlnum(langStr[s+LS]) {
return true
}
langStr = langStr[s+LS:]
}
}
func langMatches(langStr, spec string) bool {
l := len(spec)
return strings.HasPrefix(langStr, spec) && (len(langStr) == l || langStr[l] == '-')
}
func languageToString(l language.Language) string { return string(l) }