File: NumberHelper.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (221 lines) | stat: -rw-r--r-- 8,469 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// NumberHelper.cs
// Author: Sergey Chaban (serge@wildwestsoftware.com)

using System;
using System.Text;
using System.Globalization;

namespace Mono.ILASM {

        /// <summary>
        /// </summary>
        internal class NumberHelper : StringHelperBase {

                private ILToken result;

                /// <summary>
                /// </summary>
                /// <param name="host"></param>
                public NumberHelper (ILTokenizer host) : base (host)
                {
                        Reset ();
                }


                private void Reset ()
                {
                        result = ILToken.Invalid.Clone() as ILToken;
                }

                /// <summary>
                /// </summary>
                /// <returns></returns>
                public override bool Start (char ch)
                {
                        bool res = (Char.IsDigit (ch) || ch == '-' || (ch == '.' && Char.IsDigit ((char) host.Reader.Peek ())));
                        Reset ();
                        return res;
                }

                bool is_hex (int e)
                {
                        return (e >= '0' && e <= '9') || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f');
                }

                bool is_sign (int ch)
                {
                        return ((ch == '+') || (ch == '-'));
                }

                bool is_e (int ch)
                {
                        return ((ch == 'e') || (ch == 'E'));
                }

                /// <summary>
                /// </summary>
                /// <returns></returns>
                public override string Build ()
                {
                        ILReader reader = host.Reader;
                        reader.MarkLocation ();
                        StringBuilder num_builder = new StringBuilder ();
                        string num;
                        int ch;
                        int peek;
                        bool is_real = false;
                        bool dec_found = false;

                        NumberStyles nstyles = NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint |
                                NumberStyles.AllowLeadingSign;

                        ch = reader.Read ();
                        peek = reader.Peek ();
                        reader.Unread (ch);

                        if (ch == '0' && (peek == 'x' || peek == 'X'))
                                return BuildHex ();

                        if (is_sign (reader.Peek ()))
                                num_builder.Append ((char) reader.Read ());

                        do {
                                ch = reader.Read ();
                                peek = reader.Peek ();
                                num_builder.Append ((char) ch);

                                if (is_e (ch)) {
                                        if (is_real)
                                                throw new ILTokenizingException (reader.Location, num_builder.ToString ());

                                        is_real = true;
                                }
                                if (ch == '.')
                                        dec_found = true;
                                if (!is_hex(peek) &&
                                    !(peek == '.' && !dec_found) && !is_e (peek) &&
                                    !(is_sign (peek) && is_real)) {
                                        break;
                                }
                        } while (ch != -1);

                        num = num_builder.ToString ();

                        // Check for hexbytes
                        if (num.Length == 2) {
                                if (Char.IsLetter (num[0]) || Char.IsLetter (num[1])) {
                                        result.token = Token.HEXBYTE;
                                        result.val = Byte.Parse (num, NumberStyles.HexNumber);
                                        return num;
                                }
                        }

                        if (ch == '.' && peek == '.') {
                                num = num.Substring (0, num.Length-1);
                                reader.Unread ('.');
                                dec_found = false;
                        } else if (ch == '.') {
                                num += '0';
                        }

                        if (!dec_found && !is_real) {        
                                try { 
                                        long i = Int64.Parse (num, nstyles);
                                        result.token = Token.INT64;
                                        result.val = i;

                                        return num;
                                } catch {
                                }

				try {
					long i = (long) UInt64.Parse (num, nstyles);
                                        result.token = Token.INT64;
                                        result.val = i;

					return num;
				} catch {
				}
                        }

                        try {
                                double d = Double.Parse (num, nstyles, NumberFormatInfo.InvariantInfo);
                                result.token = Token.FLOAT64;
                                result.val = d;
                        } catch {
                                reader.Unread (num.ToCharArray ());
                                reader.RestoreLocation ();
                                num = String.Empty;
                                Reset ();
                                throw new ILTokenizingException (reader.Location, num_builder.ToString ());
                        }
                        return num;
                }

                public string BuildHex ()
                {
                        ILReader reader = host.Reader;
                        reader.MarkLocation ();
                        StringBuilder num_builder = new StringBuilder ();
                        NumberStyles nstyles = NumberStyles.HexNumber;

                        string num;
                        int ch;
                        int peek;

                        ch = reader.Read ();
                        if (ch != '0')
                                throw new ILTokenizingException (reader.Location, ((char) ch).ToString ());

                        ch = reader.Read ();

                        if (ch != 'x' && ch != 'X')
                                throw new ILTokenizingException (reader.Location, "0" + (char) ch);

                        do {
                                ch = reader.Read ();
                                peek = reader.Peek ();
                                num_builder.Append ((char) ch);

                                if (!is_hex ((char) peek))
                                        break;

                                if (num_builder.Length == 32)
                                        throw new ILTokenizingException (reader.Location, num_builder.ToString ());

                        } while (ch != -1);

                        num = num_builder.ToString ();

                        try {
                                long i = (long) UInt64.Parse (num, nstyles);
                                //if (i < Int32.MinValue || i > Int32.MaxValue) {
                                        result.token = Token.INT64;
                                        result.val = i;
                                //} else {
                                //        result.token = Token.INT32;
                                //        result.val = (int) i;
                                //}
                        } catch {
                                string tnum = num;
                                reader.Unread (num.ToCharArray ());
                                reader.RestoreLocation ();
                                num = String.Empty;
                                Reset ();
                                throw new ILTokenizingException (reader.Location, tnum);
                        }
                        return num;
                }

                /// <summary>
                /// </summary>
                public ILToken ResultToken {
                        get {
                                return result;
                        }
                }


        }

}