示例#1
0
        internal void WriteRawWithSurrogateChecking(string text)
        {
            if (text == null)
            {
                return;
            }
            if (_cacheAttrValue)
            {
                _attrValue.Append(text);
            }

            int  len = text.Length;
            int  i   = 0;
            char ch  = (char)0;

            for (; ;)
            {
                unsafe
                {
                    while (i < len &&
                           ((_xmlCharType.charProperties[ch = text[i]] & XmlCharType.fCharData) != 0 || // ( xmlCharType.IsCharData( ( ch = text[i] ) )
                            ch < 0x20))
                    {
                        i++;
                    }
                }
                if (i == len)
                {
                    break;
                }
                if (XmlCharType.IsHighSurrogate(ch))
                {
                    if (i + 1 < len)
                    {
                        char lowChar = text[i + 1];
                        if (XmlCharType.IsLowSurrogate(lowChar))
                        {
                            i += 2;
                            continue;
                        }
                        else
                        {
                            throw XmlConvert.CreateInvalidSurrogatePairException(lowChar, ch);
                        }
                    }
                    throw new ArgumentException(ResXml.Xml_InvalidSurrogateMissingLowChar);
                }
                else if (XmlCharType.IsLowSurrogate(ch))
                {
                    throw XmlConvert.CreateInvalidHighSurrogateCharException(ch);
                }
                else
                {
                    i++;
                }
            }

            _textWriter.Write(text);
            return;
        }
示例#2
0
        public override bool Fallback(char charUnknownHigh, char charUnknownLow, int index)
        {
            // check input surrogate pair
            if (!char.IsSurrogatePair(charUnknownHigh, charUnknownLow))
            {
                throw XmlConvert.CreateInvalidSurrogatePairException(charUnknownHigh, charUnknownLow);
            }

            // If we are already in fallback, throw, it's probably at the suspect character in charEntity
            if (_charEntityIndex >= 0)
            {
                (new EncoderExceptionFallback()).CreateFallbackBuffer().Fallback(charUnknownHigh, charUnknownLow, index);
            }

            if (_parent.CanReplaceAt(index))
            {
                // Create the replacement character entity
                _charEntity      = string.Format(CultureInfo.InvariantCulture, "&#x{0:X};", new object[] { SurrogateCharToUtf32(charUnknownHigh, charUnknownLow) });
                _charEntityIndex = 0;
                return(true);
            }
            else
            {
                EncoderFallbackBuffer errorFallbackBuffer = (new EncoderExceptionFallback()).CreateFallbackBuffer();
                errorFallbackBuffer.Fallback(charUnknownHigh, charUnknownLow, index);
                return(false);
            }
        }
示例#3
0
        internal void WriteSurrogateChar(char lowChar, char highChar)
        {
            if (!XmlCharType.IsLowSurrogate(lowChar) ||
                !XmlCharType.IsHighSurrogate(highChar))
            {
                throw XmlConvert.CreateInvalidSurrogatePairException(lowChar, highChar);
            }

            _textWriter.Write(highChar);
            _textWriter.Write(lowChar);
        }
示例#4
0
        internal void WriteSurrogateCharEntity(char lowChar, char highChar)
        {
            if (!XmlCharType.IsLowSurrogate(lowChar) ||
                !XmlCharType.IsHighSurrogate(highChar))
            {
                throw XmlConvert.CreateInvalidSurrogatePairException(lowChar, highChar);
            }
            int surrogateChar = XmlCharType.CombineSurrogateChar(lowChar, highChar);

            if (_cacheAttrValue)
            {
                _attrValue.Append(highChar);
                _attrValue.Append(lowChar);
            }

            _textWriter.Write("&#x");
            _textWriter.Write(surrogateChar.ToString("X", NumberFormatInfo.InvariantInfo));
            _textWriter.Write(';');
        }
示例#5
0
        internal void Write(string text)
        {
            if (text == null)
            {
                return;
            }

            if (_cacheAttrValue)
            {
                _attrValue.Append(text);
            }

            // scan through the string to see if there are any characters to be escaped
            int  len      = text.Length;
            int  i        = 0;
            int  startPos = 0;
            char ch       = (char)0;

            for (; ;)
            {
                unsafe
                {
                    while (i < len && (_xmlCharType.charProperties[ch = text[i]] & XmlCharType.fAttrValue) != 0)
                    { // ( xmlCharType.IsAttributeValueChar( ( ch = text[i] ) ) ) ) {
                        i++;
                    }
                }
                if (i == len)
                {
                    // reached the end of the string -> write it whole out
                    _textWriter.Write(text);
                    return;
                }
                if (_inAttribute)
                {
                    if (ch == 0x9)
                    {
                        i++;
                        continue;
                    }
                }
                else
                {
                    if (ch == 0x9 || ch == 0xA || ch == 0xD || ch == '"' || ch == '\'')
                    {
                        i++;
                        continue;
                    }
                }
                // some character that needs to be escaped is found:
                break;
            }

            char[] helperBuffer = new char[256];
            for (; ;)
            {
                if (startPos < i)
                {
                    WriteStringFragment(text, startPos, i - startPos, helperBuffer);
                }
                if (i == len)
                {
                    break;
                }

                switch (ch)
                {
                case (char)0x9:
                    _textWriter.Write(ch);
                    break;

                case (char)0xA:
                case (char)0xD:
                    if (_inAttribute)
                    {
                        WriteCharEntityImpl(ch);
                    }
                    else
                    {
                        _textWriter.Write(ch);
                    }
                    break;

                case '<':
                    WriteEntityRefImpl("lt");
                    break;

                case '>':
                    WriteEntityRefImpl("gt");
                    break;

                case '&':
                    WriteEntityRefImpl("amp");
                    break;

                case '\'':
                    if (_inAttribute && _quoteChar == ch)
                    {
                        WriteEntityRefImpl("apos");
                    }
                    else
                    {
                        _textWriter.Write('\'');
                    }
                    break;

                case '"':
                    if (_inAttribute && _quoteChar == ch)
                    {
                        WriteEntityRefImpl("quot");
                    }
                    else
                    {
                        _textWriter.Write('"');
                    }
                    break;

                default:
                    if (XmlCharType.IsHighSurrogate(ch))
                    {
                        if (i + 1 < len)
                        {
                            WriteSurrogateChar(text[++i], ch);
                        }
                        else
                        {
                            throw XmlConvert.CreateInvalidSurrogatePairException(text[i], ch);
                        }
                    }
                    else if (XmlCharType.IsLowSurrogate(ch))
                    {
                        throw XmlConvert.CreateInvalidHighSurrogateCharException(ch);
                    }
                    else
                    {
                        Debug.Assert((ch < 0x20 && !_xmlCharType.IsWhiteSpace(ch)) || (ch > 0xFFFD));
                        WriteCharEntityImpl(ch);
                    }
                    break;
                }
                i++;
                startPos = i;
                unsafe
                {
                    while (i < len && (_xmlCharType.charProperties[ch = text[i]] & XmlCharType.fAttrValue) != 0)
                    { // ( xmlCharType.IsAttributeValueChar( ( text[i] ) ) ) ) {
                        i++;
                    }
                }
            }
        }