Skip to content

Instantly share code, notes, and snippets.

@catatsuy
Created October 24, 2023 14:11
Show Gist options
  • Save catatsuy/b64b3d99eab90eb72cd154b5b032efd9 to your computer and use it in GitHub Desktop.
Save catatsuy/b64b3d99eab90eb72cd154b5b032efd9 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"unicode/utf8"
)
const (
t1 = 0b00000000
tx = 0b10000000
t2 = 0b00000110
t3 = 0b00001110
t4 = 0b00011110
)
func main() {
a := []byte("こん😅にちは")
l := FindValidUTF8Position(a, 12)
fmt.Println(string(a[0:l]), utf8.Valid(a[0:l]), l)
}
func FindValidUTF8Position(a []byte, l int) int {
if len(a) < l {
return -1
}
for i := 0; i < 4; i++ {
if validUtf8(a[l+i-1]) {
return l + i - 1
}
}
return -1
}
func validUtf8(tmp byte) bool {
if tmp&tx == t1 {
return true
}
tmp >>= 3
if tmp == t4 {
return true
}
tmp >>= 1
if tmp == t3 {
return true
}
tmp >>= 1
if tmp == t2 {
return true
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment