Generate random UTF-16 string ensuring valid surrogate pairs
| 147 | |
| 148 | // Generate random UTF-16 string ensuring valid surrogate pairs |
| 149 | std::u16string generate_random_utf16_string(size_t length) { |
| 150 | std::u16string str; |
| 151 | std::mt19937 generator(std::random_device{}()); |
| 152 | std::uniform_int_distribution<uint32_t> distribution(0, 0x10FFFF); |
| 153 | |
| 154 | while (str.size() < length) { |
| 155 | uint32_t code_point = distribution(generator); |
| 156 | |
| 157 | if (code_point <= 0xD7FF || |
| 158 | (code_point >= 0xE000 && code_point <= 0xFFFF)) { |
| 159 | str.push_back(static_cast<char16_t>(code_point)); |
| 160 | } else if (code_point >= 0x10000 && code_point <= 0x10FFFF) { |
| 161 | code_point -= 0x10000; |
| 162 | str.push_back(static_cast<char16_t>((code_point >> 10) + 0xD800)); |
| 163 | str.push_back(static_cast<char16_t>((code_point & 0x3FF) + 0xDC00)); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | return str; |
| 168 | } |
| 169 | |
| 170 | TEST(StringUtilTest, TestUtf16HasSurrogatePairs) { |
| 171 | EXPECT_FALSE(utf16_has_surrogate_pairs(std::u16string({0x99, 0x100}))); |