Skip to content

Instantly share code, notes, and snippets.

@ytlvy
Forked from markqiu/isgbk.c
Created November 14, 2016 09:08
Show Gist options
  • Save ytlvy/4aff890a7f692bf5d3a53e1491661a3a to your computer and use it in GitHub Desktop.
Save ytlvy/4aff890a7f692bf5d3a53e1491661a3a to your computer and use it in GitHub Desktop.
判断一个字串是否是GBK编码
/* http://en.wikipedia.org/wiki/GBK */
size_t fixGBK(const char *str, size_t len)
{
const unsigned char *string = (const unsigned char *)str;
size_t idx = 0;
for (idx = 0; idx < len; idx++) {
int val = string[idx], val2;
if (val < 128)
continue;
if (idx + 1 >= len)
return idx;
val2 = string[idx + 1];
if (((val >= 0xA1 && val <= 0xA9) && (val2 >= 0xA1 && val2 <= 0xFE)) ||
((val >= 0xB0 && val <= 0xF7) && (val2 >= 0xA1 && val2 <= 0xFE)) ||
((val >= 0x81 && val <= 0xA0) && (val2 >= 0x40 && val2 <= 0xFE && val2 != 0x7F)) ||
((val >= 0xAA && val <= 0xFE) && (val2 >= 0x40 && val2 <= 0xA0 && val2 != 0x7F)) ||
((val >= 0xA8 && val <= 0xA9) && (val2 >= 0x40 && val2 <= 0xA0 && val2 != 0x7F)) ||
((val >= 0xAA && val <= 0xAF) && (val2 >= 0xA1 && val2 <= 0xFE)) ||
((val >= 0xF8 && val <= 0xFE) && (val2 >= 0xA1 && val2 <= 0xFE)) ||
((val >= 0xA1 && val <= 0xA7) && (val2 >= 0x40 && val2 <= 0xA0 && val2 != 0x7F))) {
idx++;
} else {
return idx;
}
}
return idx;
}
#define isGBK(str, len) (fixGBK(str, len) == (len))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment