5 // Miguel de Icaza (miguel@ximian.com)
6 // Marek Safar (marek.safar@gmail.com)
8 // (C) Ximian, Inc. http://www.ximian.com
9 // Copyright (C) 2004 Novell (http://www.novell.com)
10 // Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Globalization;
33 using System.Threading;
38 [CLSCompliant (false)]
39 [System.Runtime.InteropServices.ComVisible (true)]
40 public struct UInt32 : IFormattable, IConvertible, IComparable, IComparable<UInt32>, IEquatable <UInt32>
42 public const uint MaxValue = 0xffffffff;
43 public const uint MinValue = 0;
45 internal uint m_value;
47 public int CompareTo (object value)
52 if (!(value is System.UInt32))
53 throw new ArgumentException (Locale.GetText ("Value is not a System.UInt32."));
55 uint val = (uint) value;
60 return (m_value < val) ? -1 : 1;
63 public override bool Equals (object obj)
65 if (!(obj is System.UInt32))
68 return ((uint) obj) == m_value;
71 public override int GetHashCode ()
76 public int CompareTo (uint value)
86 public bool Equals (uint obj)
88 return obj == m_value;
91 internal static bool Parse (string s, bool tryParse, out uint result, out Exception exc)
96 bool digits_seen = false;
97 bool has_negative_sign = false;
104 exc = new ArgumentNullException ("s");
111 for (i = 0; i < len; i++) {
113 if (!Char.IsWhiteSpace (c))
119 exc = Int32.GetFormatException ();
128 has_negative_sign = true;
131 for (; i < len; i++) {
134 if (c >= '0' && c <= '9') {
135 uint d = (uint) (c - '0');
137 if ((val > MaxValue/10) || (val == (MaxValue / 10) && d > (MaxValue % 10))){
139 exc = new OverflowException (Locale.GetText ("Value is too large"));
142 val = (val * 10) + d;
144 } else if (!Int32.ProcessTrailingWhitespace (tryParse, s, i, ref exc)){
150 exc = Int32.GetFormatException ();
154 // -0 is legal but other negative values are not
155 if (has_negative_sign && (val > 0)) {
157 exc = new OverflowException (
158 Locale.GetText ("Negative number"));
166 internal static bool Parse (string s, NumberStyles style, IFormatProvider provider, bool tryParse, out uint result, out Exception exc)
173 exc = new ArgumentNullException ("s");
179 exc = Int32.GetFormatException ();
183 NumberFormatInfo nfi = null;
184 if (provider != null) {
185 Type typeNFI = typeof (NumberFormatInfo);
186 nfi = provider.GetFormat (typeNFI) as NumberFormatInfo;
189 nfi = Thread.CurrentThread.CurrentCulture.NumberFormat;
191 if (!Int32.CheckStyle (style, tryParse, ref exc))
194 bool AllowCurrencySymbol = (style & NumberStyles.AllowCurrencySymbol) != 0;
195 bool AllowHexSpecifier = (style & NumberStyles.AllowHexSpecifier) != 0;
196 bool AllowThousands = (style & NumberStyles.AllowThousands) != 0;
197 bool AllowDecimalPoint = (style & NumberStyles.AllowDecimalPoint) != 0;
198 bool AllowParentheses = (style & NumberStyles.AllowParentheses) != 0;
199 bool AllowTrailingSign = (style & NumberStyles.AllowTrailingSign) != 0;
200 bool AllowLeadingSign = (style & NumberStyles.AllowLeadingSign) != 0;
201 bool AllowTrailingWhite = (style & NumberStyles.AllowTrailingWhite) != 0;
202 bool AllowLeadingWhite = (style & NumberStyles.AllowLeadingWhite) != 0;
203 bool AllowExponent = (style & NumberStyles.AllowExponent) != 0;
207 if (AllowLeadingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc))
210 bool foundOpenParentheses = false;
211 bool negative = false;
212 bool foundSign = false;
213 bool foundCurrency = false;
216 if (AllowParentheses && s [pos] == '(') {
217 foundOpenParentheses = true;
219 negative = true; // MS always make the number negative when there parentheses
220 // even when NumberFormatInfo.NumberNegativePattern != 0!!!
222 if (AllowLeadingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc))
225 if (s.Substring (pos, nfi.NegativeSign.Length) == nfi.NegativeSign) {
227 exc = Int32.GetFormatException ();
231 if (s.Substring (pos, nfi.PositiveSign.Length) == nfi.PositiveSign) {
233 exc = Int32.GetFormatException ();
238 if (AllowLeadingSign && !foundSign) {
240 Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative);
242 if (AllowLeadingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc))
244 if (AllowCurrencySymbol) {
245 Int32.FindCurrency (ref pos, s, nfi,
247 if (foundCurrency && AllowLeadingWhite &&
248 !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc))
254 if (AllowCurrencySymbol && !foundCurrency) {
256 Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency);
258 if (AllowLeadingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc))
261 if (!foundSign && AllowLeadingSign) {
262 Int32.FindSign (ref pos, s, nfi, ref foundSign,
264 if (foundSign && AllowLeadingWhite &&
265 !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc))
274 int decimalPointPos = -1;
279 // Just the same as Int32, but this one adds instead of substract
280 while (pos < s.Length) {
282 if (!Int32.ValidDigit (s [pos], AllowHexSpecifier)) {
283 if (AllowThousands &&
284 (Int32.FindOther (ref pos, s, nfi.NumberGroupSeparator)
285 || Int32.FindOther (ref pos, s, nfi.CurrencyGroupSeparator)))
288 if (AllowDecimalPoint && decimalPointPos < 0 &&
289 (Int32.FindOther (ref pos, s, nfi.NumberDecimalSeparator)
290 || Int32.FindOther (ref pos, s, nfi.CurrencyDecimalSeparator))) {
291 decimalPointPos = nDigits;
300 if (AllowHexSpecifier) {
301 hexDigit = s [pos++];
302 if (Char.IsDigit (hexDigit))
303 digitValue = (uint) (hexDigit - '0');
304 else if (Char.IsLower (hexDigit))
305 digitValue = (uint) (hexDigit - 'a' + 10);
307 digitValue = (uint) (hexDigit - 'A' + 10);
310 ulong l = number * 16 + digitValue;
316 number = checked (number * 16 + digitValue);
322 number = checked (number * 10 + (uint) (s [pos++] - '0'));
323 } catch (OverflowException) {
325 exc = new OverflowException (Locale.GetText ("Value too large or too small."));
333 exc = Int32.GetFormatException ();
339 if (Int32.FindExponent (ref pos, s, ref exponent, tryParse, ref exc) && exc != null)
342 if (AllowTrailingSign && !foundSign) {
344 Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative);
345 if (foundSign && pos < s.Length) {
346 if (AllowTrailingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc))
351 if (AllowCurrencySymbol && !foundCurrency) {
352 if (AllowTrailingWhite && pos < s.Length && !Int32.JumpOverWhite (ref pos, s, false, tryParse, ref exc))
356 Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency);
357 if (foundCurrency && pos < s.Length) {
358 if (AllowTrailingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc))
360 if (!foundSign && AllowTrailingSign)
361 Int32.FindSign (ref pos, s, nfi, ref foundSign,
366 if (AllowTrailingWhite && pos < s.Length && !Int32.JumpOverWhite (ref pos, s, false, tryParse, ref exc))
369 if (foundOpenParentheses) {
370 if (pos >= s.Length || s [pos++] != ')') {
372 exc = Int32.GetFormatException ();
375 if (AllowTrailingWhite && pos < s.Length && !Int32.JumpOverWhite (ref pos, s, false, tryParse, ref exc))
379 if (pos < s.Length && s [pos] != '\u0000') {
381 exc = Int32.GetFormatException ();
385 // -0 is legal but other negative values are not
386 if (negative && (number > 0)) {
388 exc = new OverflowException (
389 Locale.GetText ("Negative number"));
393 if (decimalPointPos >= 0)
394 exponent = exponent - nDigits + decimalPointPos;
398 // Any non-zero values after decimal point are not allowed
401 number = (uint) Math.DivRem (number, (int) Math.Pow (10, -exponent), out remainder);
402 if (remainder != 0) {
404 exc = new OverflowException ("Value too large or too small.");
407 } else if (exponent > 0) {
409 // result *= 10^exponent
411 // Reduce the risk of throwing an overflow exc
413 double res = checked (Math.Pow (10, exponent) * number);
414 if (res < MinValue || res > MaxValue) {
416 exc = new OverflowException ("Value too large or too small.");
427 [CLSCompliant (false)]
428 public static uint Parse (string s)
433 if (!Parse (s, false, out res, out exc))
439 [CLSCompliant (false)]
440 public static uint Parse (string s, NumberStyles style, IFormatProvider provider)
445 if (!Parse (s, style, provider, false, out res, out exc))
451 [CLSCompliant (false)]
452 public static uint Parse (string s, IFormatProvider provider)
454 return Parse (s, NumberStyles.Integer, provider);
457 [CLSCompliant (false)]
458 public static uint Parse (string s, NumberStyles style)
460 return Parse (s, style, null);
463 [CLSCompliant (false)]
464 public static bool TryParse (string s, out uint result)
467 if (!Parse (s, true, out result, out exc)) {
475 [CLSCompliant (false)]
476 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out uint result)
479 if (!Parse (s, style, provider, true, out result, out exc)) {
487 public override string ToString ()
489 return NumberFormatter.NumberToString (m_value, null);
492 public string ToString (IFormatProvider provider)
494 return NumberFormatter.NumberToString (m_value, provider);
497 public string ToString (string format)
499 return ToString (format, null);
502 public string ToString (string format, IFormatProvider provider)
504 return NumberFormatter.NumberToString (format, m_value, provider);
507 // =========== IConvertible Methods =========== //
508 public TypeCode GetTypeCode ()
510 return TypeCode.UInt32;
513 bool IConvertible.ToBoolean (IFormatProvider provider)
515 return System.Convert.ToBoolean (m_value);
518 byte IConvertible.ToByte (IFormatProvider provider)
520 return System.Convert.ToByte (m_value);
523 char IConvertible.ToChar (IFormatProvider provider)
525 return System.Convert.ToChar (m_value);
528 DateTime IConvertible.ToDateTime (IFormatProvider provider)
530 return System.Convert.ToDateTime (m_value);
533 decimal IConvertible.ToDecimal (IFormatProvider provider)
535 return System.Convert.ToDecimal (m_value);
538 double IConvertible.ToDouble (IFormatProvider provider)
540 return System.Convert.ToDouble (m_value);
543 short IConvertible.ToInt16 (IFormatProvider provider)
545 return System.Convert.ToInt16 (m_value);
548 int IConvertible.ToInt32 (IFormatProvider provider)
550 return System.Convert.ToInt32 (m_value);
553 long IConvertible.ToInt64 (IFormatProvider provider)
555 return System.Convert.ToInt64 (m_value);
558 sbyte IConvertible.ToSByte (IFormatProvider provider)
560 return System.Convert.ToSByte (m_value);
563 float IConvertible.ToSingle (IFormatProvider provider)
565 return System.Convert.ToSingle (m_value);
568 object IConvertible.ToType (Type targetType, IFormatProvider provider)
570 if (targetType == null)
571 throw new ArgumentNullException ("targetType");
572 return System.Convert.ToType (m_value, targetType, provider, false);
575 ushort IConvertible.ToUInt16 (IFormatProvider provider)
577 return System.Convert.ToUInt16 (m_value);
580 uint IConvertible.ToUInt32 (IFormatProvider provider)
585 ulong IConvertible.ToUInt64 (IFormatProvider provider)
587 return System.Convert.ToUInt64 (m_value);