Skip to content

Instantly share code, notes, and snippets.

@ysugimoto
Last active August 2, 2018 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ysugimoto/447e2dbcc0002194f20bba8162965ea4 to your computer and use it in GitHub Desktop.
Save ysugimoto/447e2dbcc0002194f20bba8162965ea4 to your computer and use it in GitHub Desktop.
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