(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / corlib / System / UInt32.cs
blobcbfc64d66400a87db5f5b01b30c219f784e13d51
1 //
2 // System.UInt32.cs
3 //
4 // Author:
5 // Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) Ximian, Inc. http://www.ximian.com
8 // Copyright (C) 2004 Novell (http://www.novell.com)
9 //
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:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
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;
33 namespace System
35 [Serializable]
36 [CLSCompliant (false)]
37 public struct UInt32 : IFormattable, IConvertible,
38 #if NET_2_0
39 IComparable, IComparable<UInt32>
40 #else
41 IComparable
42 #endif
44 public const uint MaxValue = 0xffffffff;
45 public const uint MinValue = 0;
47 internal uint m_value;
49 public int CompareTo (object value)
51 if (value == null)
52 return 1;
54 if (!(value is System.UInt32))
55 throw new ArgumentException (Locale.GetText ("Value is not a System.UInt32."));
57 if (this.m_value == (uint) value)
58 return 0;
60 if (this.m_value < (uint) value)
61 return -1;
63 return 1;
66 public override bool Equals (object obj)
68 if (!(obj is System.UInt32))
69 return false;
71 return ((uint) obj) == m_value;
74 public override int GetHashCode ()
76 return (int) m_value;
79 #if NET_2_0
80 public int CompareTo (uint value)
82 if (m_value == value)
83 return 0;
84 if (m_value > value)
85 return 1;
86 else
87 return -1;
90 public bool Equals (uint value)
92 return value == m_value;
94 #endif
96 internal static bool Parse (string s, bool tryParse, out uint result)
98 uint val = 0;
99 int len;
100 int i;
101 bool digits_seen = false;
102 bool has_negative_sign = false;
104 result = 0;
106 if (s == null)
107 if (tryParse)
108 return false;
109 else
110 throw new ArgumentNullException ("s");
112 len = s.Length;
114 char c;
115 for (i = 0; i < len; i++) {
116 c = s [i];
117 if (!Char.IsWhiteSpace (c))
118 break;
121 if (i == len)
122 if (tryParse)
123 return false;
124 else
125 throw new FormatException ();
127 if (s [i] == '+')
128 i++;
129 else
130 if (s[i] == '-') {
131 i++;
132 has_negative_sign = true;
135 for (; i < len; i++) {
136 c = s [i];
138 if (c >= '0' && c <= '9') {
139 uint d = (uint) (c - '0');
141 val = checked (val * 10 + d);
142 digits_seen = true;
144 else {
145 if (Char.IsWhiteSpace (c)) {
146 for (i++; i < len; i++) {
147 if (!Char.IsWhiteSpace (s [i]))
148 if (tryParse)
149 return false;
150 else
151 throw new FormatException ();
153 break;
154 } else
155 if (tryParse)
156 return false;
157 else
158 throw new FormatException ();
161 if (!digits_seen)
162 if (tryParse)
163 return false;
164 else
165 throw new FormatException ();
167 // -0 is legal but other negative values are not
168 if (has_negative_sign && (val > 0)) {
169 if (tryParse)
170 return false;
171 else
172 throw new OverflowException (
173 Locale.GetText ("Negative number"));
176 result = val;
177 return true;
180 internal static bool Parse (string s, NumberStyles style, IFormatProvider provider, bool tryParse, out uint result)
182 result = 0;
184 if (s == null)
185 if (tryParse)
186 return false;
187 else
188 throw new ArgumentNullException ("s");
190 if (s.Length == 0)
191 if (tryParse)
192 return false;
193 else
194 throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
196 NumberFormatInfo nfi;
197 if (provider != null) {
198 Type typeNFI = typeof (NumberFormatInfo);
199 nfi = (NumberFormatInfo) provider.GetFormat (typeNFI);
201 else
202 nfi = Thread.CurrentThread.CurrentCulture.NumberFormat;
204 Int32.CheckStyle (style);
206 bool AllowCurrencySymbol = (style & NumberStyles.AllowCurrencySymbol) != 0;
207 bool AllowHexSpecifier = (style & NumberStyles.AllowHexSpecifier) != 0;
208 bool AllowThousands = (style & NumberStyles.AllowThousands) != 0;
209 bool AllowDecimalPoint = (style & NumberStyles.AllowDecimalPoint) != 0;
210 bool AllowParentheses = (style & NumberStyles.AllowParentheses) != 0;
211 bool AllowTrailingSign = (style & NumberStyles.AllowTrailingSign) != 0;
212 bool AllowLeadingSign = (style & NumberStyles.AllowLeadingSign) != 0;
213 bool AllowTrailingWhite = (style & NumberStyles.AllowTrailingWhite) != 0;
214 bool AllowLeadingWhite = (style & NumberStyles.AllowLeadingWhite) != 0;
216 int pos = 0;
218 if (AllowLeadingWhite)
219 pos = Int32.JumpOverWhite (pos, s, true);
221 bool foundOpenParentheses = false;
222 bool negative = false;
223 bool foundSign = false;
224 bool foundCurrency = false;
226 // Pre-number stuff
227 if (AllowParentheses && s [pos] == '(') {
228 foundOpenParentheses = true;
229 foundSign = true;
230 negative = true; // MS always make the number negative when there parentheses
231 // even when NumberFormatInfo.NumberNegativePattern != 0!!!
232 pos++;
233 if (AllowLeadingWhite)
234 pos = Int32.JumpOverWhite (pos, s, true);
236 if (s.Substring (pos, nfi.NegativeSign.Length) == nfi.NegativeSign)
237 if (tryParse)
238 return false;
239 else
240 throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
241 if (s.Substring (pos, nfi.PositiveSign.Length) == nfi.PositiveSign)
242 if (tryParse)
243 return false;
244 else
245 throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
248 if (AllowLeadingSign && !foundSign) {
249 // Sign + Currency
250 Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative);
251 if (foundSign) {
252 if (AllowLeadingWhite)
253 pos = Int32.JumpOverWhite (pos, s, true);
254 if (AllowCurrencySymbol) {
255 Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency);
256 if (foundCurrency && AllowLeadingWhite)
257 pos = Int32.JumpOverWhite (pos, s, true);
262 if (AllowCurrencySymbol && !foundCurrency) {
263 // Currency + sign
264 Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency);
265 if (foundCurrency) {
266 if (AllowLeadingWhite)
267 pos = Int32.JumpOverWhite (pos, s, true);
268 if (foundCurrency) {
269 if (!foundSign && AllowLeadingSign) {
270 Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative);
271 if (foundSign && AllowLeadingWhite)
272 pos = Int32.JumpOverWhite (pos, s, true);
278 uint number = 0;
279 int nDigits = 0;
280 bool decimalPointFound = false;
281 uint digitValue;
282 char hexDigit;
284 // Number stuff
285 // Just the same as Int32, but this one adds instead of substract
286 do {
288 if (!Int32.ValidDigit (s [pos], AllowHexSpecifier)) {
289 if (AllowThousands && Int32.FindOther (ref pos, s, nfi.NumberGroupSeparator))
290 continue;
291 else
292 if (!decimalPointFound && AllowDecimalPoint &&
293 Int32.FindOther (ref pos, s, nfi.NumberDecimalSeparator)) {
294 decimalPointFound = true;
295 continue;
297 break;
299 else if (AllowHexSpecifier) {
300 nDigits++;
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);
306 else
307 digitValue = (uint) (hexDigit - 'A' + 10);
309 number = checked (number * 16 + digitValue);
311 else if (decimalPointFound) {
312 nDigits++;
313 // Allows decimal point as long as it's only
314 // followed by zeroes.
315 if (s [pos++] != '0')
316 if (tryParse)
317 return false;
318 else
319 throw new OverflowException (Locale.GetText ("Value too large or too small."));
321 else {
322 nDigits++;
324 try {
325 number = checked (number * 10 + (uint) (s [pos++] - '0'));
327 catch (OverflowException) {
328 if (tryParse)
329 return false;
330 else
331 throw new OverflowException (Locale.GetText ("Value too large or too small."));
334 } while (pos < s.Length);
336 // Post number stuff
337 if (nDigits == 0)
338 if (tryParse)
339 return false;
340 else
341 throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
343 if (AllowTrailingSign && !foundSign) {
344 // Sign + Currency
345 Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative);
346 if (foundSign) {
347 if (AllowTrailingWhite)
348 pos = Int32.JumpOverWhite (pos, s, true);
349 if (AllowCurrencySymbol)
350 Int32. FindCurrency (ref pos, s, nfi, ref foundCurrency);
354 if (AllowCurrencySymbol && !foundCurrency) {
355 // Currency + sign
356 Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency);
357 if (foundCurrency) {
358 if (AllowTrailingWhite)
359 pos = Int32.JumpOverWhite (pos, s, true);
360 if (!foundSign && AllowTrailingSign)
361 Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative);
365 if (AllowTrailingWhite && pos < s.Length)
366 pos = Int32.JumpOverWhite (pos, s, false);
368 if (foundOpenParentheses) {
369 if (pos >= s.Length || s [pos++] != ')')
370 if (tryParse)
371 return false;
372 else
373 throw new FormatException (Locale.GetText
374 ("Input string was not in the correct format."));
375 if (AllowTrailingWhite && pos < s.Length)
376 pos = Int32.JumpOverWhite (pos, s, false);
379 if (pos < s.Length && s [pos] != '\u0000')
380 if (tryParse)
381 return false;
382 else
383 throw new FormatException (Locale.GetText ("Input string was not in the correct format."));
385 // -0 is legal but other negative values are not
386 if (negative && (number > 0)) {
387 if (tryParse)
388 return false;
389 else
390 throw new OverflowException (
391 Locale.GetText ("Negative number"));
394 result = number;
396 return true;
399 [CLSCompliant (false)]
400 public static uint Parse (string s) {
401 uint res;
403 Parse (s, false, out res);
405 return res;
408 [CLSCompliant (false)]
409 public static uint Parse (string s, NumberStyles style, IFormatProvider fp) {
410 uint res;
412 Parse (s, style, fp, false, out res);
414 return res;
417 [CLSCompliant (false)]
418 public static uint Parse (string s, IFormatProvider provider)
420 return Parse (s, NumberStyles.Integer, provider);
423 [CLSCompliant (false)]
424 public static uint Parse (string s, NumberStyles style)
426 return Parse (s, style, null);
429 #if NET_2_0
430 [CLSCompliant (false)]
431 public static bool TryParse (string s, out uint result) {
432 try {
433 return Parse (s, true, out result);
435 catch (Exception) {
436 result = 0;
437 return false;
441 [CLSCompliant (false)]
442 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out uint result) {
443 try {
444 return Parse (s, style, provider, true, out result);
446 catch (Exception) {
447 result = 0;
448 return false;
451 #endif
453 public override string ToString ()
455 return ToString (null, null);
458 public string ToString (IFormatProvider fp)
460 return ToString (null, fp);
463 public string ToString (string format)
465 return ToString (format, null);
468 public string ToString (string format, IFormatProvider fp)
470 NumberFormatInfo nfi = NumberFormatInfo.GetInstance (fp);
472 // use "G" when format is null or String.Empty
473 if ((format == null) || (format.Length == 0))
474 format = "G";
476 return IntegerFormatter.NumberToString (format, nfi, m_value);
479 // =========== IConvertible Methods =========== //
480 public TypeCode GetTypeCode ()
482 return TypeCode.UInt32;
485 bool IConvertible.ToBoolean (IFormatProvider provider)
487 return System.Convert.ToBoolean (m_value);
490 byte IConvertible.ToByte (IFormatProvider provider)
492 return System.Convert.ToByte (m_value);
495 char IConvertible.ToChar (IFormatProvider provider)
497 return System.Convert.ToChar (m_value);
500 DateTime IConvertible.ToDateTime (IFormatProvider provider)
502 return System.Convert.ToDateTime (m_value);
505 decimal IConvertible.ToDecimal (IFormatProvider provider)
507 return System.Convert.ToDecimal (m_value);
510 double IConvertible.ToDouble (IFormatProvider provider)
512 return System.Convert.ToDouble (m_value);
515 short IConvertible.ToInt16 (IFormatProvider provider)
517 return System.Convert.ToInt16 (m_value);
520 int IConvertible.ToInt32 (IFormatProvider provider)
522 return System.Convert.ToInt32 (m_value);
525 long IConvertible.ToInt64 (IFormatProvider provider)
527 return System.Convert.ToInt64 (m_value);
530 sbyte IConvertible.ToSByte (IFormatProvider provider)
532 return System.Convert.ToSByte (m_value);
535 float IConvertible.ToSingle (IFormatProvider provider)
537 return System.Convert.ToSingle (m_value);
540 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
542 return System.Convert.ToType (m_value, conversionType, provider);
545 ushort IConvertible.ToUInt16 (IFormatProvider provider)
547 return System.Convert.ToUInt16 (m_value);
550 uint IConvertible.ToUInt32 (IFormatProvider provider)
552 return m_value;
555 ulong IConvertible.ToUInt64 (IFormatProvider provider)
557 return System.Convert.ToUInt64 (m_value);