2005-07-27 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / corlib / Mono.Globalization.Unicode / SortKey.cs
1 using System;
2 using System.IO;
3 using System.Globalization;
4
5 namespace System.Globalization
6 {
7         public class SortKey
8         {
9                 #region Static members
10                 public static int Compare (SortKey sk1, SortKey sk2)
11                 {
12                         if (Object.ReferenceEquals (sk1, sk2)
13                                 || Object.ReferenceEquals (sk1.OriginalString,
14                                 sk2.OriginalString))
15                                 return 0;
16
17                         byte [] d1 = sk1.KeyData;
18                         byte [] d2 = sk2.KeyData;
19
20                         int len = d1.Length > d2.Length ? d2.Length : d1.Length;
21                         for (int i = 0; i < len; i++)
22                                 if (d1 [i] != d2 [i])
23                                         return d1 [i] < d2 [i] ? -1 : 1;
24                         return d1.Length == d2.Length ? 0 : d1.Length < d2.Length ? -1 : 1;
25                 }
26                 #endregion
27
28                 readonly string source;
29                 readonly CompareOptions options;
30                 readonly byte [] key;
31                 readonly int lcid;
32
33                 // for legacy unmanaged one
34                 internal SortKey (int lcid, string source, CompareOptions opt)
35                 {
36                         this.lcid = lcid;
37                         this.source = source;
38                         this.options = opt;
39                 }
40
41                 internal SortKey (int lcid, string source, byte [] buffer, CompareOptions opt,
42                         int lv1Length, int lv2Length, int lv3Length,
43                         int kanaSmallLength, int markTypeLength,
44                         int katakanaLength, int kanaWidthLength,
45                         int identLength)
46                 {
47                         this.lcid = lcid;
48                         this.source = source;
49                         this.key = buffer;
50                         this.options = opt;
51                 }
52
53                 public string OriginalString {
54                         get { return source; }
55                 }
56
57                 public byte [] KeyData {
58                         get { return key; }
59                 }
60
61                 // copy from original SortKey.cs
62                 public override bool Equals (object value)
63                 {
64                         SortKey other = (value as SortKey);
65                         if(other!=null) {
66                                 if((this.lcid==other.lcid) &&
67                                    (this.options==other.options) &&
68                                    (Compare (this, other)==0)) {
69                                         return(true);
70                                 }
71                         }
72
73                         return(false);
74                 }
75
76                 public override int GetHashCode ()
77                 {
78                         if (key.Length == 0)
79                                 return 0; // should not happen though.
80                         int val = key [0];
81                         for (int i = 1; i < key.Length; i++)
82                                 val ^= (int) key [i] << (i & 3);
83                         return (int) val;
84                 }
85
86                 // copy from original SortKey.cs
87                 public override string ToString()
88                 {
89                         return("SortKey - "+lcid+", "+options+", "+source);
90                 }
91         }
92 }