2002-01-14 Miguel de Icaza <miguel@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 //
9
10 using System.Globalization;
11
12 namespace System {
13         
14         public struct Int16 : IComparable, IFormattable { //, IConvertible {
15                 private static Type Type = typeof (short);
16
17                 public const short MaxValue =  32767;
18                 public const short MinValue = -32768;
19                 
20                 // VES needs to know about value.  public is workaround
21                 // so source will compile
22                 public short value;
23
24                 public int CompareTo (object v)
25                 {
26                         if (v == null)
27                                 return 1;
28
29                         if (!(v is System.Int16))
30                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Int16"));
31
32                         short xv = (short) v;
33                         if (value == xv)
34                                 return 0;
35                         if (value > xv)
36                                 return 1;
37                         else
38                                 return -1;
39                 }
40
41                 public override bool Equals (object o)
42                 {
43                         if (!(o is System.Int16))
44                                 return false;
45
46                         return ((short) o) == value;
47                 }
48
49                 public override int GetHashCode ()
50                 {
51                         return value;
52                 }
53
54                 public static short Parse (string s)
55                 {
56                         short val = 0;
57                         int len;
58                         int i;
59                         bool neg = false;
60                         bool digits_seen = false;
61                         
62                         if (s == null)
63                                 throw new ArgumentNullException (Locale.GetText ("s is null"));
64
65                         len = s.Length;
66
67                         char c;
68                         for (i = 0; i < len; i++){
69                                 c = s [i];
70                                 if (!Char.IsWhiteSpace (c))
71                                         break;
72                         }
73                         
74                         if (i == len)
75                                 throw new FormatException ();
76
77                         c = s [i];
78                         if (c == '+')
79                                 i++;
80                         else if (c == '-'){
81                                 neg = true;
82                                 i++;
83                         }
84                         
85                         for (; i < len; i++){
86                                 c = s [i];
87
88                                 if (c >= '0' && c <= '9'){
89                                         val = checked ((short) (val * 10 + (c - '0')));
90                                         digits_seen = true;
91                                 } else {
92                                         if (Char.IsWhiteSpace (c)){
93                                                 for (i++; i < len; i++){
94                                                         if (!Char.IsWhiteSpace (s [i]))
95                                                                 throw new FormatException ();
96                                                 }
97                                                 break;
98                                         } else
99                                                 throw new FormatException ();
100                                 }
101                         }
102                         if (!digits_seen)
103                                 throw new FormatException ();
104                         
105                         if (neg)
106                                 val = checked ((short) -val);
107
108                         return val;
109                 }
110
111                 public static short Parse (string s, IFormatProvider fp)
112                 {
113                         return Parse (s, NumberStyles.Integer, fp);
114                 }
115
116                 public static short Parse (string s, NumberStyles style)
117                 {
118                         return Parse (s, style, null);
119                 }
120
121                 public static short Parse (string s, NumberStyles style, IFormatProvider fp)
122                 {
123                         throw new NotImplementedException ();
124                 }
125
126                 public override string ToString ()
127                 {
128                         return ToString ("G", null);
129                 }
130
131                 public string ToString (IFormatProvider fp)
132                 {
133                         return ToString ("G", fp);
134                 }
135
136                 public string ToString (string format)
137                 {
138                         return ToString (format, null);
139                 }
140
141                 public string ToString (string format, IFormatProvider fp)
142                 {
143                         string fmt;
144                         NumberFormatInfo nfi;
145                         
146                         fmt = (format == null) ? "G" : format;
147                         
148                         if (fp == null)
149                                 nfi = NumberFormatInfo.CurrentInfo;
150                         else {
151                                 nfi = (NumberFormatInfo) fp.GetFormat (Type);
152                                 
153                                 if (nfi == null)
154                                         nfi = NumberFormatInfo.CurrentInfo;
155                         }
156
157                         return IntegerFormatter.NumberToString (fmt, nfi, value);
158                 }
159
160                 // =========== IConvertible Methods =========== //
161
162                 public TypeCode GetTypeCode ()
163                 {
164                         return TypeCode.Int16;
165                 }
166         }
167 }