You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.3 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace Net3_Proxy
  7. {
  8. internal static class Utils
  9. {
  10. public static bool IsNullOrWhiteSpace(string value)
  11. {
  12. if (value == null)
  13. {
  14. return true;
  15. }
  16. for (int i = 0; i < value.Length; i++)
  17. {
  18. if (!char.IsWhiteSpace(value[i]))
  19. {
  20. return false;
  21. }
  22. }
  23. return true;
  24. }
  25. /// <summary>
  26. /// Adds a value to the beginning of the sequence.
  27. /// </summary>
  28. /// <typeparam name="T">the type of the elements of <paramref name="seq"/></typeparam>
  29. /// <param name="seq">a sequence of values</param>
  30. /// <param name="prep">the value to prepend to <paramref name="seq"/></param>
  31. /// <returns>a new sequence beginning with <paramref name="prep"/></returns>
  32. public static IEnumerable<T> Prepend<T>(this IEnumerable<T> seq, T prep)
  33. => new PrependEnumerable<T>(seq, prep);
  34. private sealed class PrependEnumerable<T> : IEnumerable<T>
  35. {
  36. private readonly IEnumerable<T> rest;
  37. private readonly T first;
  38. public PrependEnumerable(IEnumerable<T> rest, T first)
  39. {
  40. this.rest = rest;
  41. this.first = first;
  42. }
  43. public IEnumerator<T> GetEnumerator() => new PrependEnumerator(this);
  44. private sealed class PrependEnumerator : IEnumerator<T>
  45. {
  46. private readonly IEnumerator<T> restEnum;
  47. private readonly PrependEnumerable<T> enumerable;
  48. private int state = 0;
  49. public PrependEnumerator(PrependEnumerable<T> enumerable)
  50. {
  51. this.enumerable = enumerable;
  52. restEnum = enumerable.rest.GetEnumerator();
  53. }
  54. public T Current { get; private set; }
  55. object IEnumerator.Current => Current;
  56. public void Dispose() => restEnum.Dispose();
  57. public bool MoveNext()
  58. {
  59. switch (state)
  60. {
  61. case 0:
  62. Current = enumerable.first;
  63. state++;
  64. return true;
  65. case 1:
  66. if (!restEnum.MoveNext())
  67. {
  68. state = 2;
  69. return false;
  70. }
  71. else
  72. Current = restEnum.Current;
  73. return true;
  74. case 2:
  75. default:
  76. return false;
  77. }
  78. }
  79. public void Reset()
  80. {
  81. restEnum.Reset();
  82. state = 0;
  83. }
  84. }
  85. IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
  86. }
  87. }
  88. }