MCPcopy Create free account
hub / github.com/apache/fory / generate_random_utf16_string

Function generate_random_utf16_string

cpp/fory/util/string_util_test.cc:149–168  ·  view source on GitHub ↗

Generate random UTF-16 string ensuring valid surrogate pairs

Source from the content-addressed store, hash-verified

147
148// Generate random UTF-16 string ensuring valid surrogate pairs
149std::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
170TEST(StringUtilTest, TestUtf16HasSurrogatePairs) {
171 EXPECT_FALSE(utf16_has_surrogate_pairs(std::u16string({0x99, 0x100})));

Callers 1

TESTFunction · 0.85

Calls 1

sizeMethod · 0.45

Tested by

no test coverage detected