| 1 |
/* |
| 2 |
Copyright 2015 Google Inc. All rights reserved. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
#ifndef JSONNET_UNICODE_H |
| 18 |
#define JSONNET_UNICODE_H |
| 19 |
|
| 20 |
/** Substituted when a unicode translation format encoding error is encountered. */ |
| 21 |
#define JSONNET_CODEPOINT_ERROR 0xfffd |
| 22 |
#define JSONNET_CODEPOINT_MAX 0x110000 |
| 23 |
|
| 24 |
namespace jsonnet::internal { |
| 25 |
|
| 26 |
/** Convert a unicode codepoint to UTF8. |
| 27 |
* |
| 28 |
* \param x The unicode codepoint. |
| 29 |
* \param s The UTF-8 string to append to. |
| 30 |
* \returns The number of characters appended. |
| 31 |
*/ |
| 32 |
static inline int encode_utf8(char32_t x, std::string &s) |
| 33 |
{ |
| 34 |
if (x >= JSONNET_CODEPOINT_MAX) |
| 35 |
x = JSONNET_CODEPOINT_ERROR; |
| 36 |
|
| 37 |
// 00ZZZzzz 00zzYYYY 00Yyyyxx 00xxxxxx |
| 38 |
long bytes = ((x & 0x1C0000) << 6) | ((x & 0x03F000) << 4) | ((x & 0x0FC0) << 2) | (x & 0x3F); |
| 39 |
|
| 40 |
if (x < 0x80) { |
| 41 |
s.push_back((char)x); |
| 42 |
return 1; |
| 43 |
} else if (x < 0x800) { // note that capital 'Y' bits must be 0 |
| 44 |
bytes |= 0xC080; |
| 45 |
s.push_back((bytes >> 8) & 0xFF); |
| 46 |
s.push_back((bytes >> 0) & 0xFF); |
| 47 |
return 2; |
| 48 |
} else if (x < 0x10000) { // note that 'z' bits must be 0 |
| 49 |
bytes |= 0xE08080; |
| 50 |
s.push_back((bytes >> 16) & 0xFF); |
| 51 |
s.push_back((bytes >> 8) & 0xFF); |
| 52 |
s.push_back((bytes >> 0) & 0xFF); |
| 53 |
return 3; |
| 54 |
} else if (x < 0x110000) { // note that capital 'Z' bits must be 0 |
| 55 |
bytes |= 0xF0808080; |
| 56 |
s.push_back((bytes >> 24) & 0xFF); |
| 57 |
s.push_back((bytes >> 16) & 0xFF); |
| 58 |
s.push_back((bytes >> 8) & 0xFF); |
| 59 |
s.push_back((bytes >> 0) & 0xFF); |
| 60 |
return 4; |
| 61 |
} else { |
| 62 |
std::cerr << "Should never get here." << std::endl; |
| 63 |
abort(); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/** Convert the UTF8 byte sequence in the given string to a unicode code point. |
| 68 |
* |
| 69 |
* \param str The string. |
| 70 |
* \param i The index of the string from which to start decoding and returns the index of the last |
| 71 |
* byte of the encoded codepoint. |
| 72 |
* \returns The decoded unicode codepoint. |
| 73 |
*/ |
| 74 |
static inline char32_t decode_utf8(const std::string &str, size_t &i) |
| 75 |
{ |
| 76 |
char c0 = str[i]; |
| 77 |
if ((c0 & 0x80) == 0) { // 0xxxxxxx |
| 78 |
return c0; |
| 79 |
} else if ((c0 & 0xE0) == 0xC0) { // 110yyyxx 10xxxxxx |
| 80 |
if (i + 1 >= str.length()) { |
| 81 |
return JSONNET_CODEPOINT_ERROR; |
| 82 |
} |
| 83 |
char c1 = str[++i]; |
| 84 |
if ((c1 & 0xC0) != 0x80) { |
| 85 |
return JSONNET_CODEPOINT_ERROR; |
| 86 |
} |
| 87 |
return ((c0 & 0x1F) << 6ul) | (c1 & 0x3F); |
| 88 |
} else if ((c0 & 0xF0) == 0xE0) { // 1110yyyy 10yyyyxx 10xxxxxx |
| 89 |
if (i + 2 >= str.length()) { |
| 90 |
return JSONNET_CODEPOINT_ERROR; |
| 91 |
} |
| 92 |
char c1 = str[++i]; |
| 93 |
if ((c1 & 0xC0) != 0x80) { |
| 94 |
return JSONNET_CODEPOINT_ERROR; |
| 95 |
} |
| 96 |
char c2 = str[++i]; |
| 97 |
if ((c2 & 0xC0) != 0x80) { |
| 98 |
return JSONNET_CODEPOINT_ERROR; |
| 99 |
} |
| 100 |
return ((c0 & 0xF) << 12ul) | ((c1 & 0x3F) << 6) | (c2 & 0x3F); |
| 101 |
} else if ((c0 & 0xF8) == 0xF0) { // 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx |
| 102 |
if (i + 3 >= str.length()) { |
| 103 |
return JSONNET_CODEPOINT_ERROR; |
| 104 |
} |
| 105 |
char c1 = str[++i]; |
| 106 |
if ((c1 & 0xC0) != 0x80) { |
| 107 |
return JSONNET_CODEPOINT_ERROR; |
| 108 |
} |
| 109 |
char c2 = str[++i]; |
| 110 |
if ((c2 & 0xC0) != 0x80) { |
| 111 |
return JSONNET_CODEPOINT_ERROR; |
| 112 |
} |
| 113 |
char c3 = str[++i]; |
| 114 |
if ((c3 & 0xC0) != 0x80) { |
| 115 |
return JSONNET_CODEPOINT_ERROR; |
| 116 |
} |
| 117 |
return ((c0 & 0x7) << 18ul) | ((c1 & 0x3F) << 12ul) | ((c2 & 0x3F) << 6) | (c3 & 0x3F); |
| 118 |
} else { |
| 119 |
return JSONNET_CODEPOINT_ERROR; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
/** A string class capable of holding unicode codepoints. */ |
| 124 |
typedef std::basic_string<char32_t> UString; |
| 125 |
|
| 126 |
static inline void encode_utf8(const UString &s, std::string &r) |
| 127 |
{ |
| 128 |
for (char32_t cp : s) |
| 129 |
encode_utf8(cp, r); |
| 130 |
} |
| 131 |
|
| 132 |
static inline std::string encode_utf8(const UString &s) |
| 133 |
{ |
| 134 |
std::string r; |
| 135 |
encode_utf8(s, r); |
| 136 |
return r; |
| 137 |
} |
| 138 |
|
| 139 |
static inline UString decode_utf8(const std::string &s) |
| 140 |
{ |
| 141 |
UString r; |
| 142 |
for (size_t i = 0; i < s.length(); ++i) |
| 143 |
r.push_back(decode_utf8(s, i)); |
| 144 |
return r; |
| 145 |
} |
| 146 |
|
| 147 |
/** A stringstream-like class capable of holding unicode codepoints. |
| 148 |
* The C++ standard does not support std::basic_stringstream<char32_t. |
| 149 |
*/ |
| 150 |
class UStringStream { |
| 151 |
UString buf; |
| 152 |
|
| 153 |
public: |
| 154 |
UStringStream &operator<<(const UString &s) |
| 155 |
{ |
| 156 |
buf.append(s); |
| 157 |
return *this; |
| 158 |
} |
| 159 |
UStringStream &operator<<(const char32_t *s) |
| 160 |
{ |
| 161 |
buf.append(s); |
| 162 |
return *this; |
| 163 |
} |
| 164 |
UStringStream &operator<<(char32_t c) |
| 165 |
{ |
| 166 |
buf.push_back(c); |
| 167 |
return *this; |
| 168 |
} |
| 169 |
template <class T> |
| 170 |
UStringStream &operator<<(T c) |
| 171 |
{ |
| 172 |
std::stringstream ss; |
| 173 |
ss << c; |
| 174 |
for (char c : ss.str()) |
| 175 |
buf.push_back(char32_t(c)); |
| 176 |
return *this; |
| 177 |
} |
| 178 |
UString str() |
| 179 |
{ |
| 180 |
return buf; |
| 181 |
} |
| 182 |
}; |
| 183 |
|
| 184 |
} // namespace jsonnet::internal |
| 185 |
|
| 186 |
#endif // JSONNET_UNICODE_H |