Read string data and return as std::u16string
| 158 | |
| 159 | /// Read string data and return as std::u16string |
| 160 | inline std::u16string read_u16string_data(ReadContext &ctx) { |
| 161 | // Read size with encoding using varuint36small |
| 162 | uint64_t size_with_encoding = ctx.read_var_uint36_small(ctx.error()); |
| 163 | |
| 164 | // Extract size and encoding from lower 2 bits |
| 165 | uint64_t length = size_with_encoding >> 2; |
| 166 | StringEncoding encoding = |
| 167 | static_cast<StringEncoding>(size_with_encoding & 0x3); |
| 168 | |
| 169 | if (length == 0) { |
| 170 | return std::u16string(); |
| 171 | } |
| 172 | |
| 173 | if (FORY_PREDICT_FALSE(length > std::numeric_limits<uint32_t>::max())) { |
| 174 | ctx.set_error(Error::invalid_data("String length exceeds uint32 range")); |
| 175 | return std::u16string(); |
| 176 | } |
| 177 | const uint32_t length_u32 = static_cast<uint32_t>(length); |
| 178 | |
| 179 | if (FORY_PREDICT_FALSE( |
| 180 | !ctx.buffer().ensure_readable(length_u32, ctx.error()))) { |
| 181 | return std::u16string(); |
| 182 | } |
| 183 | |
| 184 | Buffer &buffer = ctx.buffer(); |
| 185 | const uint8_t *data = buffer.data() + buffer.reader_index(); |
| 186 | |
| 187 | // Handle different encodings |
| 188 | switch (encoding) { |
| 189 | case StringEncoding::LATIN1: { |
| 190 | // Latin1 bytes map directly to char16_t (codepoints 0-255) |
| 191 | std::u16string result(length_u32, u'\0'); |
| 192 | for (size_t i = 0; i < length_u32; ++i) { |
| 193 | result[i] = static_cast<char16_t>(data[i]); |
| 194 | } |
| 195 | buffer.unsafe_increase_reader_index(length_u32); |
| 196 | return result; |
| 197 | } |
| 198 | case StringEncoding::UTF16: { |
| 199 | if (FORY_PREDICT_FALSE((length_u32 & 1) != 0)) { |
| 200 | ctx.set_error(Error::invalid_data("UTF-16 length must be even")); |
| 201 | return std::u16string(); |
| 202 | } |
| 203 | std::u16string result(length_u32 / 2, u'\0'); |
| 204 | std::memcpy(&result[0], data, length_u32); |
| 205 | buffer.unsafe_increase_reader_index(length_u32); |
| 206 | return result; |
| 207 | } |
| 208 | case StringEncoding::UTF8: { |
| 209 | // Read UTF-8 bytes and convert to UTF-16 |
| 210 | std::string utf8(length_u32, '\0'); |
| 211 | std::memcpy(&utf8[0], data, length_u32); |
| 212 | buffer.unsafe_increase_reader_index(length_u32); |
| 213 | return utf8_to_utf16(utf8, true /* little endian */); |
| 214 | } |
| 215 | default: |
| 216 | ctx.set_error( |
| 217 | Error::encoding_error("Unknown string encoding: " + |