@brief Query the number of UTF-8 character bytes with the first byte. @param first_byte The first byte of a UTF-8 character. @return The number of UTF-8 character bytes.
| 1840 | /// @param first_byte The first byte of a UTF-8 character. |
| 1841 | /// @return The number of UTF-8 character bytes. |
| 1842 | inline uint32_t get_num_bytes(uint8_t first_byte) { |
| 1843 | // The first byte starts with 0b0XXX'XXXX -> 1-byte character |
| 1844 | if FK_YAML_LIKELY (first_byte < 0x80) { |
| 1845 | return 1; |
| 1846 | } |
| 1847 | // The first byte starts with 0b110X'XXXX -> 2-byte character |
| 1848 | if ((first_byte & 0xE0) == 0xC0) { |
| 1849 | return 2; |
| 1850 | } |
| 1851 | // The first byte starts with 0b1110'XXXX -> 3-byte character |
| 1852 | if ((first_byte & 0xF0) == 0xE0) { |
| 1853 | return 3; |
| 1854 | } |
| 1855 | // The first byte starts with 0b1111'0XXX -> 4-byte character |
| 1856 | if ((first_byte & 0xF8) == 0xF0) { |
| 1857 | return 4; |
| 1858 | } |
| 1859 | |
| 1860 | // The first byte starts with 0b10XX'XXXX or 0b1111'1XXX -> invalid |
| 1861 | throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", {first_byte}); |
| 1862 | } |
| 1863 | |
| 1864 | /// @brief Checks if `byte` is a valid 1-byte UTF-8 character. |
| 1865 | /// @param[in] byte The byte value. |
no test coverage detected