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
|
/*
Copyright 2015 Google Inc. All rights reserved.
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.
*/
#ifndef JSONNET_UNICODE_H
#define JSONNET_UNICODE_H
/** Substituted when a unicode translation format encoding error is encountered. */
#define JSONNET_CODEPOINT_ERROR 0xfffd
#define JSONNET_CODEPOINT_MAX 0x110000
namespace jsonnet::internal {
/** Convert a unicode codepoint to UTF8.
*
* \param x The unicode codepoint.
* \param s The UTF-8 string to append to.
* \returns The number of characters appended.
*/
static inline int encode_utf8(char32_t x, std::string &s)
{
if (x >= JSONNET_CODEPOINT_MAX)
x = JSONNET_CODEPOINT_ERROR;
// 00ZZZzzz 00zzYYYY 00Yyyyxx 00xxxxxx
long bytes = ((x & 0x1C0000) << 6) | ((x & 0x03F000) << 4) | ((x & 0x0FC0) << 2) | (x & 0x3F);
if (x < 0x80) {
s.push_back((char)x);
return 1;
} else if (x < 0x800) { // note that capital 'Y' bits must be 0
bytes |= 0xC080;
s.push_back((bytes >> 8) & 0xFF);
s.push_back((bytes >> 0) & 0xFF);
return 2;
} else if (x < 0x10000) { // note that 'z' bits must be 0
bytes |= 0xE08080;
s.push_back((bytes >> 16) & 0xFF);
s.push_back((bytes >> 8) & 0xFF);
s.push_back((bytes >> 0) & 0xFF);
return 3;
} else if (x < 0x110000) { // note that capital 'Z' bits must be 0
bytes |= 0xF0808080;
s.push_back((bytes >> 24) & 0xFF);
s.push_back((bytes >> 16) & 0xFF);
s.push_back((bytes >> 8) & 0xFF);
s.push_back((bytes >> 0) & 0xFF);
return 4;
} else {
std::cerr << "Should never get here." << std::endl;
abort();
}
}
/** Convert the UTF8 byte sequence in the given string to a unicode code point.
*
* \param str The string.
* \param i The index of the string from which to start decoding and returns the index of the last
* byte of the encoded codepoint.
* \returns The decoded unicode codepoint.
*/
static inline char32_t decode_utf8(const std::string &str, size_t &i)
{
char c0 = str[i];
if ((c0 & 0x80) == 0) { // 0xxxxxxx
return c0;
} else if ((c0 & 0xE0) == 0xC0) { // 110yyyxx 10xxxxxx
if (i + 1 >= str.length()) {
return JSONNET_CODEPOINT_ERROR;
}
char c1 = str[++i];
if ((c1 & 0xC0) != 0x80) {
return JSONNET_CODEPOINT_ERROR;
}
return ((c0 & 0x1F) << 6ul) | (c1 & 0x3F);
} else if ((c0 & 0xF0) == 0xE0) { // 1110yyyy 10yyyyxx 10xxxxxx
if (i + 2 >= str.length()) {
return JSONNET_CODEPOINT_ERROR;
}
char c1 = str[++i];
if ((c1 & 0xC0) != 0x80) {
return JSONNET_CODEPOINT_ERROR;
}
char c2 = str[++i];
if ((c2 & 0xC0) != 0x80) {
return JSONNET_CODEPOINT_ERROR;
}
return ((c0 & 0xF) << 12ul) | ((c1 & 0x3F) << 6) | (c2 & 0x3F);
} else if ((c0 & 0xF8) == 0xF0) { // 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx
if (i + 3 >= str.length()) {
return JSONNET_CODEPOINT_ERROR;
}
char c1 = str[++i];
if ((c1 & 0xC0) != 0x80) {
return JSONNET_CODEPOINT_ERROR;
}
char c2 = str[++i];
if ((c2 & 0xC0) != 0x80) {
return JSONNET_CODEPOINT_ERROR;
}
char c3 = str[++i];
if ((c3 & 0xC0) != 0x80) {
return JSONNET_CODEPOINT_ERROR;
}
return ((c0 & 0x7) << 24ul) | ((c1 & 0x3F) << 12ul) | ((c2 & 0x3F) << 6) | (c3 & 0x3F);
} else {
return JSONNET_CODEPOINT_ERROR;
}
}
/** A string class capable of holding unicode codepoints. */
typedef std::basic_string<char32_t> UString;
static inline void encode_utf8(const UString &s, std::string &r)
{
for (char32_t cp : s)
encode_utf8(cp, r);
}
static inline std::string encode_utf8(const UString &s)
{
std::string r;
encode_utf8(s, r);
return r;
}
static inline UString decode_utf8(const std::string &s)
{
UString r;
for (size_t i = 0; i < s.length(); ++i)
r.push_back(decode_utf8(s, i));
return r;
}
/** A stringstream-like class capable of holding unicode codepoints.
* The C++ standard does not support std::basic_stringstream<char32_t.
*/
class UStringStream {
UString buf;
public:
UStringStream &operator<<(const UString &s)
{
buf.append(s);
return *this;
}
UStringStream &operator<<(const char32_t *s)
{
buf.append(s);
return *this;
}
UStringStream &operator<<(char32_t c)
{
buf.push_back(c);
return *this;
}
template <class T>
UStringStream &operator<<(T c)
{
std::stringstream ss;
ss << c;
for (char c : ss.str())
buf.push_back(char32_t(c));
return *this;
}
UString str()
{
return buf;
}
};
} // namespace jsonnet::internal
#endif // JSONNET_UNICODE_H
|