5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
10 using System.Globalization;
11 using System.Threading;
16 public struct Int32 : IComparable, IFormattable, IConvertible {
18 public const int MaxValue = 0x7fffffff;
19 public const int MinValue = -2147483648;
23 public int CompareTo (object v)
28 if (!(v is System.Int32))
29 throw new ArgumentException (Locale.GetText ("Value is not a System.Int32"));
40 public override bool Equals (object o)
42 if (!(o is System.Int32))
45 return ((int) o) == value;
48 public override int GetHashCode ()
53 public static int Parse (string s)
58 bool digits_seen = false;
61 throw new ArgumentNullException (Locale.GetText ("s is null"));
66 for (i = 0; i < len; i++){
68 if (!Char.IsWhiteSpace (c))
73 throw new FormatException ();
86 if (c >= '0' && c <= '9'){
87 val = checked (val * 10 + (c - '0') * sign);
90 if (Char.IsWhiteSpace (c)){
91 for (i++; i < len; i++){
92 if (!Char.IsWhiteSpace (s [i]))
93 throw new FormatException ();
97 throw new FormatException ();
101 throw new FormatException ();
106 public static int Parse (string s, IFormatProvider fp)
108 return Parse (s, NumberStyles.Integer, fp);
111 public static int Parse (string s, NumberStyles style)
113 return Parse (s, style, null);
116 internal static void CheckStyle (NumberStyles style)
118 if ((style & NumberStyles.AllowHexSpecifier) != 0) {
119 NumberStyles ne = style ^ NumberStyles.AllowHexSpecifier;
120 if ((ne & NumberStyles.AllowLeadingWhite) != 0)
121 ne ^= NumberStyles.AllowLeadingWhite;
122 if ((ne & NumberStyles.AllowTrailingWhite) != 0)
123 ne ^= NumberStyles.AllowTrailingWhite;
125 throw new ArgumentException (
126 "With AllowHexSpecifier only " +
127 "AllowLeadingWhite and AllowTrailingWhite " +
132 internal static int JumpOverWhite (int pos, string s, bool excp)
134 while (pos < s.Length && Char.IsWhiteSpace (s [pos]))
137 if (excp && pos >= s.Length)
138 throw new FormatException ("Input string was not in the correct format.");
143 internal static void FindSign (ref int pos, string s, NumberFormatInfo nfi,
144 ref bool foundSign, ref bool negative)
146 if ((pos + nfi.NegativeSign.Length) <= s.Length &&
147 s.Substring (pos, nfi.NegativeSign.Length) == nfi.NegativeSign) {
150 pos += nfi.NegativeSign.Length;
152 else if ((pos + nfi.PositiveSign.Length) < s.Length &&
153 s.Substring (pos, nfi.PositiveSign.Length) == nfi.PositiveSign) {
155 pos += nfi.PositiveSign.Length;
160 internal static void FindCurrency (ref int pos,
162 NumberFormatInfo nfi,
163 ref bool foundCurrency)
165 if ((pos + nfi.CurrencySymbol.Length) <= s.Length &&
166 s.Substring (pos, nfi.CurrencySymbol.Length) == nfi.CurrencySymbol) {
167 foundCurrency = true;
168 pos += nfi.CurrencySymbol.Length;
172 internal static bool FindOther (ref int pos,
176 if ((pos + other.Length) <= s.Length &&
177 s.Substring (pos, other.Length) == other) {
185 internal static bool ValidDigit (char e, bool allowHex)
188 return Char.IsDigit (e) || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f');
190 return Char.IsDigit (e);
193 public static int Parse (string s, NumberStyles style, IFormatProvider fp)
196 throw new ArgumentNullException ();
199 throw new FormatException ("Input string was not " +
200 "in the correct format.");
202 NumberFormatInfo nfi;
204 Type typeNFI = typeof (System.Globalization.NumberFormatInfo);
205 nfi = (NumberFormatInfo) fp.GetFormat (typeNFI);
208 nfi = Thread.CurrentThread.CurrentCulture.NumberFormat;
212 bool AllowCurrencySymbol = (style & NumberStyles.AllowCurrencySymbol) != 0;
213 bool AllowExponent = (style & NumberStyles.AllowExponent) != 0;
214 bool AllowHexSpecifier = (style & NumberStyles.AllowHexSpecifier) != 0;
215 bool AllowThousands = (style & NumberStyles.AllowThousands) != 0;
216 bool AllowDecimalPoint = (style & NumberStyles.AllowDecimalPoint) != 0;
217 bool AllowParentheses = (style & NumberStyles.AllowParentheses) != 0;
218 bool AllowTrailingSign = (style & NumberStyles.AllowTrailingSign) != 0;
219 bool AllowLeadingSign = (style & NumberStyles.AllowLeadingSign) != 0;
220 bool AllowTrailingWhite = (style & NumberStyles.AllowTrailingWhite) != 0;
221 bool AllowLeadingWhite = (style & NumberStyles.AllowLeadingWhite) != 0;
225 if (AllowLeadingWhite)
226 pos = JumpOverWhite (pos, s, true);
228 bool foundOpenParentheses = false;
229 bool negative = false;
230 bool foundSign = false;
231 bool foundCurrency = false;
234 if (AllowParentheses && s [pos] == '(') {
235 foundOpenParentheses = true;
237 negative = true; // MS always make the number negative when there parentheses
238 // even when NumberFormatInfo.NumberNegativePattern != 0!!!
240 if (AllowLeadingWhite)
241 pos = JumpOverWhite (pos, s, true);
243 if (s.Substring (pos, nfi.NegativeSign.Length) == nfi.NegativeSign)
244 throw new FormatException ("Input string was not in the correct " +
246 if (s.Substring (pos, nfi.PositiveSign.Length) == nfi.PositiveSign)
247 throw new FormatException ("Input string was not in the correct " +
251 if (AllowLeadingSign && !foundSign) {
253 FindSign (ref pos, s, nfi, ref foundSign, ref negative);
255 if (AllowLeadingWhite)
256 pos = JumpOverWhite (pos, s, true);
257 if (AllowCurrencySymbol) {
258 FindCurrency (ref pos, s, nfi,
260 if (foundCurrency && AllowLeadingWhite)
261 pos = JumpOverWhite (pos, s, true);
266 if (AllowCurrencySymbol && !foundCurrency) {
268 FindCurrency (ref pos, s, nfi, ref foundCurrency);
270 if (AllowLeadingWhite)
271 pos = JumpOverWhite (pos, s, true);
273 if (!foundSign && AllowLeadingSign) {
274 FindSign (ref pos, s, nfi, ref foundSign,
276 if (foundSign && AllowLeadingWhite)
277 pos = JumpOverWhite (pos, s, true);
285 bool decimalPointFound = false;
292 if (!ValidDigit (s [pos], AllowHexSpecifier)) {
293 if (AllowThousands &&
294 FindOther (ref pos, s, nfi.NumberGroupSeparator))
297 if (!decimalPointFound && AllowDecimalPoint &&
298 FindOther (ref pos, s, nfi.NumberDecimalSeparator)) {
299 decimalPointFound = true;
305 else if (AllowHexSpecifier) {
307 hexDigit = s [pos++];
308 if (Char.IsDigit (hexDigit))
309 digitValue = (int) (hexDigit - '0');
310 else if (Char.IsLower (hexDigit))
311 digitValue = (int) (hexDigit - 'a' + 10);
313 digitValue = (int) (hexDigit - 'A' + 10);
315 number = checked (number * 16 - digitValue);
317 else if (decimalPointFound) {
319 // Allows decimal point as long as it's only
320 // followed by zeroes.
321 if (s [pos++] != '0')
322 throw new OverflowException ("Value too large or too " +
329 // Calculations done as negative
330 // (abs (MinValue) > abs (MaxValue))
333 (int) (s [pos++] - '0')
335 } catch (OverflowException) {
336 throw new OverflowException ("Value too large or too " +
340 } while (pos < s.Length);
344 throw new FormatException ("Input string was not in the correct format.");
346 if (AllowTrailingSign && !foundSign) {
348 FindSign (ref pos, s, nfi, ref foundSign, ref negative);
350 if (AllowTrailingWhite)
351 pos = JumpOverWhite (pos, s, true);
352 if (AllowCurrencySymbol)
353 FindCurrency (ref pos, s, nfi,
358 if (AllowCurrencySymbol && !foundCurrency) {
360 FindCurrency (ref pos, s, nfi, ref foundCurrency);
362 if (AllowTrailingWhite)
363 pos = JumpOverWhite (pos, s, true);
364 if (!foundSign && AllowTrailingSign)
365 FindSign (ref pos, s, nfi, ref foundSign,
370 if (AllowTrailingWhite && pos < s.Length)
371 pos = JumpOverWhite (pos, s, false);
373 if (foundOpenParentheses) {
374 if (pos >= s.Length || s [pos++] != ')')
375 throw new FormatException ("Input string was not in the correct " +
377 if (AllowTrailingWhite && pos < s.Length)
378 pos = JumpOverWhite (pos, s, false);
382 throw new FormatException ("Input string was not in the correct format.");
391 public override string ToString ()
393 return IntegerFormatter.FormatGeneral(value, 0, null, true);
396 public string ToString (IFormatProvider fp)
398 return ToString (null, fp);
401 public string ToString (string format)
403 return ToString (format, null);
406 public string ToString (string format, IFormatProvider fp )
408 NumberFormatInfo nfi = NumberFormatInfo.GetInstance( fp );
410 if ( format == null )
413 return IntegerFormatter.NumberToString (format, nfi, value);
416 // =========== IConvertible Methods =========== //
418 public TypeCode GetTypeCode ()
420 return TypeCode.Int32;
423 bool IConvertible.ToBoolean (IFormatProvider provider)
425 return System.Convert.ToBoolean (value);
427 byte IConvertible.ToByte (IFormatProvider provider)
429 return System.Convert.ToByte (value);
431 char IConvertible.ToChar (IFormatProvider provider)
433 return System.Convert.ToChar (value);
435 DateTime IConvertible.ToDateTime (IFormatProvider provider)
437 return System.Convert.ToDateTime (value);
439 decimal IConvertible.ToDecimal (IFormatProvider provider)
441 return System.Convert.ToDecimal (value);
443 double IConvertible.ToDouble (IFormatProvider provider)
445 return System.Convert.ToDouble (value);
447 short IConvertible.ToInt16 (IFormatProvider provider)
449 return System.Convert.ToInt16 (value);
451 int IConvertible.ToInt32 (IFormatProvider provider)
455 long IConvertible.ToInt64 (IFormatProvider provider)
457 return System.Convert.ToInt64 (value);
459 [CLSCompliant(false)]
460 sbyte IConvertible.ToSByte (IFormatProvider provider)
462 return System.Convert.ToSByte (value);
464 float IConvertible.ToSingle (IFormatProvider provider)
466 return System.Convert.ToSingle (value);
469 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
471 return System.Convert.ToType (value, conversionType, provider);
474 [CLSCompliant(false)]
475 ushort IConvertible.ToUInt16 (IFormatProvider provider)
477 return System.Convert.ToUInt16 (value);
479 [CLSCompliant(false)]
480 uint IConvertible.ToUInt32 (IFormatProvider provider)
482 return System.Convert.ToUInt32 (value);
484 [CLSCompliant(false)]
485 ulong IConvertible.ToUInt64 (IFormatProvider provider)
487 return System.Convert.ToUInt64 (value);