2 using System.Globalization;
5 namespace Mono.Globalization.Unicode
7 internal class CodePointIndexer
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
13 public TableRange (int start, int end, int indexStart)
18 IndexStart = indexStart;
19 IndexEnd = IndexStart + Count;
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;
29 readonly TableRange [] ranges;
31 public const int TotalCount = char.MaxValue + 1;
33 public readonly int TotalCount;
35 public CodePointIndexer (int [] starts, int [] ends)
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;
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);
52 public int GetIndexForCodePoint (int cp)
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;
62 // throw new SystemException (String.Format ("Should not happen: no map definition for cp {0:x}({1})", cp, (char) cp));
66 public int GetCodePointForIndex (int i)
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
81 // throw new SystemException (String.Format ("Should not happen: no map definition for index {0:x}({1})", i, i));