CharEnumerator.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. /*============================================================
  5. **
  6. **
  7. **
  8. ** Purpose: Enumerates the characters on a string. skips range
  9. ** checks.
  10. **
  11. **
  12. ============================================================*/
  13. using System.Collections;
  14. using System.Collections.Generic;
  15. namespace System
  16. {
  17. public sealed class CharEnumerator : IEnumerator, IEnumerator<char>, IDisposable, ICloneable
  18. {
  19. private string _str;
  20. private int _index;
  21. private char _currentElement;
  22. internal CharEnumerator(string str)
  23. {
  24. _str = str;
  25. _index = -1;
  26. }
  27. public object Clone()
  28. {
  29. return MemberwiseClone();
  30. }
  31. public bool MoveNext()
  32. {
  33. if (_index < (_str.Length - 1))
  34. {
  35. _index++;
  36. _currentElement = _str[_index];
  37. return true;
  38. }
  39. else
  40. _index = _str.Length;
  41. return false;
  42. }
  43. public void Dispose()
  44. {
  45. if (_str != null)
  46. _index = _str.Length;
  47. _str = null;
  48. }
  49. object IEnumerator.Current
  50. {
  51. get { return Current; }
  52. }
  53. public char Current
  54. {
  55. get
  56. {
  57. if (_index == -1)
  58. throw new InvalidOperationException(SR.InvalidOperation_EnumNotStarted);
  59. if (_index >= _str.Length)
  60. throw new InvalidOperationException(SR.InvalidOperation_EnumEnded);
  61. return _currentElement;
  62. }
  63. }
  64. public void Reset()
  65. {
  66. _currentElement = (char)0;
  67. _index = -1;
  68. }
  69. }
  70. }