diff options
author | Ryooooooga <eial5q265e5@gmail.com> | 2021-10-23 20:46:37 +0900 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2021-10-30 18:26:06 +1100 |
commit | 6171690b000af4fb495fc9d2930dfa988800ee3f (patch) | |
tree | 339b405dbd874ba4412f2c2015137a05b08b91f4 /pkg/utils | |
parent | 253504a0947e0eb734e47d1b869e1a674d21655b (diff) |
Fix multibyte initial characters
Diffstat (limited to 'pkg/utils')
-rw-r--r-- | pkg/utils/slice.go | 8 | ||||
-rw-r--r-- | pkg/utils/slice_test.go | 10 |
2 files changed, 16 insertions, 2 deletions
diff --git a/pkg/utils/slice.go b/pkg/utils/slice.go index 0ee8dec66..dc3e85069 100644 --- a/pkg/utils/slice.go +++ b/pkg/utils/slice.go @@ -146,8 +146,12 @@ func Reverse(values []string) []string { } func LimitStr(value string, limit int) string { - if len(value) > limit { - return value[:limit] + n := 0 + for i := range value { + if n >= limit { + return value[:i] + } + n++ } return value } diff --git a/pkg/utils/slice_test.go b/pkg/utils/slice_test.go index b5919b3e6..67dcd00bf 100644 --- a/pkg/utils/slice_test.go +++ b/pkg/utils/slice_test.go @@ -290,6 +290,16 @@ func TestLimitStr(t *testing.T) { limit: 3, want: "abc", }, + { + values: "あいう", + limit: 1, + want: "あ", + }, + { + values: "あいう", + limit: 2, + want: "あい", + }, } { if got := LimitStr(test.values, test.limit); !assert.EqualValues(t, got, test.want) { t.Errorf("LimitString(%v, %d) = %v; want %v", test.values, test.limit, got, test.want) |