919641395b8a57c6a64d2abd09ad4ff90f0102d8
[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                                 if (c >= '0' && c <= '9'){
138                                         byte d = (byte) (c - '0');
139                                                 
140                                         if (val > (MaxValue/10))
141                                                 goto overflow;
142                                         
143                                         if (val == (MaxValue/10)){
144                                                 if ((d > (MaxValue % 10)) && (sign == 1 || (d > ((MaxValue % 10) + 1))))
145                                                         goto overflow;
146                                                 if (sign == -1)
147                                                         val = (short) ((val * sign * 10) - d);
148                                                 else
149                                                         val = (short) ((val * 10) + d);
150
151                                                 if (Int32.ProcessTrailingWhitespace (tryParse, s, i + 1, ref exc)){
152                                                         result = val;
153                                                         return true;
154                                                 }
155                                                 goto overflow;
156                                         } else 
157                                                 val = (short) (val * 10 + d);
158                                         
159                                         
160                                         digits_seen = true;
161                                 } else if (!Int32.ProcessTrailingWhitespace (tryParse, s, i, ref exc))
162                                         return false;
163                                         
164                         }
165                         if (!digits_seen) {
166                                 if (!tryParse)
167                                         exc = Int32.GetFormatException ();
168                                 return false;
169                         }
170                         
171                         if (sign == -1)
172                                 result = (short) (val * sign);
173                         else
174                                 result = val;
175
176                         return true;
177
178                 overflow:
179                         if (!tryParse)
180                                 exc = new OverflowException ("Value is too large");
181                         return false;
182                 }
183
184                 public static short Parse (string s, IFormatProvider fp)
185                 {
186                         return Parse (s, NumberStyles.Integer, fp);
187                 }
188
189                 public static short Parse (string s, NumberStyles style)
190                 {
191                         return Parse (s, style, null);
192                 }
193
194                 public static short Parse (string s, NumberStyles style, IFormatProvider fp)
195                 {
196                         int tmpResult = Int32.Parse (s, style, fp);
197                         if (tmpResult > Int16.MaxValue || tmpResult < Int16.MinValue)
198                                 throw new OverflowException ("Value too large or too small.");
199
200                         return (short) tmpResult;
201                 }
202
203                 public static short Parse (string s) 
204                 {
205                         Exception exc;
206                         short res;
207
208                         if (!Parse (s, false, out res, out exc))
209                                 throw exc;
210
211                         return res;
212                 }
213
214 #if NET_2_0
215                 public static bool TryParse (string s, out short result) 
216                 {
217                         Exception exc;
218                         if (!Parse (s, true, out result, out exc)) {
219                                 result = 0;
220                                 return false;
221                         }
222
223                         return true;
224                 }
225
226                 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out short result) 
227                 {
228                         int tmpResult;
229                         result = 0;
230                                 
231                         if (!Int32.TryParse (s, style, provider, out tmpResult))
232                                 return false;
233                         
234                         if (tmpResult > Int16.MaxValue || tmpResult < Int16.MinValue)
235                                 return false;
236                                 
237                         result = (short)tmpResult;
238                         return true;
239                 }
240 #endif
241
242                 public override string ToString ()
243                 {
244                         return new NumberFormatter(null, m_value).FormatDecimal(-1, null);
245                 }
246
247                 public string ToString (IFormatProvider provider)
248                 {
249                         NumberFormatInfo nfi = NumberFormatInfo.GetInstance (provider);
250                         return new NumberFormatter(null, m_value).FormatDecimal(-1, nfi);
251                 }
252
253                 public string ToString (string format)
254                 {
255                         return ToString (format, null);
256                 }
257
258                 public string ToString (string format, IFormatProvider fp)
259                 {
260                         NumberFormatInfo nfi = NumberFormatInfo.GetInstance( fp );
261                         return NumberFormatter.NumberToString(format, m_value, nfi);
262                 }
263
264                 // =========== IConvertible Methods =========== //
265
266                 public TypeCode GetTypeCode ()
267                 {
268                         return TypeCode.Int16;
269                 }
270
271                 bool IConvertible.ToBoolean (IFormatProvider provider)
272                 {
273                         return System.Convert.ToBoolean (m_value);
274                 }
275
276                 byte IConvertible.ToByte (IFormatProvider provider)
277                 {
278                         return System.Convert.ToByte (m_value);
279                 }
280
281                 char IConvertible.ToChar (IFormatProvider provider)
282                 {
283                         return System.Convert.ToChar (m_value);
284                 }
285
286                 DateTime IConvertible.ToDateTime (IFormatProvider provider)
287                 {
288                         return System.Convert.ToDateTime (m_value);
289                 }
290
291                 decimal IConvertible.ToDecimal (IFormatProvider provider)
292                 {
293                         return System.Convert.ToDecimal (m_value);
294                 }
295
296                 double IConvertible.ToDouble (IFormatProvider provider)
297                 {
298                         return System.Convert.ToDouble (m_value);
299                 }
300
301                 short IConvertible.ToInt16 (IFormatProvider provider)
302                 {
303                         return System.Convert.ToInt16 (m_value);
304                 }
305
306                 int IConvertible.ToInt32 (IFormatProvider provider)
307                 {
308                         return System.Convert.ToInt32 (m_value);
309                 }
310
311                 long IConvertible.ToInt64 (IFormatProvider provider)
312                 {
313                         return System.Convert.ToInt64 (m_value);
314                 }
315
316                 sbyte IConvertible.ToSByte (IFormatProvider provider)
317                 {
318                         return System.Convert.ToSByte (m_value);
319                 }
320                 
321                 float IConvertible.ToSingle (IFormatProvider provider)
322                 {
323                         return System.Convert.ToSingle (m_value);
324                 }
325
326                 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
327                 {
328                         return System.Convert.ToType (m_value, conversionType, provider);
329                 }
330
331                 ushort IConvertible.ToUInt16 (IFormatProvider provider)
332                 {
333                         return System.Convert.ToUInt16 (m_value);
334                 }
335
336                 uint IConvertible.ToUInt32 (IFormatProvider provider)
337                 {
338                         return System.Convert.ToUInt32 (m_value);
339                 }
340
341                 ulong IConvertible.ToUInt64 (IFormatProvider provider)
342                 {
343                         return System.Convert.ToUInt64 (m_value);
344                 }
345         }
346 }