wabbajack/Wabbajack.Paths/ArrayExtensions.cs

56 lines
1.5 KiB
C#
Raw Normal View History

2021-09-27 12:42:46 +00:00
using System;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.Paths;
public static class ArrayExtensions
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
public static bool AreEqual<T>(T[] a, int startA, T[] b, int startB, int length)
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
if (startA + length > a.Length) return false;
if (startB + length > b.Length) return false;
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
for (var i = 0; i < length; i++)
if (!a[startA + i]!.Equals(b[startB + i]))
return false;
return true;
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public static int Compare<T>(T[] a, T[] b)
where T : IComparable<T>
{
var idx = 0;
while (true)
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
if (idx == a.Length && idx == b.Length) return 0;
if (idx == a.Length && idx < b.Length) return -1;
if (idx == b.Length && idx < a.Length) return 1;
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
var comp = a[idx].CompareTo(b[idx]);
if (comp != 0) return comp;
idx++;
2021-09-27 12:42:46 +00:00
}
2021-10-23 16:51:17 +00:00
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public static int CompareString(string[] a, string[] b)
{
var idx = 0;
while (true)
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
if (idx == a.Length && idx == b.Length) return 0;
if (idx == a.Length && idx < b.Length) return -1;
if (idx == b.Length && idx < a.Length) return 1;
var comp = string.Compare(a[idx], b[idx], StringComparison.CurrentCultureIgnoreCase);
if (comp != 0) return comp;
idx++;
2021-09-27 12:42:46 +00:00
}
}
2021-10-23 16:51:17 +00:00
public static T[] Add<T>(this T[] arr, T itm)
{
var newArr = new T[arr.Length + 1];
Array.Copy(arr, 0, newArr, 0, arr.Length);
newArr[arr.Length] = itm;
return newArr;
}
2021-09-27 12:42:46 +00:00
}