| 132 | } |
| 133 | |
| 134 | private static void WriteUtf8(WriteContext context, string value) |
| 135 | { |
| 136 | int maxByteLength = Encoding.UTF8.GetMaxByteCount(value.Length); |
| 137 | Span<byte> destination = context.Writer.GetSpan(MaxVarUInt36SmallBytes + maxByteLength); |
| 138 | Span<byte> payload = destination.Slice(MaxVarUInt36SmallBytes); |
| 139 | int written = Encoding.UTF8.GetBytes(value.AsSpan(), payload); |
| 140 | |
| 141 | ulong header = ((ulong)written << 2) | (ulong)ForyStringEncoding.Utf8; |
| 142 | Span<byte> headerBuf = stackalloc byte[MaxVarUInt36SmallBytes]; |
| 143 | int headerBytes = EncodeVarUInt36Small(headerBuf, header); |
| 144 | if (headerBytes != MaxVarUInt36SmallBytes) |
| 145 | { |
| 146 | payload.Slice(0, written).CopyTo(destination.Slice(headerBytes)); |
| 147 | } |
| 148 | |
| 149 | headerBuf.Slice(0, headerBytes).CopyTo(destination); |
| 150 | context.Writer.Advance(headerBytes + written); |
| 151 | } |
| 152 | |
| 153 | private static void WriteUtf16(WriteContext context, string value) |
| 154 | { |