blob: 480ad748538f8123b0c09d85db020a885235aa87 [file] [log] [blame]
Naveen Sampath04696f72022-06-13 15:19:14 +05301// +build !windows
2// +build !js
3// +build !appengine
4
5package runewidth
6
7import (
8 "os"
9 "regexp"
10 "strings"
11)
12
13var reLoc = regexp.MustCompile(`^[a-z][a-z][a-z]?(?:_[A-Z][A-Z])?\.(.+)`)
14
15var mblenTable = map[string]int{
16 "utf-8": 6,
17 "utf8": 6,
18 "jis": 8,
19 "eucjp": 3,
20 "euckr": 2,
21 "euccn": 2,
22 "sjis": 2,
23 "cp932": 2,
24 "cp51932": 2,
25 "cp936": 2,
26 "cp949": 2,
27 "cp950": 2,
28 "big5": 2,
29 "gbk": 2,
30 "gb2312": 2,
31}
32
33func isEastAsian(locale string) bool {
34 charset := strings.ToLower(locale)
35 r := reLoc.FindStringSubmatch(locale)
36 if len(r) == 2 {
37 charset = strings.ToLower(r[1])
38 }
39
40 if strings.HasSuffix(charset, "@cjk_narrow") {
41 return false
42 }
43
44 for pos, b := range []byte(charset) {
45 if b == '@' {
46 charset = charset[:pos]
47 break
48 }
49 }
50 max := 1
51 if m, ok := mblenTable[charset]; ok {
52 max = m
53 }
54 if max > 1 && (charset[0] != 'u' ||
55 strings.HasPrefix(locale, "ja") ||
56 strings.HasPrefix(locale, "ko") ||
57 strings.HasPrefix(locale, "zh")) {
58 return true
59 }
60 return false
61}
62
63// IsEastAsian return true if the current locale is CJK
64func IsEastAsian() bool {
65 locale := os.Getenv("LC_ALL")
66 if locale == "" {
67 locale = os.Getenv("LC_CTYPE")
68 }
69 if locale == "" {
70 locale = os.Getenv("LANG")
71 }
72
73 // ignore C locale
74 if locale == "POSIX" || locale == "C" {
75 return false
76 }
77 if len(locale) > 1 && locale[0] == 'C' && (locale[1] == '.' || locale[1] == '-') {
78 return false
79 }
80
81 return isEastAsian(locale)
82}