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, 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, 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 string Serialize <T>(T obj) { var stringWriter = new Utf8Writer(); var serializer = new XmlSerializer(typeof(T)); using (var writer = XmlWriter.Create(stringWriter, new XmlWriterSettings() { Encoding = Encoding.UTF8 })) { serializer.Serialize(writer, obj); return(stringWriter.ToString()); } }
public string Save() { // StringWriter.Encoding always returns UTF16. We need it to return UTF8, so the // XmlDocument will write the UTF8 header. Utf8Writer sw = new Utf8Writer(); sw.NewLine = newLine; doc.Save(sw); string txt = sw.ToString(); if (endsWithEmptyLine && !txt.EndsWith(newLine)) { txt += newLine; } return(txt); }
public static bool TryWriteUtf8Bytes(ref ResizableMemory <byte> writer, ReadOnlySpan <byte> value) { if (value.Length > ushort.MaxValue) { return(false); } writer.Push((byte)EtfTokenType.Binary); BinaryPrimitives.WriteUInt32BigEndian(writer.RequestSpan(4), (uint)value.Length); writer.Advance(4); if (!Utf8Writer.TryWriteString(ref writer, value)) { return(false); } return(true); }
public static void WriteUtf8(EventLogRecord readableLog, Stream stream) { //using (var ms = _RecyclableMemoryStreamManager.GetStream()) var w = new Utf8Writer(stream); w.WriteString("-"); w.WriteString("\n lg: "); w.WriteString(readableLog.LgId.ToString()); if (readableLog.DT != DateTime.MinValue) { w.WriteString("\n at: "); w.WriteString(readableLog.DT.ToString("u")); } if (!string.IsNullOrEmpty(readableLog.Key)) { w.WriteString("\n ky: "); w.WriteString(readableLog.Key); } if (!string.IsNullOrEmpty(readableLog.TypeName)) { w.WriteString("\n ty: "); w.WriteString(readableLog.TypeName); } if (readableLog.DataByte is object) { w.WriteString("\n da: "); w.WriteRaw(readableLog.DataByte); } else if (readableLog.DataText is object) { w.WriteString("\n da: |"); if (readableLog.DataText.Contains("\n")) { foreach (var line in readableLog.DataText.Split(arrayCharNewLine)) { w.WriteString("\n "); w.WriteString(line); } } else { w.WriteString("\n "); w.WriteString(readableLog.DataText); } } w.WriteRaw(10); w.Flush(); }
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); }
public string Save() { // StringWriter.Encoding always returns UTF16. We need it to return UTF8, so the // XmlDocument will write the UTF8 header. Utf8Writer sw = new Utf8Writer(); sw.NewLine = newLine; doc.Save(sw); string txt = sw.ToString(); if (endsWithEmptyLine && !txt.EndsWith(newLine)) txt += newLine; return txt; }
public void Setup() { stream = new MemoryStream(); writer = new Utf8Writer(stream); }
public void DoThings(string[] args) { // Data file available at http://www.unicode.org/Public/UCD/latest/ucd/UnicodeData.txt var data = new Utf8String(File.ReadAllBytes(args[0])); var lines = new Splitter('\n', data); this.main = new FileStream("chardata", FileMode.Create, FileAccess.Write); var namesRaw = new FileStream("charnames", FileMode.Create, FileAccess.Write); var names = new Utf8Writer(namesRaw); // TODO normalization map int i = 0; long per = 0; foreach (var line in lines) { i++; if (line.IsEmpty) { continue; } var start = main.Position; Console.WriteLine("writing {0} at offset {1}", i, main.Position); var parts = line.Split(separator); // Schema given in ftp://ftp.unicode.org/Public/3.0-Update/UnicodeData-3.0.0.html // The current codepoint (eg U+00A2). WriteCodepoint(parts[0]); // The name of this codepoint (eg LATIN SMALL LETTER E WITH MACRON). // WriteOffset(namesRaw.Position); names.Append(parts[1]); WriteOffset(namesRaw.Position); // The major category, like Ll or Sm WriteByte(ParseCategory(parts[2])); // Numeric value. WriteFloat(GetNumericValue(parts)); // Whether this thing is mirrored WriteByte((byte)(parts[9] == IsMirrored ? 1 : 0)); // Upper WriteCodepoint(parts[12], parts[0]); // Lower WriteCodepoint(parts[13], parts[0]); // Title WriteCodepoint(parts[14], parts[0]); var end = main.Position; if (per == 0) { per = end - start; } else { if (per != end - start) { throw new Exception($"at entry $i, expected $per bytes written; actual was ${end - start}"); } } } main.Flush(); main.Close(); namesRaw.Flush(); namesRaw.Close(); }