陈天

陈天--只求真实的记录

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

static string utf8_substr(const string& str, unsigned int start, unsigned int len)
{
if (len == 0) return "";

unsigned int min = string::npos, max = string::npos;
unsigned int str_len = str.length();
unsigned int c;
unsigned int i, tmp;
for (tmp = 0, i = 0; i < str_len; i++, tmp++)
{
if (tmp == start) min = i;
if (tmp <= start + len || len == string::npos) max = i;

c = (unsigned char)str[i];
if (c >= 0 && c <= 127) i += 0;
else if ((c & 0xE0) == 0xC0) i += 1;
else if ((c & 0xF0) == 0xE0) i += 2;
else if ((c & 0xF8) == 0xF0) i += 3;
else return "";
}
if (tmp <= start + len || len == string::npos) max = i;
if (min == string::npos || max == string::npos) return "";
return str.substr(min, max);
}

posted on 2017-05-26 17:40  陈天  阅读(1978)  评论(0编辑  收藏  举报