public BlobNode(Utf8String name, ObjectNodeSection section, byte[] data, int alignment) { _name = name; _section = section; _data = data; _alignment = alignment; }
internal unsafe bool EqualsCaseInsensitive(Utf8String s) { if (this.m_pStringHeap == null) { return (s.m_StringHeapByteLength == 0); } return (((s.m_StringHeapByteLength == this.m_StringHeapByteLength) && (this.m_StringHeapByteLength != 0)) && EqualsCaseInsensitive(s.m_pStringHeap, this.m_pStringHeap, this.m_StringHeapByteLength)); }
internal bool EqualsCaseInsensitive(Utf8String s) { if (this.m_pStringHeap == null) { return s.m_StringHeapByteLength == 0; } return s.m_StringHeapByteLength == this.m_StringHeapByteLength && this.m_StringHeapByteLength != 0 && Utf8String.EqualsCaseInsensitive(s.m_pStringHeap, this.m_pStringHeap, this.m_StringHeapByteLength); }
public JsonReader(string str) { _str = new Utf8String(str).Trim(); _index = 0; _insideObject = 0; _insideArray = 0; TokenType = 0; _jsonStartIsObject = (byte)_str[0] == '{'; }
private static void SplitTest_Common(ustring source, Utf8SpanSplitDelegate splitAction, Range[] expectedRanges) { using BoundedUtf8Span boundedSpan = new BoundedUtf8Span(source.AsBytes()); Utf8Span span = boundedSpan.Span; int totalSpanLengthInBytes = span.Bytes.Length; source = null; // to avoid inadvertently using this for the remainder of the method // First, run the split with default options and make sure the ranges are equivalent List <Range> actualRanges = new List <Range>(); foreach (Utf8Span slice in splitAction(span, Utf8StringSplitOptions.None)) { actualRanges.Add(GetRangeOfSubspan(span, slice)); } Assert.Equal(expectedRanges, actualRanges, new RangeEqualityComparer(totalSpanLengthInBytes)); // Next, run the split with empty entries removed actualRanges = new List <Range>(); foreach (Utf8Span slice in splitAction(span, Utf8StringSplitOptions.RemoveEmptyEntries)) { actualRanges.Add(GetRangeOfSubspan(span, slice)); } Assert.Equal(expectedRanges.Where(range => !range.IsEmpty(totalSpanLengthInBytes)), actualRanges, new RangeEqualityComparer(totalSpanLengthInBytes)); // Next, run the split with results trimmed (but allowing empty results) expectedRanges = (Range[])expectedRanges.Clone(); // clone the array since we're about to mutate it for (int i = 0; i < expectedRanges.Length; i++) { expectedRanges[i] = GetRangeOfSubspan(span, span[expectedRanges[i]].Trim()); } actualRanges = new List <Range>(); foreach (Utf8Span slice in splitAction(span, Utf8StringSplitOptions.TrimEntries)) { actualRanges.Add(GetRangeOfSubspan(span, slice)); } Assert.Equal(expectedRanges, actualRanges, new RangeEqualityComparer(totalSpanLengthInBytes)); // Finally, run the split both trimmed and with empty entries removed actualRanges = new List <Range>(); foreach (Utf8Span slice in splitAction(span, Utf8StringSplitOptions.TrimEntries | Utf8StringSplitOptions.RemoveEmptyEntries)) { actualRanges.Add(GetRangeOfSubspan(span, slice)); } Assert.Equal(expectedRanges.Where(range => !range.IsEmpty(totalSpanLengthInBytes)), actualRanges, new RangeEqualityComparer(totalSpanLengthInBytes)); }
private static void ShouldBeIdentical(Utf8String s, uint[] expected) { var codePoints = s.CodePoints.ToArray(); Assert.AreEqual(expected.Length, codePoints.Length); for (int i = 0; i < codePoints.Length; i++) { Assert.AreEqual(expected[i], codePoints[i].Value); } }
public byte[] Serialize() { byte[] publicKey = PublicKey.Serialize(), m1 = new OctetSequence(Proof).Serialize(), cIV = new OctetSequence(InitialVector).Serialize(), options = new Utf8String(BuildOptionsString()).Serialize(); int length = publicKey.Length + m1.Length + cIV.Length + options.Length; return new ByteBuilder() .Append(length, true) .Append(publicKey) .Append(m1) .Append(options) .Append(cIV) .ToArray(); }
static void RunLoop(bool log) { var loop = new UVLoop(); var listener = new TcpListener(s_ipAddress, s_port, loop); var formatter = new BufferFormatter(512, FormattingData.InvariantUtf8); listener.ConnectionAccepted += (Tcp connection) => { if (log) { Console.WriteLine("connection accepted"); } connection.ReadCompleted += (ByteSpan data) => { if (log) { unsafe { var requestString = new Utf8String(data.UnsafeBuffer, data.Length); 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 response = formatter.Buffer.Slice(0, formatter.CommitedByteCount); // formatter should have a property for written bytes GCHandle gcHandle; var byteSpan = response.Pin(out gcHandle); connection.TryWrite(byteSpan); connection.Dispose(); gcHandle.Free(); // TODO: formatter should format to ByteSpan, to avoid pinning }; connection.ReadStart(); }; listener.Listen(); loop.Run(); }
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 Utf8String(data.Span); Console.WriteLine("*REQUEST:\n {0}", requestString.ToString()); } } var formatter = new ArrayFormatter(512, EncodingData.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 OwnedPinnedArray<byte>(segment.Array)) { connection.TryWrite(memory.Memory.Slice(segment.Offset, segment.Count)); connection.Dispose(); } }; connection.ReadStart(); }; listener.Listen(); loop.Run(); }
private static void NoAllocationWithForeach(Utf8String s, int n) { var start = GC.GetTotalMemory(false); for (int i = 0; i < n; i++) { foreach (var x in s.CodePoints) ; if (s.Length > 3) { var sub1 = s.Substring(1, 1); var sub2 = s.Substring(2); var sub3 = s.Substring(3); } } var end = GC.GetTotalMemory(false); Assert.AreEqual(start, end); }
public static void Main(string[] args) { var buffer = new byte[1024]; var quote = new Utf8String("Insanity: doing the same thing over and over again and expecting different results. - Albert Einstein"); ; var loop = new UVLoop(); var listener = new TcpListener("0.0.0.0", 17, loop); listener.ConnectionAccepted += (Tcp connection) => { connection.ReadCompleted += (data) => { quote.CopyTo(buffer); connection.TryWrite(buffer, quote.Length); }; connection.ReadStart(); }; listener.Listen(); loop.Run(); }
// This method is a bit of a mess. We need to fix many Http and Json APIs void WriteResponseForPostJson(BufferFormatter formatter, HttpRequestLine requestLine, ReadOnlySpan<byte> body) { Console.WriteLine(new Utf8String(body)); uint requestedCount = ReadCountUsingReader(body).GetValueOrDefault(1); //uint requestedCount = ReadCountUsingNonAllocatingDom(body).GetValueOrDefault(1); // TODO: this needs to be written directly to the buffer after content length reservation is implemented. var buffer = ArrayPool<byte>.Shared.Rent(2048); var spanFormatter = new SpanFormatter(buffer.Slice(), FormattingData.InvariantUtf8); var json = new JsonWriter<SpanFormatter>(spanFormatter, prettyPrint: true); json.WriteObjectStart(); json.WriteArrayStart(); for (int i = 0; i < requestedCount; i++) { json.WriteString(DateTime.UtcNow.ToString()); // TODO: this needs to not allocate. } json.WriteArrayEnd(); ; json.WriteObjectEnd(); var responseBodyText = new Utf8String(buffer, 0, spanFormatter.CommitedByteCount); formatter.AppendHttpStatusLine(HttpVersion.V1_1, 200, new Utf8String("OK")); formatter.Append(new Utf8String("Content-Length : ")); formatter.Append(responseBodyText.Length); formatter.AppendHttpNewLine(); formatter.Append("Content-Type : text/plain; charset=UTF-8"); formatter.AppendHttpNewLine(); formatter.Append("Server : .NET Core Sample Serve"); formatter.AppendHttpNewLine(); formatter.Append(new Utf8String("Date : ")); formatter.Append(DateTime.UtcNow.ToString("R")); formatter.AppendHttpNewLine(); formatter.AppendHttpNewLine(); formatter.Append(responseBodyText); ArrayPool<byte>.Shared.Return(buffer); }
[System.Security.SecurityCritical] // auto-generated public unsafe void GetPInvokeMap( int token, out PInvokeAttributes attributes, out String importName, out String importDll) { int _attributes; void* _importName, _importDll; _GetPInvokeMap(m_metadataImport2, token, out _attributes, &_importName, &_importDll); importName = new Utf8String(_importName).ToString(); importDll = new Utf8String(_importDll).ToString(); attributes = (PInvokeAttributes)_attributes; }
public static void Split_Char(ustring source, char separator, Range[] expectedRanges) { SplitTest_Common(source, (span, splitOptions) => span.Split(separator, splitOptions), expectedRanges); }
public void CanParseBodylessRequest() { var request = new Utf8String("GET / HTTP/1.1\r\nConnection: close\r\n\r\n").CopyBytes().Slice(); var parsed = HttpRequest.Parse(request); Assert.Equal(HttpMethod.Get, parsed.RequestLine.Method); Assert.Equal(HttpVersion.V1_1, parsed.RequestLine.Version); Assert.Equal(new Utf8String("/"), parsed.RequestLine.RequestUri); Assert.Equal(1, parsed.Headers.Count); Assert.Equal(0, parsed.Body.Length); }
public static void Split_Utf8Span(ustring source, ustring separator, Range[] expectedRanges) { SplitTest_Common(source, (span, splitOptions) => span.Split(separator.AsSpan(), splitOptions), expectedRanges); }
private static void RunTestJsonReader(string str, bool output) { var utf8Str = new Utf8String(str); Timer.Restart(); for (var i = 0; i < NumberOfIterations; i++) { JsonReaderHelper(utf8Str, output); } TimingResultsJsonReader.Add(Timer.ElapsedMilliseconds); }
private Utf8String ComputeMangledFieldName(FieldDesc field) { string prependTypeName = null; if (!_mangleForCplusPlus) prependTypeName = GetMangledTypeName(field.OwningType); if (field is EcmaField) { var deduplicator = new HashSet<string>(); // Add consistent names for all fields of the type, independent on the order in which // they are compiled lock (this) { foreach (var f in field.OwningType.GetFields()) { string name = SanitizeName(f.Name); name = DisambiguateName(name, deduplicator); deduplicator.Add(name); if (prependTypeName != null) name = prependTypeName + "__" + name; _mangledFieldNames = _mangledFieldNames.Add(f, name); } } return _mangledFieldNames[field]; } string mangledName = SanitizeName(field.Name); if (prependTypeName != null) mangledName = prependTypeName + "__" + mangledName; Utf8String utf8MangledName = new Utf8String(mangledName); lock (this) { _mangledFieldNames = _mangledFieldNames.Add(field, utf8MangledName); } return utf8MangledName; }
static void WriteResponseForHelloWorld(BufferFormatter formatter) { var responseBodyText = new Utf8String("Hello, World"); formatter.AppendHttpStatusLine(HttpVersion.V1_1, 200, new Utf8String("OK")); formatter.Append(new Utf8String("Content-Length : ")); formatter.Append(responseBodyText.Length); formatter.AppendHttpNewLine(); formatter.Append("Content-Type : text/plain; charset=UTF-8"); formatter.AppendHttpNewLine(); formatter.Append("Server : .NET Core Sample Serve"); formatter.AppendHttpNewLine(); formatter.Append(new Utf8String("Date : ")); formatter.Append(DateTime.UtcNow.ToString("R")); formatter.AppendHttpNewLine(); formatter.AppendHttpNewLine(); formatter.Append(responseBodyText); }
public unsafe void GetPInvokeMap(int token, out PInvokeAttributes attributes, out string importName, out string importDll) { int num; void* voidPtr; void* voidPtr2; _GetPInvokeMap(this.m_metadataImport2, out MetadataArgs.Skip, token, out num, &voidPtr, &voidPtr2); importName = new Utf8String(voidPtr).ToString(); importDll = new Utf8String(voidPtr2).ToString(); attributes = (PInvokeAttributes) num; }
private static void ReaderTestSystemTextJson(string str, bool output) { var utf8Str = new Utf8String(str); Timer.Restart(); for (var i = 0; i < NumberOfIterations; i++) { JsonReaderHelper(utf8Str, OutputJsonData); } if (output) Console.WriteLine(Timer.ElapsedTicks); }
public static void Run() { var utf8RawData = new byte[] { 0x7B, 0x20, 0x22, 0x6B, 0x65, 0x79, 0x22, 0x3A, 0x20, 0x22, 0x61, 0xE3, 0x81, 0x82, 0xF0, 0x9F, 0x98, 0x80, 0x22, 0x20, 0x7D }; var utf16RawData = new char[] { '{', ' ', '"', 'k', 'e', 'y', '"', ':', ' ', '"', 'a', 'あ', (char)0xD83D, (char)0xDE00, '"', ' ', '}' }; // string 型 { // UTF-8 → UTF-16 の変換でヒープ確保が必要 var s1 = System.Text.Encoding.UTF8.GetString(utf8RawData); // string 型は char[] を受け取る場合でも、内部でコピーを作るのでヒープ確保発生 var s2 = new string(utf16RawData); // string.Substring もコピー発生 var sub = s1.Substring(10, 4); Console.WriteLine(sub); } // Utf8String 型 { // ヒープ確保しない実装 var s = new Utf8String(utf8RawData); // インデックスでの文字取得はできない。s[0] は byte 単位のアクセスになる // コード ポイントの取り出しには CodePoints を使う // foreach もすべて構造体で展開されるのでヒープ確保不要 foreach (var c in s.CodePoints) { Console.WriteLine(c); } // Substring もコピー不要な実装になっている var sub = s.Substring(10, 8); foreach (var c in sub.CodePoints) { Console.WriteLine(c); } } // string 型 { // 内部でコピーしているので… var s1 = new string(utf16RawData); var s2 = new string(utf16RawData); // 元データを書き換えても utf16RawData[0] = '['; utf16RawData[16] = ']'; // 影響は出ない Console.WriteLine(s1); // { "key": "aあ😀" } Console.WriteLine(s2); // { "key": "aあ😀" } } // Utf8String 型 { // データを共有しているので… var s1 = new Utf8String(utf8RawData); var s2 = new Utf8String(utf8RawData); //98, 227, 129, 132, 240, 159, 144, 136 // 元データを書き換えると utf8RawData[10] = 98; utf8RawData[11] = 227; utf8RawData[12] = 129; utf8RawData[13] = 132; utf8RawData[14] = 240; utf8RawData[15] = 159; utf8RawData[16] = 144; utf8RawData[17] = 136; // 影響がある Console.WriteLine(s1); // { "key": "bい🐈" } Console.WriteLine(s2); // { "key": "bい🐈" } Console.WriteLine(s1.Substring(10, 8)); // bい🐈 } }
private static void JsonReaderHelper(Utf8String str, bool output) { var reader = new JsonReader(str); while (reader.Read()) { var tokenType = reader.TokenType; switch (tokenType) { case JsonReader.JsonTokenType.ObjectStart: case JsonReader.JsonTokenType.ObjectEnd: case JsonReader.JsonTokenType.ArrayStart: case JsonReader.JsonTokenType.ArrayEnd: if (output) Console.WriteLine(tokenType); break; case JsonReader.JsonTokenType.Property: var name = reader.GetName(); if (output) Console.WriteLine(name); var value = reader.GetValue(); if (output) Console.WriteLine(value); break; case JsonReader.JsonTokenType.Value: value = reader.GetValue(); if (output) Console.WriteLine(value); break; default: throw new ArgumentOutOfRangeException(); } } }
public Utf8StringBuilder Append(Utf8String value) { return Append(value.UnderlyingArray); }
public void Write(Utf8String value) { this.Write(value.CopyBytes()); }
public int this[Utf8String name] => _table[name];
private Utf8String ComputeMangledMethodName(MethodDesc method) { string prependTypeName = null; if (!_mangleForCplusPlus) prependTypeName = GetMangledTypeName(method.OwningType); if (method is EcmaMethod) { var deduplicator = new HashSet<string>(); // Add consistent names for all methods of the type, independent on the order in which // they are compiled lock (this) { foreach (var m in method.OwningType.GetMethods()) { string name = SanitizeName(m.Name); name = DisambiguateName(name, deduplicator); deduplicator.Add(name); if (prependTypeName != null) name = prependTypeName + "__" + name; _mangledMethodNames = _mangledMethodNames.Add(m, name); } } return _mangledMethodNames[method]; } string mangledName; var methodDefinition = method.GetTypicalMethodDefinition(); if (methodDefinition != method) { mangledName = GetMangledMethodName(methodDefinition.GetMethodDefinition()).ToString(); var inst = method.Instantiation; string mangledInstantiation = ""; for (int i = 0; i < inst.Length; i++) { string instArgName = GetMangledTypeName(inst[i]); if (_mangleForCplusPlus) instArgName = instArgName.Replace("::", "_"); if (i > 0) mangledInstantiation += "__"; mangledInstantiation += instArgName; } mangledName += NestMangledName(mangledInstantiation); } else { // Assume that Name is unique for all other methods mangledName = SanitizeName(method.Name); } if (prependTypeName != null) mangledName = prependTypeName + "__" + mangledName; Utf8String utf8MangledName = new Utf8String(mangledName); lock (this) { _mangledMethodNames = _mangledMethodNames.Add(method, utf8MangledName); } return utf8MangledName; }
public void BuildSymbolDefinitionMap(ObjectNode node, ISymbolNode[] definedSymbols) { _offsetToDefName.Clear(); foreach (ISymbolNode n in definedSymbols) { if (!_offsetToDefName.ContainsKey(n.Offset)) { _offsetToDefName[n.Offset] = new List<ISymbolNode>(); } _offsetToDefName[n.Offset].Add(n); } var symbolNode = node as ISymbolNode; if (symbolNode != null) { _sb.Clear(); AppendExternCPrefix(_sb); symbolNode.AppendMangledName(NodeFactory.NameMangler, _sb); _currentNodeZeroTerminatedName = _sb.Append('\0').ToUtf8String(); } else { _currentNodeZeroTerminatedName = default(Utf8String); } }