Exemple #1
0
        /// <summary>
        /// Consume a run of HTML-encoding characters from the string
        /// </summary>
        /// <returns>The new position</returns>
        private async ValueTask <int> WriteEncodingChars(string s, int position)
        {
            while (position < s.Length)
            {
                int codePoint;
                int numberOfCharactersConsumed;
                if (char.IsSurrogatePair(s, position))
                {
                    codePoint = char.ConvertToUtf32(s, position);
                    numberOfCharactersConsumed = 2;
                }
                else if (char.IsSurrogate(s, position))
                {
                    await WriteUnicodeReplacementChar().ConfigureAwait(false);

                    position += 1;
                    continue;
                }
                else
                {
                    codePoint = char.ConvertToUtf32(s, position);
                    numberOfCharactersConsumed = 1;
                }

                if (!_htmlEncoder.WillEncode(codePoint))
                {
                    break;
                }

                if (!_htmlEncoder.TryEncodeUnicodeScalar(codePoint, _buffer, _bufPos, out var numberOfCharactersWritten))
                {
                    await Flush().ConfigureAwait(false);

                    if (!_htmlEncoder.TryEncodeUnicodeScalar(codePoint, _buffer, _bufPos, out numberOfCharactersWritten))
                    {
                        throw new InvalidOperationException("Buffer overflow when encoding HTML. Please report this as a bug in Eighty!");
                    }
                }
                position += numberOfCharactersConsumed;
                _bufPos  += numberOfCharactersWritten;
            }
            return(position);
        }
        /// <summary>
        /// Consume a run of HTML-encoding characters from the string
        /// </summary>
        private void WriteEncodingChars(ref char *start, char *end)
        {
            while (start < end)
            {
                int codePoint;
                int numberOfCharactersConsumed;
                if (end - start > 1 && char.IsSurrogatePair(*start, *(start + 1)))
                {
                    codePoint = char.ConvertToUtf32(*start, *(start + 1));
                    numberOfCharactersConsumed = 2;
                }
                else if (char.IsSurrogate(*start))
                {
                    WriteUnicodeReplacementChar();
                    start += 1;
                    continue;
                }
                else
                {
                    codePoint = (int)*start;
                    numberOfCharactersConsumed = 1;
                }

                if (!_htmlEncoder.WillEncode(codePoint))
                {
                    break;
                }

                if (!_htmlEncoder.TryEncodeUnicodeScalar(codePoint, _current, (int)(_end - _current), out var numberOfCharactersWritten))
                {
                    Flush();  // _current gets reset here - don't try to deduplicate (_end - _current)!
                    if (!_htmlEncoder.TryEncodeUnicodeScalar(codePoint, _current, (int)(_end - _current), out numberOfCharactersWritten))
                    {
                        throw new InvalidOperationException("Buffer overflow when encoding HTML. Please report this as a bug in Eighty!");
                    }
                }
                start    += numberOfCharactersConsumed;
                _current += numberOfCharactersWritten;
            }
        }