(ByteArrayExt): methods made generic.

This commit is contained in:
morkt 2016-10-16 16:14:27 +04:00
parent 762396d045
commit f38f1363a4

View File

@ -140,17 +140,17 @@ namespace GameRes
public static class ByteArrayExt
{
public static ushort ToUInt16 (this CowArray<byte> arr, int index)
public static ushort ToUInt16<TArray> (this TArray arr, int index) where TArray : IList<byte>
{
return (ushort)(arr[index] | arr[index+1] << 8);
}
public static short ToInt16 (this CowArray<byte> arr, int index)
public static short ToInt16<TArray> (this TArray arr, int index) where TArray : IList<byte>
{
return (short)(arr[index] | arr[index+1] << 8);
}
public static int ToInt24 (this CowArray<byte> arr, int index)
public static int ToInt24<TArray> (this TArray arr, int index) where TArray : IList<byte>
{
return arr[index] | arr[index+1] << 8 | arr[index+2] << 16;
}
@ -165,26 +165,29 @@ namespace GameRes
return (int)ToUInt32 (arr, index);
}
public static ulong ToUInt64 (this CowArray<byte> arr, int index)
public static ulong ToUInt64<TArray> (this TArray arr, int index) where TArray : IList<byte>
{
return (ulong)ToUInt32 (arr, index) | ((ulong)ToUInt32 (arr, index+4) << 32);
}
public static long ToInt64 (this CowArray<byte> arr, int index)
public static long ToInt64<TArray> (this TArray arr, int index) where TArray : IList<byte>
{
return (long)ToUInt64 (arr, index);
}
public static bool AsciiEqual (this CowArray<byte> arr, int index, string str)
public static bool AsciiEqual<TArray> (this TArray arr, int index, string str) where TArray : IList<byte>
{
arr.Reclaim();
return Binary.AsciiEqual (arr.ToArray(), index, str);
if (arr.Length-index < str.Length)
return false;
for (int i = 0; i < str.Length; ++i)
if ((char)arr[index+i] != str[i])
return false;
return true;
}
public static bool AsciiEqual (this CowArray<byte> arr, string str)
public static bool AsciiEqual<TArray> (this TArray arr, string str) where TArray : IList<byte>
{
arr.Reclaim();
return Binary.AsciiEqual (arr.ToArray(), str);
return arr.AsciiEqual (0, str);
}
public static string GetCString (this CowArray<byte> arr, int index, int length_limit)