Exemple #1
0
func ExampleFullRuneInString() {
	str := "世"
	fmt.Println(utf8.FullRuneInString(str))
	fmt.Println(utf8.FullRuneInString(str[:2]))
	// Output:
	// true
	// false
}
Exemple #2
0
func testUTF8() {
	b := []byte("Hello, 世界")
	r, size := utf8.DecodeLastRune(b)
	TEQ("", '界', r)
	TEQ("", size, 3)
	b = b[:len(b)-size]
	r, size = utf8.DecodeLastRune(b)
	TEQ("", '世', r)
	TEQ("", size, 3)
	b = b[:len(b)-size]
	r, size = utf8.DecodeLastRune(b)
	TEQ("", ' ', r)
	TEQ("", size, 1)

	//fmt.Println("len(Zi)=", len("字"), hx.CodeInt(`'字'.length;`))

	str := "Hello, 世界"
	r, size = utf8.DecodeLastRuneInString(str)
	TEQ("", '界', r)
	TEQ("", size, 3)
	str = str[:len(str)-size]
	r, size = utf8.DecodeLastRuneInString(str)
	TEQ("", '世', r)
	TEQ("", size, 3)
	str = str[:len(str)-size]
	r, size = utf8.DecodeLastRuneInString(str)
	TEQ("", ' ', r)
	TEQ("", size, 1)

	ru := '世'
	buf := make([]byte, 3)
	n := utf8.EncodeRune(buf, ru)
	TEQ("", n, 3)
	TEQbyteSlice("", buf, []byte{228, 184, 150})

	buf = []byte{228, 184, 150} // 世
	TEQ("", true, utf8.FullRune(buf))
	TEQ("", false, utf8.FullRune(buf[:2]))

	str = "世"
	TEQ("", true, utf8.FullRuneInString(str))
	//if ShowKnownErrors || hx.GetInt("", "'字'.length") == 3 {
	TEQ(""+" NOTE: known error handling incorrect strings on UTF16 platforms", false, utf8.FullRuneInString(str[:2]))
	//}
	buf = []byte("Hello, 世界")
	TEQ("", 13, len(buf))
	TEQ("", 9, utf8.RuneCount(buf))

	str = "Hello, 世界"
	TEQ("", 13, len(str))
	TEQ("", 9, utf8.RuneCountInString(str))

	TEQ("", 1, utf8.RuneLen('a'))
	TEQ("", 3, utf8.RuneLen('界'))

	buf = []byte("a界")
	TEQ("", true, utf8.RuneStart(buf[0]))
	TEQ("", true, utf8.RuneStart(buf[1]))
	TEQ("", false, utf8.RuneStart(buf[2]))

	valid := []byte("Hello, 世界")
	invalid := []byte{0xff, 0xfe, 0xfd}
	TEQ("", true, utf8.Valid(valid))
	TEQ("", false, utf8.Valid(invalid))

	valid_rune := 'a'
	invalid_rune := rune(0xfffffff)
	TEQ("", true, utf8.ValidRune(valid_rune))
	TEQ("", false, utf8.ValidRune(invalid_rune))

	valid_string := "Hello, 世界"
	invalid_string := string([]byte{0xff, 0xfe, 0xfd})
	TEQ("", true, utf8.ValidString(valid_string))
	//if ShowKnownErrors || hx.GetInt("", "'字'.length") == 3 {
	TEQ(""+" NOTE: known error handling incorrect strings on UTF16 platforms", false, utf8.ValidString(invalid_string))
	//}
}
Exemple #3
0
func testUTF8() {
	b := []byte("Hello, 世界")
	r, size := utf8.DecodeLastRune(b)
	TEQ(tardisgolib.CPos(), '界', r)
	TEQ(tardisgolib.CPos(), size, 3)
	b = b[:len(b)-size]
	r, size = utf8.DecodeLastRune(b)
	TEQ(tardisgolib.CPos(), '世', r)
	TEQ(tardisgolib.CPos(), size, 3)
	b = b[:len(b)-size]
	r, size = utf8.DecodeLastRune(b)
	TEQ(tardisgolib.CPos(), ' ', r)
	TEQ(tardisgolib.CPos(), size, 1)

	str := "Hello, 世界"
	r, size = utf8.DecodeLastRuneInString(str)
	TEQ(tardisgolib.CPos(), '界', r)
	TEQ(tardisgolib.CPos(), size, 3)
	str = str[:len(str)-size]
	r, size = utf8.DecodeLastRuneInString(str)
	TEQ(tardisgolib.CPos(), '世', r)
	TEQ(tardisgolib.CPos(), size, 3)
	str = str[:len(str)-size]
	r, size = utf8.DecodeLastRuneInString(str)
	TEQ(tardisgolib.CPos(), ' ', r)
	TEQ(tardisgolib.CPos(), size, 1)

	ru := '世'
	buf := make([]byte, 3)
	n := utf8.EncodeRune(buf, ru)
	TEQ(tardisgolib.CPos(), n, 3)
	TEQbyteSlice(tardisgolib.CPos(), buf, []byte{228, 184, 150})

	buf = []byte{228, 184, 150} // 世
	TEQ(tardisgolib.CPos(), true, utf8.FullRune(buf))
	TEQ(tardisgolib.CPos(), false, utf8.FullRune(buf[:2]))

	str = "世"
	TEQ(tardisgolib.CPos(), true, utf8.FullRuneInString(str))
	if ShowKnownErrors || tardisgolib.Zilen() == 3 {
		TEQ(tardisgolib.CPos()+" NOTE: known error handling incorrect strings on UTF16 platforms", false, utf8.FullRuneInString(str[:2]))
	}
	buf = []byte("Hello, 世界")
	TEQ(tardisgolib.CPos(), 13, len(buf))
	TEQ(tardisgolib.CPos(), 9, utf8.RuneCount(buf))

	str = "Hello, 世界"
	TEQ(tardisgolib.CPos(), 13, len(str))
	TEQ(tardisgolib.CPos(), 9, utf8.RuneCountInString(str))

	TEQ(tardisgolib.CPos(), 1, utf8.RuneLen('a'))
	TEQ(tardisgolib.CPos(), 3, utf8.RuneLen('界'))

	buf = []byte("a界")
	TEQ(tardisgolib.CPos(), true, utf8.RuneStart(buf[0]))
	TEQ(tardisgolib.CPos(), true, utf8.RuneStart(buf[1]))
	TEQ(tardisgolib.CPos(), false, utf8.RuneStart(buf[2]))

	valid := []byte("Hello, 世界")
	invalid := []byte{0xff, 0xfe, 0xfd}
	TEQ(tardisgolib.CPos(), true, utf8.Valid(valid))
	TEQ(tardisgolib.CPos(), false, utf8.Valid(invalid))

	valid_rune := 'a'
	invalid_rune := rune(0xfffffff)
	TEQ(tardisgolib.CPos(), true, utf8.ValidRune(valid_rune))
	TEQ(tardisgolib.CPos(), false, utf8.ValidRune(invalid_rune))

	valid_string := "Hello, 世界"
	invalid_string := string([]byte{0xff, 0xfe, 0xfd})
	TEQ(tardisgolib.CPos(), true, utf8.ValidString(valid_string))
	if ShowKnownErrors || tardisgolib.Zilen() == 3 {
		TEQ(tardisgolib.CPos()+" NOTE: known error handling incorrect strings on UTF16 platforms", false, utf8.ValidString(invalid_string))
	}
}