using System.Collections;
namespace Numbers.NumberString;
///
/// Allows to enumerate only the digits in a string
///
///
public sealed class DigitEnumerator : IEnumerator
{
private readonly string _text;
private int _index;
///
/// Creates a new instance of based on the given text.
/// This will be called within .
///
/// The text to iterate over (containing both letters and digits)
public DigitEnumerator(string text)
{
_text = text;
_index = -1;
}
public bool MoveNext()
{
while (++_index < _text.Length)
{
if (char.IsDigit(_text[_index]))
{
return true;
}
}
return false;
}
public void Reset()
{
_index = -1;
}
public int Current
{
get
{
if (_index < 0 || _index >= _text.Length || !char.IsDigit(_text[_index]))
{
return -1;
}
return GetDigit(_text[_index]);
static int GetDigit(char c) => c - '0';
}
}
object IEnumerator.Current => Current;
public void Dispose() { }
}