From 82c8b9fc3a00d6b9e4d1233afe69fe8c62d31312 Mon Sep 17 00:00:00 2001 From: sgf Date: Wed, 22 Jun 2022 00:20:16 +0300 Subject: [PATCH] Save utf8 experiments. --- str.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 str.go diff --git a/str.go b/str.go new file mode 100644 index 0000000..d701a18 --- /dev/null +++ b/str.go @@ -0,0 +1,40 @@ + +package main + +import ( + "fmt" + "unicode/utf8" +) + +func main() { + //const sample = "\xbd\xb2\x3d\xbc\x20\xe2\x8c\x98" + var sample []byte = []byte("\xbd\xb2\x3d\xbc\x20\xe2\x8c\x98") + fmt.Println(sample) + + for i := 0; i < len(sample); i++ { + fmt.Printf("%x ", sample[i]) + fmt.Printf("(%q ", sample[i]) + fmt.Printf("%+q) ", sample[i]) + } + fmt.Printf("\n") + fmt.Printf("% x\n", sample) + + fmt.Printf("%q\n", sample) + fmt.Printf("%+q\n", sample) + fmt.Printf("%b %b %b\n", 0xe2, 0x8c, 0x98) + fmt.Printf("%b\n", 0x2318) + + const nihongo = "日\xef本語" + for index, runeValue := range nihongo { + fmt.Printf("%#U starts at byte position %d\n", runeValue, index) + } + + fmt.Println("with utf8") + for i, w := 0, 0; i < len(nihongo); i += w { + runeValue, width := utf8.DecodeRuneInString(nihongo[i:]) + fmt.Printf("%#U starts at byte position %d with width %d\n", runeValue, i, width) + w = width + } + + fmt.Printf("%x %v", nihongo, len(nihongo)) +} -- 2.20.1