public static bool TryWrite(ref ResizableMemory <byte> writer, int value, StandardFormat standardFormat) { if (standardFormat.IsDefault) { if (value <= byte.MaxValue && value >= byte.MinValue) { return(TryWrite(ref writer, (byte)value, standardFormat)); } writer.Push((byte)EtfTokenType.Integer); BinaryPrimitives.WriteInt32BigEndian(writer.RequestSpan(4), value); writer.Advance(4); } else { int start = writer.Length; writer.Push((byte)EtfTokenType.Binary); writer.Advance(4); if (!Utf8Writer.TryWrite(ref writer, value, standardFormat)) { return(false); } int length = writer.Length - start - 5; writer.Array[start + 1] = (byte)(length >> 24); writer.Array[start + 2] = (byte)(length >> 16); writer.Array[start + 3] = (byte)(length >> 8); writer.Array[start + 4] = (byte)length; } return(true); }
public static bool TryWrite(ref ResizableMemory <byte> writer, bool value, StandardFormat standardFormat) { if (standardFormat.IsDefault) { if (value) { _trueValue.Span.CopyTo(writer.RequestSpan(6)); writer.Advance(6); } else { _falseValue.Span.CopyTo(writer.RequestSpan(7)); writer.Advance(7); } } else { int start = writer.Length; writer.Push((byte)EtfTokenType.Binary); writer.Advance(4); if (!Utf8Writer.TryWrite(ref writer, value, standardFormat)) { return(false); } int length = writer.Length - start - 5; writer.Array[start + 1] = (byte)(length >> 24); writer.Array[start + 2] = (byte)(length >> 16); writer.Array[start + 3] = (byte)(length >> 8); writer.Array[start + 4] = (byte)length; } return(true); }
public static bool TryWrite(ref ResizableMemory <byte> writer, decimal value, StandardFormat standardFormat = default) { writer.Push((byte)'"'); if (!Utf8Writer.TryWrite(ref writer, value, standardFormat)) { return(false); } writer.Push((byte)'"'); return(true); }
public static bool TryWrite(ref ResizableMemory <byte> writer, DateTimeOffset value, StandardFormat standardFormat) { writer.Push((byte)'"'); if (!Utf8Writer.TryWrite(ref writer, value, standardFormat)) { return(false); } writer.Push((byte)'"'); return(true); }
public static bool TryWrite(ref ResizableMemory <byte> writer, long value, StandardFormat standardFormat) { if (standardFormat.IsDefault) { if (value <= byte.MaxValue && value >= byte.MinValue) { return(TryWrite(ref writer, (byte)value, standardFormat)); } if (value <= int.MaxValue && value >= int.MinValue) { return(TryWrite(ref writer, (int)value, standardFormat)); } writer.Push((byte)EtfTokenType.SmallBig); writer.Push(8); if (value >= 0) { writer.Push(0); BinaryPrimitives.WriteUInt64LittleEndian(writer.RequestSpan(8), (ulong)value); } else { writer.Push(1); if (value == long.MinValue) { BinaryPrimitives.WriteUInt64LittleEndian(writer.RequestSpan(8), 9223372036854775808); } else { BinaryPrimitives.WriteUInt64LittleEndian(writer.RequestSpan(8), (ulong)-value); } } writer.Advance(8); } else { int start = writer.Length; writer.Push((byte)EtfTokenType.Binary); writer.Advance(4); if (!Utf8Writer.TryWrite(ref writer, value, standardFormat)) { return(false); } int length = writer.Length - start - 5; writer.Array[start + 1] = (byte)(length >> 24); writer.Array[start + 2] = (byte)(length >> 16); writer.Array[start + 3] = (byte)(length >> 8); writer.Array[start + 4] = (byte)length; } return(true); }
public static bool TryWrite(ref ResizableMemory <byte> writer, DateTime value, StandardFormat standardFormat) { int start = writer.Length; writer.Push((byte)EtfTokenType.Binary); writer.Advance(4); if (!Utf8Writer.TryWrite(ref writer, value, standardFormat)) { return(false); } int length = writer.Length - start - 5; writer.Array[start + 1] = (byte)(length >> 24); writer.Array[start + 2] = (byte)(length >> 16); writer.Array[start + 3] = (byte)(length >> 8); writer.Array[start + 4] = (byte)length; return(true); }
public static bool TryWrite(ref ResizableMemory <byte> writer, float value, StandardFormat standardFormat = default) { if (standardFormat.IsDefault && !float.IsInfinity(value) && !float.IsNaN(value)) { if (!Utf8Writer.TryWrite(ref writer, value, JsonSerializer.FloatFormat.Symbol)) { return(false); } } else { writer.Push((byte)'"'); if (!Utf8Writer.TryWrite(ref writer, value, !standardFormat.IsDefault ? standardFormat : JsonSerializer.FloatFormat.Symbol)) { return(false); } writer.Push((byte)'"'); } return(true); }
public static bool TryWrite(ref ResizableMemory <byte> writer, bool value, StandardFormat standardFormat = default) { if (standardFormat.IsDefault) { if (!Utf8Writer.TryWrite(ref writer, value, JsonSerializer.BooleanFormat.Symbol)) { return(false); } } else { writer.Push((byte)'"'); if (!Utf8Writer.TryWrite(ref writer, value, standardFormat)) { return(false); } writer.Push((byte)'"'); } return(true); }
public static bool TryWrite(ref ResizableMemory <byte> writer, float value, StandardFormat standardFormat) { // TODO: Untested, does Discord have any endpoints that accept floats? if (standardFormat.IsDefault) { writer.Push((byte)EtfTokenType.NewFloat); // Swap endian Span <double> src = stackalloc double[] { value }; var srcBytes = MemoryMarshal.AsBytes(src); var dst = writer.RequestSpan(8); dst[0] = srcBytes[7]; dst[1] = srcBytes[6]; dst[2] = srcBytes[5]; dst[3] = srcBytes[4]; dst[4] = srcBytes[3]; dst[5] = srcBytes[2]; dst[6] = srcBytes[1]; dst[7] = srcBytes[0]; writer.Advance(8); } else { int start = writer.Length; writer.Push((byte)EtfTokenType.Binary); writer.Advance(4); if (!Utf8Writer.TryWrite(ref writer, value, standardFormat)) { return(false); } int length = writer.Length - start - 5; writer.Array[start + 1] = (byte)(length >> 24); writer.Array[start + 2] = (byte)(length >> 16); writer.Array[start + 3] = (byte)(length >> 8); writer.Array[start + 4] = (byte)length; } return(true); }