Save utf8 experiments.
authorsgf <sgf.dma@gmail.com>
Tue, 21 Jun 2022 21:20:16 +0000 (00:20 +0300)
committersgf <sgf.dma@gmail.com>
Tue, 21 Jun 2022 21:20:16 +0000 (00:20 +0300)
str.go [new file with mode: 0644]

diff --git a/str.go b/str.go
new file mode 100644 (file)
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))
+}