Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Detect surrogate pair on Golang
package main
import (
"fmt"
)
func detect_nullbyte(str string) bool {
for _, b := range str {
if b == '\u0000' {
return true
}
}
return false
}
func main() {
notExists := "Golang"
exists := "Golang\000"
fmt.Println(detect_nullbyte(notExists)) // => false
fmt.Println(detect_nullbyte(exists)) // => true
}
package main
import (
"fmt"
)
func detect_surrogate(str string) bool {
return (len([]byte(str)) % len([]rune(str))) > 0
}
func main() {
notExists := "吉野家で鯵"
exists := "𠮷野家で𩸽"
fmt.Println(detect_surrogate(notExists)) // => false
fmt.Println(detect_surrogate(exists)) // => true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment