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 || 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 XmlConvertEx.CreateInvalidSurrogatePairException(lowChar, ch); } } throw new ArgumentException(SR.Xml_InvalidSurrogateMissingLowChar); } else if (XmlCharType.IsLowSurrogate(ch)) { throw XmlConvertEx.CreateInvalidHighSurrogateCharException(ch); } else { i++; } } textWriter.Write(text); return; }
internal void WriteSurrogateChar(char lowChar, char highChar) { if (!XmlCharType.IsLowSurrogate(lowChar) || !XmlCharType.IsHighSurrogate(highChar)) { throw XmlConvertEx.CreateInvalidSurrogatePairException(lowChar, highChar); } textWriter.Write(highChar); textWriter.Write(lowChar); }
internal void WriteSurrogateCharEntity(char lowChar, char highChar) { if (!XmlCharType.IsLowSurrogate(lowChar) || !XmlCharType.IsHighSurrogate(highChar)) { throw XmlConvertEx.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(';'); }
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) { 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 XmlConvertEx.CreateInvalidSurrogatePairException(text[i], ch); } } else if (XmlCharType.IsLowSurrogate(ch)) { throw XmlConvertEx.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) { i++; } } } }