2015-09-23 17:30:00 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
// IsValidUtf8String checks for in valid UTF-8 characters
|
|
|
|
func IsValidUtf8String(s string) bool {
|
2015-09-23 19:13:54 +00:00
|
|
|
return InvalidUtf8Index([]byte(s)) == -1
|
2015-09-23 17:30:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsValidUtf8Btyes checks for in valid UTF-8 characters
|
|
|
|
func IsValidUtf8Btyes(b []byte) bool {
|
2015-09-23 19:13:54 +00:00
|
|
|
return InvalidUtf8Index(b) == -1
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvalidUtf8Index returns the offset of the first invalid UTF-8 character.
|
|
|
|
// Default is to return -1 for a wholly valid sequence.
|
|
|
|
func InvalidUtf8Index(b []byte) int {
|
|
|
|
for i, r := range string(b) {
|
2015-09-23 17:30:00 +00:00
|
|
|
if int(r) == 0xfffd {
|
2015-09-23 19:13:54 +00:00
|
|
|
return i
|
2015-09-23 17:30:00 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-23 19:13:54 +00:00
|
|
|
return -1
|
2015-09-23 17:30:00 +00:00
|
|
|
}
|