public static string Join(this Generic.IEnumerator <string> me, string separator) { System.Text.StringBuilder result = new System.Text.StringBuilder(); if (me.MoveNext()) { result.Append(me.Current); while (me.MoveNext()) { result.Append(separator).Append(me.Current); } } return(result.ToString()); }
public static async Tasks.Task <bool> Join(this ITextWriter me, Generic.IEnumerator <string> items, string separator = null) { bool result = items.MoveNext() && await me.Write(items.Current); while (result && items.MoveNext()) { if (separator.NotNull()) { result &= await me.Write(separator); } result &= await me.Write(items.Current); } return(result); }
public static Generic.IEnumerator <T> While <T>(this Generic.IEnumerator <T> me, Func <T, bool> predicate) { while (me.MoveNext() && predicate(me.Current)) { yield return(me.Current); } }
public static string PercentDecode(this string me) { Text.Builder result = null; if (me.NotNull()) { Generic.IEnumerator <char> enumerator = me.GetEnumerator(); while (enumerator.MoveNext()) { if (enumerator.Current == '%') { char[] characters = new char[] { enumerator.Next(), enumerator.Next() }; byte value; if (byte.TryParse(new string(characters), System.Globalization.NumberStyles.HexNumber, System.Globalization.NumberFormatInfo.InvariantInfo, out value)) { result += System.Text.Encoding.ASCII.GetChars(new byte[] { value }); } else { result += "%" + characters; } } else { result += enumerator.Current; } } } return(result); }
public static void Apply <T>(this Generic.IEnumerator <T> me, Action <T> function) { while (me.MoveNext()) { function(me.Current); } }
public static Generic.IEnumerator <S> Map <T, S>(this Generic.IEnumerator <T> me, Func <T, S> function) { while (me.MoveNext()) { yield return(function(me.Current)); } }
/// <summary> /// Skip the next <paramref name="count"/> elements in <paramref name="me"/>. /// </summary> /// <param name="me">Enumerator to skip in.</param> /// <param name="count">Number of elements to skip.</param> /// <typeparam name="T">Any type.</typeparam> public static Generic.IEnumerator <T> Skip <T>(this Generic.IEnumerator <T> me, int count) { while (count > 0 && me.MoveNext()) { count--; } return(me); }
public static Generic.IEnumerable <byte> Encode(this Generic.IEnumerable <char> me, Encoding encoding) { Generic.IEnumerator <byte> enumerator = me.GetEnumerator().Encode(encoding); while (enumerator.MoveNext()) { yield return(enumerator.Current); } }
public static S Fold <T, S>(this Generic.IEnumerator <T> me, Func <T, S, S> function, S initial) { while (me.MoveNext()) { initial = function(me.Current, initial); } return(initial); }
public static bool All <T>(this Generic.IEnumerable <T> me, Func <T, bool, bool> function) { bool result = true; Generic.IEnumerator <T> enumerator = me.GetEnumerator(); bool notLast = enumerator.MoveNext(); while (notLast) { T current = enumerator.Current; if (!function(current, !(notLast = enumerator.MoveNext()))) { result = false; break; } } return(result); }
public static int Count <T>(this Generic.IEnumerator <T> me) { int result = 0; while (me.MoveNext()) { result++; } return(result); }
public static Generic.IEnumerator <TResult> Zip <TLeft, TRight, TResult>(this Generic.IEnumerator <TLeft> me, Generic.IEnumerator <TRight> other, Func <TLeft, TRight, TResult> combine) { if (me.NotNull() && other.NotNull() && combine.NotNull()) { while (me.MoveNext() && other.MoveNext()) { yield return(combine(me.Current, other.Current)); } } }
public static Generic.IEnumerator <byte> Encode(this Generic.IEnumerator <char> me, Encoding encoding) { while (me.MoveNext()) { foreach (var b in encoding.Encode(me.Current)) { yield return(b); } } }
public static string Join(this Generic.IEnumerable <string> me) { System.Text.StringBuilder result = new System.Text.StringBuilder(); Generic.IEnumerator <string> enumerator = me.GetEnumerator(); while (enumerator.MoveNext()) { result.Append(enumerator.Current); } return(result.ToString()); }
/// <summary> /// Return new enumerator containing the next <paramref name="count"/> elements in <paramref name="me"/>. /// </summary> /// <param name="me">Enumerator to read from.</param> /// <param name="count">Number of elements read.</param> /// <typeparam name="T">Any type.</typeparam> public static Generic.IEnumerator <T> Read <T>(this Generic.IEnumerator <T> me, int count) { if (count > 0) { do { yield return(me.Current); }while (--count > 0 && me.MoveNext()); } }
public static IList <T> Add <T>(this IList <T> me, Generic.IEnumerator <T> items) { if (items.NotNull()) { while (items.MoveNext()) { me.Add(items.Current); } } return(me); }
public static Generic.IEnumerable <char> Decode(this Generic.IEnumerable <byte> me, System.Text.Encoding encoding) { //if (encoding == System.Text.Encoding.UTF8) { byte[] buffer = new byte[6]; int length = 0; Generic.IEnumerator <byte> enumerator = me.GetEnumerator(); while (enumerator.MoveNext()) { buffer[0] = enumerator.Current; length = buffer[0] < 0x80 ? 1 : buffer[0] < 0xc0 ? 0 : buffer[0] < 0xe0 ? 2 : buffer[0] < 0xf0 ? 3 : buffer[0] < 0xf8 ? 4 : buffer[0] < 0xfc ? 5 : 6; if (length > 0) { int i = 1; for (; i < length && enumerator.MoveNext(); i++) { buffer[i] = enumerator.Current; } if (length == 3 && buffer[0] == 0xef && buffer[1] == 0xbb && buffer[2] == 0xbf) { length = 0; // Skip "zero width no break space" (0xefbbbf) //yield return ; } if (i == length) { foreach (char c in encoding.GetChars(buffer, 0, length)) { yield return(c); } } } } } }
public char?Decode(Generic.IEnumerator <byte> data) { return(this.Decode(() => { byte?result = null; if (data.MoveNext()) { result = data.Current; } return result; })); }
public static S Find <T, S>(this Generic.IEnumerator <T> me, Func <T, S> function) { S result = default(S); while (me.MoveNext()) { if ((result = function(me.Current)) != null) { break; } } return(result); }
public static T Find <T>(this Generic.IEnumerator <T> me, Func <T, bool> function) { T result = default(T); while (me.MoveNext()) { if (function(me.Current)) { result = me.Current; break; } } return(result); }
public static bool All <T>(this Generic.IEnumerator <T> me, Func <T, bool> function) { bool result = true; while (me.MoveNext()) { if (!function(me.Current)) { result = false; break; } } return(result); }
public static T First <T>(this Generic.IEnumerator <T> me) { T result; if (me.NotNull()) { result = me.MoveNext() ? me.Current : default(T); me.Dispose(); } else { result = default(T); } return(result); }
static T[] ToArray <T>(this Generic.IEnumerator <T> me, int count) { T[] result; if (me.MoveNext()) { var head = me.Current; result = me.ToArray(count + 1); result[count] = head; } else { result = new T[count]; } return(result); }
public static T First <T>(this Generic.IEnumerable <T> me) { T result; if (me.NotNull()) { using (Generic.IEnumerator <T> enumerator = me.GetEnumerator()) result = enumerator.MoveNext() ? enumerator.Current : default(T); } else { result = default(T); } return(result); }
public static bool Contains <T>(this Generic.IEnumerator <T> me, params T[] needles) where T : IEquatable <T> { bool result = false; while (me.MoveNext()) { if (needles.Contains(me.Current)) { result = true; break; } } return(result); }
public static T?FirstOrNull <T>(this Generic.IEnumerable <T> me) where T : struct { T?result; if (me.NotNull()) { using (Generic.IEnumerator <T> enumerator = me.GetEnumerator()) result = enumerator.MoveNext() ? enumerator.Current : default(T); } else { result = null; } return(result); }
public static Generic.IEnumerator <T> Append <T>(this Generic.IEnumerator <T> me, Generic.IEnumerator <T> other) { if (me.NotNull()) { while (me.MoveNext()) { yield return(me.Current); } } if (other.NotNull()) { while (other.MoveNext()) { yield return(other.Current); } } }
/// <summary> /// Skip past the first occurance of separator. /// </summary> /// <param name="me">Enumerator to skip on.</param> /// <param name="separator">Separator to skip past. Shall not contain null.</param> /// <typeparam name="T">Any type implementing <c>IEquatable</c>.</typeparam> public static Generic.IEnumerator <T> Skip <T>(this Generic.IEnumerator <T> me, params T[] separator) where T : IEquatable <T> { int position = 0; while (me.MoveNext()) { if (!me.Current.Equals(separator[position++])) { position = 0; } else if (separator.Length == position) { break; } } return(me); }
public static int Index <T>(this Generic.IEnumerator <T> me, Func <T, bool> function) { int result = -1; int i = 0; while (me.MoveNext()) { if (function(me.Current)) { result = i; break; } else { i++; } } return(result); }
/// <summary> /// Create an enumerator containing all elements in <paramref name="me"/> until <paramref name="separator"/>. /// </summary> /// <param name="me">Enumerator to read from.</param> /// <param name="separator">Separator to read from and move past.</param> /// <typeparam name="T">Any type implementing <c>IEquatable</c>.</typeparam> public static Generic.IEnumerator <T> Read <T>(this Generic.IEnumerator <T> me, params T[] separator) where T : IEquatable <T> { int position = 0; while (me.MoveNext()) { if (!me.Current.Equals(separator[position++])) { for (int i = 0; i < position - 1; i++) { yield return(separator[i]); } yield return(me.Current); position = 0; } else if (separator.Length == position) { yield break; } } }