From 1c0bb56bde9b0b908f99401feb186665db870b9e Mon Sep 17 00:00:00 2001 From: Shad Storhaug Date: Thu, 17 Oct 2019 20:43:18 +0700 Subject: [PATCH] ICU4N.Text.CanonicalIterator: Renamed CanonicalEnumerator, added MoveNext() and Current, made Next() private (#21) --- .../Impl/Coll/CollationBuilder.cs | 21 ++++----- src/ICU4N/Text/CanonicalIterator.cs | 28 ++++++++++-- .../Dev/Test/Translit/TransliteratorTest.cs | 16 ++++--- .../Test/Normalizers/TestCanonicalIterator.cs | 45 ++++++++++++------- 4 files changed, 72 insertions(+), 38 deletions(-) diff --git a/src/ICU4N.Collation/Impl/Coll/CollationBuilder.cs b/src/ICU4N.Collation/Impl/Coll/CollationBuilder.cs index f605a481b..c6f59183e 100644 --- a/src/ICU4N.Collation/Impl/Coll/CollationBuilder.cs +++ b/src/ICU4N.Collation/Impl/Coll/CollationBuilder.cs @@ -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); } diff --git a/src/ICU4N/Text/CanonicalIterator.cs b/src/ICU4N/Text/CanonicalIterator.cs index 116807363..9322d9a98 100644 --- a/src/ICU4N/Text/CanonicalIterator.cs +++ b/src/ICU4N/Text/CanonicalIterator.cs @@ -35,14 +35,14 @@ namespace ICU4N.Text /// /// M. Davis /// ICU 2.4 - public sealed class CanonicalIterator // ICU4N TODO: API - rename CanonicalEnumerator + public sealed class CanonicalEnumerator // ICU4N specific - renamed from CanonicalIterator { /// - /// Construct a object. + /// Construct a object. /// /// String to get results for. /// ICU 2.4 - public CanonicalIterator(string source) + public CanonicalEnumerator(string source) { Norm2AllModes allModes = Norm2AllModes.GetNFCInstance(); nfd = allModes.Decomp; @@ -73,6 +73,26 @@ public void Reset() } } + /// + /// Gets the current canonically equivalent string. + /// + public string Current { get; private set; } + + /// + /// Move to the next canonically equivalent string. + /// + /// Warning: The strings are not guaranteed to be in any particular order. + /// + /// true if the enumerator was advancet to the next canonically equivalent element; false if the enumerator has passed the end of the collection. + /// ICU4N 60.1 + public bool MoveNext() + { + if (done) + return false; + Current = Next(); + return Current != null; + } + /// /// Get the next canonically equivalent string. /// @@ -81,7 +101,7 @@ public void Reset() /// The next string that is canonically equivalent. The value null is returned when /// the iteration is done. /// ICU 2.4 - public string Next() // ICU4N TODO: API - change to MoveNext(), Current + private string Next() { if (done) return null; diff --git a/tests/ICU4N.Tests.Transliterator/Dev/Test/Translit/TransliteratorTest.cs b/tests/ICU4N.Tests.Transliterator/Dev/Test/Translit/TransliteratorTest.cs index cd860b0ec..39aa3ec58 100644 --- a/tests/ICU4N.Tests.Transliterator/Dev/Test/Translit/TransliteratorTest.cs +++ b/tests/ICU4N.Tests.Transliterator/Dev/Test/Translit/TransliteratorTest.cs @@ -3344,7 +3344,7 @@ public void TestSourceTargetSet2() UnicodeMap leadToTrail = new UnicodeMap(); UnicodeMap leadToSources = new UnicodeMap(); UnicodeSet nonStarters = new UnicodeSet("[:^ccc=0:]").Freeze(); - CanonicalIterator can = new CanonicalIterator(""); + CanonicalEnumerator can = new CanonicalEnumerator(""); UnicodeSet disorderedMarks = new UnicodeSet(); @@ -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); @@ -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); } diff --git a/tests/ICU4N.Tests/Dev/Test/Normalizers/TestCanonicalIterator.cs b/tests/ICU4N.Tests/Dev/Test/Normalizers/TestCanonicalIterator.cs index a331a8415..efae0d4fd 100644 --- a/tests/ICU4N.Tests/Dev/Test/Normalizers/TestCanonicalIterator.cs +++ b/tests/ICU4N.Tests/Dev/Test/Normalizers/TestCanonicalIterator.cs @@ -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; @@ -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; @@ -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; } } @@ -123,7 +122,7 @@ public void TestBasic() // NOTE: we use a TreeSet below to sort the output, which is not guaranteed to be sorted! ISet results = new SortedSet(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 @@ -131,25 +130,37 @@ public void TestBasic() 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"); } @@ -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; @@ -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; @@ -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("");