Beispiel #1
0
        public string ReadUtf8LineStrict(long limit)
        {
            if (limit < 0)
            {
                throw new ArgumentException("limit < 0: " + limit);
            }
            long scanLength = limit == long.MaxValue ? long.MaxValue : limit + 1;
            long newline    = IndexOf((byte)'\n', 0, scanLength);

            if (newline != -1)
            {
                return(_easyBuffer.ReadUtf8Line(newline));
            }
            if (scanLength < long.MaxValue &&
                Request(scanLength) && _easyBuffer.GetByte(scanLength - 1) == '\r' &&
                Request(scanLength + 1) && _easyBuffer.GetByte(scanLength) == '\n')
            {
                return(_easyBuffer.ReadUtf8Line(scanLength)); // The line was 'limit' UTF-8 bytes followed by \r\n.
            }
            var data = new EasyBuffer();

            _easyBuffer.CopyTo(data, 0, Math.Min(32, _easyBuffer.Size));
            throw new IndexOutOfRangeException("\\n not found: limit=" + Math.Min(_easyBuffer.Size, limit)
                                               + " content=" + data.ReadByteString().Hex() + '…');
        }
Beispiel #2
0
        private void assertStringEncoded(String hex, String @string)
        {
            ByteString expectedUtf8 = ByteString.DecodeHex(hex);

            // Confirm our implementation matches those expectations.
            ByteString actualUtf8 = new EasyBuffer().WriteUtf8(@string).ReadByteString();

            Assert.AreEqual(expectedUtf8, actualUtf8);

            //// Confirm our expectations are consistent with the platform.
            //ByteString platformUtf8 = ByteString.Of(Encoding.UTF8.GetBytes(@string));
            //Assert.AreEqual(expectedUtf8, platformUtf8);

            // Confirm we are consistent when writing one code point at a time.
            var bufferUtf8 = new EasyBuffer();

            for (int i = 0; i < @string.Length;)
            {
                int c = @string.CodePointAt(i);
                bufferUtf8.WriteUtf8CodePoint(c);
                i += Character.CharCount(c);
            }
            Assert.AreEqual(expectedUtf8, bufferUtf8.ReadByteString());

            // Confirm we are consistent when measuring lengths.
            Assert.AreEqual(expectedUtf8.Size(), Utf8.Size(@string));
            Assert.AreEqual(expectedUtf8.Size(), Utf8.Size(@string, 0, @string.Length()));
        }
Beispiel #3
0
        private void assertCodePointEncoded(String hex, params int[] codePoints)
        {
            var buffer = new EasyBuffer();

            foreach (var codePoint in codePoints)
            {
                buffer.WriteUtf8CodePoint(codePoint);
            }
            var b1 = buffer.ReadByteString();
            var b2 = ByteString.DecodeHex(hex);

            Assert.AreEqual(b1, b2);
        }
Beispiel #4
0
 public ByteString ReadByteString()
 {
     _easyBuffer.WriteAll(_source);
     return(_easyBuffer.ReadByteString());
 }