Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,7 @@ internal void WriteSurrogateCharEntity(char lowChar, char highChar)
_attrValue.Append(lowChar);
}

_textWriter.Write("&#x");
_textWriter.Write(surrogateChar.ToString("X", NumberFormatInfo.InvariantInfo));
_textWriter.Write(';');
WriteIntEntityImpl(surrogateChar);
}

internal void Write(ReadOnlySpan<char> text)
Expand Down Expand Up @@ -477,16 +475,17 @@ internal void WriteCharEntity(char ch)
throw new ArgumentException(SR.Xml_InvalidSurrogateMissingLowChar);
}

string strVal = ((int)ch).ToString("X", NumberFormatInfo.InvariantInfo);
Span<char> chAsSpan = stackalloc char[8];
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if 8 is the correct value here.

((int)ch).TryFormat(chAsSpan, out int charsWritten, "X", NumberFormatInfo.InvariantInfo);
if (_cacheAttrValue)
{
Debug.Assert(_attrValue != null);
_attrValue.Append("&#x");
_attrValue.Append(strVal);
_attrValue.Append(chAsSpan.Slice(0, charsWritten));
_attrValue.Append(';');
}

WriteCharEntityImpl(strVal);
WriteCharEntityImpl(chAsSpan.Slice(0, charsWritten));
}

internal void WriteEntityRef(string name)
Expand All @@ -508,13 +507,22 @@ internal void WriteEntityRef(string name)

private void WriteCharEntityImpl(char ch)
{
WriteCharEntityImpl(((int)ch).ToString("X", NumberFormatInfo.InvariantInfo));
WriteIntEntityImpl(ch);
}

private void WriteIntEntityImpl(int value)
{
Span<char> chAsSpan = stackalloc char[8];
if (value.TryFormat(chAsSpan, out int charsWritten, "X", NumberFormatInfo.InvariantInfo))
WriteCharEntityImpl(chAsSpan.Slice(0, charsWritten));
else
WriteCharEntityImpl(value.ToString("X", NumberFormatInfo.InvariantInfo));
}

private void WriteCharEntityImpl(string strVal)
private void WriteCharEntityImpl(ReadOnlySpan<char> strSpan)
{
_textWriter.Write("&#x");
_textWriter.Write(strVal);
_textWriter.Write(strSpan);
_textWriter.Write(';');
}

Expand Down