Ejemplo n.º 1
0
 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());
 }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
 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);
     }
 }
Ejemplo n.º 4
0
 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);
 }
Ejemplo n.º 5
0
 public static void Apply <T>(this Generic.IEnumerator <T> me, Action <T> function)
 {
     while (me.MoveNext())
     {
         function(me.Current);
     }
 }
Ejemplo n.º 6
0
 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));
     }
 }
Ejemplo n.º 7
0
 /// <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);
 }
Ejemplo n.º 8
0
 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);
     }
 }
Ejemplo n.º 9
0
 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);
 }
Ejemplo n.º 10
0
        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);
        }
Ejemplo n.º 11
0
        public static int Count <T>(this Generic.IEnumerator <T> me)
        {
            int result = 0;

            while (me.MoveNext())
            {
                result++;
            }
            return(result);
        }
Ejemplo n.º 12
0
 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));
         }
     }
 }
Ejemplo n.º 13
0
 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);
         }
     }
 }
Ejemplo n.º 14
0
 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());
 }
Ejemplo n.º 15
0
 /// <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());
     }
 }
Ejemplo n.º 16
0
 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);
 }
Ejemplo n.º 17
0
 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);
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 18
0
 public char?Decode(Generic.IEnumerator <byte> data)
 {
     return(this.Decode(() =>
     {
         byte?result = null;
         if (data.MoveNext())
         {
             result = data.Current;
         }
         return result;
     }));
 }
Ejemplo n.º 19
0
        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);
        }
Ejemplo n.º 20
0
        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);
        }
Ejemplo n.º 21
0
        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);
        }
Ejemplo n.º 22
0
        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);
        }
Ejemplo n.º 23
0
 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);
 }
Ejemplo n.º 24
0
        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);
        }
Ejemplo n.º 25
0
        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);
        }
Ejemplo n.º 26
0
        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);
        }
Ejemplo n.º 27
0
 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);
         }
     }
 }
Ejemplo n.º 28
0
        /// <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);
        }
Ejemplo n.º 29
0
        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);
        }
Ejemplo n.º 30
0
        /// <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;
                }
            }
        }