예제 #1
0
        public void Write(char[] array, int offset, int count)
        {
            if (null == array)
            {
                throw new ArgumentNullException(nameof(array));
            }

            if (0 > offset)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }

            if (0 > count)
            {
                throw new ArgumentOutOfRangeException(nameof(count));
            }

            if (count > array.Length - offset)
            {
                throw new ArgumentOutOfRangeException(nameof(count));
            }

            if (_cacheAttrValue)
            {
                _attrValue.Append(array, offset, count);
            }

            int  endPos = offset + count;
            int  i      = offset;
            char ch     = (char)0;

            for (;;)
            {
                int startPos = i;
                //unsafe
                //{
                while (i < endPos && _xmlCharType.IsAttributeValueChar(ch = array[i]))
                {
                    i++;
                }
                //}

                if (startPos < i)
                {
                    _textWriter.Write(array, startPos, i - startPos);
                }
                if (i == endPos)
                {
                    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 < endPos)
                        {
                            WriteSurrogateChar(array[++i], ch);
                        }
                        else
                        {
                            throw new Exception("throw new ArgumentException(SR.Xml_SurrogatePairSplit);");
                        }
                    }
                    else if (XmlCharType.IsLowSurrogate(ch))
                    {
                        throw new Exception("throw XmlConvert.CreateInvalidHighSurrogateCharException(ch)");
                    }
                    else
                    {
                        Debug.Assert((ch < 0x20 && !_xmlCharType.IsWhiteSpace(ch)) || (ch > 0xFFFD));
                        WriteCharEntityImpl(ch);
                    }
                    break;
                }
                i++;
            }
        }