3 // CollationbElementTableCodeGenerator.cs
6 // Atsushi Enomoto <atsushi@ximian.com>
8 // Copyright 2005 Novell, Inc
10 // It supports creation of CollationElementTable.cs
14 using System.Globalization;
17 namespace Mono.Globalization.Unicode
19 internal class CollationElementTableCodeGenerator
21 private int lineCount = 0;
23 int keyCount = 1; // 0 indicates that there is no matching entry
24 int [] collElem = new int [CollationElementTableUtil.Indexer.TotalCount];
25 SortKeyValue [] keyValues = new SortKeyValue [32768];
27 public static void Main ()
29 new CollationElementTableCodeGenerator ().Run ();
38 Console.Error.WriteLine ("Internal error at line " + lineCount);
43 for (int x = 1; x < CollationElementTableUtil.Indexer.TotalCount; x++) {
44 int i = CollationElementTableUtil.Indexer.GetIndexForCodePoint (x);
45 int q = keyValues [collElem [i]].Quarternary;
46 // if (i != q && q != 0)
47 // Console.Error.WriteLine ("differs : {0} -> {1}", i, q);
50 Console.Error.WriteLine ("latter was smaller at {2} : {0} / {1}", lastQ, q, i);
57 private void Serialize ()
59 Console.WriteLine ("static readonly int [] collElem = new int [] {");
60 DumpArray (collElem, CollationElementTableUtil.Indexer.TotalCount, true);
61 Console.WriteLine ("};");
62 Console.WriteLine ("static readonly SortKeyValue [] keyValues = new SortKeyValue [] {");
63 for (int i = 0; i < keyCount; i++) {
64 SortKeyValue s = keyValues [i];
65 Console.WriteLine (" new SortKeyValue ({0}, 0x{1:X04}, 0x{2:X04}, 0x{3:X04}, 0x{4:X04}),",
66 s.Alt ? "true" : "false", s.Primary, s.Secondary,
67 s.Thirtiary, s.Quarternary);
69 Console.WriteLine ("};");
72 private void DumpArray (int [] array, int count, bool getCP)
74 if (array.Length < count)
75 throw new ArgumentOutOfRangeException ("count");
76 for (int i = 0; i < count; i++) {
78 Console.Write ("0, ");
80 Console.Write ("0x{0:X}, ", array [i]);
82 int l = getCP ? CollationElementTableUtil.Indexer.ToCodePoint (i) : i;
83 Console.WriteLine ("// {0:X04}-{1:X04}", l - 15, l);
90 ushort [] v = new ushort [4];
92 TextReader reader = Console.In;
93 while (reader.Peek () != -1) {
94 string line = reader.ReadLine ();
96 if (line.StartsWith ("@"))
97 continue; // @version, @variable etc.
98 int idx = line.IndexOf ('#');
100 line = line.Substring (0, idx);
101 if (line.Length == 0)
104 int cp = int.Parse (line.Substring (0, 5), NumberStyles.HexNumber);
105 int collElemIdx = CollationElementTableUtil.Indexer.ToIndex (cp);
106 if (collElemIdx < 0) {
107 Console.Error.WriteLine ("WARNING: handle character {0:x} in collation element table.", cp);
111 line = line.Substring (line.IndexOf (';') + 1).Trim ();
112 // count entries in a line
113 int entryPerLine = 0;
114 for (int e = 0; (e = line.IndexOf ('[', e) + 1) > 0;)
118 for (int e = 0; e < entryPerLine; e++) {
119 start = line.IndexOf ('[', start) + 1;
120 string s = line.Substring (start, line.IndexOf (']', start) - start);
125 string [] vslist = s.Substring (1).Split ('.');
127 for (int i = 0; i < 4; i++) {
128 if (vslist [i].Length > 4)
131 v [i] = ushort.Parse (vslist [i], NumberStyles.HexNumber);
134 // Console.Error.WriteLine ("WARNING: skipped entry {0:X}", cp);
138 if (entryPerLine == 1) {
139 // idx = 0 means "no matching entry", so here we start from 1
140 for (idx = 1; idx < keyCount; idx++) {
141 SortKeyValue k = keyValues [idx];
143 k.Primary == v [0] &&
144 k.Secondary == v [1] &&
145 k.Thirtiary == v [2] &&
146 k.Quarternary == v [3])
153 if (entryPerLine == 1)
154 collElem [collElemIdx] = idx;
156 collElem [collElemIdx] =
157 (short) (keyCount - entryPerLine)
158 + (short) (entryPerLine << 16);
163 private void AddEntry (bool alt, ushort [] v)
165 if (keyCount == keyValues.Length) {
166 SortKeyValue [] tmp = new SortKeyValue [keyCount * 2];
167 Array.Copy (keyValues, tmp, keyCount);
170 keyValues [keyCount] =
171 new SortKeyValue (alt,
172 v [0], (byte) v [1], (byte) v [2], v [3]);