2005-04-25 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / corlib / Mono.Globalization.Unicode / CodePointIndexer.cs
1 using System;
2 using System.Globalization;
3 using System.Text;
4
5 namespace Mono.Globalization.Unicode
6 {
7         internal class CodePointIndexer
8         {
9                 // This class is used to compactize indexes to limited areas so that
10                 // we can save extraneous 0,0,0,0,0... in the tables.
11                 internal class TableRange
12                 {
13                         public TableRange (int start, int end, int indexStart)
14                         {
15                                 Start = start;
16                                 End = end;
17                                 Count = End - Start;
18                                 IndexStart = indexStart;
19                                 IndexEnd = IndexStart + Count;
20                         }
21
22                         public readonly int Start;
23                         public readonly int End;
24                         public readonly int Count;
25                         public readonly int IndexStart;
26                         public readonly int IndexEnd;
27                 }
28
29                 readonly TableRange [] ranges;
30 #if DumpFullArray
31                 public const int TotalCount = char.MaxValue + 1;
32 #else
33                 public readonly int TotalCount;
34
35                 public CodePointIndexer (int [] starts, int [] ends)
36                 {
37                         ranges = new TableRange [starts.Length];
38                         for (int i = 0; i < ranges.Length; i++)
39                                 ranges [i] = new TableRange (starts [i],
40                                         ends [i], i == 0 ? 0 :
41                                         ranges [i - 1].IndexStart +
42                                         ranges [i - 1].Count);
43                         for (int i = 0; i < ranges.Length; i++)
44                                 TotalCount += ranges [i].Count;
45
46 //                      for (int i = 0; i < ranges.Length; i++)
47 //                              Console.Error.WriteLine ("RANGES [{0}] : {1:x} to {2:x} index {3:x} to {4:x}. total {5:x}", i, ranges [i].Start, ranges [i].End, ranges [i].IndexStart, ranges [i].IndexEnd, ranges [i].Count);
48 //                      Console.Error.WriteLine ("Total items: {0:X} ({1})", TotalCount, TotalCount);
49                 }
50 #endif
51
52                 public int GetIndexForCodePoint (int cp)
53                 {
54 #if DumpFullArray
55                         return cp;
56 #else
57                         for (int t = 0; t < ranges.Length; t++)
58                                 if (ranges [t].Start <= cp && cp < ranges [t].End)
59                                         return cp - ranges [t].Start + ranges [t].IndexStart;
60                         return 0;
61 //                      return -1;
62 //                      throw new SystemException (String.Format ("Should not happen: no map definition for cp {0:x}({1})", cp, (char) cp));
63 #endif
64                 }
65
66                 public int GetCodePointForIndex (int i)
67                 {
68 #if DumpFullArray
69                         return i;
70 #else
71                         for (int t = 0; t < ranges.Length; t++) {
72                                 if (t > 0 && i < ranges [t - 1].IndexEnd)
73                                         return -1; // unexpected out of range
74                                 if (ranges [t].IndexStart <= i &&
75                                         i < ranges [t].IndexEnd)
76                                         return i - ranges [t].IndexStart
77                                                 + ranges [t].Start;
78                         }
79                         return 0;
80 //                      return -1;
81 //                      throw new SystemException (String.Format ("Should not happen: no map definition for index {0:x}({1})", i, i));
82 #endif
83                 }
84         }
85 }