Skip to content

Commit d7a64bb

Browse files
authored
encoding.utf8: fix len and ulen and optimize raw_index (#9682)
1 parent 67d8639 commit d7a64bb

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

vlib/encoding/utf8/utf8_util.v

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ Utility functions
1515

1616
// len return the length as number of unicode chars from a string
1717
pub fn len(s string) int {
18+
if s.len == 0 {
19+
return 0
20+
}
21+
1822
mut count := 0
1923
mut index := 0
2024

@@ -82,10 +86,16 @@ pub fn get_uchar(s string, index int) int {
8286
// raw_index - get the raw chracter from the string by the given index value.
8387
// example: '我是V Lang'.raw_index(1) => '是'
8488

89+
// raw_index - get the raw chracter from the string by the given index value.
90+
// example: utf8.raw_index('我是V Lang', 1) => '是'
8591
pub fn raw_index(s string, index int) string {
8692
mut r := []rune{}
8793

8894
for i := 0; i < s.len; i++ {
95+
if r.len - 1 == index {
96+
break
97+
}
98+
8999
b := s[i]
90100
ch_len := ((0xe5000000 >> ((b >> 3) & 0x1e)) & 3)
91101

vlib/encoding/utf8/utf8_util_test.v

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ fn test_utf8_util() {
2121
assert lower1 == (src_lower.ustring())
2222

2323
// test len function
24+
assert utf8.len('') == 0
2425
assert utf8.len('pippo') == 5
2526
assert utf8.len(src) == 15 // 29
2627
assert src.len == 24 // 49
2728
// test u_len function
29+
assert utf8.u_len(''.ustring()) == 0
2830
assert utf8.u_len(src1) == 15 // 29
2931
assert utf8.u_len('pippo'.ustring()) == 5
3032

0 commit comments

Comments
 (0)