607dc0c199e8ae2e2da01566281e4a5f074862f8
[mono.git] / mcs / class / corlib / System.Globalization / CharUnicodeInfo.cs
1 //
2 // System.Globalization.CharUnicodeInfo.cs
3 //
4 // Author:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
6 //
7 // (C)2005 Novell Inc,
8 //
9
10 //
11 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32 using System;
33
34 namespace System.Globalization
35 {
36         public sealed class CharUnicodeInfo
37         {
38                 private CharUnicodeInfo () {}
39
40                 public static int GetDecimalDigitValue (char ch)
41                 {
42                         int i = (int) ch;
43                         switch (i) {
44                         // They are not decimal digits but are regarded as they were.
45                         case 178:
46                                 return 2;
47                         case 179:
48                                 return 3;
49                         case 185:
50                                 return 1;
51                         case 8304:
52                                 return 0;
53                         }
54
55                         // They are not decimal digits but are regarded as they were.
56                         if (8308 <= i && i < 8314)
57                                 return i - 8304;
58                         if (8320 <= i && i < 8330)
59                                 return i - 8320;
60
61                         if (!Char.IsDigit (ch))
62                                 return -1;
63
64                         if (i < 58)
65                                 return i - 48;
66
67                         if (i < 1642)
68                                 return i - 1632;
69                         if (i < 1786)
70                                 return i - 1776;
71                         if (i < 2416)
72                                 return i - 2406;
73                         if (i < 2544)
74                                 return i - 2534;
75                         if (i < 2672)
76                                 return i - 2662;
77                         if (i < 2800)
78                                 return i - 2790;
79                         if (i < 2928)
80                                 return i - 2918;
81                         if (i < 3056)
82                                 return i - 3046;
83                         if (i < 3184)
84                                 return i - 3174;
85                         if (i < 3312)
86                                 return i - 3302;
87                         if (i < 3440)
88                                 return i - 3430;
89                         if (i < 3674)
90                                 return i - 3664;
91                         if (i < 3802)
92                                 return i - 3792;
93                         if (i < 3882)
94                                 return i - 3872;
95                         if (i < 4170)
96                                 return i - 4160;
97                         if (i < 4978)
98                                 return i - 4968;
99                         if (i < 6122)
100                                 return i - 6112;
101                         if (i < 6170)
102                                 return i - 6160;
103                         if (i < 8314)
104                                 return i - 8304;
105                         if (i < 8330)
106                                 return i - 8320;
107                         if (i < 65296)
108                                 return -1;
109                         if (i < 65306)
110                                 return i - 65296;
111                         return -1;
112                 }
113
114                 public static int GetDecimalDigitValue (string s, int index)
115                 {
116                         if (s == null)
117                                 throw new ArgumentNullException ("s");
118                         return GetDecimalDigitValue (s [index]);
119                 }
120
121                 public static int GetDigitValue (char ch)
122                 {
123                         int i = GetDecimalDigitValue (ch);
124
125                         if (i >= 0)
126                                 return i;
127                         i = (int) ch;
128
129                         if (i == 9450)
130                                 return 0;
131
132                         // They are False in Char.IsDigit(), but returns a digit
133                         if (i >= 9312 && i < 9321)
134                                 return i - 9311;
135                         if (i >= 9332 && i < 9341)
136                                 return i - 9331;
137                         if (i >= 9352 && i < 9361)
138                                 return i - 9351;
139                         if (i >= 9461 && i < 9470)
140                                 return i - 9460;
141                         if (i >= 10102 && i < 10111)
142                                 return i - 10101;
143                         if (i >= 10112 && i < 10121)
144                                 return i - 10111;
145                         if (i >= 10122 && i < 10131)
146                                 return i - 10121;
147
148                         return -1;
149                 }
150
151                 public static int GetDigitValue (string s, int index)
152                 {
153                         if (s == null)
154                                 throw new ArgumentNullException ("s");
155                         return GetDigitValue (s [index]);
156                 }
157
158                 public static double GetNumericValue (char ch)
159                 {
160                         int i = GetDigitValue (ch);
161                         if (i >= 0)
162                                 return i;
163
164                         i = (int) ch;
165
166                         switch (i) {
167                         case 188:
168                                 return 0.25;
169                         case 189:
170                                 return 0.5;
171                         case 190:
172                                 return 0.75;
173                         case 2548:
174                                 return 1;
175                         case 2549:
176                                 return 2;
177                         case 2550:
178                                 return 3;
179                         case 2551:
180                                 return 4;
181                         case 2553:
182                                 return 16;
183                         case 3056:
184                                 return 10;
185                         case 3057:
186                                 return 100;
187                         case 3058:
188                                 return 1000;
189                         case 4988:
190                                 return 10000;
191                         case 5870:
192                                 return 17;
193                         case 5871:
194                                 return 18;
195                         case 5872:
196                                 return 19;
197                         case 8531:
198                                 return 1.0 / 3;
199                         case 8532:
200                                 return 2.0 / 3;
201                         case 8537:
202                                 return 1.0 / 6;
203                         case 8538:
204                                 return 5.0 / 6;
205                         case 8539:
206                                 return 1.0 / 8;
207                         case 8540:
208                                 return 3.0 / 8;
209                         case 8541:
210                                 return 5.0 / 8;
211                         case 8542:
212                                 return 7.0 / 8;
213                         case 8543:
214                                 return 1;
215                         case 8556:
216                                 return 50;
217                         case 8557:
218                                 return 100;
219                         case 8558:
220                                 return 500;
221                         case 8559:
222                                 return 1000;
223                         case 8572:
224                                 return 50;
225                         case 8573:
226                                 return 100;
227                         case 8574:
228                                 return 500;
229                         case 8575:
230                                 return 1000;
231                         case 8576:
232                                 return 1000;
233                         case 8577:
234                                 return 5000;
235                         case 8578:
236                                 return 10000;
237                         case 9470: // IsNumber(c) is False BTW.
238                         case 10111:
239                         case 10121:
240                         case 10131:
241                                 return 10;
242                         case 12295:
243                                 return 0;
244                         case 12344:
245                                 return 10;
246                         case 12345:
247                                 return 20;
248                         case 12346:
249                                 return 30;
250                         }
251
252                         // They are not True by IsNumber() but regarded as they were.
253                         if (9451 <= i && i < 9461)
254                                 return i - 9440;
255                         if (12321 <= i && i < 12330)
256                                 return i - 12320;
257                         if (12881 <= i && i < 12896)
258                                 return i - 12860;
259                         if (12977 <= i && i < 12992)
260                                 return i - 12941;
261
262                         if (!char.IsNumber (ch))
263                                 return -1;
264
265                         if (i < 3891)
266                                 return 0.5 + i - 3882;
267                         if (i < 4988)
268                                 return (i - 4977) * 10;
269                         if (i < 8537)
270                                 return 0.2 * (i - 8532);
271                         if (i < 8556)
272                                 return i - 8543;
273                         if (i < 8572)
274                                 return i - 8559;
275                         if (i < 9332)
276                                 return i - 9311;
277                         if (i < 9352)
278                                 return i - 9331;
279                         if (i < 9372)
280                                 return i - 9351;
281                         if (i < 12694)
282                                 return i - 12689;
283                         if (i < 12842)
284                                 return i - 12831;
285                         if (i < 12938)
286                                 return i - 12927;
287
288                         return -1;
289                 }
290
291                 public static double GetNumericValue (string s, int index)
292                 {
293                         if (s == null)
294                                 throw new ArgumentNullException ("s");
295                         return GetNumericValue (s [index]);
296                 }
297
298                 public static UnicodeCategory GetUnicodeCategory (char ch)
299                 {
300                         return Char.GetUnicodeCategory (ch);
301                 }
302
303                 public static UnicodeCategory GetUnicodeCategory (string s, int index)
304                 {
305                         if (s == null)
306                                 throw new ArgumentNullException ("s");
307                         return Char.GetUnicodeCategory (s, index);
308                 }
309         }
310 }