MCPcopy Create free account
hub / github.com/bytedance/Fastbot_Android / FromUTF8

Function FromUTF8

native/thirdpart/flatbuffers/util.h:512–553  ·  view source on GitHub ↗

Converts whatever prefix of the incoming string corresponds to a valid UTF-8 sequence into a unicode code. The incoming pointer will have been advanced past all bytes parsed. returns -1 upon corrupt UTF-8 encoding (ignore the incoming pointer in this case).

Source from the content-addressed store, hash-verified

510// returns -1 upon corrupt UTF-8 encoding (ignore the incoming pointer in
511// this case).
512inline int FromUTF8(const char **in) {
513 int len = 0;
514 // Count leading 1 bits.
515 for (int mask = 0x80; mask >= 0x04; mask >>= 1) {
516 if (**in & mask) {
517 len++;
518 } else {
519 break;
520 }
521 }
522 if ((static_cast<unsigned char>(**in) << len) & 0x80)
523 return -1; // Bit after leading 1's must be 0.
524 if (!len) return *(*in)++;
525 // UTF-8 encoded values with a length are between 2 and 4 bytes.
526 if (len < 2 || len > 4) { return -1; }
527 // Grab initial bits of the code.
528 int ucc = *(*in)++ & ((1 << (7 - len)) - 1);
529 for (int i = 0; i < len - 1; i++) {
530 if ((**in & 0xC0) != 0x80) return -1; // Upper bits must 1 0.
531 ucc <<= 6;
532 ucc |= *(*in)++ & 0x3F; // Grab 6 more bits of the code.
533 }
534 // UTF-8 cannot encode values between 0xD800 and 0xDFFF (reserved for
535 // UTF-16 surrogate pairs).
536 if (ucc >= 0xD800 && ucc <= 0xDFFF) { return -1; }
537 // UTF-8 must represent code points in their shortest possible encoding.
538 switch (len) {
539 case 2:
540 // Two bytes of UTF-8 can represent code points from U+0080 to U+07FF.
541 if (ucc < 0x0080 || ucc > 0x07FF) { return -1; }
542 break;
543 case 3:
544 // Three bytes of UTF-8 can represent code points from U+0800 to U+FFFF.
545 if (ucc < 0x0800 || ucc > 0xFFFF) { return -1; }
546 break;
547 case 4:
548 // Four bytes of UTF-8 can represent code points from U+10000 to U+10FFFF.
549 if (ucc < 0x10000 || ucc > 0x10FFFF) { return -1; }
550 break;
551 }
552 return ucc;
553}
554
555#ifndef FLATBUFFERS_PREFER_PRINTF
556// Wraps a string to a maximum length, inserting new lines where necessary. Any

Callers 2

ValidateUTF8Function · 0.85
EscapeStringFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected