2 ms·
How do you feel about how golang handles runes? Example: https://play.golang.org/p/7XWC9Irui3N https://play.golang.org/p/7XWC9Irui3N x = abc 3 vs 3 y = é
by mjevans 7y ago
How do you feel about how golang handles runes?
Example: https://play.golang.org/p/7XWC9Irui3N https://play.golang.org/p/7XWC9Irui3N
x = abc
3 vs 3
y = é
3 vs 2
z = 世界
6 vs 2
s>>r 0 1 true e
s>>r 1 2 true ́
[]b>>b 0 true true 00000065
[]b>>b 1 false true 000000CC
[]b>>b 2 true false 00000081
(not putting in a code block given the above and mobile...)
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
var x, y, z string;
x = "abc";
y = string([]byte{0x65, 0xCC, 0x81}) // 'e' + 0x0301 ( 0(011 00)(00 0001)) = "é";
z = "世界";
fmt.Printf("x = %s\n%d vs %d\n\ny = %s\n%d vs %d\n\nz = %s\n%d vs %d\n\n",
x, len(x), utf8.RuneCountInString(x),
y, len(y), utf8.RuneCountInString(y),
z, len(z), utf8.RuneCountInString(y));
for k, v := range y {
// iterates as runes, not string slices
//fmt.Printf("s %d\t%s\t%v\t%s\n", k, utf8.RuneCountInString(v), utf8.ValidString(v), v);
fmt.Printf("s>>r %d\t%d\t%v\t%c\n", k, utf8.RuneLen(v), utf8.ValidRune(v), v);
}
for k, v := range []byte(y) {
// iterates as byte units, not runes
fmt.Printf("[]b>>b %d\t%v\t%v\t%08X\n", k, utf8.FullRune([]byte{v}), utf8.RuneStart(v), v);
}
}