Fourth patch from: Damien Diederen <dd@crosstwine.com>
[mono.git] / mcs / class / corlib / Mono.Globalization.Unicode / CodePointIndexer.cs
index 7e1f73cee4c9960d626dd260b2aee26c01f486c0..c655f00cecee76bfe35699b23273de5d7e18ffae 100644 (file)
@@ -1,3 +1,30 @@
+//
+// CodePointIndexer.cs : indexing table optimizer
+//
+// 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.Globalization;
 using System.Text;
@@ -26,7 +53,8 @@ namespace Mono.Globalization.Unicode
 
                // This class is used to compactize indexes to limited areas so that
                // we can save extraneous 0,0,0,0,0... in the tables.
-               internal class TableRange
+               [Serializable]
+               internal struct TableRange
                {
                        public TableRange (int start, int end, int indexStart)
                        {
@@ -45,9 +73,7 @@ namespace Mono.Globalization.Unicode
                }
 
                readonly TableRange [] ranges;
-#if DumpFullArray
-               public const int TotalCount = char.MaxValue + 1;
-#else
+
                public readonly int TotalCount;
 
                int defaultIndex;
@@ -70,37 +96,38 @@ namespace Mono.Globalization.Unicode
 //                             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);
 //                     Console.Error.WriteLine ("Total items: {0:X} ({1})", TotalCount, TotalCount);
                }
-#endif
 
                public int ToIndex (int cp)
                {
-#if DumpFullArray
-                       return cp;
-#else
-                       for (int t = 0; t < ranges.Length; t++)
-                               if (ranges [t].Start <= cp && cp < ranges [t].End)
+                       for (int t = 0; t < ranges.Length; t++) {
+                               if (cp < ranges [t].Start)
+                                       return defaultIndex;
+                               else if (cp < ranges [t].End)
                                        return cp - ranges [t].Start + ranges [t].IndexStart;
+                       }
                        return defaultIndex;
 //                     throw new SystemException (String.Format ("Should not happen: no map definition for cp {0:x}({1})", cp, (char) cp));
-#endif
                }
 
                public int ToCodePoint (int i)
                {
-#if DumpFullArray
-                       return i;
-#else
                        for (int t = 0; t < ranges.Length; t++) {
+/*
                                if (t > 0 && i < ranges [t - 1].IndexEnd)
-                                       return -1; // unexpected out of range
+                                       return defaultCP; // unexpected out of range
                                if (ranges [t].IndexStart <= i &&
                                        i < ranges [t].IndexEnd)
                                        return i - ranges [t].IndexStart
                                                + ranges [t].Start;
+*/
+                               if (i < ranges [t].IndexStart)
+                                       return defaultCP;
+                               if (i < ranges [t].IndexEnd)
+                                       return i - ranges [t].IndexStart
+                                               + ranges [t].Start;
                        }
                        return defaultCP;
 //                     throw new SystemException (String.Format ("Should not happen: no map definition for index {0:x}({1})", i, i));
-#endif
                }
        }
 }