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