Merge 12735:15568 from xmlbuildsystem branch
[reactos.git] / irc / TechBot / TechBot.Library / NumberParser.cs
1 using System;
2 using System.Globalization;
3
4 namespace TechBot.Library
5 {
6 public class NumberParser
7 {
8 public bool Error = false;
9
10 private const string SpecialHexCharacters = "ABCDEF";
11
12 private bool IsSpecialHexCharacter(char ch)
13 {
14 foreach (char specialChar in SpecialHexCharacters)
15 {
16 if (ch.ToString().ToUpper() == specialChar.ToString())
17 return true;
18 }
19 return false;
20 }
21
22 private bool HasSpecialHexCharacters(string s)
23 {
24 foreach (char ch in s)
25 {
26 if (IsSpecialHexCharacter(ch))
27 return true;
28 }
29 return false;
30 }
31
32 public long Parse(string s)
33 {
34 try
35 {
36 Error = false;
37 bool useHex = false;
38 if (s.StartsWith("0x"))
39 {
40 s = s.Substring(2);
41 useHex = true;
42 }
43 if (HasSpecialHexCharacters(s))
44 useHex = true;
45 if (useHex)
46 return Int64.Parse(s,
47 NumberStyles.HexNumber);
48 else
49 return Int64.Parse(s);
50 }
51 catch (FormatException)
52 {
53 Error = true;
54 }
55 catch (OverflowException)
56 {
57 Error = true;
58 }
59 return -1;
60 }
61 }
62 }