| 52 | } // namespace internal |
| 53 | |
| 54 | template <typename LENGTH_TYPE> class UTF8StringSliceBase { |
| 55 | public: |
| 56 | typedef LENGTH_TYPE LengthType; |
| 57 | |
| 58 | UTF8StringSliceBase(const char* _str) |
| 59 | : str(_str), utf8Length(static_cast<LengthType>(UTF8Util::Length(_str))), |
| 60 | byteLength(static_cast<LengthType>(strlen(_str))) {} |
| 61 | |
| 62 | UTF8StringSliceBase(const char* _str, const LengthType _utf8Length) |
| 63 | : str(_str), utf8Length(_utf8Length) { |
| 64 | CalculateByteLength(); |
| 65 | } |
| 66 | |
| 67 | UTF8StringSliceBase(const char* _str, const LengthType _utf8Length, |
| 68 | const LengthType _byteLength) |
| 69 | : str(_str), utf8Length(_utf8Length), byteLength(_byteLength) { |
| 70 | CalculateByteLength(); |
| 71 | } |
| 72 | |
| 73 | LengthType UTF8Length() const { return utf8Length; } |
| 74 | |
| 75 | LengthType ByteLength() const { return byteLength; } |
| 76 | |
| 77 | UTF8StringSliceBase Left(const LengthType numberOfCharacters) const { |
| 78 | if (numberOfCharacters == UTF8Length()) { |
| 79 | return *this; |
| 80 | } else { |
| 81 | return UTF8StringSliceBase(str, numberOfCharacters); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | UTF8StringSliceBase Right(const LengthType numberOfCharacters) const { |
| 86 | if (numberOfCharacters == UTF8Length()) { |
| 87 | return *this; |
| 88 | } else { |
| 89 | const char* pstr = str + byteLength; |
| 90 | for (size_t i = 0; i < numberOfCharacters; i++) { |
| 91 | pstr = UTF8Util::PrevChar(pstr); |
| 92 | } |
| 93 | return UTF8StringSliceBase(pstr, numberOfCharacters); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | UTF8StringSliceBase SubString(const LengthType offset, |
| 98 | const LengthType numberOfCharacters) const { |
| 99 | if (offset == 0) { |
| 100 | return Left(numberOfCharacters); |
| 101 | } else { |
| 102 | const char* pstr = str; |
| 103 | for (size_t i = 0; i < offset; i++) { |
| 104 | pstr = UTF8Util::NextChar(pstr); |
| 105 | } |
| 106 | return UTF8StringSliceBase(pstr, numberOfCharacters); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | std::string ToString() const { return std::string(str, str + byteLength); } |
| 111 | |