Author: atsushi
Date: 2005-06-20 23:52:46 -0400 (Mon, 20 Jun 2005)
New Revision: 46269

Modified:
   branches/atsushi/mcs/class/corlib/Mono.Globalization.Unicode/ChangeLog
   
branches/atsushi/mcs/class/corlib/Mono.Globalization.Unicode/MSCompatUnicodeTable.template
   
branches/atsushi/mcs/class/corlib/Mono.Globalization.Unicode/SimpleCollator.cs
   branches/atsushi/mcs/class/corlib/Mono.Globalization.Unicode/TestDriver.cs
Log:
2005-06-21  Atsushi Enomoto  <[EMAIL PROTECTED]>

        * SimpleCollator.cs : implemented IndexOf(string,string,CompareOptions)
          and IsPrefix(). Tiny code refactory.
        * TestDriver.cs : sample IsPrefix() and IndexOf() usage.
        * MSCompatUnicodeTable.cs : tiny refactory for CodePointIndexer use.



Modified: branches/atsushi/mcs/class/corlib/Mono.Globalization.Unicode/ChangeLog
===================================================================
--- branches/atsushi/mcs/class/corlib/Mono.Globalization.Unicode/ChangeLog      
2005-06-20 23:32:09 UTC (rev 46268)
+++ branches/atsushi/mcs/class/corlib/Mono.Globalization.Unicode/ChangeLog      
2005-06-21 03:52:46 UTC (rev 46269)
@@ -1,3 +1,10 @@
+2005-06-21  Atsushi Enomoto  <[EMAIL PROTECTED]>
+
+       * SimpleCollator.cs : implemented IndexOf(string,string,CompareOptions)
+         and IsPrefix(). Tiny code refactory.
+       * TestDriver.cs : sample IsPrefix() and IndexOf() usage.
+       * MSCompatUnicodeTable.cs : tiny refactory for CodePointIndexer use.
+
 2005-06-20  Atsushi Enomoto  <[EMAIL PROTECTED]>
 
        * SimpleCollator.cs :

Modified: 
branches/atsushi/mcs/class/corlib/Mono.Globalization.Unicode/MSCompatUnicodeTable.template
===================================================================
--- 
branches/atsushi/mcs/class/corlib/Mono.Globalization.Unicode/MSCompatUnicodeTable.template
  2005-06-20 23:32:09 UTC (rev 46268)
+++ 
branches/atsushi/mcs/class/corlib/Mono.Globalization.Unicode/MSCompatUnicodeTable.template
  2005-06-21 03:52:46 UTC (rev 46269)
@@ -21,20 +21,25 @@
 
        internal class MSCompatUnicodeTable
        {
-               public static byte [] Categories {
-                       get { return categories; }
+
+               public static byte Categories (int index)
+               {
+                       return categories [index];
                }
 
-               public static byte [] Level1 {
-                       get { return level1; }
+               public static byte Level1 (int index)
+               {
+                       return level1 [index];
                }
 
-               public static byte [] Level2 {
-                       get { return level2; }
+               public static byte Level2 (int index)
+               {
+                       return level2 [index];
                }
 
-               public static byte [] Level3 {
-                       get { return level3; }
+               public static byte Level3 (int index)
+               {
+                       return level3 [index];
                }
 
                public static bool IsIgnorable (int i)
@@ -82,6 +87,8 @@
 
                #region Utilities
 
+               // FIXME: this is just a stub. Should be replaced by an
+               // implementation which uses tailorings table.
                public static string GetExpansion (char c)
                {
                        switch (c) {

Modified: 
branches/atsushi/mcs/class/corlib/Mono.Globalization.Unicode/SimpleCollator.cs
===================================================================
--- 
branches/atsushi/mcs/class/corlib/Mono.Globalization.Unicode/SimpleCollator.cs  
    2005-06-20 23:32:09 UTC (rev 46268)
+++ 
branches/atsushi/mcs/class/corlib/Mono.Globalization.Unicode/SimpleCollator.cs  
    2005-06-21 03:52:46 UTC (rev 46269)
@@ -99,14 +99,22 @@
                void FillSortKeyRaw (int i)
                {
                        if (0x3400 <= i && i <= 0x4DB5) {
-                               FillCJKExtensionSortKeyRaw (i);
+                               int diff = i - 0x3400;
+                               buf.AppendCJKExtension (
+                                       (byte) (0x10 + diff / 254),
+                                       (byte) (diff % 254 + 2));
                                return;
                        }
 
                        UnicodeCategory uc = char.GetUnicodeCategory ((char) i);
                        switch (uc) {
                        case UnicodeCategory.PrivateUse:
-                               FillPrivateUseSortKeyRaw (i);
+                               int diff = i - 0xE000;
+                               buf.AppendNormal (
+                                       (byte) (0xE5 + diff / 254),
+                                       (byte) (diff % 254 + 2),
+                                       0,
+                                       0);
                                return;
                        case UnicodeCategory.Surrogate:
                                FillSurrogateSortKeyRaw (i);
@@ -115,10 +123,10 @@
 
                        if (Uni.HasSpecialWeight ((char) i))
                                buf.AppendKana (
-                                       Uni.Categories [i],
-                                       Uni.Level1 [i],
-                                       Uni.Level2 [i],
-                                       Uni.Level3 [i],
+                                       Uni.Categories (i),
+                                       Uni.Level1 (i),
+                                       Uni.Level2 (i),
+                                       Uni.Level3 (i),
                                        Uni.IsJapaneseSmallLetter ((char) i),
                                        Uni.GetJapaneseDashType ((char) i),
                                        !Uni.IsHiragana ((char) i),
@@ -126,31 +134,12 @@
                                        );
                        else
                                buf.AppendNormal (
-                                       Uni.Categories [i],
-                                       Uni.Level1 [i],
-                                       Uni.Level2 [i],
-                                       Uni.Level3 [i]);
+                                       Uni.Categories (i),
+                                       Uni.Level1 (i),
+                                       Uni.Level2 (i),
+                                       Uni.Level3 (i));
                }
 
-               void FillCJKExtensionSortKeyRaw (int i)
-               {
-                       int diff = i - 0x3400;
-
-                       buf.AppendCJKExtension (
-                               (byte) (0x10 + diff / 254),
-                               (byte) (diff % 254 + 2));
-               }
-
-               void FillPrivateUseSortKeyRaw (int i)
-               {
-                       int diff = i - 0xE000;
-                       buf.AppendNormal (
-                               (byte) (0xE5 + diff / 254),
-                               (byte) (diff % 254 + 2),
-                               0,
-                               0);
-               }
-
                void FillSurrogateSortKeyRaw (int i)
                {
                        int diffbase = 0;
@@ -213,6 +202,77 @@
 
                #endregion
 
+               #region IsPrefix()
+
+               public bool IsPrefix (string src, string target, CompareOptions 
opt)
+               {
+                       return IsPrefix (src, target, 0, src.Length, opt);
+               }
+
+               public bool IsPrefix (string s, string target, int start, int 
length, CompareOptions opt)
+               {
+                       SetOptions (opt);
+
+                       int min = length > target.Length ? target.Length : 
length;
+                       int si = start;
+
+                       // FIXME: this is not enough to handle tailorings.
+                       for (int j = 0; j < min; j++, si++) {
+                               int ci = FilterOptions (s [si]);
+                               int cj = FilterOptions (target [j]);
+                               if (ci == cj)
+                                       continue;
+                               if (IsIgnorable (s [si])) {
+                                       if (!IsIgnorable (target [j]))
+                                               j--;
+                                       continue;
+                               }
+                               else if (IsIgnorable (target [j])) {
+                                       si--;
+                                       continue;
+                               }
+
+                               // FIXME: should handle expansions (and it 
+                               // should be before codepoint comparison).
+                               string expansion = GetExpansion (s [si]);
+                               if (expansion != null)
+                                       return false;
+                               expansion = GetExpansion (target [j]);
+                               if (expansion != null)
+                                       return false;
+
+                               if (Uni.Categories (ci) != Uni.Categories (cj) 
||
+                                       Uni.Level1 (ci) != Uni.Level1 (cj) ||
+                                       !ignoreNonSpace && Uni.Level2 (ci) != 
Uni.Level2 (cj) ||
+                                       Uni.Level3 (ci) != Uni.Level3 (cj))
+                                       return false;
+                               if (!Uni.HasSpecialWeight ((char) ci))
+                                       continue;
+                               if (Uni.IsJapaneseSmallLetter ((char) ci) !=
+                                       Uni.IsJapaneseSmallLetter ((char) cj) ||
+                                       Uni.GetJapaneseDashType ((char) ci) !=
+                                       Uni.GetJapaneseDashType ((char) cj) ||
+                                       !Uni.IsHiragana ((char) ci) !=
+                                       !Uni.IsHiragana ((char) cj) ||
+                                       Uni.IsHalfWidthKana ((char) ci) !=
+                                       Uni.IsHalfWidthKana ((char) cj))
+                                       return false;
+                       }
+                       if (si == min) {
+                               // All codepoints in the compared range
+                               // matches. In that case, what matters 
+                               // is whether the remaining part of 
+                               // "target" is ignorable or not.
+                               for (int i = min; i < target.Length; i++)
+                                       if (!IsIgnorable (target [i]))
+                                               return false;
+                               return true;
+                       }
+                       return true;
+               }
+
+               #endregion
+
                #region IndexOf()
 
                public int IndexOf (string s, char target)
@@ -227,6 +287,7 @@
 
                public int IndexOf (string s, char target, int start, int 
length, CompareOptions opt)
                {
+                       // If target has an expansion, then use string search.
                        string expansion = GetExpansion (target);
                        if (expansion != null)
                                return IndexOf (s, expansion, start, length, 
opt);
@@ -234,17 +295,25 @@
                        SetOptions (opt);
 
                        int ti = FilterOptions ((int) target);
-                       for (int idx = 0; idx < s.Length; idx++) {
+                       for (int idx = start; idx < length; idx++) {
+                               switch (char.GetUnicodeCategory (s [idx])) {
+                               case UnicodeCategory.PrivateUse:
+                               case UnicodeCategory.Surrogate:
+                                       if (s [idx] != target)
+                                               continue;
+                                       return idx;
+                               }
+
                                expansion = GetExpansion (s [idx]);
                                if (expansion != null)
                                        continue; // since target cannot be 
expansion as conditioned above.
                                if (s [idx] == target)
                                        return idx;
                                int si = FilterOptions ((int) s [idx]);
-                               if (Uni.Categories [si] != Uni.Categories [ti] 
||
-                                       Uni.Level1 [si] != Uni.Level1 [ti] ||
-                                       !ignoreNonSpace && Uni.Level2 [si] != 
Uni.Level2 [ti] ||
-                                       Uni.Level3 [si] != Uni.Level3 [ti])
+                               if (Uni.Categories (si) != Uni.Categories (ti) 
||
+                                       Uni.Level1 (si) != Uni.Level1 (ti) ||
+                                       !ignoreNonSpace && Uni.Level2 (si) != 
Uni.Level2 (ti) ||
+                                       Uni.Level3 (si) != Uni.Level3 (ti))
                                        continue;
                                if (!Uni.HasSpecialWeight ((char) si))
                                        return idx;
@@ -261,9 +330,27 @@
                        return -1;
                }
 
+               public int IndexOf (string s, string target, CompareOptions opt)
+               {
+                       return IndexOf (s, target, 0, s.Length, opt);
+               }
+
                public int IndexOf (string s, string target, int start, int 
length, CompareOptions opt)
                {
-                       throw new NotImplementedException ();
+                       SetOptions (opt);
+                       do {
+                               // FIXME: this should be modified to handle
+                               // expansions
+                               int idx = IndexOf (s, target [0], start, 
length, opt);
+                               if (idx < 0)
+                                       return idx;
+
+                               if (IsPrefix (s, target, start + idx, length - 
idx, opt))
+                                       return idx;
+                               start++;
+                               length--;
+                       } while (length > 0);
+                       return -1;
                }
 
                #endregion

Modified: 
branches/atsushi/mcs/class/corlib/Mono.Globalization.Unicode/TestDriver.cs
===================================================================
--- branches/atsushi/mcs/class/corlib/Mono.Globalization.Unicode/TestDriver.cs  
2005-06-20 23:32:09 UTC (rev 46268)
+++ branches/atsushi/mcs/class/corlib/Mono.Globalization.Unicode/TestDriver.cs  
2005-06-21 03:52:46 UTC (rev 46269)
@@ -35,9 +35,20 @@
                        IndexOf ("ABC", '\uFF22', CompareOptions.IgnoreCase | 
CompareOptions.IgnoreWidth);
                        IndexOf ("ABCDE", '\u0117', 
CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase);
 
+                       IsPrefix ("ABC", "c", CompareOptions.IgnoreCase);
+                       IsPrefix ("BC", "c", CompareOptions.IgnoreCase);
+                       IsPrefix ("C", "c", CompareOptions.IgnoreCase);
+                       IsPrefix ("E", "\u0117", CompareOptions.IgnoreNonSpace 
| CompareOptions.IgnoreCase);
+
+                       IndexOf ("ABC", "1", CompareOptions.None);
+                       IndexOf ("ABC", "c", CompareOptions.IgnoreCase);
+                       IndexOf ("ABC", "\uFF22", CompareOptions.IgnoreCase | 
CompareOptions.IgnoreWidth);
+                       IndexOf ("ABCDE", "\u0117", 
CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase);
+
                        DumpSortKey ("AE");
                        DumpSortKey ("\u00C6");
 
+/*
                        // dump sortkey for every single character.
                        for (int i = 0; i <= char.MaxValue; i++) {
                                byte [] data = coll.GetSortKey (new string 
((char) i, 1)).KeyData;
@@ -50,6 +61,7 @@
                                        i, Char.GetUnicodeCategory ((char) i));
                        }
                        Output.Close ();
+*/
                }
 
                void Compare (string s1, string s2)
@@ -65,10 +77,22 @@
 
                void IndexOf (string s, char c, CompareOptions opt)
                {
-                       Console.Error.WriteLine ("Index: {0} {1} / {2}",
+                       Console.Error.WriteLine ("cIndex: {0} {1} / {2}",
                                coll.IndexOf (s, c, opt), s, c);
                }
 
+               void IndexOf (string s1, string s2, CompareOptions opt)
+               {
+                       Console.Error.WriteLine ("sIndex: {0} {1} / {2}",
+                               coll.IndexOf (s1, s2, opt), s1, s2);
+               }
+
+               void IsPrefix (string s1, string s2, CompareOptions opt)
+               {
+                       Console.Error.WriteLine ("IsPrefix: {0} {1} / {2}",
+                               coll.IsPrefix (s1, s2, opt), s1, s2);
+               }
+
                void DumpSortKey (string s)
                {
                        byte [] data = coll.GetSortKey (s).KeyData;

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to