Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/core/StarUnicode.cpp
blob: 01e660b436516c5cff2393d123a83bf52cc10e0d (plain)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include "StarUnicode.hpp"
#include "StarEncode.hpp"

namespace Star {

void throwInvalidUtf8Sequence() {
  throw UnicodeException("Invalid UTF-8 code unit sequence in utf8Length");
}

void throwMissingUtf8End() {
  throw UnicodeException("UTF-8 string missing trailing code units in utf8Length");
}

void throwInvalidUtf32CodePoint(Utf32Type val) {
  throw UnicodeException::format("Invalid UTF-32 code point {} encountered while trying to encode UTF-8", (int32_t)val);
}

size_t utf8Length(const Utf8Type* utf8, size_t remain) {
  bool stopOnNull = remain == NPos;
  size_t length = 0;

  while (true) {
    if (remain == 0)
      break;

    if (stopOnNull && utf8[0] == 0)
      break;

    if ((utf8[0] & 0x80) == 0x00) {
      ++length;
      ++utf8;
      --remain;
      continue;
    }

    if (remain == 1)
      throwMissingUtf8End();

    if ((utf8[0] & 0xe0) == 0xc0 && (utf8[1] & 0xc0) == 0x80) {
      if (((utf8[0] & 0x1fL) << 6) >= 0x00000080L) {
        ++length;
        utf8 += 2;
        remain -= 2;
        continue;
      } else {
        throwInvalidUtf8Sequence();
      }
    }

    if (remain == 2)
      throwMissingUtf8End();

    if ((utf8[0] & 0xf0) == 0xe0 && (utf8[1] & 0xc0) == 0x80 && (utf8[2] & 0xc0) == 0x80) {
      if ((((utf8[0] & 0x0fL) << 12) | ((utf8[1] & 0x3fL) << 6)) >= 0x00000800L) {
        ++length;
        utf8 += 3;
        remain -= 3;
        continue;
      } else {
        throwInvalidUtf8Sequence();
      }
    }

    if (remain == 3)
      throwMissingUtf8End();

    if ((utf8[0] & 0xf8) == 0xf0 && (utf8[1] & 0xc0) == 0x80 && (utf8[2] & 0xc0) == 0x80 && (utf8[3] & 0xc0) == 0x80) {
      if ((((utf8[0] & 0x07L) << 18) | ((utf8[1] & 0x3fL) << 12)) >= 0x00010000L) {
        ++length;
        utf8 += 4;
        remain -= 4;
        continue;
      } else {
        throwInvalidUtf8Sequence();
      }
    } else {
      throwInvalidUtf8Sequence();
    }
  }

  return length;
}

size_t utf8DecodeChar(const Utf8Type* utf8, Utf32Type* utf32, size_t remain) {
  const Utf8Type* start = utf8;
  bool stopOnNull = remain == NPos;

  while (true) {
    if (remain == 0)
      break;

    if (stopOnNull && utf8[0] == 0)
      break;

    if ((utf8[0] & 0x80) == 0x00) {
      *utf32 = utf8[0];
      return utf8 - start + 1;
    }

    if (remain == 1)
      throwMissingUtf8End();

    if ((utf8[0] & 0xe0) == 0xc0 && (utf8[1] & 0xc0) == 0x80) {
      *utf32 = ((utf8[0] & 0x1fL) << 6) | ((utf8[1] & 0x3fL) << 0);
      if (*utf32 >= 0x00000080L)
        return utf8 - start + 2;
      else
        throwInvalidUtf8Sequence();
    }

    if (remain == 2)
      throwMissingUtf8End();

    if ((utf8[0] & 0xf0) == 0xe0 && (utf8[1] & 0xc0) == 0x80 && (utf8[2] & 0xc0) == 0x80) {
      *utf32 = ((utf8[0] & 0x0fL) << 12) | ((utf8[1] & 0x3fL) << 6) | ((utf8[2] & 0x3fL) << 0);
      if (*utf32 >= 0x00000800L)
        return utf8 - start + 3;
      else
        throwInvalidUtf8Sequence();
    }

    if (remain == 3)
      throwMissingUtf8End();

    if ((utf8[0] & 0xf8) == 0xf0 && (utf8[1] & 0xc0) == 0x80 && (utf8[2] & 0xc0) == 0x80 && (utf8[3] & 0xc0) == 0x80) {
      *utf32 =
          ((utf8[0] & 0x07L) << 18) | ((utf8[1] & 0x3fL) << 12) | ((utf8[2] & 0x3fL) << 6) | ((utf8[3] & 0x3fL) << 0);
      if (*utf32 >= 0x00010000L)
        return utf8 - start + 4;
      else
        throwInvalidUtf8Sequence();
    } else {
      throwInvalidUtf8Sequence();
    }
  }

  return utf8 - start;
}

size_t utf8EncodeChar(Utf8Type* utf8, Utf32Type utf32, size_t len) {
  if (utf32 > 0x10FFFFu)
    throwInvalidUtf32CodePoint(utf32);

  if (utf32 <= 0x0000007fL) {
    if (len < 1)
      return 0;

    utf8[0] = utf32;
    return 1;
  } else if (utf32 <= 0x000007ffL) {
    if (len < 2)
      return 0;

    utf8[0] = 0xc0 | ((utf32 >> 6) & 0x1f);
    utf8[1] = 0x80 | ((utf32 >> 0) & 0x3f);

    return 2;
  } else if (utf32 <= 0x0000ffffL) {
    if (len < 3)
      return 0;

    utf8[0] = 0xe0 | ((utf32 >> 12) & 0x0f);
    utf8[1] = 0x80 | ((utf32 >> 6) & 0x3f);
    utf8[2] = 0x80 | ((utf32 >> 0) & 0x3f);

    return 3;
  } else {
    if (len < 4)
      return 0;

    utf8[0] = 0xf0 | ((utf32 >> 18) & 0x07);
    utf8[1] = 0x80 | ((utf32 >> 12) & 0x3f);
    utf8[2] = 0x80 | ((utf32 >> 6) & 0x3f);
    utf8[3] = 0x80 | ((utf32 >> 0) & 0x3f);

    return 4;
  }
}

static const char32_t MIN_LEAD = 0xd800;
static const char32_t MAX_LEAD = 0xdbff;
static const char32_t MIN_TRAIL = 0xdc00;
static const char32_t MAX_TRAIL = 0xdfff;
static const char32_t SURR_MASK = 0x3ff;
static const char32_t MIN_PAIR = 0x10000;
static const char32_t MAX_CODEPOINT = 0x10ffff;

Utf32Type hexStringToUtf32(std::string const& codepoint, Maybe<Utf32Type> previousCodepoint) {
  bool continuation = false;
  if (previousCodepoint && isUtf16LeadSurrogate(*previousCodepoint)) {
    continuation = true;
  }

  auto hexBytes = hexDecode(codepoint);
  if (hexBytes.size() < sizeof(Utf32Type)) {
    ByteArray newHexBytes{(size_t)(sizeof(Utf32Type) - hexBytes.size()), (char)'\0'};
    newHexBytes.append(hexBytes);
    hexBytes = newHexBytes;
  }

  if (hexBytes.size() > sizeof(Utf32Type))
    throw UnicodeException("Codepoint size is too big in parseUnicodeCodepoint");

  auto res = fromBigEndian(*(Utf32Type*)hexBytes.ptr());

  if (continuation) {
    res = utf32FromUtf16SurrogatePair(*previousCodepoint, res);
  }

  return res;
}

std::string hexStringFromUtf32(Utf32Type character) {
  if (character > MAX_CODEPOINT)
    throw UnicodeException("Codepoint too big in hexStringFromUtf32");
  Utf32Type lead;
  Maybe<Utf32Type> trail;
  tie(lead, trail) = utf32ToUtf16SurrogatePair(character);

  char16_t leadOut = toBigEndian((char16_t)lead);
  auto leadHex = hexEncode(reinterpret_cast<char*>(&leadOut), sizeof(leadOut)).takeUtf8();

  starAssert(leadHex.size() == 4);

  if (!trail)
    return leadHex;

  char16_t trailOut = toBigEndian((char16_t)*trail);
  auto trailHex = hexEncode(reinterpret_cast<char*>(&trailOut), sizeof(trailOut));

  starAssert(trailHex.size() == 4);

  return (leadHex + trailHex).takeUtf8();
}

bool isUtf16LeadSurrogate(Utf32Type codepoint) {
  return codepoint >= MIN_LEAD && codepoint <= MAX_LEAD;
}

bool isUtf16TrailSurrogate(Utf32Type codepoint) {
  return codepoint >= MIN_TRAIL && codepoint <= MAX_TRAIL;
}

Utf32Type utf32FromUtf16SurrogatePair(Utf32Type lead, Utf32Type trail) {
  if (!isUtf16LeadSurrogate(lead))
    throw UnicodeException("Invalid lead surrogate passed to utf32FromUtf16SurrogatePair");
  if (!isUtf16TrailSurrogate(trail))
    throw UnicodeException("Invalid trail surrogate passed to utf32FromUtf16SurrogatePair");

  lead -= MIN_LEAD;
  trail -= MIN_TRAIL;

  Utf32Type codepoint = (lead << 10) + trail + MIN_PAIR;

  return codepoint;
}

pair<Utf32Type, Maybe<Utf32Type>> utf32ToUtf16SurrogatePair(Utf32Type codepoint) {
  if (codepoint >= MIN_PAIR) {
    codepoint -= MIN_PAIR;
    Utf32Type lead = (codepoint >> 10) + MIN_LEAD;
    Utf32Type trail = (codepoint & SURR_MASK) + MIN_TRAIL;

    if (!isUtf16LeadSurrogate(lead))
      throw UnicodeException("Invalid codepoint passed to utf32ToUtf16SurrogatePair");

    return {lead, trail};
  }

  return {codepoint, {}};
}

}