Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor XmlQualifiedName #57646

Merged
merged 4 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
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
80 changes: 29 additions & 51 deletions src/libraries/System.Private.Xml/src/System/Xml/XmlQualifiedName.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

namespace System.Xml
Expand All @@ -12,14 +10,12 @@ namespace System.Xml
/// </devdoc>
public class XmlQualifiedName
{
private string _name;
private string _ns;
private int _hash;

/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public static readonly XmlQualifiedName Empty = new XmlQualifiedName(string.Empty);
public static readonly XmlQualifiedName Empty = new(string.Empty);

/// <devdoc>
/// <para>[To be supplied.]</para>
Expand All @@ -36,83 +32,69 @@ public XmlQualifiedName(string? name) : this(name, string.Empty) { }
/// </devdoc>
public XmlQualifiedName(string? name, string? ns)
{
_ns = ns ?? string.Empty;
_name = name ?? string.Empty;
Namespace = ns ?? string.Empty;
Name = name ?? string.Empty;
}

/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public string Namespace
{
get { return _ns; }
}

public string Namespace { get; private set; }
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public string Name
{
get { return _name; }
}

public string Name { get; private set; }
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public override int GetHashCode()
{
if (_hash == 0)
{
_hash = Name.GetHashCode() /*+ Namespace.GetHashCode()*/; // for perf reasons we are not taking ns's hashcode.
_hash = Name.GetHashCode(); /*+ Namespace.GetHashCode()*/ // for perf reasons we are not taking ns's hashcode.
}
return _hash;
}

/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public bool IsEmpty
{
get { return Name.Length == 0 && Namespace.Length == 0; }
}

public bool IsEmpty => Name.Length == 0 && Namespace.Length == 0;
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public override string ToString()
{
return Namespace.Length == 0 ? Name : string.Concat(Namespace, ":", Name);
return Namespace.Length == 0 ? Name : $"{Namespace}:{Name}";
}

/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public override bool Equals([NotNullWhen(true)] object? other)
{
if ((object)this == other)
if (ReferenceEquals(this, other))
{
return true;
}

XmlQualifiedName? qname = other as XmlQualifiedName;
if (qname != null)
{
return (Name == qname.Name && Namespace == qname.Namespace);
}

return false;
return other is XmlQualifiedName qName && Name == qName.Name && Namespace == qName.Namespace;
}

/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public static bool operator ==(XmlQualifiedName? a, XmlQualifiedName? b)
{
if ((object?)a == (object?)b)
if (ReferenceEquals(a, b))
{
return true;
}

if (a is null || b is null)
{
return false;
}

return a.Name == b.Name && a.Namespace == b.Namespace;
}
Expand All @@ -128,44 +110,42 @@ public override bool Equals([NotNullWhen(true)] object? other)
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public static string ToString(string name, string ns)
public static string ToString(string name, string? ns)
{
return ns == null || ns.Length == 0 ? name : ns + ":" + name;
return ns == null || ns.Length == 0 ? name : $"{ns}:{name}";
}

// --------- Some useful internal stuff -----------------
internal void Init(string? name, string? ns)
{
_name = name ?? string.Empty;
_ns = ns ?? string.Empty;
Name = name ?? string.Empty;
Namespace = ns ?? string.Empty;
_hash = 0;
}

internal void SetNamespace(string? ns)
{
_ns = ns ?? string.Empty; // Not changing hash since ns is not used to compute hashcode
Namespace = ns ?? string.Empty; // Not changing hash since ns is not used to compute hashcode
}

internal void Verify()
{
XmlConvert.VerifyNCName(_name);
if (_ns.Length != 0)
XmlConvert.VerifyNCName(Name);
if (Namespace.Length != 0)
{
XmlConvert.ToUri(_ns);
XmlConvert.ToUri(Namespace);
}
}

internal void Atomize(XmlNameTable nameTable)
{
Debug.Assert(_name != null);
_name = nameTable.Add(_name);
_ns = nameTable.Add(_ns);
Name = nameTable.Add(Name);
Namespace = nameTable.Add(Namespace);
}

internal static XmlQualifiedName Parse(string s, IXmlNamespaceResolver nsmgr, out string prefix)
{
string localName;
ValidateNames.ParseQNameThrow(s, out prefix, out localName);
ValidateNames.ParseQNameThrow(s, out prefix, out string localName);

string? uri = nsmgr.LookupNamespace(prefix);
if (uri == null)
Expand All @@ -174,11 +154,9 @@ internal static XmlQualifiedName Parse(string s, IXmlNamespaceResolver nsmgr, ou
{
throw new XmlException(SR.Xml_UnknownNs, prefix);
}
else
{
// Re-map namespace of empty prefix to string.Empty when there is no default namespace declared
uri = string.Empty;
}

// Re-map namespace of empty prefix to string.Empty when there is no default namespace declared
uri = string.Empty;
}

return new XmlQualifiedName(localName, uri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ public XmlQualifiedName(string? name, string? ns) { }
public static bool operator ==(System.Xml.XmlQualifiedName? a, System.Xml.XmlQualifiedName? b) { throw null; }
public static bool operator !=(System.Xml.XmlQualifiedName? a, System.Xml.XmlQualifiedName? b) { throw null; }
public override string ToString() { throw null; }
public static string ToString(string name, string ns) { throw null; }
public static string ToString(string name, string? ns) { throw null; }
}
[System.Diagnostics.DebuggerDisplayAttribute("{debuggerDisplayProxy}")]
[System.Diagnostics.DebuggerDisplayAttribute("{debuggerDisplayProxy}")]
Expand Down