diff options
Diffstat (limited to 'LibGit/Extensions/IEnumerableExtensions.cs')
-rw-r--r-- | LibGit/Extensions/IEnumerableExtensions.cs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/LibGit/Extensions/IEnumerableExtensions.cs b/LibGit/Extensions/IEnumerableExtensions.cs new file mode 100644 index 0000000..d8fc54d --- /dev/null +++ b/LibGit/Extensions/IEnumerableExtensions.cs @@ -0,0 +1,45 @@ +using System.IO.Compression; +using System.Text; + +namespace LibGit.Extensions; + +public static class IEnumerableExtensions +{ + public static bool StartsWith<T>(this IEnumerable<T> list, IEnumerable<T> prefix) + { + return prefix.SequenceEqual(list.Take(prefix.Count())); + } + + public static void HexDump(this IEnumerable<byte> bytes, int width = 32) + { + var data = new Queue<(string hex, char utf8)>(bytes.ToArray().Select(x => ($"{x:X2}", (char)x)).ToArray()); + while (data.Count > 0) + { + var line = data.Dequeue(Math.Min(width, data.Count)).ToArray(); + Console.WriteLine( + string.Join(" ", line.Select(x => x.hex)).PadRight(width * 3) + + " | " + + string.Join("", line.Select(x => x.utf8)) + .Replace('\n', '.') + .Replace('\r', '.') + .Replace('\0', '.') + ); + } + } + + public static string AsHexString(this IEnumerable<byte> bytes) => string.Join(' ', bytes.Select(x => $"{x:X2}")); + public static string AsString(this IEnumerable<byte> bytes) => Encoding.UTF8.GetString(bytes.ToArray()); + + + //zlib decompress + public static byte[] ZlibDecompress(this IEnumerable<byte> bytes) + { + var inStream = new MemoryStream(bytes.ToArray()); + using ZLibStream stream = new ZLibStream(inStream, CompressionMode.Decompress); + using var result = new MemoryStream(); + stream.CopyTo(result); + stream.Flush(); + stream.Close(); + return result.ToArray(); + } +} \ No newline at end of file |