mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-17 05:35:07 +00:00
Merge remote-tracking branch 'origin/dev' into dev
This commit is contained in:
@@ -192,8 +192,19 @@ namespace ICD.Common.Utils.Collections
|
||||
/// <param name="items"></param>
|
||||
public void AddRange(IEnumerable<T> items)
|
||||
{
|
||||
if (items == null)
|
||||
throw new ArgumentNullException("items");
|
||||
|
||||
foreach (T item in items)
|
||||
Add(item);
|
||||
{
|
||||
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||
if (item == null)
|
||||
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||
throw new InvalidOperationException("item");
|
||||
|
||||
if (!m_Dict.ContainsKey(item))
|
||||
m_Dict[item] = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -53,12 +53,12 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="item">Outputs the first item in the sequence.</param>
|
||||
/// <returns></returns>
|
||||
public static bool TryFirstOrDefault<T>(this IEnumerable<T> extends, out T item)
|
||||
public static bool TryFirst<T>(this IEnumerable<T> extends, out T item)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
return extends.TryFirstOrDefault(i => true, out item);
|
||||
return extends.TryFirst(i => true, out item);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -69,7 +69,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="item">Outputs the first item in the sequence.</param>
|
||||
/// <returns></returns>
|
||||
public static bool TryFirstOrDefault<T>(this IEnumerable<T> extends, Func<T, bool> predicate, out T item)
|
||||
public static bool TryFirst<T>(this IEnumerable<T> extends, Func<T, bool> predicate, out T item)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
68
ICD.Common.Utils/Extensions/ListExtensions.cs
Normal file
68
ICD.Common.Utils/Extensions/ListExtensions.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using ICD.Common.Properties;
|
||||
|
||||
namespace ICD.Common.Utils.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for working with Lists.
|
||||
/// </summary>
|
||||
public static class ListExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds the item into a sorted list.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="item"></param>
|
||||
[PublicAPI]
|
||||
public static void AddSorted<T>(this List<T> extends, T item)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
extends.AddSorted(item, Comparer<T>.Default);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the item into a sorted list.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="comparer"></param>
|
||||
[PublicAPI]
|
||||
public static void AddSorted<T>(this List<T> extends, T item, IComparer<T> comparer)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
if (comparer == null)
|
||||
throw new ArgumentNullException("comparer");
|
||||
|
||||
if (extends.Count == 0)
|
||||
{
|
||||
extends.Add(item);
|
||||
return;
|
||||
}
|
||||
|
||||
if (comparer.Compare(extends[extends.Count - 1], item) <= 0)
|
||||
{
|
||||
extends.Add(item);
|
||||
return;
|
||||
}
|
||||
|
||||
if (comparer.Compare(extends[0], item) >= 0)
|
||||
{
|
||||
extends.Insert(0, item);
|
||||
return;
|
||||
}
|
||||
|
||||
int index = extends.BinarySearch(item);
|
||||
if (index < 0)
|
||||
index = ~index;
|
||||
|
||||
extends.Insert(index, item);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -85,6 +85,7 @@
|
||||
<Compile Include="EventArguments\XmlRecursionEventArgs.cs" />
|
||||
<None Include="ObfuscationSettings.cs" />
|
||||
<Compile Include="Extensions\ByteExtensions.cs" />
|
||||
<Compile Include="Extensions\ListExtensions.cs" />
|
||||
<Compile Include="Properties\Annotations.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Services\Logging\ILoggerService.cs" />
|
||||
|
||||
@@ -28,7 +28,7 @@ namespace ICD.Common.Utils.Xml
|
||||
}
|
||||
catch (XmlException e)
|
||||
{
|
||||
throw new IcdXmlException(e.Message, e, e.LineNumber, e.LinePosition);
|
||||
throw new IcdXmlException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
using System;
|
||||
#if SIMPLSHARP
|
||||
using Crestron.SimplSharp;
|
||||
#else
|
||||
using System.Xml;
|
||||
#endif
|
||||
|
||||
namespace ICD.Common.Utils.Xml
|
||||
{
|
||||
@@ -20,5 +25,14 @@ namespace ICD.Common.Utils.Xml
|
||||
m_LineNumber = lineNumber;
|
||||
m_LinePosition = linePosition;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="inner"></param>
|
||||
public IcdXmlException(XmlException inner)
|
||||
: this(inner.Message, inner, inner.LineNumber, inner.LinePosition)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,26 +50,54 @@ namespace ICD.Common.Utils.Xml
|
||||
|
||||
public bool MoveToNextAttribute()
|
||||
{
|
||||
return m_Reader.MoveToNextAttribute();
|
||||
try
|
||||
{
|
||||
return m_Reader.MoveToNextAttribute();
|
||||
}
|
||||
catch (XmlException e)
|
||||
{
|
||||
throw new IcdXmlException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void MoveToElement()
|
||||
{
|
||||
m_Reader.MoveToElement();
|
||||
try
|
||||
{
|
||||
m_Reader.MoveToElement();
|
||||
}
|
||||
catch (XmlException e)
|
||||
{
|
||||
throw new IcdXmlException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public string GetAttribute(string name)
|
||||
{
|
||||
return m_Reader.GetAttribute(name);
|
||||
try
|
||||
{
|
||||
return m_Reader.GetAttribute(name);
|
||||
}
|
||||
catch (XmlException e)
|
||||
{
|
||||
throw new IcdXmlException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public string ReadString()
|
||||
{
|
||||
try
|
||||
{
|
||||
#if SIMPLSHARP
|
||||
return m_Reader.ReadString();
|
||||
return m_Reader.ReadString();
|
||||
#else
|
||||
return m_Reader.ReadElementContentAsString();
|
||||
return m_Reader.ReadElementContentAsString();
|
||||
#endif
|
||||
}
|
||||
catch (XmlException e)
|
||||
{
|
||||
throw new IcdXmlException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Read()
|
||||
@@ -80,47 +108,96 @@ namespace ICD.Common.Utils.Xml
|
||||
}
|
||||
catch (XmlException e)
|
||||
{
|
||||
throw new IcdXmlException(e.Message, e, e.LineNumber, e.LinePosition);
|
||||
throw new IcdXmlException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
try
|
||||
{
|
||||
#if SIMPLSHARP
|
||||
m_Reader.Dispose(true);
|
||||
m_Reader.Dispose(true);
|
||||
#else
|
||||
m_Reader.Dispose();
|
||||
m_Reader.Dispose();
|
||||
#endif
|
||||
}
|
||||
catch (XmlException e)
|
||||
{
|
||||
throw new IcdXmlException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void Skip()
|
||||
{
|
||||
m_Reader.Skip();
|
||||
try
|
||||
{
|
||||
m_Reader.Skip();
|
||||
}
|
||||
catch (XmlException e)
|
||||
{
|
||||
throw new IcdXmlException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public string ReadElementContentAsString()
|
||||
{
|
||||
return m_Reader.ReadElementContentAsString();
|
||||
try
|
||||
{
|
||||
return m_Reader.ReadElementContentAsString();
|
||||
}
|
||||
catch (XmlException e)
|
||||
{
|
||||
throw new IcdXmlException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public string ReadOuterXml()
|
||||
{
|
||||
return m_Reader.ReadOuterXml();
|
||||
try
|
||||
{
|
||||
return m_Reader.ReadOuterXml();
|
||||
}
|
||||
catch (XmlException e)
|
||||
{
|
||||
throw new IcdXmlException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public string ReadInnerXml()
|
||||
{
|
||||
return m_Reader.ReadInnerXml();
|
||||
try
|
||||
{
|
||||
return m_Reader.ReadInnerXml();
|
||||
}
|
||||
catch (XmlException e)
|
||||
{
|
||||
throw new IcdXmlException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public long ReadElementContentAsLong()
|
||||
{
|
||||
return m_Reader.ReadElementContentAsLong();
|
||||
try
|
||||
{
|
||||
return m_Reader.ReadElementContentAsLong();
|
||||
}
|
||||
catch (XmlException e)
|
||||
{
|
||||
throw new IcdXmlException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public float ReadElementContentAsFloat()
|
||||
{
|
||||
return m_Reader.ReadElementContentAsFloat();
|
||||
try
|
||||
{
|
||||
return m_Reader.ReadElementContentAsFloat();
|
||||
}
|
||||
catch (XmlException e)
|
||||
{
|
||||
throw new IcdXmlException(e);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using ICD.Common.EventArguments;
|
||||
using ICD.Common.Properties;
|
||||
using ICD.Common.Utils.Extensions;
|
||||
using ICD.Common.Utils.IO;
|
||||
|
||||
namespace ICD.Common.Utils.Xml
|
||||
@@ -67,7 +68,11 @@ namespace ICD.Common.Utils.Xml
|
||||
[PublicAPI]
|
||||
public static IcdXmlAttribute GetAttribute(string xml, string name)
|
||||
{
|
||||
return GetAttributes(xml).First(a => a.Name == name);
|
||||
IcdXmlAttribute output;
|
||||
if (GetAttributes(xml).TryFirst(a => a.Name == name, out output))
|
||||
return output;
|
||||
|
||||
throw new KeyNotFoundException(string.Format("No attribute with name {0}", name));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -130,6 +135,9 @@ namespace ICD.Common.Utils.Xml
|
||||
[PublicAPI]
|
||||
public static void Recurse(string xml, Action<XmlRecursionEventArgs> callback)
|
||||
{
|
||||
if (callback == null)
|
||||
throw new ArgumentNullException("callback");
|
||||
|
||||
Recurse(xml, new Stack<string>(), callback);
|
||||
}
|
||||
|
||||
@@ -141,6 +149,12 @@ namespace ICD.Common.Utils.Xml
|
||||
/// <param name="callback"></param>
|
||||
private static void Recurse(string xml, Stack<string> path, Action<XmlRecursionEventArgs> callback)
|
||||
{
|
||||
if (path == null)
|
||||
throw new ArgumentNullException("path");
|
||||
|
||||
if (callback == null)
|
||||
throw new ArgumentNullException("callback");
|
||||
|
||||
IcdXmlReader childReader;
|
||||
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user