Merge pull request #726 from pruiz/xamarin-bug-13708
[mono.git] / mcs / class / corlib / System.Globalization / TextInfo.cs
1 //
2 // System.Globalization.TextInfo.cs
3 //
4 // Authors:
5 //      Dick Porter (dick@ximian.com)
6 //      Duncan Mak (duncan@ximian.com)
7 //      Atsushi Enomoto (atsushi@ximian.com)
8 //      Sebastien Pouliot  <sebastien@ximian.com>
9 //
10 // (C) 2002 Ximian, Inc.
11 // (C) 2005 Novell, Inc.
12 //
13 // TODO:
14 //   Missing the various code page mappings.
15 //   Missing the OnDeserialization implementation.
16 //
17 // Copyright (C) 2004, 2005 Novell, Inc (http://www.novell.com)
18 //
19 // Permission is hereby granted, free of charge, to any person obtaining
20 // a copy of this software and associated documentation files (the
21 // "Software"), to deal in the Software without restriction, including
22 // without limitation the rights to use, copy, modify, merge, publish,
23 // distribute, sublicense, and/or sell copies of the Software, and to
24 // permit persons to whom the Software is furnished to do so, subject to
25 // the following conditions:
26 // 
27 // The above copyright notice and this permission notice shall be
28 // included in all copies or substantial portions of the Software.
29 // 
30 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
34 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
35 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
36 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 //
38
39 using System.Runtime.Serialization;
40 using System.Runtime.InteropServices;
41 using System.Text;
42
43 namespace System.Globalization {
44
45         [Serializable]
46         [ComVisible (true)]
47         [MonoTODO ("IDeserializationCallback isn't implemented.")]
48         public class TextInfo: IDeserializationCallback, ICloneable
49         {
50                 [StructLayout (LayoutKind.Sequential)]
51                 struct Data {
52                         public int ansi;
53                         public int ebcdic;
54                         public int mac;
55                         public int oem;
56                         public bool right_to_left;
57                         public byte list_sep;
58                 }
59
60                 string m_listSeparator;
61                 bool m_isReadOnly;
62                 string customCultureName;
63
64 #pragma warning disable 169
65                 [NonSerialized]
66                 int m_nDataItem;
67                 bool m_useUserOverride;
68 #pragma warning restore 169             
69
70                 int m_win32LangID;
71
72                 [NonSerialized]
73                 readonly CultureInfo ci;
74
75                 [NonSerialized]
76                 readonly bool handleDotI;
77
78                 [NonSerialized]
79                 readonly Data data;
80
81                 internal unsafe TextInfo (CultureInfo ci, int lcid, void* data, bool read_only)
82                 {
83                         this.m_isReadOnly = read_only;
84                         this.m_win32LangID = lcid;
85                         this.ci = ci;
86                         if (data != null)
87                                 this.data = *(Data*) data;
88                         else {
89                                 this.data = new Data ();
90                                 this.data.list_sep = (byte) ',';
91                         }
92
93                         CultureInfo tmp = ci;
94                         while (tmp.Parent != null && tmp.Parent.LCID != 0x7F && tmp.Parent != tmp)
95                                 tmp = tmp.Parent;
96
97                         if (tmp != null) {
98                                 switch (tmp.LCID) {
99                                 case 44: // Azeri (az)
100                                 case 31: // Turkish (tr)
101                                         handleDotI = true;
102                                         break;
103                                 }
104                         }
105                 }
106
107                 private TextInfo (TextInfo textInfo)
108                 {
109                         m_win32LangID = textInfo.m_win32LangID;
110                         m_nDataItem = textInfo.m_nDataItem;
111                         m_useUserOverride = textInfo.m_useUserOverride;
112                         m_listSeparator = textInfo.ListSeparator;
113                         customCultureName = textInfo.CultureName;
114                         ci = textInfo.ci;
115                         handleDotI = textInfo.handleDotI;
116                         data = textInfo.data;
117                 }
118
119                 public virtual int ANSICodePage
120                 {
121                         get {
122                                 return data.ansi;
123                         }
124                 }
125
126                 public virtual int EBCDICCodePage
127                 {
128                         get {
129                                 return data.ebcdic;
130                         }
131                 }
132
133                 [ComVisible (false)]
134                 public int LCID {
135                         get { return m_win32LangID; }
136                 }
137
138                 public virtual string ListSeparator {
139                         get {
140                                 if (m_listSeparator == null)
141                                         m_listSeparator = ((char) data.list_sep).ToString ();
142                                 return m_listSeparator;
143                         }
144                         [ComVisible (false)]
145                         set { m_listSeparator = value; }
146                 }
147
148                 public virtual int MacCodePage
149                 {
150                         get {
151                                 return data.mac;
152                         }
153                 }
154
155                 public virtual int OEMCodePage
156                 {
157                         get {
158                                 return data.oem;
159                         }
160                 }
161
162                 [ComVisible (false)]
163                 public string CultureName {
164                         get {
165                                 if (customCultureName == null)
166                                         customCultureName = ci == null ? String.Empty : ci.Name;
167                                 return customCultureName;
168                         }
169                 }
170
171                 [ComVisible (false)]
172                 public bool IsReadOnly {
173                         get { return m_isReadOnly; }
174                 }
175
176                 [ComVisible (false)]
177                 public bool IsRightToLeft {
178                         get {
179                                 return data.right_to_left;
180                         }
181                 }
182
183                 public override bool Equals (object obj)
184                 {
185                         if (obj == null)
186                                 return false;
187                         TextInfo other = obj as TextInfo;
188                         if (other == null)
189                                 return false;
190                         if (other.m_win32LangID != m_win32LangID)
191                                 return false;
192                         if (other.ci != ci)
193                                 return false;
194                         return true;
195                 }
196
197                 public override int GetHashCode()
198                 {
199                         return (m_win32LangID);
200                 }
201                 
202                 public override string ToString()
203                 {
204                         return "TextInfo - " + m_win32LangID;
205                 }
206
207                 public string ToTitleCase (string str)
208                 {
209                         if(str == null)
210                                 throw new ArgumentNullException ("str");
211
212                         StringBuilder sb = null;
213                         int i = 0;
214                         int start = 0;
215                         while (i < str.Length) {
216                                 if (!Char.IsLetter (str [i++]))
217                                         continue;
218                                 i--;
219                                 char t = ToTitleCase (str [i]);
220                                 bool capitalize = true;
221                                 if (t == str [i]) {
222                                         capitalize = false;
223                                         bool allTitle = true;
224                                         // if the word is all titlecase,
225                                         // then don't capitalize it.
226                                         int saved = i;
227                                         while (++i < str.Length) {
228                                                 if (Char.IsWhiteSpace (str [i]))
229                                                         break;
230                                                 t = ToTitleCase (str [i]);
231                                                 if (t != str [i]) {
232                                                         allTitle = false;
233                                                         break;
234                                                 }
235                                         }
236                                         if (allTitle)
237                                                 continue;
238                                         i = saved;
239
240                                         // still check if all remaining
241                                         // characters are lowercase,
242                                         // where we don't have to modify
243                                         // the source word.
244                                         while (++i < str.Length) {
245                                                 if (Char.IsWhiteSpace (str [i]))
246                                                         break;
247                                                 if (ToLower (str [i]) != str [i]) {
248                                                         capitalize = true;
249                                                         i = saved;
250                                                         break;
251                                                 }
252                                         }
253                                 }
254
255                                 if (capitalize) {
256                                         if (sb == null)
257                                                 sb = new StringBuilder (str.Length);
258                                         sb.Append (str, start, i - start);
259                                         sb.Append (ToTitleCase (str [i]));
260                                         start = i + 1;
261                                         while (++i < str.Length) {
262                                                 if (Char.IsWhiteSpace (str [i]))
263                                                         break;
264                                                 sb.Append (ToLower (str [i]));
265                                         }
266                                         start = i;
267                                 }
268                         }
269                         if (sb != null)
270                                 sb.Append (str, start, str.Length - start);
271
272                         return sb != null ? sb.ToString () : str;
273                 }
274
275                 // Only Azeri and Turkish have their own special cases.
276                 // Other than them, all languages have common special case
277                 // (enumerable enough).
278                 public virtual char ToLower (char c)
279                 {
280                         // quick ASCII range check
281                         if (c < 0x40 || 0x60 < c && c < 128)
282                                 return c;
283                         else if ('A' <= c && c <= 'Z' && (!handleDotI || c != 'I'))
284                                 return (char) (c + 0x20);
285
286                         if (ci == null || ci.LCID == 0x7F)
287                                 return Char.ToLowerInvariant (c);
288
289                         switch (c) {
290                         case '\u0049': // Latin uppercase I
291                                 if (handleDotI)
292                                         return '\u0131'; // I becomes dotless i
293                                 break;
294                         case '\u0130': // I-dotted
295                                 return '\u0069'; // i
296
297                         case '\u01c5': // LATIN CAPITAL LETTER D WITH SMALL LETTER Z WITH CARON
298                                 return '\u01c6';
299                         // \u01c7 -> \u01c9 (LJ) : invariant
300                         case '\u01c8': // LATIN CAPITAL LETTER L WITH SMALL LETTER J
301                                 return '\u01c9';
302                         // \u01ca -> \u01cc (NJ) : invariant
303                         case '\u01cb': // LATIN CAPITAL LETTER N WITH SMALL LETTER J
304                                 return '\u01cc';
305                         // WITH CARON : invariant
306                         // WITH DIAERESIS AND * : invariant
307
308                         case '\u01f2': // LATIN CAPITAL LETTER D WITH SMALL LETTER Z
309                                 return '\u01f3';
310                         case '\u03d2':  // ? it is not in ICU
311                                 return '\u03c5';
312                         case '\u03d3':  // ? it is not in ICU
313                                 return '\u03cd';
314                         case '\u03d4':  // ? it is not in ICU
315                                 return '\u03cb';
316                         }
317                         return Char.ToLowerInvariant (c);
318                 }
319
320                 public virtual char ToUpper (char c)
321                 {
322                         // quick ASCII range check
323                         if (c < 0x60)
324                                 return c;
325                         else if ('a' <= c && c <= 'z' && (!handleDotI || c != 'i'))
326                                 return (char) (c - 0x20);
327
328                         if (ci == null || ci.LCID == 0x7F)
329                                 return Char.ToUpperInvariant (c);
330
331                         switch (c) {
332                         case '\u0069': // Latin lowercase i
333                                 if (handleDotI)
334                                         return '\u0130'; // dotted capital I
335                                 break;
336                         case '\u0131': // dotless i
337                                 return '\u0049'; // I
338
339                         case '\u01c5': // see ToLower()
340                                 return '\u01c4';
341                         case '\u01c8': // see ToLower()
342                                 return '\u01c7';
343                         case '\u01cb': // see ToLower()
344                                 return '\u01ca';
345                         case '\u01f2': // see ToLower()
346                                 return '\u01f1';
347                         case '\u0390': // GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS
348                                 return '\u03aa'; // it is not in ICU
349                         case '\u03b0': // GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS
350                                 return '\u03ab'; // it is not in ICU
351                         case '\u03d0': // GREEK BETA
352                                 return '\u0392';
353                         case '\u03d1': // GREEK THETA
354                                 return '\u0398';
355                         case '\u03d5': // GREEK PHI
356                                 return '\u03a6';
357                         case '\u03d6': // GREEK PI
358                                 return '\u03a0';
359                         case '\u03f0': // GREEK KAPPA
360                                 return '\u039a';
361                         case '\u03f1': // GREEK RHO
362                                 return '\u03a1';
363                         // am not sure why miscellaneous GREEK symbols are 
364                         // not handled here.
365                         }
366
367                         return Char.ToUpperInvariant (c);
368                 }
369
370                 private char ToTitleCase (char c)
371                 {
372                         // Handle some Latin characters.
373                         switch (c) {
374                         case '\u01c4':
375                         case '\u01c5':
376                         case '\u01c6':
377                                 return '\u01c5';
378                         case '\u01c7':
379                         case '\u01c8':
380                         case '\u01c9':
381                                 return '\u01c8';
382                         case '\u01ca':
383                         case '\u01cb':
384                         case '\u01cc':
385                                 return '\u01cb';
386                         case '\u01f1':
387                         case '\u01f2':
388                         case '\u01f3':
389                                 return '\u01f2';
390                         }
391                         if ('\u2170' <= c && c <= '\u217f' || // Roman numbers
392                                 '\u24d0' <= c && c <= '\u24e9')
393                                 return c;
394                         return ToUpper (c);
395                 }
396
397                 public unsafe virtual string ToLower (string str)
398                 {
399                         // In ICU (3.2) there are a few cases that one single
400                         // character results in multiple characters in e.g.
401                         // tr-TR culture. So I tried brute force conversion
402                         // test with single character as a string input, but 
403                         // there was no such conversion. So I think it just
404                         // invokes ToLower(char).
405                         if (str == null)
406                                 throw new ArgumentNullException ("str");
407
408                         if (str.Length == 0)
409                                 return String.Empty;
410
411                         string tmp = String.InternalAllocateStr (str.Length);
412                         fixed (char* source = str, dest = tmp) {
413
414                                 char* destPtr = (char*)dest;
415                                 char* sourcePtr = (char*)source;
416
417                                 for (int n = 0; n < str.Length; n++) {
418                                         *destPtr = ToLower (*sourcePtr);
419                                         sourcePtr++;
420                                         destPtr++;
421                                 }
422                         }
423                         return tmp;
424                 }
425
426                 public unsafe virtual string ToUpper (string str)
427                 {
428                         // In ICU (3.2) there is a case that string
429                         // is handled beyond per-character conversion, but
430                         // it is only lt-LT culture where MS.NET does not
431                         // handle any special transliteration. So I keep
432                         // ToUpper() just as character conversion.
433                         if (str == null)
434                                 throw new ArgumentNullException ("str");
435
436                         if (str.Length == 0)
437                                 return String.Empty;
438
439                         string tmp = String.InternalAllocateStr (str.Length);
440                         fixed (char* source = str, dest = tmp) {
441
442                                 char* destPtr = (char*)dest;
443                                 char* sourcePtr = (char*)source;
444
445                                 for (int n = 0; n < str.Length; n++) {
446                                         *destPtr = ToUpper (*sourcePtr);
447                                         sourcePtr++;
448                                         destPtr++;
449                                 }
450                         }
451                         return tmp;
452                 }
453
454                 [ComVisible (false)]
455                 public static TextInfo ReadOnly (TextInfo textInfo)
456                 {
457                         if (textInfo == null)
458                                 throw new ArgumentNullException ("textInfo");
459
460                         TextInfo ti = new TextInfo (textInfo);
461                         ti.m_isReadOnly = true;
462                         return ti;
463                 }
464
465                 /* IDeserialization interface */
466                 [MonoTODO]
467                 void IDeserializationCallback.OnDeserialization(object sender)
468                 {
469                         // FIXME: we need to re-create "data" in order to get most properties working
470                 }
471
472                 /* IClonable */
473                 [ComVisible (false)]
474                 public virtual object Clone ()
475                 {
476                         return new TextInfo (this);
477                 }
478         }
479 }