private static void WriterSystemTextJsonBasic(bool formatted, ArrayFormatter output) { var json = new JsonWriter(output, formatted); json.WriteObjectStart(); json.WriteAttribute("age", 42); json.WriteAttribute("first", "John"); json.WriteAttribute("last", "Smith"); json.WriteArrayStart("phoneNumbers"); json.WriteValue("425-000-1212"); json.WriteValue("425-000-1213"); json.WriteArrayEnd(); json.WriteObjectStart("address"); json.WriteAttribute("street", "1 Microsoft Way"); json.WriteAttribute("city", "Redmond"); json.WriteAttribute("zip", 98052); json.WriteObjectEnd(); // Add a large array of values json.WriteArrayStart("ExtraArray"); for (var i = 0; i < ExtraArraySize; i++) { json.WriteValue(i); } json.WriteArrayEnd(); json.WriteObjectEnd(); }
private static void WriterSystemTextJsonBasicUtf16(bool formatted, ArrayFormatter output, ReadOnlySpan <int> data) { var json = new JsonWriter(output, false, formatted); json.WriteObjectStart(); json.WriteAttribute("age", 42); json.WriteAttribute("first", "John"); json.WriteAttribute("last", "Smith"); json.WriteArrayStart("phoneNumbers"); json.WriteValue("425-000-1212"); json.WriteValue("425-000-1213"); json.WriteArrayEnd(); json.WriteObjectStart("address"); json.WriteAttribute("street", "1 Microsoft Way"); json.WriteAttribute("city", "Redmond"); json.WriteAttribute("zip", 98052); json.WriteObjectEnd(); json.WriteArrayStart("ExtraArray"); for (var i = 0; i < data.Length; i++) { json.WriteValue(data[i]); } json.WriteArrayEnd(); json.WriteObjectEnd(); }
private static void WriterSystemTextJsonHelloWorld(bool formatted, ArrayFormatter output) { var json = new JsonWriter(output, formatted); json.WriteObjectStart(); json.WriteAttribute("message", "Hello, World!"); json.WriteObjectEnd(); }
static PayloadFormatter() { _nullableDateTimeFormatter = new StaticNullableFormatter <DateTime>(UnixTimestampDateTimeFormatter.Instance); _dateTimeArrayFormatter = new ArrayFormatter <DateTime>(); _nullableDateTimeOffsetFormatter = new StaticNullableFormatter <DateTimeOffset>(UnixTimestampDateTimeOffsetFormatter.Instance); _dateTimeOffsetArrayFormatter = new ArrayFormatter <DateTimeOffset>(); Instance = new PayloadFormatter(); }
public void Setup() { var enc = Target == EncoderTarget.InvariantUtf8 ? Encoding.UTF8 : Encoding.Unicode; var buffer = new byte[BufferSize]; _stream = new MemoryStream(buffer); _writer = new StreamWriter(_stream, enc, BufferSize, true); _symbolTable = GetTargetEncoder(Target); _arrayFormatter = new ArrayFormatter(BufferSize, _symbolTable); }
public void ArrayFormatter_String() { var testArray = new string[] { "alpha", "beta", "gamma" }; var arrayFormatter = new ArrayFormatter(",", Scrubber); var result = arrayFormatter.Format(testArray, 0); Assert.Single(result); Assert.True(result.ContainsKey(string.Empty), "Should have a single, empty key"); Assert.Equal("alpha,beta,gamma", result[string.Empty]); }
public void ArrayFormatter_Double() { var testArray = new double[] { 1.1, 2.2, 3.3, 4.4 }; var arrayFormatter = new ArrayFormatter(",", Scrubber); var result = arrayFormatter.Format(testArray, 0); Assert.Single(result); Assert.True(result.ContainsKey(string.Empty), "Should have a single, empty key"); Assert.Equal("1.1,2.2,3.3,4.4", result[string.Empty]); }
private static void WriterSystemTextJsonArrayOnlyUtf16(bool formatted, ArrayFormatter output, ReadOnlySpan <int> data) { var json = new JsonWriter(output, false, formatted); json.WriteArrayStart("ExtraArray"); for (var i = 0; i < data.Length; i++) { json.WriteValue(data[i]); } json.WriteArrayEnd(); }
public void BasicArrayFormatter() { using (var sb = new ArrayFormatter(256, SymbolTable.InvariantUtf16)) { sb.Append("hi"); sb.Append(1); sb.Append("hello"); sb.Append((sbyte)-20); Assert.Equal("hi1hello-20", Encoding.Unicode.GetString(sb.Formatted.Array, 0, sb.CommitedByteCount)); } }
//[Fact(Skip = "System.TypeLoadException : The generic type 'System.IEquatable`1' was used with an invalid instantiation in assembly 'System.Private.CoreLib")] public void EagerWrite() { dynamic json = new JsonDynamicObject(); json.First = "John"; var formatter = new ArrayFormatter(1024, SymbolTable.InvariantUtf8); formatter.Append((JsonDynamicObject)json); Assert.Equal("{\"First\":\"John\"}", formatter.Formatted.ToString()); }
static void RunLoop(bool log) { var loop = new UVLoop(); var listener = new TcpListener(s_ipAddress, s_port, loop); var formatter = new ArrayFormatter(512, EncodingData.InvariantUtf8); listener.ConnectionAccepted += (Tcp connection) => { if (log) { Console.WriteLine("connection accepted"); } connection.ReadCompleted += (data) => { if (log) { unsafe { var requestString = new Utf8String(data); Console.WriteLine("*REQUEST:\n {0}", requestString.ToString()); } } formatter.Clear(); formatter.Append("HTTP/1.1 200 OK"); formatter.Append("\r\n\r\n"); formatter.Append("Hello World!"); if (log) { formatter.Format(" @ {0:O}", DateTime.UtcNow); } var segment = formatter.Formatted; unsafe { fixed(byte *p = segment.Array) { var response = new Memory <byte>(segment.Array, segment.Offset, segment.Count, pointer: p); connection.TryWrite(response); } } connection.Dispose(); }; connection.ReadStart(); }; listener.Listen(); loop.Run(); }
public void WriteJsonUtf8() { var formatter = new ArrayFormatter(1024, TextEncoder.Utf8); var json = new JsonWriter(formatter, prettyPrint: true); Write(ref json); var formatted = formatter.Formatted; var str = Encoding.UTF8.GetString(formatted.Array, formatted.Offset, formatted.Count); Assert.Equal(expected, str.Replace("\r\n", "").Replace(" ", "")); }
public void WriteJsonUtf16() { var formatter = new ArrayFormatter(1024, EncodingData.InvariantUtf16); var json = new JsonWriter <ArrayFormatter>(formatter, prettyPrint: false); Write(ref json); var formatted = formatter.Formatted; var str = Encoding.Unicode.GetString(formatted.Array, formatted.Offset, formatted.Count); Assert.Equal(expected, str.Replace(" ", "")); }
public void EagerWrite() { dynamic json = new JsonDynamicObject(); json.First = "John"; var formatter = new ArrayFormatter(1024, TextEncoder.Utf8); formatter.Append((JsonDynamicObject)json); var formattedText = new Utf8String(formatter.Formatted); Assert.Equal(new Utf8String("{\"First\":\"John\"}"), formattedText); }
//[Fact(Skip = "System.TypeLoadException : The generic type 'System.IEquatable`1' was used with an invalid instantiation in assembly 'System.Private.CoreLib")] public void NestedEagerWrite() { var jsonText = new Utf8Span("{\"FirstName\":\"John\",\"LastName\":\"Smith\",\"Address\":{\"Street\":\"21 2nd Street\",\"City\":\"New York\",\"State\":\"NY\",\"Zip\":\"10021-3100\"},\"IsAlive\":true,\"Age\":25,\"Spouse\":null}"); JsonDynamicObject json = JsonDynamicObject.Parse(jsonText, 100); var formatter = new ArrayFormatter(1024, SymbolTable.InvariantUtf8); formatter.Append(json); var formattedText = new Utf8Span(formatter.Formatted); // The follwoing check only works given the current implmentation of Dictionary. // If the implementation changes, the properties might round trip to different places in the JSON text. Assert.Equal(jsonText.ToString(), formattedText.ToString()); }
public void FormatLongStringToUtf8() { int length = 260; { var formatter = new ArrayFormatter(length, SymbolTable.InvariantUtf8); string data = new string('#', length); formatter.Append(data); Assert.Equal(length, formatter.CommitedByteCount); for (int i = 0; i < formatter.CommitedByteCount; i++) { Assert.Equal((byte)'#', formatter.Formatted.Array[i]); } } }
public ArraySegment <byte> EncodeStringToUtf8() { const string text = "Hello World!"; int stringsToWrite = 2000; int size = stringsToWrite * text.Length + stringsToWrite; var formatter = new ArrayFormatter(size, SymbolTable.InvariantUtf8, pool); for (int i = 0; i < stringsToWrite; i++) { formatter.Append(text); formatter.Append(1); } return(formatter.Formatted); }
static void RunLoop(bool log) { var loop = new UVLoop(); var listener = new TcpListener(s_ipAddress, s_port, loop); listener.ConnectionAccepted += (Tcp connection) => { if (log) { Console.WriteLine("connection accepted"); } connection.ReadCompleted += (data) => { if (log) { unsafe { var requestString = new Utf8Span(data.Span); Console.WriteLine("*REQUEST:\n {0}", requestString.ToString()); } } var formatter = new ArrayFormatter(512, SymbolTable.InvariantUtf8); formatter.Clear(); formatter.Append("HTTP/1.1 200 OK"); formatter.Append("\r\n\r\n"); formatter.Append("Hello World!"); if (log) { formatter.Format(" @ {0:O}", DateTime.UtcNow); } var segment = formatter.Formatted; using (var memory = new OwnedPinnedBuffer <byte>(segment.Array)) { connection.TryWrite(memory.Memory.Slice(segment.Offset, segment.Count)); connection.Dispose(); } }; connection.ReadStart(); }; listener.Listen(); loop.Run(); }
public void FormatDateTimeRUtf8() { var time = DateTime.UtcNow; var expected = time.ToString("R"); var sb = new ArrayFormatter(100, TextEncoder.Utf8); sb.Append(time, 'R'); var result = sb.Formatted.AsSpan().ToArray(); var resultString = Encoding.UTF8.GetString(result); Assert.Equal(expected, resultString); sb.Clear(); }
public void WriteHelloWorldJsonUtf8(bool prettyPrint) { string expectedStr = GetHelloWorldExpectedString(prettyPrint, isUtf8: true); var output = new ArrayFormatter(1024, SymbolTable.InvariantUtf8); var jsonUtf8 = new JsonWriterUtf8(output, prettyPrint); jsonUtf8.WriteObjectStart(); jsonUtf8.WriteAttribute("message", "Hello, World!"); jsonUtf8.WriteObjectEnd(); ArraySegment <byte> formatted = output.Formatted; string actualStr = Encoding.UTF8.GetString(formatted.Array, formatted.Offset, formatted.Count); Assert.Equal(expectedStr, actualStr); }
private void speech_synthesizer_SpeakProgress(object sender, SpeakProgressEventArgs e) { // Add the current word to our list last_words.Add(e.Text); while (last_words.Count > 10) { last_words.RemoveAt(0); last_words.RemoveAt(0); last_words.RemoveAt(0); last_words.RemoveAt(0); last_words.RemoveAt(0); } string textwindow = ArrayFormatter.ListElements(last_words, " "); StatusManager.Instance.UpdateStatus("ReadOutAloud", textwindow, e.CharacterPosition, current_prompt_length); }
public void WriterSystemTextJsonBasic(bool formatted, EncoderTarget encoderTarget) { var encoder = GetTargetEncoder(encoderTarget); var f = new ArrayFormatter(BufferSize, encoder); foreach (var iteration in Benchmark.Iterations) { using (iteration.StartMeasurement()) { for (int i = 0; i < Benchmark.InnerIterationCount; i++) { f.Clear(); TestWriterSystemTextJsonBasic(formatted, f); } } } }
public void Setup() { _data = new int[ExtraArraySize]; Random rand = new Random(42); for (int i = 0; i < ExtraArraySize; i++) { _data[i] = rand.Next(-10000, 10000); } var buffer = new byte[BufferSize]; _memoryStream = new MemoryStream(buffer); _streamWriter = new StreamWriter(_memoryStream, new UTF8Encoding(false), BufferSize, true); _arrayFormatterWrapper = new ArrayFormatterWrapper(10000, SymbolTable.InvariantUtf8); _arrayFormatter = new ArrayFormatter(BufferSize, SymbolTable.InvariantUtf8); // To pass an initialBuffer to Utf8Json: // _output = new byte[BufferSize]; _output = null; var random = new Random(42); const int numberOfItems = 10; _longs = new long[numberOfItems]; _longs[0] = 0; _longs[1] = long.MaxValue; _longs[2] = long.MinValue; _longs[3] = 12345678901; _longs[4] = -12345678901; for (int i = 5; i < numberOfItems; i++) { long value = random.Next(int.MinValue, int.MaxValue); value += value < 0 ? int.MinValue : int.MaxValue; _longs[i] = value; } dataArray = new int[100]; for (int i = 0; i < 100; i++) { dataArray[i] = 12345; } MyDate = DateTime.Now; }
public static Utf8String ToUtf8String(this ReadOnlyBytes bytes, TextEncoder encoder) { var sb = new ArrayFormatter(bytes.ComputeLength(), TextEncoder.Utf8); if (encoder.Encoding == TextEncoder.EncodingName.Utf8) { var position = Position.First; ReadOnlyBuffer <byte> segment; while (bytes.TryGet(ref position, out segment, true)) { sb.Append(new Utf8String(segment.Span)); } } else { throw new NotImplementedException(); } return(new Utf8String(sb.Formatted.AsSpan())); }
public static Utf8String ToUtf8String(this ReadOnlyBytes bytes, SymbolTable symbolTable) { var sb = new ArrayFormatter(bytes.ComputeLength(), SymbolTable.InvariantUtf8); if (symbolTable == SymbolTable.InvariantUtf8) { var position = Position.First; ReadOnlyBuffer <byte> segment; while (bytes.TryGet(ref position, out segment, true)) { sb.Append(new Utf8String(segment.Span)); } } else { throw new NotImplementedException(); } return(new Utf8String(sb.Formatted.AsSpan())); }
public static void doTestHarness() { try { if (1 == 1) { QuadraticSearchFunction2D qsf2d = new QuadraticSearchFunction2D(); NelderMead2D nm2d = new NelderMead2D(); nm2d.MAX_ITERATIONS = 500; nm2d.initialiseSearch(qsf2d, new Point2D(0, 0), new Point2D(100, 100), new Point2D(-100, -50)); string error_message; double optimal_score; Point2D result = nm2d.search(out error_message, out optimal_score); Console.WriteLine("Lowest at " + result + " with score " + optimal_score + " with error " + error_message); } if (1 == 1) { QuadraticSearchFunction qsf = new QuadraticSearchFunction(); NelderMead nm = new NelderMead(3); nm.MAX_ITERATIONS = 500; double[][] initial_points = new double[4][]; for (int i = 0; i < 4; ++i) { initial_points[i] = new double[3]; } initial_points[0][0] = 0; initial_points[0][1] = 0; initial_points[1][0] = 100; initial_points[1][1] = 100; initial_points[2][0] = -100; initial_points[2][1] = -50; initial_points[3][0] = -200; initial_points[3][1] = -150; nm.initialiseSearch(qsf, initial_points); string error_message; double optimal_score; double[] result = nm.search(out error_message, out optimal_score); Console.WriteLine("Lowest at " + ArrayFormatter.listElements(result) + " with score " + optimal_score + " with error " + error_message); } } catch (GenericException e) { Console.WriteLine("There was an error: {0}", e.Message); } }
private void EncodeStringToUtf8() { string text = "Hello World!"; int stringsToWrite = 2000; int size = stringsToWrite * text.Length + stringsToWrite; ArrayFormatter formatter = new ArrayFormatter(size, SymbolTable.InvariantUtf8, pool); timer.Restart(); for (int itteration = 0; itteration < itterationsInvariant; itteration++) { formatter.Clear(); for (int i = 0; i < stringsToWrite; i++) { formatter.Append(text); formatter.Append(1); } Assert.Equal(size, formatter.CommitedByteCount); } PrintTime(); }
public void WriteJsonUtf16() { var formatter = new ArrayFormatter(1024, SymbolTable.InvariantUtf16); var json = new JsonWriterUtf16(formatter, prettyPrint: false); Write(ref json); var formatted = formatter.Formatted; var str = Encoding.Unicode.GetString(formatted.Array, formatted.Offset, formatted.Count); Assert.Equal(expected, str.Replace(" ", "")); formatter.Clear(); json = new JsonWriterUtf16(formatter, prettyPrint: true); Write(ref json); formatted = formatter.Formatted; str = Encoding.Unicode.GetString(formatted.Array, formatted.Offset, formatted.Count); Assert.Equal(expected, str.Replace("\r\n", "").Replace("\n", "").Replace(" ", "")); }
public void Setup() { _data = new int[ExtraArraySize]; Random rand = new Random(42); for (int i = 0; i < ExtraArraySize; i++) { _data[i] = rand.Next(-10000, 10000); } var buffer = new byte[BufferSize]; _memoryStream = new MemoryStream(buffer); _streamWriter = new StreamWriter(_memoryStream, new UTF8Encoding(false), BufferSize, true); _arrayFormatterWrapper = new ArrayFormatterWrapper(BufferSize, SymbolTable.InvariantUtf8); _arrayFormatter = new ArrayFormatter(BufferSize, SymbolTable.InvariantUtf8); // To pass an initialBuffer to Utf8Json: // _output = new byte[BufferSize]; _output = new byte[21 * 1_000 + 4];
public void FormatXUtf8() { var x = StandardFormat.Parse("x"); var X = StandardFormat.Parse("X"); var sb = new ArrayFormatter(256, SymbolTable.InvariantUtf8); sb.Append((ulong)255, x); sb.Append((uint)255, X); Assert.Equal("ffFF", new Utf8Span(sb.Formatted.AsSpan()).ToString()); sb.Clear(); sb.Append((int)-1, X); Assert.Equal("FFFFFFFF", new Utf8Span(sb.Formatted.AsSpan()).ToString()); sb.Clear(); sb.Append((int)-2, X); Assert.Equal("FFFFFFFE", new Utf8Span(sb.Formatted.AsSpan()).ToString()); }
public void FormatLongStringToUtf8() { int length = 260; { var formatter = new ArrayFormatter(length, EncodingData.InvariantUtf8); string data = new string('#', length); formatter.Append(data); Assert.Equal(length, formatter.CommitedByteCount); for(int i=0; i<formatter.CommitedByteCount; i++) { Assert.Equal((byte)'#', formatter.Formatted.Array[i]); } } }
private void EncodeStringToUtf8() { string text = "Hello World!"; int stringsToWrite = 2000; int size = stringsToWrite * text.Length + stringsToWrite; ArrayFormatter formatter = new ArrayFormatter(size, EncodingData.InvariantUtf8, pool); timer.Restart(); for (int itteration = 0; itteration < itterationsInvariant; itteration++) { formatter.Clear(); for (int i = 0; i < stringsToWrite; i++) { formatter.Append(text); formatter.Append(1); } Assert.Equal(size, formatter.CommitedByteCount); } PrintTime(); }
public void FormatXUtf8() { var x = TextFormat.Parse("x"); var X = TextFormat.Parse("X"); var sb = new ArrayFormatter(256, EncodingData.InvariantUtf8); sb.Append((ulong)255, x); sb.Append((uint)255, X); Assert.Equal("ffFF", new Utf8String(sb.Formatted.Slice()).ToString()); sb.Clear(); sb.Append((int)-1, X); Assert.Equal("FFFFFFFF", new Utf8String(sb.Formatted.Slice()).ToString()); sb.Clear(); sb.Append((int)-2, X); Assert.Equal("FFFFFFFE", new Utf8String(sb.Formatted.Slice()).ToString()); }