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 = (NumberFormatInfo) provider.GetFormat (typeNFI);
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 bool decimalPointFound = false;
280 // Just the same as Int32, but this one adds instead of substract
283 if (!Int32.ValidDigit (s [pos], AllowHexSpecifier)) {
284 if (AllowThousands &&
285 (Int32.FindOther (ref pos, s, nfi.NumberGroupSeparator)
286 || Int32.FindOther (ref pos, s, nfi.CurrencyGroupSeparator)))
289 if (!decimalPointFound && AllowDecimalPoint &&
290 (Int32.FindOther (ref pos, s, nfi.NumberDecimalSeparator)
291 || Int32.FindOther (ref pos, s, nfi.CurrencyDecimalSeparator))) {
292 decimalPointFound = true;
298 if (AllowHexSpecifier) {
300 hexDigit = s [pos++];
301 if (Char.IsDigit (hexDigit))
302 digitValue = (uint) (hexDigit - '0');
303 else if (Char.IsLower (hexDigit))
304 digitValue = (uint) (hexDigit - 'a' + 10);
306 digitValue = (uint) (hexDigit - 'A' + 10);
309 ulong l = number * 16 + digitValue;
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') {
323 exc = new OverflowException (Locale.GetText ("Value too large or too small."));
331 number = checked (number * 10 + (uint) (s [pos++] - '0'));
333 catch (OverflowException) {
335 exc = new OverflowException (Locale.GetText ("Value too large or too small."));
339 } while (pos < s.Length);
344 exc = Int32.GetFormatException ();
349 if (Int32.FindExponent (ref pos, s, ref exponent, tryParse, ref exc) && exc != null)
352 if (AllowTrailingSign && !foundSign) {
354 Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative);
355 if (foundSign && pos < s.Length) {
356 if (AllowTrailingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc))
361 if (AllowCurrencySymbol && !foundCurrency) {
362 if (AllowTrailingWhite && pos < s.Length && !Int32.JumpOverWhite (ref pos, s, false, tryParse, ref exc))
366 Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency);
367 if (foundCurrency && pos < s.Length) {
368 if (AllowTrailingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc))
370 if (!foundSign && AllowTrailingSign)
371 Int32.FindSign (ref pos, s, nfi, ref foundSign,
376 if (AllowTrailingWhite && pos < s.Length && !Int32.JumpOverWhite (ref pos, s, false, tryParse, ref exc))
379 if (foundOpenParentheses) {
380 if (pos >= s.Length || s [pos++] != ')') {
382 exc = Int32.GetFormatException ();
385 if (AllowTrailingWhite && pos < s.Length && !Int32.JumpOverWhite (ref pos, s, false, tryParse, ref exc))
389 if (pos < s.Length && s [pos] != '\u0000') {
391 exc = Int32.GetFormatException ();
395 // -0 is legal but other negative values are not
396 if (negative && (number > 0)) {
398 exc = new OverflowException (
399 Locale.GetText ("Negative number"));
403 // result *= 10^exponent
405 // Reduce the risk of throwing an overflow exc
406 double res = checked (Math.Pow (10, exponent) * number);
407 if (res < Int32.MinValue || res > Int32.MaxValue) {
409 exc = new OverflowException ("Value too large or too small.");
421 [CLSCompliant (false)]
422 public static uint Parse (string s)
427 if (!Parse (s, false, out res, out exc))
433 [CLSCompliant (false)]
434 public static uint Parse (string s, NumberStyles style, IFormatProvider provider)
439 if (!Parse (s, style, provider, false, out res, out exc))
445 [CLSCompliant (false)]
446 public static uint Parse (string s, IFormatProvider provider)
448 return Parse (s, NumberStyles.Integer, provider);
451 [CLSCompliant (false)]
452 public static uint Parse (string s, NumberStyles style)
454 return Parse (s, style, null);
457 [CLSCompliant (false)]
458 public static bool TryParse (string s, out uint result)
461 if (!Parse (s, true, out result, out exc)) {
469 [CLSCompliant (false)]
470 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out uint result)
473 if (!Parse (s, style, provider, true, out result, out exc)) {
481 public override string ToString ()
483 return NumberFormatter.NumberToString (m_value, null);
486 public string ToString (IFormatProvider provider)
488 return NumberFormatter.NumberToString (m_value, provider);
491 public string ToString (string format)
493 return ToString (format, null);
496 public string ToString (string format, IFormatProvider provider)
498 return NumberFormatter.NumberToString (format, m_value, provider);
501 // =========== IConvertible Methods =========== //
502 public TypeCode GetTypeCode ()
504 return TypeCode.UInt32;
507 bool IConvertible.ToBoolean (IFormatProvider provider)
509 return System.Convert.ToBoolean (m_value);
512 byte IConvertible.ToByte (IFormatProvider provider)
514 return System.Convert.ToByte (m_value);
517 char IConvertible.ToChar (IFormatProvider provider)
519 return System.Convert.ToChar (m_value);
522 DateTime IConvertible.ToDateTime (IFormatProvider provider)
524 return System.Convert.ToDateTime (m_value);
527 decimal IConvertible.ToDecimal (IFormatProvider provider)
529 return System.Convert.ToDecimal (m_value);
532 double IConvertible.ToDouble (IFormatProvider provider)
534 return System.Convert.ToDouble (m_value);
537 short IConvertible.ToInt16 (IFormatProvider provider)
539 return System.Convert.ToInt16 (m_value);
542 int IConvertible.ToInt32 (IFormatProvider provider)
544 return System.Convert.ToInt32 (m_value);
547 long IConvertible.ToInt64 (IFormatProvider provider)
549 return System.Convert.ToInt64 (m_value);
552 sbyte IConvertible.ToSByte (IFormatProvider provider)
554 return System.Convert.ToSByte (m_value);
557 float IConvertible.ToSingle (IFormatProvider provider)
559 return System.Convert.ToSingle (m_value);
562 object IConvertible.ToType (Type targetType, IFormatProvider provider)
564 if (targetType == null)
565 throw new ArgumentNullException ("targetType");
566 return System.Convert.ToType (m_value, targetType, provider, false);
569 ushort IConvertible.ToUInt16 (IFormatProvider provider)
571 return System.Convert.ToUInt16 (m_value);
574 uint IConvertible.ToUInt32 (IFormatProvider provider)
579 ulong IConvertible.ToUInt64 (IFormatProvider provider)
581 return System.Convert.ToUInt64 (m_value);