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);
}