/
Constructors_NumberP.cs
275 lines (245 loc) · 8.54 KB
/
Constructors_NumberP.cs
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
using System;
using System.Globalization;
using System.Linq;
namespace FlexibleParser
{
///<summary>
///<para>NumberP is the only NumberX class parsing string inputs.</para>
///<para>It is implicitly convertible to Number, NumberD, NumberO and string.</para>
///</summary>
public partial class NumberP
{
///<summary><para>Readonly primary value under the current conditions.</para></summary>
public readonly dynamic Value;
///<summary><para>Readonly Base-ten exponent under the current conditions.</para></summary>
public readonly int BaseTenExponent;
///<summary><para>Readonly string variable including the original input to be parsed.</para></summary>
public readonly string OriginalString;
///<summary><para>Readonly ParseConfig variable defining the current parsing configuration.</para></summary>
public readonly ParseConfig Config = new ParseConfig();
///<summary><para>Readonly member of the ErrorTypesNumber enum which best suits the current conditions.</para></summary>
public readonly ErrorTypesNumber Error;
///<summary><para>Initialises a new NumberP instance.</para></summary>
///<param name="input">String variable whose contents will be parsed.</param>
public NumberP(string input) : this (input, new ParseConfig()) { }
///<summary><para>Initialises a new NumberP instance.</para></summary>
///<param name="input">String variable whose contents will be parsed.</param>
///<param name="config">ParseConfig variable whose information will be used.</param>
public NumberP(string input, ParseConfig config)
{
if (input == null || input.Trim().Length < 1)
{
Error = ErrorTypesNumber.InvalidInput;
return;
}
OriginalString = input;
Config = new ParseConfig(config);
//NumberD is lighter than NumberP and contains all what matters here (i.e., dynamic Value and BaseTenExponent).
NumberD tempVar = StartParse(new ParseInfo(this));
if (tempVar.Error != ErrorTypesNumber.None)
{
Error = tempVar.Error;
}
else
{
BaseTenExponent = tempVar.BaseTenExponent;
Value = tempVar.Value;
}
}
///<summary><para>Initialises a new NumberP instance.</para></summary>
///<param name="numberP">NumberP variable whose information will be used.</param>
public NumberP(NumberP numberP)
{
if (numberP == null)
{
Error = ErrorTypesNumber.InvalidInput;
}
else
{
BaseTenExponent = numberP.BaseTenExponent;
Value = numberP.Value;
OriginalString = numberP.OriginalString;
Config = new ParseConfig(numberP.Config);
Error = numberP.Error;
}
}
///<summary><para>Initialises a new NumberP instance.</para></summary>
///<param name="number">Number variable whose information will be used.</param>
public NumberP(Number number)
{
Number tempVar = Common.ExtractDynamicToNumber(number);
if (tempVar.Error != ErrorTypesNumber.None)
{
Error = tempVar.Error;
}
else
{
BaseTenExponent = number.BaseTenExponent;
Value = number.Value;
Config = new ParseConfig(typeof(decimal));
}
}
///<summary><para>Initialises a new NumberP instance.</para></summary>
///<param name="numberD">NumberD variable whose information will be used.</param>
public NumberP(NumberD numberD)
{
NumberD tempVar = Common.ExtractDynamicToNumberD(numberD);
if (tempVar.Error != ErrorTypesNumber.None)
{
Error = tempVar.Error;
}
else
{
BaseTenExponent = numberD.BaseTenExponent;
Value = numberD.Value;
Config = new ParseConfig(numberD.Type);
}
}
///<summary><para>Initialises a new NumberP instance.</para></summary>
///<param name="numberO">NumberO variable whose information will be used.</param>
public NumberP(NumberO numberO)
{
Number tempVar = Common.ExtractDynamicToNumber(numberO);
if (tempVar.Error != ErrorTypesNumber.None)
{
Error = tempVar.Error;
}
else
{
BaseTenExponent = numberO.BaseTenExponent;
Value = numberO.Value;
Config = new ParseConfig(typeof(decimal));
}
}
internal NumberP(Number number, string originalString, ParseConfig config)
{
Number tempVar = Common.ExtractDynamicToNumber(number);
if (tempVar.Error != ErrorTypesNumber.None)
{
Error = tempVar.Error;
}
else
{
BaseTenExponent = number.BaseTenExponent;
Value = number.Value;
OriginalString = originalString;
Config = new ParseConfig(config);
}
}
internal NumberP(dynamic value, int baseTenExponent)
{
BaseTenExponent = baseTenExponent;
Value = value;
}
internal NumberP(ErrorTypesNumber error) { Error = error; }
}
///<summary>
///<para>ParseConfig defines the way in which the ParseConfig parsing actions are being performed.</para>
///</summary>
public class ParseConfig
{
///<summary><para>CultureInfo variable to be used while parsing. Its default value is CultureInfo.InvariantCulture.</para></summary>
public CultureInfo Culture { get; set; }
///<summary><para>Member of the NumberStyles enum to be used while parsing. Its default value is NumberStyles.Any.</para></summary>
public NumberStyles NumberStyle { get; set; }
///<summary><para>Member of the ParseTypes enum defining the parsing type. Its default value is ParseTypes.ParseAll.</para></summary>
public ParseTypes ParseType { get; set; }
///<summary><para>Numeric type targeted by the parsing actions. Its default value is decimal.</para></summary>
public readonly Type Target;
///<summary>
///<para>Outputs the information in all the public fields (one per line).</para>
///</summary>
public override string ToString()
{
string output = "Culture: " + Culture.DisplayName + Environment.NewLine;
output += "Style: " + NumberStyle.ToString() + Environment.NewLine;
output += "ParseType: " + ParseType.ToString() + Environment.NewLine;
output += "Target: " + (Target != null ? Target.ToString() : "nothing");
return output;
}
///<summary><para>Initialises a new ParseConfig instance.</para></summary>
public ParseConfig()
{
Culture = CultureInfo.InvariantCulture;
NumberStyle = NumberStyles.Any;
ParseType = ParseTypes.ParseAll;
Target = typeof(decimal);
}
///<summary><para>Initialises a new ParseConfig instance.</para></summary>
///<param name="target">Type variable defining the numeric type targeted by the parsing actions.</param>
public ParseConfig(Type target)
{
Culture = CultureInfo.InvariantCulture;
NumberStyle = NumberStyles.Any;
ParseType = ParseTypes.ParseAll;
Target =
(
Basic.AllNumericTypes.Contains(target) ?
target : typeof(decimal)
);
}
///<summary><para>Initialises a new ParseConfig instance.</para></summary>
///<param name="config">ParseConfig variable whose information will be used.</param>
public ParseConfig(ParseConfig config)
{
if (config == null)
{
config = new ParseConfig();
Culture = config.Culture;
NumberStyle = config.NumberStyle;
ParseType = config.ParseType;
Target = config.Target;
return;
}
Culture = new CultureInfo
(
(
config.Culture != null ? config.Culture :
CultureInfo.InvariantCulture
)
.LCID
);
NumberStyle = config.NumberStyle;
ParseType = config.ParseType;
Target =
(
config.Target != null ? config.Target : typeof(decimal)
);
}
///<summary><para>Determines whether two ParseConfig variables are equal.</para></summary>
///<param name="first">First operand.</param>
///<param name="second">Second operand.</param>
public static bool operator ==(ParseConfig first, ParseConfig second)
{
return Operations.NoNullEquals(first, second);
}
///<summary><para>Determines whether two ParseConfig variables are different.</para></summary>
///<param name="first">First operand.</param>
///<param name="second">Second operand.</param>
public static bool operator !=(ParseConfig first, ParseConfig second)
{
return !Operations.NoNullEquals(first, second);
}
///<summary><para>Determines whether the current ParseConfig variable is equal to other one.</para></summary>
///<param name="other">Other variable.</param>
public bool Equals(ParseConfig other)
{
return
(
object.Equals(other, null) ? false :
Operations.ParseConfigsAreEqual(this, other)
);
}
///<summary><para>Determines whether the current ParseConfig variable is equal to other one.</para></summary>
///<param name="obj">Other variable.</param>
public override bool Equals(object obj)
{
return Equals(obj as ParseConfig);
}
///<summary><para>Returns the hash code for this ParseConfig variable.</para></summary>
public override int GetHashCode()
{
return 0;
}
}
}