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 public struct UInt32 : IComparable, IFormattable, IConvertible
39 public const uint MaxValue = 0xffffffff;
40 public const uint MinValue = 0;
42 internal uint m_value;
44 public int CompareTo (object value)
49 if (!(value is System.UInt32))
50 throw new ArgumentException (Locale.GetText ("Value is not a System.UInt32."));
52 if (this.m_value == (uint) value)
55 if (this.m_value < (uint) value)
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 [CLSCompliant (false)]
75 public static uint Parse (string s)
80 bool digits_seen = false;
81 bool has_negative_sign = false;
84 throw new ArgumentNullException ("s");
89 for (i = 0; i < len; i++) {
91 if (!Char.IsWhiteSpace (c))
96 throw new FormatException ();
103 has_negative_sign = true;
106 for (; i < len; i++) {
109 if (c >= '0' && c <= '9') {
110 uint d = (uint) (c - '0');
112 val = checked (val * 10 + d);
116 if (Char.IsWhiteSpace (c)) {
117 for (i++; i < len; i++) {
118 if (!Char.IsWhiteSpace (s [i]))
119 throw new FormatException ();
123 throw new FormatException ();
127 throw new FormatException ();
129 // -0 is legal but other negative values are not
130 if (has_negative_sign && (val > 0)) {
131 throw new OverflowException (
132 Locale.GetText ("Negative number"));
138 [CLSCompliant (false)]
139 public static uint Parse (string s, IFormatProvider provider)
141 return Parse (s, NumberStyles.Integer, provider);
144 [CLSCompliant (false)]
145 public static uint Parse (string s, NumberStyles style)
147 return Parse (s, style, null);
150 [CLSCompliant (false)]
151 public static uint Parse (string s, NumberStyles style, IFormatProvider provider)
154 throw new ArgumentNullException ("s");
157 throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
159 NumberFormatInfo nfi;
160 if (provider != null) {
161 Type typeNFI = typeof (NumberFormatInfo);
162 nfi = (NumberFormatInfo) provider.GetFormat (typeNFI);
165 nfi = Thread.CurrentThread.CurrentCulture.NumberFormat;
167 Int32.CheckStyle (style);
169 bool AllowCurrencySymbol = (style & NumberStyles.AllowCurrencySymbol) != 0;
170 bool AllowExponent = (style & NumberStyles.AllowExponent) != 0;
171 bool AllowHexSpecifier = (style & NumberStyles.AllowHexSpecifier) != 0;
172 bool AllowThousands = (style & NumberStyles.AllowThousands) != 0;
173 bool AllowDecimalPoint = (style & NumberStyles.AllowDecimalPoint) != 0;
174 bool AllowParentheses = (style & NumberStyles.AllowParentheses) != 0;
175 bool AllowTrailingSign = (style & NumberStyles.AllowTrailingSign) != 0;
176 bool AllowLeadingSign = (style & NumberStyles.AllowLeadingSign) != 0;
177 bool AllowTrailingWhite = (style & NumberStyles.AllowTrailingWhite) != 0;
178 bool AllowLeadingWhite = (style & NumberStyles.AllowLeadingWhite) != 0;
182 if (AllowLeadingWhite)
183 pos = Int32.JumpOverWhite (pos, s, true);
185 bool foundOpenParentheses = false;
186 bool negative = false;
187 bool foundSign = false;
188 bool foundCurrency = false;
191 if (AllowParentheses && s [pos] == '(') {
192 foundOpenParentheses = true;
194 negative = true; // MS always make the number negative when there parentheses
195 // even when NumberFormatInfo.NumberNegativePattern != 0!!!
197 if (AllowLeadingWhite)
198 pos = Int32.JumpOverWhite (pos, s, true);
200 if (s.Substring (pos, nfi.NegativeSign.Length) == nfi.NegativeSign)
201 throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
202 if (s.Substring (pos, nfi.PositiveSign.Length) == nfi.PositiveSign)
203 throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
206 if (AllowLeadingSign && !foundSign) {
208 Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative);
210 if (AllowLeadingWhite)
211 pos = Int32.JumpOverWhite (pos, s, true);
212 if (AllowCurrencySymbol) {
213 Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency);
214 if (foundCurrency && AllowLeadingWhite)
215 pos = Int32.JumpOverWhite (pos, s, true);
220 if (AllowCurrencySymbol && !foundCurrency) {
222 Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency);
224 if (AllowLeadingWhite)
225 pos = Int32.JumpOverWhite (pos, s, true);
227 if (!foundSign && AllowLeadingSign) {
228 Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative);
229 if (foundSign && AllowLeadingWhite)
230 pos = Int32.JumpOverWhite (pos, s, true);
238 bool decimalPointFound = false;
243 // Just the same as Int32, but this one adds instead of substract
246 if (!Int32.ValidDigit (s [pos], AllowHexSpecifier)) {
247 if (AllowThousands && Int32.FindOther (ref pos, s, nfi.NumberGroupSeparator))
250 if (!decimalPointFound && AllowDecimalPoint &&
251 Int32.FindOther (ref pos, s, nfi.NumberDecimalSeparator)) {
252 decimalPointFound = true;
257 else if (AllowHexSpecifier) {
259 hexDigit = s [pos++];
260 if (Char.IsDigit (hexDigit))
261 digitValue = (uint) (hexDigit - '0');
262 else if (Char.IsLower (hexDigit))
263 digitValue = (uint) (hexDigit - 'a' + 10);
265 digitValue = (uint) (hexDigit - 'A' + 10);
267 number = checked (number * 16 + digitValue);
269 else if (decimalPointFound) {
271 // Allows decimal point as long as it's only
272 // followed by zeroes.
273 if (s [pos++] != '0')
274 throw new OverflowException (Locale.GetText ("Value too large or too small."));
280 number = checked (number * 10 + (uint) (s [pos++] - '0'));
282 catch (OverflowException) {
283 throw new OverflowException (Locale.GetText ("Value too large or too small."));
286 } while (pos < s.Length);
290 throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
292 if (AllowTrailingSign && !foundSign) {
294 Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative);
296 if (AllowTrailingWhite)
297 pos = Int32.JumpOverWhite (pos, s, true);
298 if (AllowCurrencySymbol)
299 Int32. FindCurrency (ref pos, s, nfi, ref foundCurrency);
303 if (AllowCurrencySymbol && !foundCurrency) {
305 Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency);
307 if (AllowTrailingWhite)
308 pos = Int32.JumpOverWhite (pos, s, true);
309 if (!foundSign && AllowTrailingSign)
310 Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative);
314 if (AllowTrailingWhite && pos < s.Length)
315 pos = Int32.JumpOverWhite (pos, s, false);
317 if (foundOpenParentheses) {
318 if (pos >= s.Length || s [pos++] != ')')
319 throw new FormatException (Locale.GetText
320 ("Input string was not in the correct format."));
321 if (AllowTrailingWhite && pos < s.Length)
322 pos = Int32.JumpOverWhite (pos, s, false);
325 if (pos < s.Length && s [pos] != '\u0000')
326 throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
328 // -0 is legal but other negative values are not
329 if (negative && (number > 0)) {
330 throw new OverflowException (
331 Locale.GetText ("Negative number"));
337 public override string ToString ()
339 return ToString (null, null);
342 public string ToString (IFormatProvider fp)
344 return ToString (null, fp);
347 public string ToString (string format)
349 return ToString (format, null);
352 public string ToString (string format, IFormatProvider fp)
354 NumberFormatInfo nfi = NumberFormatInfo.GetInstance (fp);
356 // use "G" when format is null or String.Empty
357 if ((format == null) || (format.Length == 0))
360 return IntegerFormatter.NumberToString (format, nfi, m_value);
363 // =========== IConvertible Methods =========== //
364 public TypeCode GetTypeCode ()
366 return TypeCode.UInt32;
369 bool IConvertible.ToBoolean (IFormatProvider provider)
371 return System.Convert.ToBoolean (m_value);
374 byte IConvertible.ToByte (IFormatProvider provider)
376 return System.Convert.ToByte (m_value);
379 char IConvertible.ToChar (IFormatProvider provider)
381 return System.Convert.ToChar (m_value);
384 DateTime IConvertible.ToDateTime (IFormatProvider provider)
386 return System.Convert.ToDateTime (m_value);
389 decimal IConvertible.ToDecimal (IFormatProvider provider)
391 return System.Convert.ToDecimal (m_value);
394 double IConvertible.ToDouble (IFormatProvider provider)
396 return System.Convert.ToDouble (m_value);
399 short IConvertible.ToInt16 (IFormatProvider provider)
401 return System.Convert.ToInt16 (m_value);
404 int IConvertible.ToInt32 (IFormatProvider provider)
406 return System.Convert.ToInt32 (m_value);
409 long IConvertible.ToInt64 (IFormatProvider provider)
411 return System.Convert.ToInt64 (m_value);
414 sbyte IConvertible.ToSByte (IFormatProvider provider)
416 return System.Convert.ToSByte (m_value);
419 float IConvertible.ToSingle (IFormatProvider provider)
421 return System.Convert.ToSingle (m_value);
424 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
426 return System.Convert.ToType (m_value, conversionType, provider);
429 ushort IConvertible.ToUInt16 (IFormatProvider provider)
431 return System.Convert.ToUInt16 (m_value);
434 uint IConvertible.ToUInt32 (IFormatProvider provider)
439 ulong IConvertible.ToUInt64 (IFormatProvider provider)
441 return System.Convert.ToUInt64 (m_value);