33 static void SkipUtf8Bom(FILE* fp);
41 if ((ch & 0xF0) == 0xE0) {
43 }
else if ((ch & 0x80) == 0x00) {
45 }
else if ((ch & 0xE0) == 0xC0) {
47 }
else if ((ch & 0xF8) == 0xF0) {
49 }
else if ((ch & 0xFC) == 0xF8) {
51 }
else if ((ch & 0xFE) == 0xFC) {
61 size_t length = NextCharLengthNoException(str);
73 const size_t length = NextCharLengthNoException(str - 3);
79 const size_t length = NextCharLengthNoException(str - 1);
85 const size_t length = NextCharLengthNoException(str - 2);
90 for (
size_t i = 4; i <= 6; i++) {
91 const size_t length = NextCharLengthNoException(str - i);
103 return str + NextCharLength(str);
110 return str - PrevCharLength(str);
118 while (*str !=
'\0') {
132 while (!IsLineEndingOrFileEnding(*str) && *str != ch) {
142 return ch ==
'\0' || ch ==
'\n' || ch ==
'\r';
150 newStr.resize(length);
151 strncpy(const_cast<char*>(newStr.c_str()), str, length);
159 while (byteLength > 0) {
175 if (NotShorterThan(str, maxByteLength)) {
177 const char* pStr = str;
179 const size_t charLength = NextCharLength(pStr);
180 if (len + charLength > maxByteLength) {
186 wordTrunc = FromSubstr(str, len);
196 static void ReplaceAll(
string& str,
const char* from,
const char* to) {
197 string::size_type pos = 0;
198 string::size_type fromLen = strlen(from);
199 string::size_type toLen = strlen(to);
200 while ((pos = str.find(from, pos)) != string::npos) {
201 str.replace(pos, fromLen, to);
209 static string Join(
const vector<string>& strings,
const string& separator) {
210 std::ostringstream buffer;
212 for (
const auto& str : strings) {
225 static string Join(
const vector<string>& strings) {
226 std::ostringstream buffer;
227 for (
const auto& str : strings) {
233 static void GetByteMap(
const char* str,
const size_t utf8Length,
234 vector<size_t>* byteMap) {
235 if (byteMap->size() < utf8Length) {
236 byteMap->resize(utf8Length);
238 const char* pstr = str;
239 for (
size_t i = 0; i < utf8Length; i++) {
240 (*byteMap)[i] = pstr - str;
241 pstr = NextChar(pstr);
static size_t PrevCharLength(const char *str)
Returns the length in byte for the previous UTF8 character.
Definition: UTF8Util.hpp:71
static string Join(const vector< string > &strings)
Joins a string vector in to a string.
Definition: UTF8Util.hpp:225
Definition: Exception.hpp:77
static string Join(const vector< string > &strings, const string &separator)
Joins a string vector in to a string with a separator.
Definition: UTF8Util.hpp:209
static string TruncateUTF8(const char *str, size_t maxByteLength)
Truncates a string with a maximal length in byte.
Definition: UTF8Util.hpp:173
static void ReplaceAll(string &str, const char *from, const char *to)
Replaces all patterns in a string in place.
Definition: UTF8Util.hpp:196
static string FromSubstr(const char *str, size_t length)
Copies a substring with given length to a new std::string.
Definition: UTF8Util.hpp:148
static bool NotShorterThan(const char *str, size_t byteLength)
Returns true if the given string is longer or as long as the given length.
Definition: UTF8Util.hpp:158
static size_t Length(const char *str)
Returns the UTF8 length of a valid UTF8 string.
Definition: UTF8Util.hpp:116
Definition: BinaryDict.hpp:24
static const char * PrevChar(const char *str)
Move the char* pointer before the previous UTF8 character.
Definition: UTF8Util.hpp:109
static const char * NextChar(const char *str)
Returns the char* pointer over the next UTF8 character.
Definition: UTF8Util.hpp:102
UTF8 string utilities.
Definition: UTF8Util.hpp:28
static const char * FindNextInline(const char *str, const char ch)
Finds a character in the same line.
Definition: UTF8Util.hpp:131
static size_t NextCharLength(const char *str)
Returns the length in byte for the next UTF8 character.
Definition: UTF8Util.hpp:60
static bool IsLineEndingOrFileEnding(const char ch)
Returns ture if the character is a line ending or end of file.
Definition: UTF8Util.hpp:141
static size_t NextCharLengthNoException(const char *str)
Returns the length in byte for the next UTF8 character.
Definition: UTF8Util.hpp:39