wabbajack/Wabbajack.Paths/ArrayExtensions.cs

68 lines
2.0 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
{
2022-07-11 20:55:54 +00:00
if (startA + length > (a?.Length ?? 0)) return false;
if (startB + length > (b?.Length ?? 0)) 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++)
2022-10-07 20:53:55 +00:00
if (!a![startA + i]!.Equals(b![startB + i]))
2021-10-23 16:51:17 +00:00
return false;
return true;
}
2021-09-27 12:42:46 +00:00
public static bool AreEqualIgnoreCase(string[] a, int startA, string[] b, int startB, int length)
{
if (startA + length > (a?.Length ?? 0)) return false;
if (startB + length > (b?.Length ?? 0)) return false;
for (var i = 0; i < length; i++)
2022-10-07 20:53:55 +00:00
if (!a![startA + i]!.Equals(b![startB + i], StringComparison.InvariantCultureIgnoreCase))
return false;
return true;
}
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
}