From 39e8a6e0b0a53e621cd997b53122663a4ae114f0 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Tue, 29 Jun 2021 08:05:01 -0400 Subject: [PATCH] Avoid allocating unused prefix/localName in XmlCharCheckingReader.ValidateQName (#54836) --- .../src/System/Xml/Core/XmlCharCheckingReader.cs | 3 +-- .../src/System/Xml/ValidateNames.cs | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlCharCheckingReader.cs b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlCharCheckingReader.cs index 38d606efedb80..f1f7bdd4e94e8 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlCharCheckingReader.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlCharCheckingReader.cs @@ -624,8 +624,7 @@ private void CheckWhitespace(string value) private void ValidateQName(string name) { - string prefix, localName; - ValidateNames.ParseQNameThrow(name, out prefix, out localName); + ValidateNames.ParseQNameThrow(name); } private void ValidateQName(string prefix, string localName) diff --git a/src/libraries/System.Private.Xml/src/System/Xml/ValidateNames.cs b/src/libraries/System.Private.Xml/src/System/Xml/ValidateNames.cs index 4aadc807bfb3c..cce4cf01d5a68 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/ValidateNames.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/ValidateNames.cs @@ -261,9 +261,9 @@ internal static int ParseQName(string s, int offset, out int colonOffset) /// /// Calls parseQName and throws exception if the resulting name is not a valid QName. - /// Returns the prefix and local name parts. + /// Returns the colon offset in the name. /// - internal static void ParseQNameThrow(string s, out string prefix, out string localName) + internal static int ParseQNameThrow(string s) { int colonOffset; int len = ParseQName(s, 0, out colonOffset); @@ -274,6 +274,16 @@ internal static void ParseQNameThrow(string s, out string prefix, out string loc ThrowInvalidName(s, 0, len); } + return colonOffset; + } + + /// + /// Calls parseQName and throws exception if the resulting name is not a valid QName. + /// Returns the prefix and local name parts. + /// + internal static void ParseQNameThrow(string s, out string prefix, out string localName) + { + int colonOffset = ParseQNameThrow(s); if (colonOffset != 0) { prefix = s.Substring(0, colonOffset);