/usr/include/opencc/UTF8StringSlice.hpp is in libopencc-dev 1.0.4-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | /*
* Open Chinese Convert
*
* Copyright 2015 BYVoid <byvoid@byvoid.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Common.hpp"
#include "UTF8Util.hpp"
namespace opencc {
namespace internal {
inline size_t FNVHash(const char* text, const size_t byteLength,
const size_t FNV_prime, const size_t FNV_offset_basis) {
size_t hash = FNV_offset_basis;
for (const char* pstr = text; pstr < text + byteLength; pstr++) {
hash ^= *pstr;
hash *= FNV_prime;
}
return hash;
}
template <int> size_t FNVHash(const char* text, const size_t byteLength);
template <>
inline size_t FNVHash<4>(const char* text, const size_t byteLength) {
return FNVHash(text, byteLength, 16777619UL, 2166136261UL);
}
template <>
inline size_t FNVHash<8>(const char* text, const size_t byteLength) {
return FNVHash(text, byteLength, 1099511628211UL, 14695981039346656037UL);
}
} // namespace internal
template <typename LENGTH_TYPE> class UTF8StringSliceBase {
public:
typedef LENGTH_TYPE LengthType;
UTF8StringSliceBase(const char* _str)
: str(_str), utf8Length(UTF8Util::Length(_str)),
byteLength(strlen(_str)) {}
UTF8StringSliceBase(const char* _str, const LengthType _utf8Length)
: str(_str), utf8Length(_utf8Length) {
CalculateByteLength();
}
UTF8StringSliceBase(const char* _str, const LengthType _utf8Length,
const LengthType _byteLength)
: str(_str), utf8Length(_utf8Length), byteLength(_byteLength) {
CalculateByteLength();
}
LengthType UTF8Length() const { return utf8Length; }
LengthType ByteLength() const { return byteLength; }
UTF8StringSliceBase Left(const LengthType utf8Length) const {
if (utf8Length == UTF8Length()) {
return *this;
} else {
return UTF8StringSliceBase(str, utf8Length);
}
}
UTF8StringSliceBase Right(const LengthType utf8Length) const {
if (utf8Length == UTF8Length()) {
return *this;
} else {
const char* pstr = str + byteLength;
for (size_t i = 0; i < utf8Length; i++) {
pstr = UTF8Util::PrevChar(pstr);
}
return UTF8StringSliceBase(pstr, utf8Length);
}
}
UTF8StringSliceBase SubString(const LengthType offset,
const LengthType utf8Length) const {
if (offset == 0) {
return Left(utf8Length);
} else {
const char* pstr = str;
for (size_t i = 0; i < offset; i++) {
pstr = UTF8Util::NextChar(pstr);
}
return UTF8StringSliceBase(pstr, utf8Length);
}
}
string ToString() const { return string(str, str + byteLength); }
const char* CString() const { return str; }
LengthType CommonPrefixLength(const UTF8StringSliceBase& that) const {
if (str == that.str) {
return std::min(utf8Length, that.utf8Length);
} else {
const char* pstr1 = str;
const char* pstr2 = that.str;
for (size_t length = 0; length < utf8Length && length < that.utf8Length;
length++) {
size_t charLen1 = UTF8Util::NextCharLength(pstr1);
size_t charLen2 = UTF8Util::NextCharLength(pstr2);
if (charLen1 != charLen2 || strncmp(pstr1, pstr2, charLen1) != 0) {
return length;
}
pstr1 += charLen1;
pstr2 += charLen2;
}
return 0;
}
}
void MoveRight() {
if (utf8Length > 0) {
const size_t charLen = UTF8Util::NextCharLength(str);
str += charLen;
utf8Length--;
byteLength -= charLen;
}
}
void MoveLeft() {
if (utf8Length > 0) {
const size_t charLen = UTF8Util::PrevCharLength(str + byteLength);
utf8Length--;
byteLength -= charLen;
}
}
int ReverseCompare(const UTF8StringSliceBase& that) const {
const char* pstr1 = str + byteLength;
const char* pstr2 = that.str + that.byteLength;
const size_t length = std::min(utf8Length, that.utf8Length);
for (size_t i = 0; i < length; i++) {
const size_t charLen1 = UTF8Util::PrevCharLength(pstr1);
const size_t charLen2 = UTF8Util::PrevCharLength(pstr2);
pstr1 -= charLen1;
pstr2 -= charLen2;
const int cmp = strncmp(pstr1, pstr2, std::min(charLen1, charLen2));
if (cmp < 0) {
return -1;
} else if (cmp > 0) {
return 1;
} else if (charLen1 < charLen2) {
return -1;
} else if (charLen1 > charLen2) {
return 1;
}
}
if (utf8Length < that.utf8Length) {
return -1;
} else if (utf8Length > that.utf8Length) {
return 1;
} else {
return 0;
}
}
LengthType FindBytePosition(const UTF8StringSliceBase& pattern) const {
return static_cast<LengthType>(
ToString().find(pattern.str, 0, pattern.byteLength));
}
bool operator<(const UTF8StringSliceBase& that) const {
return Compare(that) < 0;
}
bool operator>(const UTF8StringSliceBase& that) const {
return Compare(that) > 0;
}
bool operator==(const UTF8StringSliceBase& that) const {
return (str == that.str && utf8Length == that.utf8Length) ||
Compare(that) == 0;
}
bool operator!=(const UTF8StringSliceBase& that) const {
return !this->operator==(that);
}
class Hasher {
public:
size_t operator()(const UTF8StringSliceBase& text) const {
return internal::FNVHash<sizeof(size_t)>(text.CString(),
text.ByteLength());
}
};
private:
inline int Compare(const UTF8StringSliceBase& that) const {
int cmp = strncmp(str, that.str, std::min(byteLength, that.byteLength));
if (cmp == 0) {
if (utf8Length < that.utf8Length) {
cmp = -1;
} else if (utf8Length > that.utf8Length) {
cmp = 1;
} else {
cmp = 0;
}
}
return cmp;
}
void CalculateByteLength() {
const char* pstr = str;
for (size_t i = 0; i < utf8Length; i++) {
pstr = UTF8Util::NextChar(pstr);
}
byteLength = pstr - str;
}
const char* str;
LengthType utf8Length;
LengthType byteLength;
};
typedef UTF8StringSliceBase<size_t> UTF8StringSlice;
template <typename LENGTH_TYPE>
std::ostream& operator<<(::std::ostream& os,
const UTF8StringSliceBase<LENGTH_TYPE>& str) {
return os << str.ToString();
}
} // namespace opencc
|