Skip to content

Commit

Permalink
ICU4N.Text.CanonicalIterator: Renamed CanonicalEnumerator, added Move…
Browse files Browse the repository at this point in the history
…Next() and Current, made Next() private (#21)
  • Loading branch information
NightOwl888 committed Oct 17, 2019
1 parent 9255c46 commit 1c0bb56
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 38 deletions.
21 changes: 9 additions & 12 deletions src/ICU4N.Collation/Impl/Coll/CollationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -996,31 +996,28 @@ private int AddOnlyClosure(ICharSequence nfdPrefix, ICharSequence nfdString,
// TODO: make CanonicalIterator work with CharSequence, or maybe change arguments here to String
if (nfdPrefix.Length == 0)
{
CanonicalIterator stringIter = new CanonicalIterator(nfdString.ToString());
CanonicalEnumerator stringIter = new CanonicalEnumerator(nfdString.ToString());
ICharSequence prefix = new StringCharSequence("");
for (; ; )
while (stringIter.MoveNext())
{
string str = stringIter.Next();
if (str == null) { break; }
string str = stringIter.Current;
if (IgnoreString(str) || str.ContentEquals(nfdString)) { continue; }
ce32 = AddIfDifferent(prefix, str.ToCharSequence(), newCEs, newCEsLength, ce32);
}
}
else
{
CanonicalIterator prefixIter = new CanonicalIterator(nfdPrefix.ToString());
CanonicalIterator stringIter = new CanonicalIterator(nfdString.ToString());
for (; ; )
CanonicalEnumerator prefixIter = new CanonicalEnumerator(nfdPrefix.ToString());
CanonicalEnumerator stringIter = new CanonicalEnumerator(nfdString.ToString());
while(prefixIter.MoveNext())
{
string prefix = prefixIter.Next();
if (prefix == null) { break; }
string prefix = prefixIter.Current;
if (IgnorePrefix(prefix)) { continue; }
bool samePrefix = prefix.ContentEquals(nfdPrefix);
ICharSequence prefixCharSequence = prefix.ToCharSequence();
for (; ; )
while (stringIter.MoveNext())
{
string str = stringIter.Next();
if (str == null) { break; }
string str = stringIter.Current;
if (IgnoreString(str) || (samePrefix && str.ContentEquals(nfdString))) { continue; }
ce32 = AddIfDifferent(prefixCharSequence, str.ToCharSequence(), newCEs, newCEsLength, ce32);
}
Expand Down
28 changes: 24 additions & 4 deletions src/ICU4N/Text/CanonicalIterator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ namespace ICU4N.Text
/// </remarks>
/// <author>M. Davis</author>
/// <stable>ICU 2.4</stable>
public sealed class CanonicalIterator // ICU4N TODO: API - rename CanonicalEnumerator
public sealed class CanonicalEnumerator // ICU4N specific - renamed from CanonicalIterator
{
/// <summary>
/// Construct a <see cref="CanonicalIterator"/> object.
/// Construct a <see cref="CanonicalEnumerator"/> object.
/// </summary>
/// <param name="source">String to get results for.</param>
/// <stable>ICU 2.4</stable>
public CanonicalIterator(string source)
public CanonicalEnumerator(string source)
{
Norm2AllModes allModes = Norm2AllModes.GetNFCInstance();
nfd = allModes.Decomp;
Expand Down Expand Up @@ -73,6 +73,26 @@ public void Reset()
}
}

/// <summary>
/// Gets the current canonically equivalent string.
/// </summary>
public string Current { get; private set; }

/// <summary>
/// Move to the next canonically equivalent string.
/// <para/>
/// <b>Warning: The strings are not guaranteed to be in any particular order.</b>
/// </summary>
/// <returns><b>true</b> if the enumerator was advancet to the next canonically equivalent element; <b>false</b> if the enumerator has passed the end of the collection.</returns>
/// <stable>ICU4N 60.1</stable>
public bool MoveNext()
{
if (done)
return false;
Current = Next();
return Current != null;
}

/// <summary>
/// Get the next canonically equivalent string.
/// <para/>
Expand All @@ -81,7 +101,7 @@ public void Reset()
/// <returns>The next string that is canonically equivalent. The value null is returned when
/// the iteration is done.</returns>
/// <stable>ICU 2.4</stable>
public string Next() // ICU4N TODO: API - change to MoveNext(), Current
private string Next()
{
if (done) return null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3344,7 +3344,7 @@ public void TestSourceTargetSet2()
UnicodeMap<UnicodeSet> leadToTrail = new UnicodeMap<UnicodeSet>();
UnicodeMap<UnicodeSet> leadToSources = new UnicodeMap<UnicodeSet>();
UnicodeSet nonStarters = new UnicodeSet("[:^ccc=0:]").Freeze();
CanonicalIterator can = new CanonicalIterator("");
CanonicalEnumerator can = new CanonicalEnumerator("");

UnicodeSet disorderedMarks = new UnicodeSet();

Expand All @@ -3357,10 +3357,12 @@ public void TestSourceTargetSet2()
}

can.SetSource(s);
for (String t = can.Next(); t != null; t = can.Next())
{
disorderedMarks.Add(t);
}
while (can.MoveNext())
disorderedMarks.Add(can.Current);
//for (String t = can.Next(); t != null; t = can.Next())
//{
// disorderedMarks.Add(t);
//}

// if s has two code points, (or more), add the lead/trail information
int first = s.CodePointAt(0);
Expand Down Expand Up @@ -3400,8 +3402,10 @@ public void TestSourceTargetSet2()
foreach (String trail in trailSet)
{
can.SetSource(source + trail);
for (String t = can.Next(); t != null; t = can.Next())
//for (String t = can.Next(); t != null; t = can.Next())
while (can.MoveNext())
{
string t = can.Current;
if (t.EndsWith(trail, StringComparison.Ordinal)) continue;
disorderedMarks.Add(t);
}
Expand Down
45 changes: 29 additions & 16 deletions tests/ICU4N.Tests/Dev/Test/Normalizers/TestCanonicalIterator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class TestCanonicalIterator : TestFmwk
public void TestExhaustive()
{
int counter = 0;
CanonicalIterator it = new CanonicalIterator("");
CanonicalEnumerator it = new CanonicalEnumerator("");
/*
CanonicalIterator slowIt = new CanonicalIterator("");
slowIt.SKIP_ZEROS = false;
Expand Down Expand Up @@ -61,7 +61,7 @@ public int TestSpeed()

string s = "\uAC01\u0345";

CanonicalIterator it = new CanonicalIterator(s);
CanonicalEnumerator it = new CanonicalEnumerator(s);
double start, end;
int x = 0; // just to keep code from optimizing away.
int iterations = 10000;
Expand Down Expand Up @@ -89,10 +89,9 @@ public int TestSpeed()
for (int i = 0; i < iterations; ++i)
{
it.SetSource(s);
while (true)
while (it.MoveNext())
{
string item = it.Next();
if (item == null) break;
string item = it.Current;
x += item.Length;
}
}
Expand Down Expand Up @@ -123,33 +122,45 @@ public void TestBasic()
// NOTE: we use a TreeSet below to sort the output, which is not guaranteed to be sorted!

ISet<string> results = new SortedSet<string>(StringComparer.Ordinal);
CanonicalIterator.Permute("ABC", false, results);
CanonicalEnumerator.Permute("ABC", false, results);
expectEqual("Simple permutation ", "", CollectionToString(results), "ABC, ACB, BAC, BCA, CAB, CBA");

// try samples
ISet<string> set = new SortedSet<string>(StringComparer.Ordinal);
for (int i = 0; i < testArray.Length; ++i)
{
//Logln("Results for: " + name.transliterate(testArray[i]));
CanonicalIterator it = new CanonicalIterator(testArray[i][0]);
CanonicalEnumerator it = new CanonicalEnumerator(testArray[i][0]);
// int counter = 0;
set.Clear();
string first = null;
while (true)
while (it.MoveNext())
{
String result = it.Next();
string result = it.Current;
if (first == null)
{
first = result;
}
if (result == null) break;
set.Add(result); // sort them
//Logln(++counter + ": " + hex.transliterate(result));
//Logln(" = " + name.transliterate(result));
}
//while (true)
//{
// String result = it.Next();
// if (first == null)
// {
// first = result;
// }
// if (result == null) break;
// set.Add(result); // sort them
// //Logln(++counter + ": " + hex.transliterate(result));
// //Logln(" = " + name.transliterate(result));
//}
expectEqual(i + ": ", testArray[i][0], CollectionToString(set), testArray[i][1]);
it.Reset();
if (!it.Next().Equals(first))
it.MoveNext();
if (!it.Current.Equals(first))
{
Errln("CanonicalIterator.reset() failed");
}
Expand Down Expand Up @@ -190,7 +201,7 @@ public string GetReadable(object obj)
return "[" + (SHOW_NAMES ? Hex(s) + "; " : "") + Hex(s) + "]";
}

private void CharacterTest(string s, int ch, CanonicalIterator it)
private void CharacterTest(string s, int ch, CanonicalEnumerator it)
{
int mixedCounter = 0;
int lastMixedCounter = -1;
Expand All @@ -206,10 +217,10 @@ private void CharacterTest(string s, int ch, CanonicalIterator it)

it.SetSource(s);

while (true)
while (it.MoveNext())
{
string item = it.Next();
if (item == null) break;
string item = it.Current;
//if (item == null) break;
if (item.Equals(s)) gotSource = true;
if (item.Equals(decomp)) gotDecomp = true;
if (item.Equals(comp)) gotComp = true;
Expand Down Expand Up @@ -257,8 +268,10 @@ private void CharacterTest(string s, int ch, CanonicalIterator it)
{
Errln("FAIL CanonicalIterator: " + s + " decomp: " + decomp + " comp: " + comp);
it.Reset();
for (string item = it.Next(); item != null; item = it.Next())
//for (string item = it.Next(); item != null; item = it.Next())
while (it.MoveNext())
{
string item = it.Current;
Err(item + " ");
}
Errln("");
Expand Down

0 comments on commit 1c0bb56

Please sign in to comment.