You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

83 lines
1.4 KiB

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