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

@@ -5,6 +5,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
## [Unreleased] ## [Unreleased]
### Added
- XML TryGetAttribute methods
### Changed
- Performance improvements when working with xml attributes
### Removed
- Removed IcdXmlAttribute
## [7.1.0] - 2018-11-08 ## [7.1.0] - 2018-11-08
### Added ### Added

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using ICD.Common.Utils.Xml; using ICD.Common.Utils.Xml;
using NUnit.Framework; using NUnit.Framework;
@@ -49,10 +50,10 @@ namespace ICD.Common.Utils.Tests.Extensions
reader.ReadToNextElement(); reader.ReadToNextElement();
IcdXmlAttribute[] attributes = reader.GetAttributes().ToArray(); KeyValuePair<string, string>[] attributes = reader.GetAttributes().ToArray();
Assert.AreEqual(2, attributes.Length); Assert.AreEqual(2, attributes.Length);
Assert.AreEqual("attr1", attributes[0].Name); Assert.AreEqual("attr1", attributes[0].Key);
Assert.AreEqual("attr2", attributes[1].Name); Assert.AreEqual("attr2", attributes[1].Key);
} }
[Test] [Test]

View File

@@ -1,17 +0,0 @@
using ICD.Common.Properties;
using ICD.Common.Utils.Xml;
using NUnit.Framework;
namespace ICD.Common.Utils.Tests.Xml
{
[TestFixture]
public sealed class IcdXmlAttributeTest
{
[Test, UsedImplicitly]
public void ValueAsIntTest()
{
IcdXmlAttribute attribute = new IcdXmlAttribute("test", "12");
Assert.AreEqual("12", attribute.Value);
}
}
}

View File

@@ -56,12 +56,12 @@ namespace ICD.Common.Utils.Tests.Xml
{ {
reader.ReadToNextElement(); reader.ReadToNextElement();
IcdXmlAttribute[] attributes = reader.GetAttributes().ToArray(); KeyValuePair<string, string>[] attributes = reader.GetAttributes().ToArray();
Assert.AreEqual(2, attributes.Length); Assert.AreEqual(2, attributes.Length);
Assert.AreEqual("attr1", attributes[0].Name); Assert.AreEqual("attr1", attributes[0].Key);
Assert.AreEqual("1", attributes[0].Value); Assert.AreEqual("1", attributes[0].Value);
Assert.AreEqual("attr2", attributes[1].Name); Assert.AreEqual("attr2", attributes[1].Key);
Assert.AreEqual("2", attributes[1].Value); Assert.AreEqual("2", attributes[1].Value);
} }
} }

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) if (extends == null)
throw new ArgumentNullException("extends"); 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> /// <summary>
@@ -37,13 +51,13 @@ namespace ICD.Common.Utils.Xml
/// <param name="extends"></param> /// <param name="extends"></param>
/// <returns></returns> /// <returns></returns>
[PublicAPI] [PublicAPI]
public static IEnumerable<IcdXmlAttribute> GetAttributes(this IcdXmlReader extends) public static IEnumerable<KeyValuePair<string, string>> GetAttributes(this IcdXmlReader extends)
{ {
if (extends == null) if (extends == null)
throw new ArgumentNullException("extends"); throw new ArgumentNullException("extends");
while (extends.MoveToNextAttribute()) 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. // Move back to element.
extends.MoveToElement(); extends.MoveToElement();

View File

@@ -55,7 +55,7 @@ namespace ICD.Common.Utils.Xml
/// <param name="xml"></param> /// <param name="xml"></param>
/// <returns></returns> /// <returns></returns>
[PublicAPI] [PublicAPI]
public static IEnumerable<IcdXmlAttribute> GetAttributes(string xml) public static IEnumerable<KeyValuePair<string, string>> GetAttributes(string xml)
{ {
using (IcdXmlReader reader = new IcdXmlReader(xml)) using (IcdXmlReader reader = new IcdXmlReader(xml))
{ {
@@ -71,13 +71,30 @@ namespace ICD.Common.Utils.Xml
/// <param name="name"></param> /// <param name="name"></param>
/// <returns></returns> /// <returns></returns>
[PublicAPI] [PublicAPI]
public static IcdXmlAttribute GetAttribute(string xml, string name) public static string GetAttribute(string xml, string name)
{ {
IcdXmlAttribute output; using (IcdXmlReader reader = new IcdXmlReader(xml))
if (GetAttributes(xml).TryFirst(a => a.Name == name, out output)) {
return output; 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> /// <summary>