Skip to content

Commit 7e964bf

Browse files
committed
Fix GetFirstCharacters for Unicode
1 parent 298dc9e commit 7e964bf

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

modules/util/string.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ func UnsafeStringToBytes(s string) []byte {
9898

9999
// GetFirstCharacters returns the first n characters of a string
100100
func GetFirstCharacters(s string, n int) string {
101+
r := []rune(s)
101102
if len(s) <= n {
102-
return s
103+
return string(r[:n])
103104
}
104105
return s[:n]
105106
}

modules/util/string_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@ func TestToSnakeCase(t *testing.T) {
4949
func TestGetFirstCharacters(t *testing.T) {
5050
assert.Equal(t, "Test123", GetFirstCharacters("Test123", 10))
5151
assert.Equal(t, "Test", GetFirstCharacters("Test123", 4))
52+
assert.Equal(t, "こん", GetFirstCharacters("こんにちは", 2))
5253
}

0 commit comments

Comments
 (0)