Skip to content

Commit eea4277

Browse files
Honor the LC_ALL environment variable.
1 parent a4df4dd commit eea4277

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

runewidth_posix.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ func isEastAsian(locale string) bool {
6262

6363
// IsEastAsian return true if the current locale is CJK
6464
func IsEastAsian() bool {
65-
locale := os.Getenv("LC_CTYPE")
65+
locale := os.Getenv("LC_ALL")
66+
if locale == "" {
67+
locale = os.Getenv("LC_CTYPE")
68+
}
6669
if locale == "" {
6770
locale = os.Getenv("LANG")
6871
}

runewidth_posix_test.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,25 @@ import (
99
"testing"
1010
)
1111

12+
type envVars struct {
13+
lang string
14+
lc_all string
15+
lc_ctype string
16+
}
17+
18+
func saveEnv() envVars {
19+
return envVars{
20+
lang: os.Getenv("LANG"),
21+
lc_all: os.Getenv("LC_ALL"),
22+
lc_ctype: os.Getenv("LC_CTYPE"),
23+
}
24+
}
25+
func restoreEnv(env *envVars) {
26+
os.Setenv("LANG", env.lang)
27+
os.Setenv("LC_ALL", env.lc_all)
28+
os.Setenv("LC_CTYPE", env.lc_ctype)
29+
}
30+
1231
func TestIsEastAsian(t *testing.T) {
1332
testcases := []struct {
1433
locale string
@@ -29,8 +48,9 @@ func TestIsEastAsian(t *testing.T) {
2948
}
3049

3150
func TestIsEastAsianLCCTYPE(t *testing.T) {
32-
lcctype := os.Getenv("LC_CTYPE")
33-
defer os.Setenv("LC_CTYPE", lcctype)
51+
env := saveEnv()
52+
defer restoreEnv(&env)
53+
os.Setenv("LC_ALL", "")
3454

3555
testcases := []struct {
3656
lcctype string
@@ -52,11 +72,9 @@ func TestIsEastAsianLCCTYPE(t *testing.T) {
5272
}
5373

5474
func TestIsEastAsianLANG(t *testing.T) {
55-
lcctype := os.Getenv("LC_CTYPE")
56-
defer os.Setenv("LC_CTYPE", lcctype)
57-
lang := os.Getenv("LANG")
58-
defer os.Setenv("LANG", lang)
59-
75+
env := saveEnv()
76+
defer restoreEnv(&env)
77+
os.Setenv("LC_ALL", "")
6078
os.Setenv("LC_CTYPE", "")
6179

6280
testcases := []struct {

0 commit comments

Comments
 (0)