46 lines
1.5 KiB
C++
Executable File
46 lines
1.5 KiB
C++
Executable File
#ifndef RC_UTF8_H_INCLUDED
|
|
#define RC_UTF8_H_INCLUDED
|
|
|
|
#include <string>
|
|
#include <algorithm>
|
|
|
|
std::string rc_utf8_substr(const std::string& str, unsigned int start, unsigned int leng)
|
|
{
|
|
if (leng==0) { return ""; }
|
|
unsigned int c, i, ix, q, min=std::string::npos, max=std::string::npos;
|
|
for (q=0, i=0, ix=str.length(); i < ix; i++, q++)
|
|
{
|
|
if (q==start){ min=i; }
|
|
if (q<=start+leng || leng==std::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 if (($c & 0xFC) == 0xF8) i+=4; // 111110bb //byte 5, unnecessary in 4 byte UTF-8
|
|
//else if (($c & 0xFE) == 0xFC) i+=5; // 1111110b //byte 6, unnecessary in 4 byte UTF-8
|
|
else return "";//invalid utf8
|
|
}
|
|
if (q<=start+leng || leng==std::string::npos){ max=i; }
|
|
if (min==std::string::npos || max==std::string::npos) { return ""; }
|
|
return str.substr(min,max-min);
|
|
}
|
|
|
|
std::size_t rc_utf8_length(std::string const &s)
|
|
{
|
|
return std::count_if(s.begin(), s.end(),
|
|
[](char c) { return (static_cast<unsigned char>(c) & 0xC0) != 0x80; } );
|
|
}
|
|
|
|
// convert UTF-8 string to wstring
|
|
std::wstring utf8_to_wstring (const std::string& str)
|
|
{
|
|
std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
|
|
return myconv.from_bytes(str);
|
|
}
|
|
|
|
#endif // RC_UTF8_H_INCLUDED
|