-
Notifications
You must be signed in to change notification settings - Fork 7
/
bidi.go
83 lines (72 loc) · 1.8 KB
/
bidi.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
73
74
75
76
77
78
79
80
81
82
83
package gostrutils
import (
"golang.org/x/text/language"
)
// CountBidiChars counts how many chars belong to a bi-directional language:
// Arabic (and sub language), Hebrew, Urdu and Yiddish
func CountBidiChars(s string) int {
result := 0
for _, ch := range s {
if // Hebrew
(ch >= 0x590 && ch <= 0x5FF) ||
// Arabic
(ch >= 0x600 && ch <= 0x6FF) ||
(ch >= 0x750 && ch <= 0x77F) ||
(ch >= 0x8A0 && ch <= 0x8FF) ||
// Syriac
(ch >= 0x700 && ch <= 0x74F) ||
// Thaana
(ch >= 0x780 && ch <= 0x7BF) ||
// NKO and Samaritan
(ch >= 0x7C0 && ch <= 0x83E) ||
(ch >= 0x840 && ch <= 0x85E) {
result++
}
}
return result
}
// CountStartBidiLanguage returns number of chars from the beginning.
// If chars are not A bidi range, and not up to 0x40 char, then 0 will be
// returned.
// If only chars up to (including) char 0x40 (@) -1 will be returned
//
// Important: result is both bidi chars and up to 0x40 count from the beginning
func CountStartBidiLanguage(s string) int {
result := 0
nunLatin := 0
for _, ch := range s {
if // Hebrew
(ch >= 0x590 && ch <= 0x5FF) ||
// Arabic
(ch >= 0x600 && ch <= 0x6FF) ||
(ch >= 0x750 && ch <= 0x77F) ||
(ch >= 0x8A0 && ch <= 0x8FF) ||
// Syriac
(ch >= 0x700 && ch <= 0x74F) ||
// Thaana
(ch >= 0x780 && ch <= 0x7BF) ||
// NKO and Samaritan
(ch >= 0x7C0 && ch <= 0x83E) ||
(ch >= 0x840 && ch <= 0x85E) {
result++
continue
}
if ch >= 0x00 && ch <= 0x40 {
nunLatin++
continue
}
break
}
if result == 0 && nunLatin > 0 {
return -1
}
return result + nunLatin
}
// TagIsBidi return true if a given tag is a bidi language
func TagIsBidi(tag language.Tag) bool {
result := tag == language.Arabic ||
tag == language.Persian ||
tag == language.Hebrew ||
tag == language.Urdu
return result
}