feat: Reworked xml attribute utils for performance

This commit is contained in:
Chris Cameron
2018-11-09 11:42:36 -05:00
parent c048c4fc65
commit 2d4bc57ed8
7 changed files with 55 additions and 112 deletions

View File

@@ -1,80 +0,0 @@
using System;
namespace ICD.Common.Utils.Xml
{
/// <summary>
/// IcdXmlAttribute represents an attribute="value" pair from xml.
/// </summary>
public struct IcdXmlAttribute : IEquatable<IcdXmlAttribute>
{
private readonly string m_Name;
private readonly string m_Value;
public string Name { get { return m_Name; } }
public string Value { get { return m_Value; } }
/// <summary>
/// Constructor.
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
public IcdXmlAttribute(string name, string value)
{
m_Name = name;
m_Value = value;
}
/// <summary>
/// Implementing default equality.
/// </summary>
/// <param name="a1"></param>
/// <param name="a2"></param>
/// <returns></returns>
public static bool operator ==(IcdXmlAttribute a1, IcdXmlAttribute a2)
{
return a1.Equals(a2);
}
/// <summary>
/// Implementing default inequality.
/// </summary>
/// <param name="a1"></param>
/// <param name="a2"></param>
/// <returns></returns>
public static bool operator !=(IcdXmlAttribute a1, IcdXmlAttribute a2)
{
return !a1.Equals(a2);
}
public bool Equals(IcdXmlAttribute other)
{
return m_Name == other.m_Name &&
m_Value == other.m_Value;
}
/// <summary>
/// Returns true if this instance is equal to the given object.
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public override bool Equals(object other)
{
return other is IcdXmlAttribute && Equals((IcdXmlAttribute)other);
}
/// <summary>
/// Gets the hashcode for this instance.
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
unchecked
{
int hash = 17;
hash = hash * 23 + (m_Name == null ? 0 : m_Name.GetHashCode());
hash = hash * 23 + (m_Value == null ? 0 : m_Value.GetHashCode());
return hash;
}
}
}
}

View File

@@ -28,7 +28,21 @@ namespace ICD.Common.Utils.Xml
if (extends == null)
throw new ArgumentNullException("extends");
return extends.GetAttribute(name) != null;
string unused;
return extends.TryGetAttribute(name, out unused);
}
/// <summary>
/// Returns true if the attribute exists.
/// </summary>
/// <param name="extends"></param>
/// <param name="name"></param>
/// <param name="value"></param>
/// <returns></returns>
[PublicAPI]
public static bool TryGetAttribute(this IcdXmlReader extends, string name, out string value)
{
return (value = extends.GetAttribute(name)) != null;
}
/// <summary>
@@ -37,13 +51,13 @@ namespace ICD.Common.Utils.Xml
/// <param name="extends"></param>
/// <returns></returns>
[PublicAPI]
public static IEnumerable<IcdXmlAttribute> GetAttributes(this IcdXmlReader extends)
public static IEnumerable<KeyValuePair<string, string>> GetAttributes(this IcdXmlReader extends)
{
if (extends == null)
throw new ArgumentNullException("extends");
while (extends.MoveToNextAttribute())
yield return new IcdXmlAttribute(extends.Name, extends.Value);
yield return new KeyValuePair<string, string>(extends.Name, extends.Value);
// Move back to element.
extends.MoveToElement();

View File

@@ -55,7 +55,7 @@ namespace ICD.Common.Utils.Xml
/// <param name="xml"></param>
/// <returns></returns>
[PublicAPI]
public static IEnumerable<IcdXmlAttribute> GetAttributes(string xml)
public static IEnumerable<KeyValuePair<string, string>> GetAttributes(string xml)
{
using (IcdXmlReader reader = new IcdXmlReader(xml))
{
@@ -71,13 +71,30 @@ namespace ICD.Common.Utils.Xml
/// <param name="name"></param>
/// <returns></returns>
[PublicAPI]
public static IcdXmlAttribute GetAttribute(string xml, string name)
public static string GetAttribute(string xml, string name)
{
IcdXmlAttribute output;
if (GetAttributes(xml).TryFirst(a => a.Name == name, out output))
return output;
using (IcdXmlReader reader = new IcdXmlReader(xml))
{
reader.ReadToNextElement();
return reader.GetAttribute(name);
}
}
throw new FormatException(string.Format("No attribute with name {0}", name));
/// <summary>
/// Convenience method for getting attribute by name.
/// </summary>
/// <param name="xml"></param>
/// <param name="name"></param>
/// <param name="value"></param>
/// <returns></returns>
[PublicAPI]
public static bool TryGetAttribute(string xml, string name, out string value)
{
using (IcdXmlReader reader = new IcdXmlReader(xml))
{
reader.ReadToNextElement();
return reader.TryGetAttribute(name, out value);
}
}
/// <summary>