| 675 | } |
| 676 | |
| 677 | size_t validate_utf8(const std::string& text) { |
| 678 | size_t len = text.size(); |
| 679 | if (len == 0) return 0; |
| 680 | |
| 681 | // Check the last few bytes to see if a multi-byte character is cut off |
| 682 | for (size_t i = 1; i <= 4 && i <= len; ++i) { |
| 683 | unsigned char c = text[len - i]; |
| 684 | // Check for start of a multi-byte sequence from the end |
| 685 | if ((c & 0xE0) == 0xC0) { |
| 686 | // 2-byte character start: 110xxxxx |
| 687 | // Needs at least 2 bytes |
| 688 | if (i < 2) return len - i; |
| 689 | } else if ((c & 0xF0) == 0xE0) { |
| 690 | // 3-byte character start: 1110xxxx |
| 691 | // Needs at least 3 bytes |
| 692 | if (i < 3) return len - i; |
| 693 | } else if ((c & 0xF8) == 0xF0) { |
| 694 | // 4-byte character start: 11110xxx |
| 695 | // Needs at least 4 bytes |
| 696 | if (i < 4) return len - i; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | // If no cut-off multi-byte character is found, return full length |
| 701 | return len; |
| 702 | } |
| 703 | |
| 704 | // Computes FNV-1a hash of the data |
| 705 | static std::string fnv_hash(const uint8_t * data, size_t len) { |
no test coverage detected