using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Net3_Proxy
{
internal static class Utils
{
public static bool IsNullOrWhiteSpace(string value)
{
if (value == null)
{
return true;
}
for (int i = 0; i < value.Length; i++)
{
if (!char.IsWhiteSpace(value[i]))
{
return false;
}
}
return true;
}
///
/// Adds a value to the beginning of the sequence.
///
/// the type of the elements of
/// a sequence of values
/// the value to prepend to
/// a new sequence beginning with
public static IEnumerable Prepend(this IEnumerable seq, T prep)
=> new PrependEnumerable(seq, prep);
private sealed class PrependEnumerable : IEnumerable
{
private readonly IEnumerable rest;
private readonly T first;
public PrependEnumerable(IEnumerable rest, T first)
{
this.rest = rest;
this.first = first;
}
public IEnumerator GetEnumerator() => new PrependEnumerator(this);
private sealed class PrependEnumerator : IEnumerator
{
private readonly IEnumerator restEnum;
private readonly PrependEnumerable enumerable;
private int state = 0;
public PrependEnumerator(PrependEnumerable enumerable)
{
this.enumerable = enumerable;
restEnum = enumerable.rest.GetEnumerator();
}
public T Current { get; private set; }
object IEnumerator.Current => Current;
public void Dispose() => restEnum.Dispose();
public bool MoveNext()
{
switch (state)
{
case 0:
Current = enumerable.first;
state++;
return true;
case 1:
if (!restEnum.MoveNext())
{
state = 2;
return false;
}
else
Current = restEnum.Current;
return true;
case 2:
default:
return false;
}
}
public void Reset()
{
restEnum.Reset();
state = 0;
}
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}
}