-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutf8.go
53 lines (46 loc) · 1 KB
/
utf8.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package idris_go_rts
import . "unicode/utf8"
//-------------------------------------------------------------------------------------------------
// Functions for UTF-8 string operations
//-------------------------------------------------------------------------------------------------
func Utf8Head(s string) rune {
if len(s) > 0 {
chr, _ := DecodeRuneInString(s)
return chr
} else {
return 0
}
}
func Utf8Tail(s string) string {
_, offset := DecodeRuneInString(s)
return s[offset:]
}
func Utf8AtIndex(s string, index int) rune {
if len(s) > 0 {
if index == 0 {
chr, _ := DecodeRuneInString(s)
return chr
} else {
i := 0
for _, chr := range s {
if i == index {
return chr
}
i++
}
}
}
return 0
}
func Utf8Reverse(s string) string {
offset := len(s)
if offset == 0 {
return ""
}
buf := make([]rune, offset)
for _, chr := range s {
offset--
buf[offset] = chr
}
return string(buf[offset:])
}