diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlTextEncoder.cs b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlTextEncoder.cs index d2fdefe60d0dc..4d894dfb1e9af 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlTextEncoder.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlTextEncoder.cs @@ -4,7 +4,6 @@ using System.IO; using System.Text; using System.Diagnostics; -using System.Globalization; namespace System.Xml { @@ -230,11 +229,11 @@ internal void Write(char[] array, int offset, int count) internal void WriteSurrogateCharEntity(char lowChar, char highChar) { - if (!XmlCharType.IsLowSurrogate(lowChar) || - !XmlCharType.IsHighSurrogate(highChar)) + if (!XmlCharType.IsLowSurrogate(lowChar) || !XmlCharType.IsHighSurrogate(highChar)) { throw XmlConvert.CreateInvalidSurrogatePairException(lowChar, highChar); } + int surrogateChar = XmlCharType.CombineSurrogateChar(lowChar, highChar); if (_cacheAttrValue) @@ -244,9 +243,9 @@ internal void WriteSurrogateCharEntity(char lowChar, char highChar) _attrValue.Append(lowChar); } - _textWriter.Write("&#x"); - _textWriter.Write(surrogateChar.ToString("X", NumberFormatInfo.InvariantInfo)); - _textWriter.Write(';'); + Span span = stackalloc char[12]; + int charsWritten = WriteCharToSpan(span, surrogateChar); + _textWriter.Write(span.Slice(0, charsWritten)); } internal void Write(ReadOnlySpan text) @@ -478,8 +477,6 @@ internal void WriteRaw(char[] array, int offset, int count) _textWriter.Write(array, offset, count); } - - internal void WriteCharEntity(char ch) { if (XmlCharType.IsSurrogate(ch)) @@ -487,16 +484,17 @@ internal void WriteCharEntity(char ch) throw new ArgumentException(SR.Xml_InvalidSurrogateMissingLowChar); } - string strVal = ((int)ch).ToString("X", NumberFormatInfo.InvariantInfo); + Span span = stackalloc char[12]; + int charsWritten = WriteCharToSpan(span, ch); + ReadOnlySpan ros = span.Slice(0, charsWritten); + if (_cacheAttrValue) { Debug.Assert(_attrValue != null); - _attrValue.Append("&#x"); - _attrValue.Append(strVal); - _attrValue.Append(';'); + _attrValue.Append(ros); } - WriteCharEntityImpl(strVal); + _textWriter.Write(ros); } internal void WriteEntityRef(string name) @@ -518,14 +516,21 @@ internal void WriteEntityRef(string name) private void WriteCharEntityImpl(char ch) { - WriteCharEntityImpl(((int)ch).ToString("X", NumberFormatInfo.InvariantInfo)); + Span span = stackalloc char[12]; + int charsWritten = WriteCharToSpan(span, ch); + _textWriter.Write(span.Slice(0, charsWritten)); } - private void WriteCharEntityImpl(string strVal) + private static int WriteCharToSpan(Span destination, int ch) { - _textWriter.Write("&#x"); - _textWriter.Write(strVal); - _textWriter.Write(';'); + Debug.Assert(destination.Length >= 12); + destination[0] = '&'; + destination[1] = '#'; + destination[2] = 'x'; + ((uint)ch).TryFormat(destination.Slice(3), out int charsWritten, "X"); + Debug.Assert(charsWritten != 0); + destination[charsWritten + 3] = ';'; + return charsWritten; } private void WriteEntityRefImpl(string name)