2007-11-30 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mcs / class / corlib / Mono.Globalization.Unicode / SortKeyBuffer.cs
index 1ed001dbabaa01550ba12085d9db6ca6da0c3db9..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;
@@ -130,7 +160,7 @@ namespace Mono.Globalization.Unicode
                        // 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];
                        }
@@ -154,16 +184,16 @@ namespace Mono.Globalization.Unicode
                        // 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 > 8064; offsetValue -= 8064)
+                       for (; offsetValue > 8192; offsetValue -= 8192)
                                AppendBufferPrimitive (0xFF, 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 % 8064;
+                       int offsetValue = (l2 + 1) % 8192;
 #endif
-                       AppendBufferPrimitive ((byte) ((offsetValue / 63) * 4 + 0x80), ref l5b, ref l5);
-                       AppendBufferPrimitive ((byte) (offsetValue % 63 * 4 + 7), ref l5b, ref l5);
+                       AppendBufferPrimitive ((byte) ((offsetValue / 64) + 0x80), ref l5b, ref l5);
+                       AppendBufferPrimitive ((byte) (offsetValue % 64 * 4 + 3), ref l5b, ref l5);
 
                        level5LastPos = l2;
 
@@ -204,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)
@@ -215,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);