Last active
December 22, 2015 19:29
-
-
Save skoowoo/6520296 to your computer and use it in GitHub Desktop.
判断一段数据是否是utf-8编码
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ( | |
UTF_BYTE_MASK1 = 0xF0 | |
UTF_BYTE_MASK2 = 0xC0 | |
UTF_BYTE_MASK3 = 0xC0 | |
UTF_BYTE_VALUE1 = 0xE0 | |
UTF_BYTE_VALUE2 = 0x80 | |
UTF_BYTE_VALUE3 = 0x80 | |
) | |
const ( | |
UTF_BYTE1 = iota | |
UTF_BYTE2 | |
UTF_BYTE3 | |
) | |
func IsUtf8(data []byte) bool { | |
state := UTF_BYTE1 | |
count := 0 | |
fail := 0 | |
for _, d := range data { | |
if d <= 127 { | |
continue | |
} | |
switch state { | |
case UTF_BYTE1: | |
if ((d & UTF_BYTE_MASK1) ^ UTF_BYTE_VALUE1) == 0 { | |
state = UTF_BYTE2 | |
} else { | |
fail++ | |
} | |
case UTF_BYTE2: | |
if ((d & UTF_BYTE_MASK2) ^ UTF_BYTE_VALUE2) == 0 { | |
state = UTF_BYTE3 | |
} else { | |
state = UTF_BYTE1 | |
fail++ | |
} | |
case UTF_BYTE3: | |
if ((d & UTF_BYTE_MASK3) ^ UTF_BYTE_VALUE3) == 0 { | |
count++ | |
} else { | |
fail++ | |
} | |
state = UTF_BYTE1 | |
} | |
} | |
if fail != 0 { | |
return false | |
} | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment