5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Globalization;
32 using System.Threading;
37 public struct Int64 : IComparable, IFormattable, IConvertible {
39 public const long MaxValue = 0x7fffffffffffffff;
40 public const long MinValue = -9223372036854775808;
42 internal long m_value;
44 public int CompareTo (object v)
49 if (!(v is System.Int64))
50 throw new ArgumentException (Locale.GetText ("Value is not a System.Int64"));
52 if (m_value == (long) v)
55 if (m_value < (long) v)
61 public override bool Equals (object o)
63 if (!(o is System.Int64))
66 return ((long) o) == m_value;
69 public override int GetHashCode ()
71 return (int)(m_value & 0xffffffff) ^ (int)(m_value >> 32);
74 public static long Parse (string s)
80 bool digits_seen = false;
83 throw new ArgumentNullException ("s");
88 for (i = 0; i < len; i++){
90 if (!Char.IsWhiteSpace (c))
95 throw new FormatException ();
105 for (; i < len; i++){
108 if (c >= '0' && c <= '9'){
109 val = checked (val * 10 + (c - '0') * sign);
112 if (Char.IsWhiteSpace (c)){
113 for (i++; i < len; i++){
114 if (!Char.IsWhiteSpace (s [i]))
115 throw new FormatException ();
119 throw new FormatException ();
123 throw new FormatException ();
128 public static long Parse (string s, IFormatProvider fp)
130 return Parse (s, NumberStyles.Integer, fp);
133 public static long Parse (string s, NumberStyles style)
135 return Parse (s, style, null);
138 public static long Parse (string s, NumberStyles style, IFormatProvider fp)
141 throw new ArgumentNullException ();
144 throw new FormatException ("Input string was not " +
145 "in the correct format: s.Length==0.");
147 NumberFormatInfo nfi;
149 Type typeNFI = typeof (System.Globalization.NumberFormatInfo);
150 nfi = (NumberFormatInfo) fp.GetFormat (typeNFI);
153 nfi = Thread.CurrentThread.CurrentCulture.NumberFormat;
155 Int32.CheckStyle (style);
157 bool AllowCurrencySymbol = (style & NumberStyles.AllowCurrencySymbol) != 0;
158 bool AllowExponent = (style & NumberStyles.AllowExponent) != 0;
159 bool AllowHexSpecifier = (style & NumberStyles.AllowHexSpecifier) != 0;
160 bool AllowThousands = (style & NumberStyles.AllowThousands) != 0;
161 bool AllowDecimalPoint = (style & NumberStyles.AllowDecimalPoint) != 0;
162 bool AllowParentheses = (style & NumberStyles.AllowParentheses) != 0;
163 bool AllowTrailingSign = (style & NumberStyles.AllowTrailingSign) != 0;
164 bool AllowLeadingSign = (style & NumberStyles.AllowLeadingSign) != 0;
165 bool AllowTrailingWhite = (style & NumberStyles.AllowTrailingWhite) != 0;
166 bool AllowLeadingWhite = (style & NumberStyles.AllowLeadingWhite) != 0;
170 if (AllowLeadingWhite)
171 pos = Int32.JumpOverWhite (pos, s, true);
173 bool foundOpenParentheses = false;
174 bool negative = false;
175 bool foundSign = false;
176 bool foundCurrency = false;
179 if (AllowParentheses && s [pos] == '(') {
180 foundOpenParentheses = true;
182 negative = true; // MS always make the number negative when there parentheses
183 // even when NumberFormatInfo.NumberNegativePattern != 0!!!
185 if (AllowLeadingWhite)
186 pos = Int32.JumpOverWhite (pos, s, true);
188 if (s.Substring (pos, nfi.NegativeSign.Length) == nfi.NegativeSign)
189 throw new FormatException ("Input string was not in the correct " +
190 "format: Has Negative Sign.");
191 if (s.Substring (pos, nfi.PositiveSign.Length) == nfi.PositiveSign)
192 throw new FormatException ("Input string was not in the correct " +
193 "format: Has Positive Sign.");
196 if (AllowLeadingSign && !foundSign) {
198 Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative);
200 if (AllowLeadingWhite)
201 pos = Int32.JumpOverWhite (pos, s, true);
202 if (AllowCurrencySymbol) {
203 Int32.FindCurrency (ref pos, s, nfi,
205 if (foundCurrency && AllowLeadingWhite)
206 pos = Int32.JumpOverWhite (pos, s, true);
211 if (AllowCurrencySymbol && !foundCurrency) {
213 Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency);
215 if (AllowLeadingWhite)
216 pos = Int32.JumpOverWhite (pos, s, true);
218 if (!foundSign && AllowLeadingSign) {
219 Int32.FindSign (ref pos, s, nfi, ref foundSign,
221 if (foundSign && AllowLeadingWhite)
222 pos = Int32.JumpOverWhite (pos, s, true);
230 bool decimalPointFound = false;
237 if (!Int32.ValidDigit (s [pos], AllowHexSpecifier)) {
238 if (AllowThousands &&
239 (Int32.FindOther (ref pos, s, nfi.NumberGroupSeparator)
240 || Int32.FindOther (ref pos, s, nfi.CurrencyGroupSeparator)))
243 if (!decimalPointFound && AllowDecimalPoint &&
244 (Int32.FindOther (ref pos, s, nfi.NumberDecimalSeparator)
245 || Int32.FindOther (ref pos, s, nfi.CurrencyDecimalSeparator))) {
246 decimalPointFound = true;
252 else if (AllowHexSpecifier) {
254 hexDigit = s [pos++];
255 if (Char.IsDigit (hexDigit))
256 digitValue = (int) (hexDigit - '0');
257 else if (Char.IsLower (hexDigit))
258 digitValue = (int) (hexDigit - 'a' + 10);
260 digitValue = (int) (hexDigit - 'A' + 10);
262 ulong unumber = (ulong)number;
263 number = (long)checked(unumber * 16ul + (ulong)digitValue);
265 else if (decimalPointFound) {
267 // Allows decimal point as long as it's only
268 // followed by zeroes.
269 if (s [pos++] != '0')
270 throw new OverflowException ("Value too large or too " +
277 // Calculations done as negative
278 // (abs (MinValue) > abs (MaxValue))
281 (long) (s [pos++] - '0')
283 } catch (OverflowException) {
284 throw new OverflowException ("Value too large or too " +
288 } while (pos < s.Length);
292 throw new FormatException ("Input string was not in the correct format: nDigits == 0.");
294 if (AllowTrailingSign && !foundSign) {
296 Int32.FindSign (ref pos, s, nfi, ref foundSign, ref negative);
298 if (AllowTrailingWhite)
299 pos = Int32.JumpOverWhite (pos, s, true);
300 if (AllowCurrencySymbol)
301 Int32.FindCurrency (ref pos, s, nfi,
306 if (AllowCurrencySymbol && !foundCurrency) {
308 if (nfi.CurrencyPositivePattern == 3 && s[pos++] != ' ')
309 throw new FormatException ("Input string was not in the correct format: no space between number and currency symbol.");
311 Int32.FindCurrency (ref pos, s, nfi, ref foundCurrency);
312 if (foundCurrency && pos < s.Length) {
313 if (AllowTrailingWhite)
314 pos = Int32.JumpOverWhite (pos, s, true);
315 if (!foundSign && AllowTrailingSign)
316 Int32.FindSign (ref pos, s, nfi, ref foundSign,
321 if (AllowTrailingWhite && pos < s.Length)
322 pos = Int32.JumpOverWhite (pos, s, false);
324 if (foundOpenParentheses) {
325 if (pos >= s.Length || s [pos++] != ')')
326 throw new FormatException ("Input string was not in the correct " +
327 "format: No room for close parens.");
328 if (AllowTrailingWhite && pos < s.Length)
329 pos = Int32.JumpOverWhite (pos, s, false);
332 if (pos < s.Length && s [pos] != '\u0000')
333 throw new FormatException ("Input string was not in the correct format: Did not parse entire string. pos = "
334 + pos + " s.Length = " + s.Length);
337 if (!negative && !AllowHexSpecifier)
343 public override string ToString ()
345 return ToString (null, null);
348 public string ToString (IFormatProvider fp)
350 return ToString (null, fp);
353 public string ToString (string format)
355 return ToString (format, null);
358 public string ToString (string format, IFormatProvider fp)
360 NumberFormatInfo nfi = NumberFormatInfo.GetInstance( fp );
362 // use "G" when format is null or String.Empty
363 if ((format == null) || (format.Length == 0))
366 return IntegerFormatter.NumberToString (format, nfi, m_value);
369 // =========== IConvertible Methods =========== //
371 public TypeCode GetTypeCode ()
373 return TypeCode.Int64;
376 bool IConvertible.ToBoolean (IFormatProvider provider)
378 return System.Convert.ToBoolean (m_value);
381 byte IConvertible.ToByte (IFormatProvider provider)
383 return System.Convert.ToByte (m_value);
386 char IConvertible.ToChar (IFormatProvider provider)
388 return System.Convert.ToChar (m_value);
391 DateTime IConvertible.ToDateTime (IFormatProvider provider)
393 return System.Convert.ToDateTime (m_value);
396 decimal IConvertible.ToDecimal (IFormatProvider provider)
398 return System.Convert.ToDecimal (m_value);
401 double IConvertible.ToDouble (IFormatProvider provider)
403 return System.Convert.ToDouble (m_value);
406 short IConvertible.ToInt16 (IFormatProvider provider)
408 return System.Convert.ToInt16 (m_value);
411 int IConvertible.ToInt32 (IFormatProvider provider)
413 return System.Convert.ToInt32 (m_value);
416 long IConvertible.ToInt64 (IFormatProvider provider)
418 return System.Convert.ToInt64 (m_value);
421 sbyte IConvertible.ToSByte (IFormatProvider provider)
423 return System.Convert.ToSByte (m_value);
426 float IConvertible.ToSingle (IFormatProvider provider)
428 return System.Convert.ToSingle (m_value);
431 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
433 return System.Convert.ToType (m_value, conversionType, provider);
436 ushort IConvertible.ToUInt16 (IFormatProvider provider)
438 return System.Convert.ToUInt16 (m_value);
441 uint IConvertible.ToUInt32 (IFormatProvider provider)
443 return System.Convert.ToUInt32 (m_value);
446 ulong IConvertible.ToUInt64 (IFormatProvider provider)
448 return System.Convert.ToUInt64 (m_value);