Generate random UTF-8 string
| 239 | |
| 240 | // Generate random UTF-8 string |
| 241 | std::string generate_random_utf8_string(size_t length) { |
| 242 | std::string str; |
| 243 | std::mt19937 generator(std::random_device{}()); |
| 244 | std::uniform_int_distribution<uint32_t> distribution(0, 0x10FFFF); |
| 245 | |
| 246 | while (str.size() < length) { |
| 247 | uint32_t code_point = distribution(generator); |
| 248 | |
| 249 | // skip surrogate pairs (0xD800 to 0xDFFF) and other invalid Unicode code |
| 250 | // points |
| 251 | if ((code_point >= 0xD800 && code_point <= 0xDFFF) || |
| 252 | code_point > 0x10FFFF) { |
| 253 | continue; |
| 254 | } |
| 255 | |
| 256 | if (code_point <= 0x7F) { |
| 257 | str.push_back(static_cast<char>(code_point)); |
| 258 | } else if (code_point <= 0x7FF) { |
| 259 | str.push_back(0xC0 | (code_point >> 6)); |
| 260 | str.push_back(0x80 | (code_point & 0x3F)); |
| 261 | } else if (code_point <= 0xFFFF) { |
| 262 | str.push_back(0xE0 | (code_point >> 12)); |
| 263 | str.push_back(0x80 | ((code_point >> 6) & 0x3F)); |
| 264 | str.push_back(0x80 | (code_point & 0x3F)); |
| 265 | } else { |
| 266 | str.push_back(0xF0 | (code_point >> 18)); |
| 267 | str.push_back(0x80 | ((code_point >> 12) & 0x3F)); |
| 268 | str.push_back(0x80 | ((code_point >> 6) & 0x3F)); |
| 269 | str.push_back(0x80 | (code_point & 0x3F)); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | return str; |
| 274 | } |
| 275 | |
| 276 | // Testing Basic Logic |
| 277 | TEST(UTF8ToUTF16Test, BasicConversion) { |