Anand S Katti | 0954135 | 2020-01-29 15:54:01 +0530 | [diff] [blame] | 1 | // +build !windows |
| 2 | // +build !js |
| 3 | // +build !appengine |
| 4 | |
| 5 | package runewidth |
| 6 | |
| 7 | import ( |
| 8 | "os" |
| 9 | "regexp" |
| 10 | "strings" |
| 11 | ) |
| 12 | |
| 13 | var reLoc = regexp.MustCompile(`^[a-z][a-z][a-z]?(?:_[A-Z][A-Z])?\.(.+)`) |
| 14 | |
| 15 | var 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 | |
| 33 | func 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 |
| 64 | func IsEastAsian() bool { |
| 65 | locale := os.Getenv("LC_CTYPE") |
| 66 | if locale == "" { |
| 67 | locale = os.Getenv("LANG") |
| 68 | } |
| 69 | |
| 70 | // ignore C locale |
| 71 | if locale == "POSIX" || locale == "C" { |
| 72 | return false |
| 73 | } |
| 74 | if len(locale) > 1 && locale[0] == 'C' && (locale[1] == '.' || locale[1] == '-') { |
| 75 | return false |
| 76 | } |
| 77 | |
| 78 | return isEastAsian(locale) |
| 79 | } |