2017-09-07 23:10:17 +02:00
using System.Collections ;
2014-11-01 02:26:07 +01:00
using System.Collections.Generic ;
2014-11-03 00:05:54 +01:00
namespace System.Unicode
2014-11-01 02:26:07 +01:00
{
2014-12-16 00:53:15 +01:00
/// <summary>Supports a standard iteration of code points in a <see cref="string"/>.</summary>
2014-11-01 02:26:07 +01:00
public struct CodePointEnumerator : IEnumerator < int >
{
private readonly string text ;
private int current ;
private int index ;
2014-12-16 00:53:15 +01:00
/// <summary>Initializes a new instance of the <see cref="PermissiveCodePointEnumerator"/> struct.</summary>
/// <param name="text">The text whose code point should be enumerated.</param>
/// <exception cref="ArgumentNullException"><paramref cref="text"/> is <see langword="null"/>.</exception>
2014-11-01 02:26:07 +01:00
public CodePointEnumerator ( string text )
{
2014-11-21 16:25:57 +01:00
if ( text == null ) throw new ArgumentNullException ( nameof ( text ));
2014-11-01 02:26:07 +01:00
this . text = text ;
this . current = 0 ;
this . index = - 1 ;
}
2014-12-16 00:53:15 +01:00
/// <summary>Gets the element in the collection at the current position of the enumerator..</summary>
/// <value>The element in the collection at the current position of the enumerator.</value>
2017-09-07 23:10:17 +02:00
public int Current => current ;
2014-11-01 02:26:07 +01:00
2017-09-07 23:10:17 +02:00
object IEnumerator . Current => current ;
2014-11-01 02:26:07 +01:00
void IDisposable . Dispose () { }
2014-12-16 00:53:15 +01:00
/// <summary>Advances the enumerator to the next element of the collection.</summary>
/// <returns><see langword="true"/> if the enumerator was successfully advanced to the next element; <see langword="false"/> if the enumerator has passed the end of the collection.</returns>
2014-11-01 02:26:07 +01:00
public bool MoveNext ()
{
if ( index < text . Length && ( index += current > 0xFFFF ? 2 : 1 ) < text . Length )
{
current = char . ConvertToUtf32 ( text , index );
return true ;
}
else
{
current = 0 ;
return false ;
}
}
void IEnumerator . Reset ()
{
current = 0 ;
index = - 1 ;
}
}
}