-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.go
64 lines (57 loc) · 1.57 KB
/
scripts.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
package language
import (
"encoding/binary"
"fmt"
)
// Script identifies different writing systems.
// It is represented as the binary encoding of a script tag of 4 letters,
// as specified by ISO 15924.
// Note that the default value is usually the Unknown script, not the 0 value (which is invalid)
type Script uint32
// ParseScript simply converts a 4 bytes string into its binary encoding.
func ParseScript(script string) (Script, error) {
if len(script) != 4 {
return 0, fmt.Errorf("invalid script string: %s", script)
}
return Script(binary.BigEndian.Uint32([]byte(script))), nil
}
// LookupScript looks up the script for a particular character (as defined by
// Unicode Standard Annex #24), and returns Unknown if not found.
func LookupScript(r rune) Script {
// binary search
for i, j := 0, len(scriptRanges); i < j; {
h := i + (j-i)/2
entry := scriptRanges[h]
if r < entry.start {
j = h
} else if entry.end < r {
i = h + 1
} else {
return entry.script
}
}
return Unknown
}
func (s Script) String() string {
for k, v := range scriptToTag {
if v == s {
return k
}
}
return fmt.Sprintf("<script unknown: %d>", s)
}
// IsRealScript return `true` if `s` if valid,
// and neither common or inherited.
func (s Script) IsRealScript() bool {
switch s {
case 0, Unknown, Common, Inherited:
return false
default:
return true
}
}
// IsSameScript compares two scripts: if one them
// is not 'real' (see IsRealScript), they are compared equal.
func (s1 Script) IsSameScript(s2 Script) bool {
return s1 == s2 || !s1.IsRealScript() || !s2.IsRealScript()
}