5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
8 // Copyright (C) 2004 Novell (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Globalization;
31 using System.Threading;
36 [CLSCompliant (false)]
37 [System.Runtime.InteropServices.ComVisible (true)]
38 public struct UInt32 : IFormattable, IConvertible, IComparable, IComparable<UInt32>, IEquatable <UInt32>
40 public const uint MaxValue = 0xffffffff;
41 public const uint MinValue = 0;
43 internal uint m_value;
45 public int CompareTo (object value)
50 if (!(value is System.UInt32))
51 throw new ArgumentException (Locale.GetText ("Value is not a System.UInt32."));
53 uint val = (uint) value;
58 return (m_value < val) ? -1 : 1;
61 public override bool Equals (object obj)
63 if (!(obj is System.UInt32))
66 return ((uint) obj) == m_value;
69 public override int GetHashCode ()
74 public int CompareTo (uint value)
84 public bool Equals (uint obj)
86 return obj == m_value;
89 internal static bool Parse (string s, bool tryParse, out uint result, out Exception exc)
94 bool digits_seen = false;
95 bool has_negative_sign = false;
102 exc = new ArgumentNullException ("s");
109 for (i = 0; i < len; i++) {
111 if (!Char.IsWhiteSpace (c))
117 exc = Int32.GetFormatException ();
126 has_negative_sign = true;
129 for (; i < len; i++) {
132 if (c >= '0' && c <= '9') {
133 uint d = (uint) (c - '0');
135 if ((val > MaxValue/10) || (val == (MaxValue / 10) && d > (MaxValue % 10))){
137 exc = new OverflowException (Locale.GetText ("Value is too large"));
140 val = (val * 10) + d;
142 } else if (!Int32.ProcessTrailingWhitespace (tryParse, s, i, ref exc)){
148 exc = Int32.GetFormatException ();
152 // -0 is legal but other negative values are not
153 if (has_negative_sign && (val > 0)) {
155 exc = new OverflowException (
156 Locale.GetText ("Negative number"));
164 internal static bool Parse (string s, NumberStyles style, IFormatProvider provider, bool tryParse, out uint result, out Exception exc)
171 exc = new ArgumentNullException ("s");
177 exc = Int32.GetFormatException ();
181 NumberFormatInfo nfi = null;
182 if (provider != null) {
183 Type typeNFI = typeof (NumberFormatInfo);
184 nfi = (NumberFormatInfo) provider.GetFormat (typeNFI);
187 nfi = Thread.CurrentThread.CurrentCulture.NumberFormat;
189 if (!Int32.CheckStyle (style, tryParse, ref exc))
192 bool AllowCurrencySymbol = (style & NumberStyles.AllowCurrencySymbol) != 0;
193 bool AllowHexSpecifier = (style & NumberStyles.AllowHexSpecifier) != 0;
194 bool AllowThousands = (style & NumberStyles.AllowThousands) != 0;
195 bool AllowDecimalPoint = (style & NumberStyles.AllowDecimalPoint) != 0;
196 bool AllowParentheses = (style & NumberStyles.AllowParentheses) != 0;
197 bool AllowTrailingSign = (style & NumberStyles.AllowTrailingSign) != 0;
198 bool AllowLeadingSign = (style & NumberStyles.AllowLeadingSign) != 0;
199 bool AllowTrailingWhite = (style & NumberStyles.AllowTrailingWhite) != 0;
200 bool AllowLeadingWhite = (style & NumberStyles.AllowLeadingWhite) != 0;
204 if (AllowLeadingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc))
207 bool foundOpenParentheses = false;
208 bool negative = false;
209 bool foundSign = false;
210 bool foundCurrency = false;
213 if (AllowParentheses && s [pos] == '(') {
214 foundOpenParentheses = true;
216 negative = true; // MS always make the number negative when there parentheses
217 // even when NumberFormatInfo.NumberNegativePattern != 0!!!
219 if (AllowLeadingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc))
222 if (s.Substring (pos, nfi.NegativeSign.Length) == nfi.NegativeSign) {
224 exc = Int32.GetFormatException ();
227 if (s.Substring (pos, nfi.PositiveSign.Length) == nfi.PositiveSign) {
229 exc = Int32.GetFormatException ();
234 if (AllowLeadingSign && !foundSign) {
236 Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative);
238 if (AllowLeadingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc))
240 if (AllowCurrencySymbol) {
241 Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency);
242 if (foundCurrency && AllowLeadingWhite &&
243 !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc))
249 if (AllowCurrencySymbol && !foundCurrency) {
251 Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency);
253 if (AllowLeadingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc))
256 if (!foundSign && AllowLeadingSign) {
257 Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative);
258 if (foundSign && AllowLeadingWhite &&
259 !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc))
268 bool decimalPointFound = false;
273 // Just the same as Int32, but this one adds instead of substract
276 if (!Int32.ValidDigit (s [pos], AllowHexSpecifier)) {
277 if (AllowThousands && Int32.FindOther (ref pos, s, nfi.NumberGroupSeparator))
280 if (!decimalPointFound && AllowDecimalPoint &&
281 Int32.FindOther (ref pos, s, nfi.NumberDecimalSeparator)) {
282 decimalPointFound = true;
287 else if (AllowHexSpecifier) {
289 hexDigit = s [pos++];
290 if (Char.IsDigit (hexDigit))
291 digitValue = (uint) (hexDigit - '0');
292 else if (Char.IsLower (hexDigit))
293 digitValue = (uint) (hexDigit - 'a' + 10);
295 digitValue = (uint) (hexDigit - 'A' + 10);
298 ulong l = number * 16 + digitValue;
304 number = checked (number * 16 + digitValue);
306 else if (decimalPointFound) {
308 // Allows decimal point as long as it's only
309 // followed by zeroes.
310 if (s [pos++] != '0') {
312 exc = new OverflowException (Locale.GetText ("Value too large or too small."));
320 number = checked (number * 10 + (uint) (s [pos++] - '0'));
322 catch (OverflowException) {
324 exc = new OverflowException (Locale.GetText ("Value too large or too small."));
328 } while (pos < s.Length);
333 exc = Int32.GetFormatException ();
337 if (AllowTrailingSign && !foundSign) {
339 Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative);
341 if (AllowTrailingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc))
343 if (AllowCurrencySymbol)
344 Int32. FindCurrency (ref pos, s, nfi, ref foundCurrency);
348 if (AllowCurrencySymbol && !foundCurrency) {
350 Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency);
352 if (AllowTrailingWhite && !Int32.JumpOverWhite (ref pos, s, true, tryParse, ref exc))
354 if (!foundSign && AllowTrailingSign)
355 Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative);
359 if (AllowTrailingWhite && pos < s.Length && !Int32.JumpOverWhite (ref pos, s, false, tryParse, ref exc))
362 if (foundOpenParentheses) {
363 if (pos >= s.Length || s [pos++] != ')') {
365 exc = Int32.GetFormatException ();
368 if (AllowTrailingWhite && pos < s.Length && !Int32.JumpOverWhite (ref pos, s, false, tryParse, ref exc))
372 if (pos < s.Length && s [pos] != '\u0000') {
374 exc = Int32.GetFormatException ();
378 // -0 is legal but other negative values are not
379 if (negative && (number > 0)) {
381 exc = new OverflowException (
382 Locale.GetText ("Negative number"));
391 [CLSCompliant (false)]
392 public static uint Parse (string s)
397 if (!Parse (s, false, out res, out exc))
403 [CLSCompliant (false)]
404 public static uint Parse (string s, NumberStyles style, IFormatProvider provider)
409 if (!Parse (s, style, provider, false, out res, out exc))
415 [CLSCompliant (false)]
416 public static uint Parse (string s, IFormatProvider provider)
418 return Parse (s, NumberStyles.Integer, provider);
421 [CLSCompliant (false)]
422 public static uint Parse (string s, NumberStyles style)
424 return Parse (s, style, null);
427 [CLSCompliant (false)]
428 public static bool TryParse (string s, out uint result)
431 if (!Parse (s, true, out result, out exc)) {
439 [CLSCompliant (false)]
440 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out uint result)
443 if (!Parse (s, style, provider, true, out result, out exc)) {
451 public override string ToString ()
453 return NumberFormatter.NumberToString (m_value, null);
456 public string ToString (IFormatProvider provider)
458 return NumberFormatter.NumberToString (m_value, provider);
461 public string ToString (string format)
463 return ToString (format, null);
466 public string ToString (string format, IFormatProvider provider)
468 return NumberFormatter.NumberToString (format, m_value, provider);
471 // =========== IConvertible Methods =========== //
472 public TypeCode GetTypeCode ()
474 return TypeCode.UInt32;
477 bool IConvertible.ToBoolean (IFormatProvider provider)
479 return System.Convert.ToBoolean (m_value);
482 byte IConvertible.ToByte (IFormatProvider provider)
484 return System.Convert.ToByte (m_value);
487 char IConvertible.ToChar (IFormatProvider provider)
489 return System.Convert.ToChar (m_value);
492 DateTime IConvertible.ToDateTime (IFormatProvider provider)
494 return System.Convert.ToDateTime (m_value);
497 decimal IConvertible.ToDecimal (IFormatProvider provider)
499 return System.Convert.ToDecimal (m_value);
502 double IConvertible.ToDouble (IFormatProvider provider)
504 return System.Convert.ToDouble (m_value);
507 short IConvertible.ToInt16 (IFormatProvider provider)
509 return System.Convert.ToInt16 (m_value);
512 int IConvertible.ToInt32 (IFormatProvider provider)
514 return System.Convert.ToInt32 (m_value);
517 long IConvertible.ToInt64 (IFormatProvider provider)
519 return System.Convert.ToInt64 (m_value);
522 sbyte IConvertible.ToSByte (IFormatProvider provider)
524 return System.Convert.ToSByte (m_value);
527 float IConvertible.ToSingle (IFormatProvider provider)
529 return System.Convert.ToSingle (m_value);
532 object IConvertible.ToType (Type targetType, IFormatProvider provider)
534 if (targetType == null)
535 throw new ArgumentNullException ("targetType");
537 return System.Convert.ToType (m_value, targetType, provider, false);
540 ushort IConvertible.ToUInt16 (IFormatProvider provider)
542 return System.Convert.ToUInt16 (m_value);
545 uint IConvertible.ToUInt32 (IFormatProvider provider)
550 ulong IConvertible.ToUInt64 (IFormatProvider provider)
552 return System.Convert.ToUInt64 (m_value);