Пример #1
0
        public void Utf8SpanToUtf16StringToUtf8SpanRoundTrip(string str)
        {
            Utf8Span Utf8Span    = new Utf8Span(str);
            string   utf16String = Utf8Span.ToString();

            TestHelper.Validate(Utf8Span, new Utf8Span(utf16String));
        }
Пример #2
0
        //[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 EagerWrite()
        {
            dynamic json = new JsonDynamicObject();

            json.First = "John";

            var formatter = new ArrayFormatter(1024, SymbolTable.InvariantUtf8);

            formatter.Append((JsonDynamicObject)json);
            var formattedText = new Utf8Span(formatter.Formatted);

            Assert.Equal("{\"First\":\"John\"}", formattedText.ToString());
        }
Пример #4
0
        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 bool TryAddString(Utf8Span value, out int index)
        {
            // If the string already exists, then just return that index.
            if (this.utf8StringToIndex.TryGetValue(value.Span, out index))
            {
                return(true);
            }

            // If we are at capacity just return false.
            if (this.size == this.strings.Length)
            {
                index = default;
                return(false);
            }

            index = this.size;
            this.strings[this.size] = UtfAllString.Create(value.ToString());
            this.utf8StringToIndex.AddOrUpdate(value.Span, index);
            this.size++;

            return(true);
        }
Пример #6
0
            public override int Compare(Utf8Span x, Utf8Span y)
            {
                // TODO_UTF8STRING: Avoid the allocations below.

                return(_compareInfo.Compare(x.ToString(), y.ToString(), _options));
            }
Пример #7
0
            public override int GetHashCode(Utf8Span obj)
            {
                // TODO_UTF8STRING: Avoid the allocations below.

                return(StringComparer.OrdinalIgnoreCase.GetHashCode(obj.ToString()));
            }
Пример #8
0
            public override bool Equals(Utf8Span x, Utf8Span y)
            {
                // TODO_UTF8STRING: Avoid the allocations below.

                return(StringComparer.OrdinalIgnoreCase.Equals(x.ToString(), y.ToString()));
            }
Пример #9
0
            public override int Compare(Utf8Span x, Utf8Span y)
            {
                // TODO_UTF8STRING: Avoid the allocations below.

                return(string.CompareOrdinal(x.ToString(), y.ToString()));
            }
Пример #10
0
            public override int GetHashCode(Utf8Span obj)
            {
                // TODO_UTF8STRING: Avoid the allocations below.

                return(_compareInfo.GetHashCode(obj.ToString(), _options));
            }
Пример #11
0
        public void Utf16StringToUtf8SpanToUtf16StringRoundTrip(string utf16String)
        {
            Utf8Span Utf8Span = new Utf8Span(utf16String);

            Assert.Equal(utf16String, Utf8Span.ToString());
        }
Пример #12
0
        public static string ToStringCached(this Utf8Span utf8)
        {
            if (utf8.IsEmpty)
            {
                return(string.Empty);
            }
            var utf8length = utf8.Length();
            int hash       = CalculateHash(utf8.CharAt(0), utf8.CharAt(utf8length / 2), utf8.CharAt(utf8length - 1), utf8length);

            if (cache == null)
            {
                cache = new CacheSlot[6841];
            }
            CacheSlot cacheSlot = cache[hash];
            var       list      = cacheSlot.List;

            if (list == null)
            {
                cacheSlot.List = list = new CacheEntry[6];
            }


            for (int i = 0; i < list.Length; i++)
            {
                var entry = list[i];
                if (entry.String == null)
                {
                    break;
                }
                if (entry.Span.SequenceEqual(utf8.Bytes))
                {
                    return(entry.String);
                }
            }

            lock (lockObj)
            {
                var entry = new CacheEntry();
                entry.Length = utf8length;
                entry.String = utf8.ToString();

                if (usedScratchpadBytes + utf8length <= scratchpad.Length)
                {
                    utf8.Bytes.CopyTo(scratchpad.Slice(usedScratchpadBytes));
                    entry.Bytes          = scratchpad;
                    entry.Offset         = usedScratchpadBytes;
                    usedScratchpadBytes += utf8length;
                }
                else
                {
                    scratchpad = new byte[Math.Max(scratchpad.Length, utf8length * 2)];
                    utf8.Bytes.CopyTo(scratchpad);
                    entry.Bytes         = scratchpad;
                    entry.Offset        = 0;
                    usedScratchpadBytes = utf8length;
                }

                cacheSlot.List[cacheSlot.NextItemToReplace] = entry;
                cacheSlot.NextItemToReplace = (cacheSlot.NextItemToReplace + 1) % cacheSlot.List.Length;

                cache[hash] = cacheSlot;
                return(entry.String);
            }
        }