File tree Expand file tree Collapse file tree 2 files changed +12
-0
lines changed Expand file tree Collapse file tree 2 files changed +12
-0
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,10 @@ Utility functions
15
15
16
16
// len return the length as number of unicode chars from a string
17
17
pub fn len (s string ) int {
18
+ if s.len == 0 {
19
+ return 0
20
+ }
21
+
18
22
mut count := 0
19
23
mut index := 0
20
24
@@ -82,10 +86,16 @@ pub fn get_uchar(s string, index int) int {
82
86
// raw_index - get the raw chracter from the string by the given index value.
83
87
// example: '我是V Lang'.raw_index(1) => '是'
84
88
89
+ // raw_index - get the raw chracter from the string by the given index value.
90
+ // example: utf8.raw_index('我是V Lang', 1) => '是'
85
91
pub fn raw_index (s string , index int ) string {
86
92
mut r := []rune {}
87
93
88
94
for i := 0 ; i < s.len; i++ {
95
+ if r.len - 1 == index {
96
+ break
97
+ }
98
+
89
99
b := s[i]
90
100
ch_len := ((0xe5000000 >> ((b >> 3 ) & 0x1e )) & 3 )
91
101
Original file line number Diff line number Diff line change @@ -21,10 +21,12 @@ fn test_utf8_util() {
21
21
assert lower1 == (src_lower.ustring ())
22
22
23
23
// test len function
24
+ assert utf8 .len ('' ) == 0
24
25
assert utf8 .len ('pippo' ) == 5
25
26
assert utf8 .len (src) == 15 // 29
26
27
assert src.len == 24 // 49
27
28
// test u_len function
29
+ assert utf8 .u_len ('' .ustring ()) == 0
28
30
assert utf8 .u_len (src1 ) == 15 // 29
29
31
assert utf8 .u_len ('pippo' .ustring ()) == 5
30
32
You can’t perform that action at this time.
0 commit comments