MCPcopy Create free account
hub / github.com/crawl/crawl / utf8_read_char

Function utf8_read_char

crawl-ref/source/json.cc:239–275  ·  view source on GitHub ↗

* Read a single UTF-8 character starting at @s, * returning the length, in bytes, of the character read. * * This function assumes input is valid UTF-8, * and that there are enough characters in front of @s. */

Source from the content-addressed store, hash-verified

237 * and that there are enough characters in front of @s.
238 */
239static int utf8_read_char(const char *s, uchar_t *out)
240{
241 const unsigned char *c = (const unsigned char*) s;
242
243 ASSERT(utf8_validate_cz(s));
244
245 if (c[0] <= 0x7F)
246 {
247 /* 00..7F */
248 *out = c[0];
249 return 1;
250 }
251 else if (c[0] <= 0xDF)
252 {
253 /* C2..DF (unless input is invalid) */
254 *out = ((uchar_t)c[0] & 0x1F) << 6 |
255 ((uchar_t)c[1] & 0x3F);
256 return 2;
257 }
258 else if (c[0] <= 0xEF)
259 {
260 /* E0..EF */
261 *out = ((uchar_t)c[0] & 0xF) << 12 |
262 ((uchar_t)c[1] & 0x3F) << 6 |
263 ((uchar_t)c[2] & 0x3F);
264 return 3;
265 }
266 else
267 {
268 /* F0..F4 (unless input is invalid) */
269 *out = ((uchar_t)c[0] & 0x7) << 18 |
270 ((uchar_t)c[1] & 0x3F) << 12 |
271 ((uchar_t)c[2] & 0x3F) << 6 |
272 ((uchar_t)c[3] & 0x3F);
273 return 4;
274 }
275}
276
277/*
278 * Write a single UTF-8 character to @s,

Callers 1

emit_stringFunction · 0.85

Calls 1

utf8_validate_czFunction · 0.85

Tested by

no test coverage detected