Created
October 22, 2012 21:05
-
-
Save eklitzke/3934294 to your computer and use it in GitHub Desktop.
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
| bool IsValidUtf8(const std::string &src) { | |
| int n = 0; | |
| for (const char &i : src) { | |
| int c = static_cast<int>(i) & 0xFF; | |
| if (n) { | |
| if ((c & 0xC0) != 0x80) { | |
| return false; | |
| } | |
| n--; | |
| continue; | |
| } | |
| if (c < 0x80) { | |
| n = 0; // 0bbbbbbb | |
| } else if ((c & 0xE0) == 0xC0) { | |
| n = 1; // 110bbbbb | |
| } else if ((c & 0xF0) == 0xE0) { | |
| n = 2; // 1110bbbb | |
| } else if ((c & 0xF8) == 0xF0) { | |
| n = 3; // 11110bbb | |
| } else if ((c & 0xFC) == 0xF8) { | |
| n = 4; // 111110bb | |
| } else if ((c & 0xFE) == 0xFC) { | |
| n = 5; // 1111110b | |
| } else { | |
| return false; | |
| } | |
| } | |
| return n == 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment