Skip to content

Instantly share code, notes, and snippets.

@joergbattermann
Created February 7, 2011 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joergbattermann/814354 to your computer and use it in GitHub Desktop.
Save joergbattermann/814354 to your computer and use it in GitHub Desktop.
IEnumerator<string> implementation
using System;
using System.Collections;
using System.Collections.Generic;
namespace WordEnumerable
{
internal class WordEnumerator : IEnumerator<string>
{
private readonly TextParser _textParser;
private int _currentStartPosition;
/// <summary>
/// Initializes a new instance of the <see cref="WordEnumerator"/> class.
/// </summary>
/// <param name="text">The text.</param>
internal WordEnumerator(string text = null)
{
_textParser = new TextParser(text);
}
#region Implementation of IEnumerator
/// <summary>
/// Advances the enumerator to the next element of the collection.
/// </summary>
/// <returns>
/// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
/// </returns>
/// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created.
/// </exception><filterpriority>2</filterpriority>
public bool MoveNext()
{
if (_textParser.IsAtEndOfText)
return false;
while (!_textParser.IsAtEndOfText && !Char.IsLetterOrDigit(_textParser.Peek()))
_textParser.MoveAhead();
_currentStartPosition = _textParser.Position;
while (Char.IsLetterOrDigit(_textParser.Peek()))
_textParser.MoveAhead();
return true;
}
/// <summary>
/// Sets the enumerator to its initial position, which is before the first element in the collection.
/// </summary>
/// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created.
/// </exception><filterpriority>2</filterpriority>
public void Reset()
{
_textParser.Reset();
_currentStartPosition = 0;
}
/// <summary>
/// Gets the element in the collection at the current position of the enumerator.
/// </summary>
/// <returns>
/// The element in the collection at the current position of the enumerator.
/// </returns>
public string Current
{
get
{
return _textParser.Extract(_currentStartPosition, _textParser.Position);
}
}
/// <summary>
/// Gets the current element in the collection.
/// </summary>
/// <returns>
/// The current element in the collection.
/// </returns>
/// <exception cref="T:System.InvalidOperationException">The enumerator is positioned before the first element of the collection or after the last element.
/// </exception><filterpriority>2</filterpriority>
object IEnumerator.Current
{
get { return Current; }
}
#endregion
#region Implementation of IDisposable
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
/// <filterpriority>2</filterpriority>
public void Dispose() { }
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment