| 57 | } |
| 58 | |
| 59 | public static string ReadString(ReadContext context) |
| 60 | { |
| 61 | ulong header = context.Reader.ReadVarUInt36Small(); |
| 62 | ulong encoding = header & 0x03; |
| 63 | int byteLength = checked((int)(header >> 2)); |
| 64 | ReadOnlySpan<byte> bytes = context.Reader.ReadSpan(byteLength); |
| 65 | return encoding switch |
| 66 | { |
| 67 | // C# intentionally preserves platform UTF-8 replacement behavior; Rust is the runtime |
| 68 | // that provides checked UTF-8 string reads by default. |
| 69 | (ulong)ForyStringEncoding.Utf8 => Encoding.UTF8.GetString(bytes), |
| 70 | (ulong)ForyStringEncoding.Latin1 => DecodeLatin1(bytes), |
| 71 | (ulong)ForyStringEncoding.Utf16 => DecodeUtf16(bytes), |
| 72 | _ => throw new EncodingException($"unsupported string encoding {encoding}"), |
| 73 | }; |
| 74 | } |
| 75 | |
| 76 | private static string DecodeLatin1(ReadOnlySpan<byte> bytes) |
| 77 | { |