2007-11-30 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mcs / class / corlib / Mono.Globalization.Unicode / SortKeyBuffer.cs
index 27375f6ed3893a8d23d621c37ce5e953b79bb931..a4f6230c3a66b5a6da0e0c5f8f2448b4f0e531da 100644 (file)
@@ -1,3 +1,31 @@
+//
+// SortKeyBuffer.cs : buffer implementation for GetSortKey()
+//
+// Author:
+//     Atsushi Enomoto  <atsushi@ximian.com>
+//
+// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
 using System;
 using System.IO;
 using System.Globalization;
@@ -37,8 +65,10 @@ namespace Mono.Globalization.Unicode
                        l1b = l2b = l3b = l4sb = l4tb = l4kb = l4wb = l5b = null;
                }
 
-               internal void Initialize (CompareOptions options, string s, bool frenchSort)
+               internal void Initialize (CompareOptions options, int lcid, string s, bool frenchSort)
                {
+                       this.source = s;
+                       this.lcid = lcid;
                        this.options = options;
                        int len = s.Length;
                        processLevel2 = (options & CompareOptions.IgnoreNonSpace) == 0;
@@ -121,10 +151,16 @@ namespace Mono.Globalization.Unicode
                        if (lv3 == 0)
                                lv3 = 2;
 
+                       // Special weight processing
+                       if (category == 6 && (options & CompareOptions.StringSort) == 0) {
+                               AppendLevel5 (category, lv1);
+                               return;
+                       }
+
                        // non-primary diacritical weight is added to that of
                        // the previous character (and does not reset level 3
                        // weight).
-                       if (processLevel2 &&category == 1 && l1 > 0) {
+                       if (processLevel2 && category == 1 && l1 > 0) {
                                lv2 = (byte) (lv2 + l2b [--l2]);
                                lv3 = l3b [--l3];
                        }
@@ -139,22 +175,31 @@ namespace Mono.Globalization.Unicode
                }
 
                // Append variable-weight character.
-               internal void AppendLevel5 (byte [] table, int idx, int currentIndex)
+               // It uses level 2 index for counting offsets (since level1
+               // might be longer than 1).
+               private void AppendLevel5 (byte category, byte lv1)
                {
                        // offset
-                       int offsetValue = currentIndex - level5LastPos;
-                       for (; offsetValue > 8064; offsetValue -= 8064)
+#if false
+                       // If it strictly matches to Windows, offsetValue is always l2.
+                       int offsetValue = l2 - level5LastPos;
+                       // If it strictly matches ti Windows, no 0xFF here.
+                       for (; offsetValue > 8192; offsetValue -= 8192)
                                AppendBufferPrimitive (0xFF, ref l5b, ref l5);
-                       if (offsetValue > 63)
-                               AppendBufferPrimitive ((byte) (offsetValue - 63 / 4 + 0x80), ref l5b, ref l5);
-                       AppendBufferPrimitive ((byte) (offsetValue % 63), ref l5b, ref l5);
+#else
+                       // LAMESPEC: Windows cannot compute lv5 values for
+                       // those string that has length larger than 8064.
+                       // (It reminds me of SQL Server varchar length).
+                       int offsetValue = (l2 + 1) % 8192;
+#endif
+                       AppendBufferPrimitive ((byte) ((offsetValue / 64) + 0x80), ref l5b, ref l5);
+                       AppendBufferPrimitive ((byte) (offsetValue % 64 * 4 + 3), ref l5b, ref l5);
 
-                       level5LastPos = currentIndex;
+                       level5LastPos = l2;
 
                        // sortkey value
-                       idx++; // skip the "variable" mark: 01
-                       while (table [idx] != 0)
-                               AppendBufferPrimitive (table [idx++], ref l5b, ref l5);
+                       AppendBufferPrimitive (category, ref l5b, ref l5);
+                       AppendBufferPrimitive (lv1, ref l5b, ref l5);
                }
 
                private void AppendBufferPrimitive (byte value, ref byte [] buf, ref int bidx)
@@ -189,7 +234,7 @@ namespace Mono.Globalization.Unicode
 
                public SortKey GetResult ()
                {
-                       if (frenchSort && !frenchSorted) {
+                       if (frenchSort && !frenchSorted && l2b != null) {
                                int i = 0;
                                for (; i < l2b.Length; i++)
                                        if (l2b [i] == 0)
@@ -200,7 +245,7 @@ namespace Mono.Globalization.Unicode
 
                        l2 = GetOptimizedLength (l2b, l2, 2);
                        l3 = GetOptimizedLength (l3b, l3, 2);
-                       bool hasJapaneseWeight = (l4s > 0);
+                       bool hasJapaneseWeight = (l4s > 0); // snapshot before being optimized
                        l4s = GetOptimizedLength (l4sb, l4s, 0xE4);
                        l4t = GetOptimizedLength (l4tb, l4t, 3);
                        l4k = GetOptimizedLength (l4kb, l4k, 0xE4);