示例#1
0
        private static bool TryFindEncodedCodePointBytesCountGoingBackwards(ReadOnlySpan <byte> buffer, out int encodedBytes)
        {
            encodedBytes = 1;
            ReadOnlySpan <byte> it = buffer;

            // TODO: Should we have something like: Span<byte>.(Slice from the back)
            for (; encodedBytes <= UnicodeConstants.Utf8MaxCodeUnitsPerCodePoint; encodedBytes++, it = it.Slice(0, it.Length - 1))
            {
                if (it.Length == 0)
                {
                    encodedBytes = default(int);
                    return(false);
                }

                // TODO: Should we have Span<byte>.Last?
                if (Utf8CodeUnit.IsFirstCodeUnitInEncodedCodePoint(it[it.Length - 1]))
                {
                    // output: encodedBytes
                    return(true);
                }
            }

            // Invalid unicode character or stream prematurely ended (which is still invalid character in that stream)
            encodedBytes = default(int);
            return(false);
        }
示例#2
0
        public bool EndsWith(Utf8CodeUnit codeUnit)
        {
            if (Length == 0)
            {
                return(false);
            }

            return(this[Length - 1] == codeUnit);
        }
示例#3
0
        public bool StartsWith(Utf8CodeUnit codeUnit)
        {
            if (Length == 0)
            {
                return(false);
            }

            return(this[0] == codeUnit);
        }
        public static bool TryCreateFrom(char value, out Utf8CodeUnit codeUnit)
        {
            if (IsAscii(value))
            {
                codeUnit = new Utf8CodeUnit(unchecked ((byte)value));
                return(true);
            }

            codeUnit = default(Utf8CodeUnit);
            return(false);
        }
        public static bool TryCreateFrom(char value, out Utf8CodeUnit codeUnit)
        {
            if (IsAscii(value))
            {
                codeUnit = new Utf8CodeUnit(unchecked((byte)value));
                return true;
            }

            codeUnit = default(Utf8CodeUnit);
            return false;
        }
示例#6
0
        public bool TrySubstringTo(Utf8CodeUnit codeUnit, out Utf8String result)
        {
            int idx = IndexOf(codeUnit);

            if (idx == StringNotFound)
            {
                result = default(Utf8String);
                return(false);
            }

            result = Substring(0, idx);
            return(true);
        }
示例#7
0
        // TODO: Should this be public?
        public int IndexOf(Utf8CodeUnit codeUnit)
        {
            // TODO: _buffer.IndexOf(codeUnit.Value); when Span has it

            for (int i = 0; i < Length; i++)
            {
                if (codeUnit == this[i])
                {
                    return(i);
                }
            }

            return(StringNotFound);
        }
示例#8
0
        private Utf8String ReadToByte(Utf8CodeUnit codeUnit, bool includeCodeUnit)
        {
            SkipAll();

            var count = 1;
            while (_str[_index] != codeUnit)
            {
                count++;
                _index++;
            }

            if (!includeCodeUnit)
            {
                count--;
                _index--;
            }

            var utf8Bytes = new byte[count];
            for (var i = 0; i < count; i++)
            {
                utf8Bytes[i] = (byte) _str[_index - count + i + 1];
            }
            _index++;

            if (!includeCodeUnit)
            {
                _index++;
            }

            return new Utf8String(utf8Bytes);
        }