* CultureInfo.cs: When creating the invariant culture's TextInfo
[mono.git] / mcs / class / corlib / System.Globalization / TextInfo.cs
1 //
2 // System.Globalization.TextInfo.cs
3 //
4 // Author:
5 //      Dick Porter (dick@ximian.com)
6 //      Duncan Mak (duncan@ximian.com)
7 //
8 // (C) 2002 Ximian, Inc.
9 //
10 // TODO:
11 //   Missing the various code page mappings.
12 //   Missing the OnDeserialization implementation.
13 //
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 //
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
23 // 
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 // 
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 //
35 using System;
36 using System.Globalization;
37 using System.Runtime.Serialization;
38
39 namespace System.Globalization {
40
41         [Serializable]
42         public class TextInfo: IDeserializationCallback
43         {
44                 int m_win32LangID;
45                 int m_nDataItem;
46                 bool m_useUserOverride;
47
48                 [NonSerialized]
49                 CultureInfo ci;
50
51                 internal TextInfo (CultureInfo ci, int lcid)
52                 {
53                         this.m_win32LangID = lcid;
54                         this.ci = ci;
55                 }
56
57                 [MonoTODO]
58                 public virtual int ANSICodePage
59                 {
60                         get {
61                                 return 0;
62                         }
63                 }
64
65                 [MonoTODO]
66                 public virtual int EBCDICCodePage
67                 {
68                         get {
69                                 return 0;
70                         }
71                 }
72                 
73                 [MonoTODO]
74                 public virtual string ListSeparator 
75                 {
76                         get {
77                                 return ",";
78                         }
79                 }
80
81                 [MonoTODO]
82                 public virtual int MacCodePage
83                 {
84                         get {
85                                 return 0;
86                         }
87                 }
88                 
89                 [MonoTODO]
90                 public virtual int OEMCodePage
91                 {
92                         get {
93                                 return 0;
94                         }
95                 }
96
97                 public override bool Equals (object obj)
98                 {
99                         if (obj == null)
100                                 return false;
101                         TextInfo other = obj as TextInfo;
102                         if (other == null)
103                                 return false;
104                         if (other.m_win32LangID != m_win32LangID)
105                                 return false;
106                         if (other.ci != ci)
107                                 return false;
108                         return true;
109                 }
110
111                 public override int GetHashCode()
112                 {
113                         return (m_win32LangID);
114                 }
115
116                 public virtual char ToLower(char c)
117                 {
118                         return Char.ToLower (c);
119                 }
120                 
121                 public virtual string ToLower(string str)
122                 {
123                         if(str==null) 
124                                 throw new ArgumentNullException("string is null");
125
126                         return str.ToLower (ci);
127                 }
128                 
129                 public override string ToString()
130                 {
131                         return "TextInfo - " + m_win32LangID;
132                 }
133
134                 public string ToTitleCase (string str)
135                 {
136                         if(str == null)
137                                 throw new ArgumentNullException("string is null");
138                         
139                         Text.StringBuilder s = new Text.StringBuilder ();
140                         bool space_seen = true;
141                                 
142                         for (int i = 0; i < str.Length; i ++){
143                                 char c = str [i];
144                                 if (Char.IsLetter (c)){
145                                         if (space_seen)
146                                                 s.Append (Char.ToUpper (c, ci));
147                                         else
148                                                 s.Append (Char.ToLower (c, ci));
149                                         space_seen = false;
150                                 } else {
151                                         s.Append (c);
152                                         if (Char.IsWhiteSpace (c))
153                                                 space_seen = true;
154                                 }
155                         }
156
157                         return s.ToString ();
158                 }
159
160                 public virtual char ToUpper (char c)
161                 {
162                         return Char.ToUpper (c, ci);
163                 }
164
165                 public virtual string ToUpper (string str)
166                 {
167                         if(str==null)
168                                 throw new ArgumentNullException("string is null");
169                         
170                         return str.ToUpper (ci);
171                 }
172
173                 /* IDeserialization interface */
174                 [MonoTODO]
175                 void IDeserializationCallback.OnDeserialization(object sender)
176                 {
177                 }
178         }
179 }