2007-05-23 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / corlib / System / Int16.cs
1 //
2 // System.Int16.cs
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 // Copyright (C) 2004 Novell (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Globalization;
31
32 namespace System {
33         
34         [Serializable]
35 #if NET_2_0
36         [System.Runtime.InteropServices.ComVisible (true)]
37 #endif
38         public struct Int16 : IFormattable, IConvertible, IComparable
39 #if NET_2_0
40                 , IComparable<Int16>, IEquatable <Int16>
41 #endif
42         {
43
44                 public const short MaxValue =  32767;
45                 public const short MinValue = -32768;
46                 
47                 internal short m_value;
48
49                 public int CompareTo (object v)
50                 {
51                         if (v == null)
52                                 return 1;
53
54                         if (!(v is System.Int16))
55                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Int16"));
56
57                         short xv = (short) v;
58                         if (m_value == xv)
59                                 return 0;
60                         if (m_value > xv)
61                                 return 1;
62                         else
63                                 return -1;
64                 }
65
66                 public override bool Equals (object o)
67                 {
68                         if (!(o is System.Int16))
69                                 return false;
70
71                         return ((short) o) == m_value;
72                 }
73
74                 public override int GetHashCode ()
75                 {
76                         return m_value;
77                 }
78
79 #if NET_2_0
80                 public int CompareTo (short value)
81                 {
82                         if (m_value == value)
83                                 return 0;
84                         if (m_value > value)
85                                 return 1;
86                         else
87                                 return -1;
88                 }
89
90                 public bool Equals (short value)
91                 {
92                         return value == m_value;
93                 }
94 #endif
95
96                 internal static bool Parse (string s, bool tryParse, out short result, out Exception exc)
97                 {
98                         short val = 0;
99                         int len;
100                         int i, sign = 1;
101                         bool digits_seen = false;
102
103                         result = 0;
104                         exc = null;
105
106                         if (s == null) {
107                                 if (!tryParse)
108                                         exc = new ArgumentNullException ("s");
109                                 return false;
110                         }
111
112                         len = s.Length;
113
114                         char c;
115                         for (i = 0; i < len; i++){
116                                 c = s [i];
117                                 if (!Char.IsWhiteSpace (c))
118                                         break;
119                         }
120                         
121                         if (i == len) {
122                                 if (!tryParse)
123                                         exc = Int32.GetFormatException ();
124                                 return false;
125                         }
126
127                         c = s [i];
128                         if (c == '+')
129                                 i++;
130                         else if (c == '-'){
131                                 sign = -1;
132                                 i++;
133                         }
134                         
135                         for (; i < len; i++){
136                                 c = s [i];
137
138                                 if (c >= '0' && c <= '9'){
139                                         val = checked ((short) (val * 10 + (c - '0') * sign));
140                                         digits_seen = true;
141                                 } else {
142                                         if (Char.IsWhiteSpace (c)){
143                                                 for (i++; i < len; i++){
144                                                         if (!Char.IsWhiteSpace (s [i])) {
145                                                                 if (!tryParse)
146                                                                         exc = Int32.GetFormatException ();
147                                                                 return false;
148                                                         }
149                                                 }
150                                                 break;
151                                         } else {
152                                                 if (!tryParse)
153                                                         exc = Int32.GetFormatException ();
154                                                 return false;
155                                         }
156                                 }
157                         }
158                         if (!digits_seen) {
159                                 if (!tryParse)
160                                         exc = Int32.GetFormatException ();
161                                 return false;
162                         }
163                         
164                         result = val;
165                         return true;
166                 }
167
168                 public static short Parse (string s, IFormatProvider fp)
169                 {
170                         return Parse (s, NumberStyles.Integer, fp);
171                 }
172
173                 public static short Parse (string s, NumberStyles style)
174                 {
175                         return Parse (s, style, null);
176                 }
177
178                 public static short Parse (string s, NumberStyles style, IFormatProvider fp)
179                 {
180                         int tmpResult = Int32.Parse (s, style, fp);
181                         if (tmpResult > Int16.MaxValue || tmpResult < Int16.MinValue)
182                                 throw new OverflowException ("Value too large or too small.");
183
184                         return (short) tmpResult;
185                 }
186
187                 public static short Parse (string s) 
188                 {
189                         Exception exc;
190                         short res;
191
192                         if (!Parse (s, false, out res, out exc))
193                                 throw exc;
194
195                         return res;
196                 }
197
198 #if NET_2_0
199                 public static bool TryParse (string s, out short result) 
200                 {
201                         Exception exc;
202                         if (!Parse (s, true, out result, out exc)) {
203                                 result = 0;
204                                 return false;
205                         }
206
207                         return true;
208                 }
209
210                 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out short result) 
211                 {
212                         int tmpResult;
213                         result = 0;
214                                 
215                         if (!Int32.TryParse (s, style, provider, out tmpResult))
216                                 return false;
217                         
218                         if (tmpResult > Int16.MaxValue || tmpResult < Int16.MinValue)
219                                 return false;
220                                 
221                         result = (short)tmpResult;
222                         return true;
223                 }
224 #endif
225
226                 public override string ToString ()
227                 {
228                         return NumberFormatter.FormatGeneral (new NumberFormatter.NumberStore (m_value));
229                 }
230
231                 public string ToString (IFormatProvider fp)
232                 {
233                         return NumberFormatter.FormatGeneral (new NumberFormatter.NumberStore (m_value), fp);
234                 }
235
236                 public string ToString (string format)
237                 {
238                         return ToString (format, null);
239                 }
240
241                 public string ToString (string format, IFormatProvider fp)
242                 {
243                         NumberFormatInfo nfi = NumberFormatInfo.GetInstance( fp );
244                         return NumberFormatter.NumberToString(format, m_value, nfi);
245                 }
246
247                 // =========== IConvertible Methods =========== //
248
249                 public TypeCode GetTypeCode ()
250                 {
251                         return TypeCode.Int16;
252                 }
253
254                 bool IConvertible.ToBoolean (IFormatProvider provider)
255                 {
256                         return System.Convert.ToBoolean (m_value);
257                 }
258
259                 byte IConvertible.ToByte (IFormatProvider provider)
260                 {
261                         return System.Convert.ToByte (m_value);
262                 }
263
264                 char IConvertible.ToChar (IFormatProvider provider)
265                 {
266                         return System.Convert.ToChar (m_value);
267                 }
268
269                 DateTime IConvertible.ToDateTime (IFormatProvider provider)
270                 {
271                         return System.Convert.ToDateTime (m_value);
272                 }
273
274                 decimal IConvertible.ToDecimal (IFormatProvider provider)
275                 {
276                         return System.Convert.ToDecimal (m_value);
277                 }
278
279                 double IConvertible.ToDouble (IFormatProvider provider)
280                 {
281                         return System.Convert.ToDouble (m_value);
282                 }
283
284                 short IConvertible.ToInt16 (IFormatProvider provider)
285                 {
286                         return System.Convert.ToInt16 (m_value);
287                 }
288
289                 int IConvertible.ToInt32 (IFormatProvider provider)
290                 {
291                         return System.Convert.ToInt32 (m_value);
292                 }
293
294                 long IConvertible.ToInt64 (IFormatProvider provider)
295                 {
296                         return System.Convert.ToInt64 (m_value);
297                 }
298
299                 sbyte IConvertible.ToSByte (IFormatProvider provider)
300                 {
301                         return System.Convert.ToSByte (m_value);
302                 }
303                 
304                 float IConvertible.ToSingle (IFormatProvider provider)
305                 {
306                         return System.Convert.ToSingle (m_value);
307                 }
308
309                 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
310                 {
311                         return System.Convert.ToType (m_value, conversionType, provider);
312                 }
313
314                 ushort IConvertible.ToUInt16 (IFormatProvider provider)
315                 {
316                         return System.Convert.ToUInt16 (m_value);
317                 }
318
319                 uint IConvertible.ToUInt32 (IFormatProvider provider)
320                 {
321                         return System.Convert.ToUInt32 (m_value);
322                 }
323
324                 ulong IConvertible.ToUInt64 (IFormatProvider provider)
325                 {
326                         return System.Convert.ToUInt64 (m_value);
327                 }
328         }
329 }