20 #include "UTF8Util.hpp" 26 inline size_t FNVHash(
const char* text,
const size_t byteLength,
27 const size_t FNV_prime,
const size_t FNV_offset_basis) {
28 size_t hash = FNV_offset_basis;
29 for (
const char* pstr = text; pstr < text + byteLength; pstr++) {
36 template <
int>
size_t FNVHash(
const char* text,
const size_t byteLength);
39 inline size_t FNVHash<4>(
const char* text,
const size_t byteLength) {
40 return FNVHash(text, byteLength, 16777619UL, 2166136261UL);
44 inline size_t FNVHash<8>(
const char* text,
const size_t byteLength) {
45 return FNVHash(text, byteLength, 1099511628211UL, 14695981039346656037UL);
52 typedef LENGTH_TYPE LengthType;
56 byteLength(strlen(_str)) {}
59 : str(_str), utf8Length(_utf8Length) {
60 CalculateByteLength();
64 const LengthType _byteLength)
65 : str(_str), utf8Length(_utf8Length), byteLength(_byteLength) {
66 CalculateByteLength();
69 LengthType UTF8Length()
const {
return utf8Length; }
71 LengthType ByteLength()
const {
return byteLength; }
74 if (utf8Length == UTF8Length()) {
82 if (utf8Length == UTF8Length()) {
85 const char* pstr = str + byteLength;
86 for (
size_t i = 0; i < utf8Length; i++) {
94 const LengthType utf8Length)
const {
96 return Left(utf8Length);
98 const char* pstr = str;
99 for (
size_t i = 0; i < offset; i++) {
106 string ToString()
const {
return string(str, str + byteLength); }
108 const char* CString()
const {
return str; }
111 if (str == that.str) {
112 return std::min(utf8Length, that.utf8Length);
114 const char* pstr1 = str;
115 const char* pstr2 = that.str;
116 for (
size_t length = 0; length < utf8Length && length < that.utf8Length;
120 if (charLen1 != charLen2 || strncmp(pstr1, pstr2, charLen1) != 0) {
131 if (utf8Length > 0) {
135 byteLength -= charLen;
140 if (utf8Length > 0) {
143 byteLength -= charLen;
148 const char* pstr1 = str + byteLength;
149 const char* pstr2 = that.str + that.byteLength;
150 const size_t length = std::min(utf8Length, that.utf8Length);
151 for (
size_t i = 0; i < length; i++) {
156 const int cmp = strncmp(pstr1, pstr2, std::min(charLen1, charLen2));
159 }
else if (cmp > 0) {
161 }
else if (charLen1 < charLen2) {
163 }
else if (charLen1 > charLen2) {
167 if (utf8Length < that.utf8Length) {
169 }
else if (utf8Length > that.utf8Length) {
177 return static_cast<LengthType
>(
178 ToString().find(pattern.str, 0, pattern.byteLength));
182 return Compare(that) < 0;
186 return Compare(that) > 0;
190 return (str == that.str && utf8Length == that.utf8Length) ||
195 return !this->operator==(that);
201 return internal::FNVHash<sizeof(size_t)>(text.CString(),
208 int cmp = strncmp(str, that.str, std::min(byteLength, that.byteLength));
210 if (utf8Length < that.utf8Length) {
212 }
else if (utf8Length > that.utf8Length) {
221 void CalculateByteLength() {
222 const char* pstr = str;
223 for (
size_t i = 0; i < utf8Length; i++) {
226 byteLength = pstr - str;
230 LengthType utf8Length;
231 LengthType byteLength;
236 template <
typename LENGTH_TYPE>
237 std::ostream& operator<<(::std::ostream& os,
239 return os << str.ToString();
static size_t PrevCharLength(const char *str)
Returns the length in byte for the previous UTF8 character.
Definition: UTF8Util.hpp:71
Definition: UTF8StringSlice.hpp:198
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
static size_t NextCharLength(const char *str)
Returns the length in byte for the next UTF8 character.
Definition: UTF8Util.hpp:60
Definition: UTF8StringSlice.hpp:50