From 7e1ed96d8493e6f1c4eb7b821372e55f19d8ee94 Mon Sep 17 00:00:00 2001 From: "Alecio Furanze (Ale)" Date: Wed, 11 Dec 2024 18:58:41 +0200 Subject: [PATCH] adding concat byte extension #36 --- src/src/extension/ByteExtension.cs | 36 +++++++++++++++++++++++++++++ test/extension/ByteExtensionTest.cs | 24 +++++++++++++++---- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/src/extension/ByteExtension.cs b/src/src/extension/ByteExtension.cs index abc7a05..3dee9e1 100644 --- a/src/src/extension/ByteExtension.cs +++ b/src/src/extension/ByteExtension.cs @@ -1,3 +1,5 @@ +using System.Collections.Generic; +using System.Linq; using System.Text; using ByEx = Byter.ByterExtension; @@ -14,5 +16,39 @@ public static string GetString(this byte[] content, Encoding encoding) { return ByEx.IsNull(content) ? ByEx.DEFAULT_STRING_VALUE : encoding.GetString(content); } + + public static byte[] Concat(this byte[] value, params byte[][] values) + { + return Concat(value, false, values); + } + + public static byte[] ConcatInverse(this byte[] value, params byte[][] values) + { + return Concat(value, true, values); + } + + public static byte[] Concat(this byte[] value, bool invert, params byte[][] values) + { + var list = new List { value }; + + list.AddRange(values); + + if (!invert || list.Count <= 1) return list.SelectMany(x => x).ToArray(); + + var reversed = new List(); + + var count = list.Count - 1; + + while (list.Count > 0) + { + reversed.Add(list[count]); + list.RemoveAt(count); + count--; + } + + list.Clear(); + + return reversed.SelectMany(x => x).ToArray(); + } } } \ No newline at end of file diff --git a/test/extension/ByteExtensionTest.cs b/test/extension/ByteExtensionTest.cs index c0bfa43..c5a427e 100644 --- a/test/extension/ByteExtensionTest.cs +++ b/test/extension/ByteExtensionTest.cs @@ -15,16 +15,16 @@ public void GetString() var utf8Bytes = BaseText.GetBytes(Encoding.UTF8); var utf16Bytes = BaseText.GetBytes(Encoding.Unicode); var utf32Bytes = BaseText.GetBytes(Encoding.UTF32); - + Assert.Equal(BaseText, utf8Bytes.GetString(Encoding.UTF8)); Assert.Equal(BaseText, utf16Bytes.GetString(Encoding.Unicode)); Assert.Equal(BaseText, utf32Bytes.GetString(Encoding.UTF32)); - + Assert.NotEqual(BaseText, utf16Bytes.GetString(Encoding.UTF8)); Assert.NotEqual(BaseText, utf32Bytes.GetString(Encoding.Unicode)); Assert.NotEqual(BaseText, utf8Bytes.GetString(Encoding.UTF32)); } - + [Fact] public void Nullable() { @@ -32,4 +32,20 @@ public void Nullable() Assert.Equal(result, Array.Empty().GetString()); Assert.Equal(result, ByteExtension.GetString(null!)); } -} + + [Fact] + public void Concat() + { + byte[] part1 = [1, 2, 3]; + byte[] part2 = [4, 5, 6]; + byte[] part3 = [7, 8, 9]; + + Assert.Equal([1, 2, 3, /**/ 4, 5, 6], part1.Concat(part2)); + Assert.Equal([1, 2, 3, /**/ 4, 5, 6], part1.Concat(false, part2)); + Assert.Equal([1, 2, 3, /**/ 4, 5, 6, /**/7, 8, 9], part1.Concat(false, part2, part3)); + + Assert.Equal([4, 5, 6, /**/ 1, 2, 3], part1.ConcatInverse(part2)); + Assert.Equal([4, 5, 6, /**/ 1, 2, 3], part1.Concat(true, part2)); + Assert.Equal([7, 8, 9, /**/ 4, 5, 6, /**/ 1, 2, 3,], part1.Concat(true, part2, part3)); + } +} \ No newline at end of file