原文地址 : http://www.zedwood.com/article/cpp-is-valid-utf8-string-function
#include <iostream>
using namespace std;
bool utf8_check_is_valid(const string& string);
int main(int argc, char *argv[])
{
string hello = "hello world"; //length 11
string portg = "ol\xc3\xa1 mundo";//olá mundo length 9
string nihao = "\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xb8\x96\xe7\x95\x8c"; //你好世界 length 4
string wrong = "\xa0\xa1";
cout << (utf8_check_is_valid(hello) ? "valid" : "invalid") << endl; // valid
cout << (utf8_check_is_valid(portg) ? "valid" : "invalid") << endl; // valid
cout << (utf8_check_is_valid(nihao) ? "valid" : "invalid") << endl; // valid
cout << (utf8_check_is_valid(wrong) ? "valid" : "invalid") << endl; // invalid
return 0;
}
//more invalid strings to test: http://*.com/questions/1301402/example-invalid-utf8-string
bool utf8_check_is_valid(const string& string)
{
int c,i,ix,n,j;
for (i=0, ix=string.length(); i < ix; i++)
{
c = (unsigned char) string[i];
//if (c==0x09 || c==0x0a || c==0x0d || (0x20 <= c && c <= 0x7e) ) n = 0; // is_printable_ascii
if (0x00 <= c && c <= 0x7f) n=0; // 0bbbbbbb
else if ((c & 0xE0) == 0xC0) n=1; // 110bbbbb
else if ( c==0xed && i<(ix-1) && ((unsigned char)string[i+1] & 0xa0)==0xa0) return false; //U+d800 to U+dfff
else if ((c & 0xF0) == 0xE0) n=2; // 1110bbbb
else if ((c & 0xF8) == 0xF0) n=3; // 11110bbb
//else if (($c & 0xFC) == 0xF8) n=4; // 111110bb //byte 5, unnecessary in 4 byte UTF-8
//else if (($c & 0xFE) == 0xFC) n=5; // 1111110b //byte 6, unnecessary in 4 byte UTF-8
else return false;
for (j=0; j<n && i<ix; j++) { // n bytes matching 10bbbbbb follow ?
if ((++i == ix) || (( (unsigned char)string[i] & 0xC0) != 0x80))
return false;
}
}
return true;
}