猿问

如何验证无效的 unicode 代码点值?

在下面的代码中:


package main


import "fmt"


func main() {

    s := []rune{'\u0041', '\u0042', '\u0043', '\u20AC', -1}

    fmt.Println(s)

    fmt.Println(string(s)) // ABC€�

    fmt.Println(s[3] == '€')

    fmt.Println(s[4] == '�')

    fmt.Println(s[4] == '\uFFFD')

}

输入流具有无效的 unicode 代码点 -1,存储为\uFFFD.


但下面的行给出的输出为false:


    fmt.Println(s[4] == '�')

    fmt.Println(s[4] == '\uFFFD')

如何验证有效 unicode 代码点范围之外的 unicode 代码点值?


森林海
浏览 197回答 1
1回答

红糖糍粑

尝试这个:slice1 := []rune{'\u0041', '\u0042', '\u0043', '\u20AC', -1}st := string(slice1)slice2 := []rune(st)fmt.Println(slice2[4] == '\uFFFD') // true由于s[4]是-1结果s[4] == '\uFFFD'是错误的。'\uFFFD'是ReplacementChar代表无效代码点的 。并且是无效代码点的替换,例如-1,使用string([]rune{'A', -1})替换-1为'\uFFFD',试试这个:package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "unicode"&nbsp; &nbsp; "unicode/utf8")func main() {&nbsp; &nbsp; fmt.Println(unicode.ReplacementChar)&nbsp; &nbsp; fmt.Println(utf8.ValidRune(unicode.ReplacementChar))&nbsp; &nbsp; rs := []rune{'A', -1}&nbsp; &nbsp; s := string(rs)&nbsp; &nbsp; fmt.Println(s)&nbsp; &nbsp; fmt.Println([]rune(s)) //[65 65533]}有效符文func ValidRune(r rune) boolValidRune 报告 r 是否可以合法地编码为 UTF-8。超出范围或代理一半的代码点是非法的。const (&nbsp; &nbsp; surrogateMin = 0xD800&nbsp; &nbsp; surrogateMax = 0xDFFF&nbsp; &nbsp; MaxRune&nbsp; &nbsp;= '\U0010FFFF' // Maximum valid Unicode code point.)// ValidRune reports whether r can be legally encoded as UTF-8.// Code points that are out of range or a surrogate half are illegal.func ValidRune(r rune) bool {&nbsp; &nbsp; switch {&nbsp; &nbsp; case 0 <= r && r < surrogateMin:&nbsp; &nbsp; &nbsp; &nbsp; return true&nbsp; &nbsp; case surrogateMax < r && r <= MaxRune:&nbsp; &nbsp; &nbsp; &nbsp; return true&nbsp; &nbsp; }&nbsp; &nbsp; return false}例子:package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "unicode/utf8")func main() {&nbsp; &nbsp; slice := []rune{'\u0041', '\u0042', '\u0043', '\u20AC', '\uFFFD', 0xfffffff, -1}&nbsp; &nbsp; for i, v := range slice {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("%d %q %v\n", i, v, utf8.ValidRune(v))&nbsp; &nbsp; }}输出:0 'A' true1 'B' true2 'C' true3 '€' true4 '�' true5 %!q(int32=268435455) false6 %!q(int32=-1) false
随时随地看视频慕课网APP

相关分类

Go
我要回答