2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[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         public struct Int16 : IFormattable, IConvertible,
36 #if NET_2_0
37                 IComparable, IComparable<Int16>
38 #else
39                 IComparable
40 #endif
41         {
42
43                 public const short MaxValue =  32767;
44                 public const short MinValue = -32768;
45                 
46                 internal short m_value;
47
48                 public int CompareTo (object v)
49                 {
50                         if (v == null)
51                                 return 1;
52
53                         if (!(v is System.Int16))
54                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Int16"));
55
56                         short xv = (short) v;
57                         if (m_value == xv)
58                                 return 0;
59                         if (m_value > xv)
60                                 return 1;
61                         else
62                                 return -1;
63                 }
64
65                 public override bool Equals (object o)
66                 {
67                         if (!(o is System.Int16))
68                                 return false;
69
70                         return ((short) o) == m_value;
71                 }
72
73                 public override int GetHashCode ()
74                 {
75                         return m_value;
76                 }
77
78 #if NET_2_0
79                 public int CompareTo (short value)
80                 {
81                         if (m_value == value)
82                                 return 0;
83                         if (m_value > value)
84                                 return 1;
85                         else
86                                 return -1;
87                 }
88
89                 public bool Equals (short value)
90                 {
91                         return value == m_value;
92                 }
93 #endif
94
95                 internal static bool Parse (string s, bool tryParse, out short result)
96                 {
97                         short val = 0;
98                         int len;
99                         int i, sign = 1;
100                         bool digits_seen = false;
101
102                         result = 0;
103
104                         if (s == null)
105                                 if (tryParse)
106                                         return false;
107                                 else
108                                         throw new ArgumentNullException ("s");
109
110                         len = s.Length;
111
112                         char c;
113                         for (i = 0; i < len; i++){
114                                 c = s [i];
115                                 if (!Char.IsWhiteSpace (c))
116                                         break;
117                         }
118                         
119                         if (i == len)
120                                 if (tryParse)
121                                         return false;
122                                 else
123                                         throw new FormatException ();
124
125                         c = s [i];
126                         if (c == '+')
127                                 i++;
128                         else if (c == '-'){
129                                 sign = -1;
130                                 i++;
131                         }
132                         
133                         for (; i < len; i++){
134                                 c = s [i];
135
136                                 if (c >= '0' && c <= '9'){
137                                         val = checked ((short) (val * 10 + (c - '0') * sign));
138                                         digits_seen = true;
139                                 } else {
140                                         if (Char.IsWhiteSpace (c)){
141                                                 for (i++; i < len; i++){
142                                                         if (!Char.IsWhiteSpace (s [i]))
143                                                                 if (tryParse)
144                                                                         return false;
145                                                                 else
146                                                                         throw new FormatException ();
147                                                 }
148                                                 break;
149                                         } else
150                                                 if (tryParse)
151                                                         return false;
152                                                 else
153                                                         throw new FormatException ();
154                                 }
155                         }
156                         if (!digits_seen)
157                                 if (tryParse)
158                                         return false;
159                                 else
160                                         throw new FormatException ();
161                         
162                         result = val;
163                         return true;
164                 }
165
166                 public static short Parse (string s, IFormatProvider fp)
167                 {
168                         return Parse (s, NumberStyles.Integer, fp);
169                 }
170
171                 public static short Parse (string s, NumberStyles style)
172                 {
173                         return Parse (s, style, null);
174                 }
175
176                 public static short Parse (string s, NumberStyles style, IFormatProvider fp)
177                 {
178                         int tmpResult = Int32.Parse (s, style, fp);
179                         if (tmpResult > Int16.MaxValue || tmpResult < Int16.MinValue)
180                                 throw new OverflowException ("Value too large or too small.");
181
182                         return (short) tmpResult;
183                 }
184
185                 public static short Parse (string s) {
186                         short res;
187
188                         Parse (s, false, out res);
189
190                         return res;
191                 }
192
193 #if NET_2_0
194                 public static bool TryParse (string s, out short result) {
195                         try {
196                                 return Parse (s, true, out result);
197                         }
198                         catch (Exception) {
199                                 result = 0;
200                                 return false;
201                         }
202                 }
203
204                 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out short result) {
205                         try {
206                                 int tmpResult;
207
208                                 result = 0;
209                                 if (!Int32.TryParse (s, style, provider, out tmpResult))
210                                         return false;
211                                 if (tmpResult > Int16.MaxValue || tmpResult < Int16.MinValue)
212                                         return false;
213                                 result = (short)tmpResult;
214                                 return true;
215                         }
216                         catch (Exception) {
217                                 result = 0;
218                                 return false;
219                         }
220                 }
221 #endif
222
223                 public override string ToString ()
224                 {
225                         return ToString (null, null);
226                 }
227
228                 public string ToString (IFormatProvider fp)
229                 {
230                         return ToString (null, fp);
231                 }
232
233                 public string ToString (string format)
234                 {
235                         return ToString (format, null);
236                 }
237
238                 public string ToString (string format, IFormatProvider fp)
239                 {
240                         NumberFormatInfo nfi = NumberFormatInfo.GetInstance( fp );
241
242                         // use "G" when format is null or String.Empty
243                         if ((format == null) || (format.Length == 0))
244                                 format = "G";
245                         
246                         return IntegerFormatter.NumberToString(format, nfi, m_value);
247                 }
248
249                 // =========== IConvertible Methods =========== //
250
251                 public TypeCode GetTypeCode ()
252                 {
253                         return TypeCode.Int16;
254                 }
255
256                 bool IConvertible.ToBoolean (IFormatProvider provider)
257                 {
258                         return System.Convert.ToBoolean (m_value);
259                 }
260
261                 byte IConvertible.ToByte (IFormatProvider provider)
262                 {
263                         return System.Convert.ToByte (m_value);
264                 }
265
266                 char IConvertible.ToChar (IFormatProvider provider)
267                 {
268                         return System.Convert.ToChar (m_value);
269                 }
270
271                 DateTime IConvertible.ToDateTime (IFormatProvider provider)
272                 {
273                         return System.Convert.ToDateTime (m_value);
274                 }
275
276                 decimal IConvertible.ToDecimal (IFormatProvider provider)
277                 {
278                         return System.Convert.ToDecimal (m_value);
279                 }
280
281                 double IConvertible.ToDouble (IFormatProvider provider)
282                 {
283                         return System.Convert.ToDouble (m_value);
284                 }
285
286                 short IConvertible.ToInt16 (IFormatProvider provider)
287                 {
288                         return System.Convert.ToInt16 (m_value);
289                 }
290
291                 int IConvertible.ToInt32 (IFormatProvider provider)
292                 {
293                         return System.Convert.ToInt32 (m_value);
294                 }
295
296                 long IConvertible.ToInt64 (IFormatProvider provider)
297                 {
298                         return System.Convert.ToInt64 (m_value);
299                 }
300
301                 sbyte IConvertible.ToSByte (IFormatProvider provider)
302                 {
303                         return System.Convert.ToSByte (m_value);
304                 }
305                 
306                 float IConvertible.ToSingle (IFormatProvider provider)
307                 {
308                         return System.Convert.ToSingle (m_value);
309                 }
310
311                 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
312                 {
313                         return System.Convert.ToType (m_value, conversionType, provider);
314                 }
315
316                 ushort IConvertible.ToUInt16 (IFormatProvider provider)
317                 {
318                         return System.Convert.ToUInt16 (m_value);
319                 }
320
321                 uint IConvertible.ToUInt32 (IFormatProvider provider)
322                 {
323                         return System.Convert.ToUInt32 (m_value);
324                 }
325
326                 ulong IConvertible.ToUInt64 (IFormatProvider provider)
327                 {
328                         return System.Convert.ToUInt64 (m_value);
329                 }
330         }
331 }