|
| 1 | +//--------------------------------------------------// |
| 2 | +// Created by basicx-StrgV // |
| 3 | +// https://github.com/basicx-StrgV/ // |
| 4 | +//--------------------------------------------------// |
| 5 | +using System; |
| 6 | + |
| 7 | +namespace WGetNET.Extensions |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// The <see langword="static"/> <see cref="ArrayExtensions"/> class, |
| 11 | + /// provieds extension methods for generic and type specific arrays. |
| 12 | + /// </summary> |
| 13 | + internal static class ArrayExtensions |
| 14 | + { |
| 15 | + /// <summary> |
| 16 | + /// Adds a new entry to a <see langword="array"/>. |
| 17 | + /// </summary> |
| 18 | + /// <typeparam name="T"> |
| 19 | + /// The type of the <see langword="array"/>. |
| 20 | + /// </typeparam> |
| 21 | + /// <param name="array"> |
| 22 | + /// The input <see langword="array"/> for the action. |
| 23 | + /// </param> |
| 24 | + /// <param name="value"> |
| 25 | + /// The value to add to the <see langword="array"/>. |
| 26 | + /// </param> |
| 27 | + /// <returns> |
| 28 | + /// The <see langword="array"/> with the added entry. |
| 29 | + /// </returns> |
| 30 | + public static T[] Add<T>(this T[] array, T value) |
| 31 | + { |
| 32 | + if (value is null) |
| 33 | + { |
| 34 | + return array; |
| 35 | + } |
| 36 | + |
| 37 | + Array.Resize(ref array, array.Length + 1); |
| 38 | + |
| 39 | + //Add the new line to the new enty in the main array. |
| 40 | +#if NETCOREAPP3_1_OR_GREATER |
| 41 | + array[^1] = value; |
| 42 | +#elif NETSTANDARD2_0 |
| 43 | + array[array.Length - 1] = value; |
| 44 | +#endif |
| 45 | + |
| 46 | + return array; |
| 47 | + } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Removes a range of entrys from a <see langword="array"/>. |
| 51 | + /// </summary> |
| 52 | + /// <typeparam name="T"> |
| 53 | + /// The type of the <see langword="array"/>. |
| 54 | + /// </typeparam> |
| 55 | + /// <param name="array"> |
| 56 | + /// The input <see langword="array"/> for the action. |
| 57 | + /// </param> |
| 58 | + /// <param name="index"> |
| 59 | + /// A <see cref="int"/> representing the zero based start index. |
| 60 | + /// </param> |
| 61 | + /// <param name="count"> |
| 62 | + /// A <see cref="int"/> representing the number of elements to remove. |
| 63 | + /// </param> |
| 64 | + /// <returns> |
| 65 | + /// The <see langword="array"/> with the range of entrys removed. |
| 66 | + /// </returns> |
| 67 | + public static T[] RemoveRange<T>(this T[] array, int index, int count) |
| 68 | + { |
| 69 | + if (array.Length - 1 < index) |
| 70 | + { |
| 71 | + return array; |
| 72 | + } |
| 73 | + |
| 74 | + //Only copy the needed range of the input array to the new array. |
| 75 | + T[] newArray = new T[array.Length - count]; |
| 76 | + for (int i = 0; i < array.Length; i++) |
| 77 | + { |
| 78 | + if (i < index) |
| 79 | + { |
| 80 | + newArray[i] = array[i]; |
| 81 | + } |
| 82 | + else if (i + count < array.Length) |
| 83 | + { |
| 84 | + newArray[i] = array[i + count]; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return newArray; |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Searches for an entry in a <see langword="array"/> that contains the given string. |
| 93 | + /// </summary> |
| 94 | + /// <param name="array"> |
| 95 | + /// The input <see langword="array"/> for the action. |
| 96 | + /// </param> |
| 97 | + /// <param name="value"> |
| 98 | + /// A <see cref="string"/> representing the value to check for. |
| 99 | + /// </param> |
| 100 | + /// <returns> |
| 101 | + /// A <see cref="int"/> representing the index of the entry (-1 if the entry is not found). |
| 102 | + /// </returns> |
| 103 | + public static int GetEntryContains(this string[] array, string value) |
| 104 | + { |
| 105 | + int index = -1; |
| 106 | + |
| 107 | + for (int i = 0; i < array.Length; i++) |
| 108 | + { |
| 109 | + if (array[i].Contains(value)) |
| 110 | + { |
| 111 | + index = i; |
| 112 | + break; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return index; |
| 117 | + } |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Removes empty entries from a <see langword="array"/> of <see cref="string"/>'s. |
| 121 | + /// </summary> |
| 122 | + /// <param name="array"> |
| 123 | + /// The input <see langword="array"/> for the action. |
| 124 | + /// </param> |
| 125 | + /// <returns> |
| 126 | + /// The <see langword="array"/> with empty entries removed. |
| 127 | + /// </returns> |
| 128 | + public static string[] RemoveEmptyEntries(this string[] array) |
| 129 | + { |
| 130 | + string[] newArray = new string[0]; |
| 131 | + |
| 132 | + for (int i = 0; i < array.Length; i++) |
| 133 | + { |
| 134 | + if (!string.IsNullOrWhiteSpace(array[i])) |
| 135 | + { |
| 136 | + newArray = newArray.Add(array[i]); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return newArray; |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments