| | | 1 | | using ExtendedNumerics; |
| | | 2 | | using System.Numerics; |
| | | 3 | | using System.Text; |
| | | 4 | | using System.Text.RegularExpressions; |
| | | 5 | | using ValidateLib.ErrorsAndWarnings.Errors; |
| | | 6 | | using ValidateLib.ErrorsAndWarnings.Warnings; |
| | | 7 | | using ValidateLib.Metadata.Descriptors; |
| | | 8 | | |
| | | 9 | | namespace ValidateLib.TabularData.Datatypes.NumericDatatypes |
| | | 10 | | { |
| | | 11 | | |
| | | 12 | | |
| | | 13 | | public abstract class NumericBaseDT : BaseDT, IValue |
| | | 14 | | { |
| | 1 | 15 | | protected float Percent { get; set; } = 1; |
| | 1 | 16 | | protected float Permile { get; set; } = 1; |
| | 1 | 17 | | protected BigDecimal Exponent { get; set; } = 1; |
| | 1 | 18 | | public BigDecimal Value { get; set; } |
| | | 19 | | |
| | 1 | 20 | | protected SpecialTypes SpecialType { get; set; } = SpecialTypes.Normal; |
| | | 21 | | |
| | 1 | 22 | | protected string? GroupChar { get; set; } = null; |
| | 1 | 23 | | protected string DecimalChar { get; set; } = "."; |
| | | 24 | | |
| | 1 | 25 | | public List<Warning> Warnings { get; set; } = new List<Warning>(); |
| | 1 | 26 | | public NumericBaseDT() { } |
| | 1 | 27 | | protected NumericBaseDT(string stringValue) : base(stringValue) |
| | | 28 | | { |
| | 1 | 29 | | ProcessPercenPermileExponent(); |
| | | 30 | | |
| | 1 | 31 | | } |
| | | 32 | | |
| | 1 | 33 | | protected NumericBaseDT(string strValue, FormatDescriptor format) : base(strValue, format) |
| | | 34 | | { |
| | 1 | 35 | | if (format is null) |
| | 0 | 36 | | ErrorFactory.ThrowInvalidFormatError("Format is null"); |
| | | 37 | | |
| | 1 | 38 | | GroupChar = format!.groupChar; |
| | 1 | 39 | | DecimalChar = format.decimalChar is null ? DecimalChar : format.decimalChar; |
| | | 40 | | |
| | 1 | 41 | | if (format.pattern is not null) |
| | | 42 | | { |
| | | 43 | | |
| | 1 | 44 | | ProcessValuePatternPresent(strValue, format); |
| | | 45 | | |
| | | 46 | | |
| | | 47 | | } |
| | | 48 | | else |
| | | 49 | | { |
| | 1 | 50 | | if (HandleSpecialTypes(strValue)) |
| | 1 | 51 | | return; |
| | | 52 | | |
| | 1 | 53 | | string pattern = $@"^[+-]?\d+(\{GroupChar}\d+)*(\{DecimalChar}\d+)?([Ee][+-]?\d+)?[%‰]?$"; |
| | 1 | 54 | | if (!Regex.IsMatch(strValue, pattern)) |
| | 1 | 55 | | ErrorFactory.ThrowDatatypeValidationError(strValue); |
| | | 56 | | |
| | 1 | 57 | | ProcessPercenPermileExponent(); |
| | | 58 | | |
| | 1 | 59 | | if (GroupChar is not null) |
| | 1 | 60 | | ProcessGroupChar(); |
| | 1 | 61 | | stringValue = stringValue.Replace(DecimalChar, "."); |
| | | 62 | | try |
| | | 63 | | { |
| | 1 | 64 | | Value = BigDecimal.Parse(stringValue); |
| | 1 | 65 | | } |
| | 0 | 66 | | catch (FormatException) |
| | | 67 | | { |
| | 0 | 68 | | ErrorFactory.ThrowDatatypeValidationError(); |
| | 0 | 69 | | } |
| | | 70 | | |
| | | 71 | | } |
| | 1 | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Here it is assumed that the format pattern has been checked for validity |
| | | 76 | | /// </summary> |
| | | 77 | | /// <param name="strValue"></param> |
| | | 78 | | /// <param name="format"></param> |
| | | 79 | | void ProcessValuePatternPresent(string strValue, FormatDescriptor format) |
| | | 80 | | { |
| | | 81 | | // set default values |
| | 1 | 82 | | if (GroupChar is null) |
| | 1 | 83 | | GroupChar = ","; |
| | 1 | 84 | | if (DecimalChar is null) |
| | 0 | 85 | | DecimalChar = "."; |
| | | 86 | | |
| | 1 | 87 | | string formatPattern = format.pattern!; |
| | 1 | 88 | | stringValue = strValue.ToUpper(); |
| | | 89 | | |
| | 1 | 90 | | string intPart = ""; |
| | 1 | 91 | | string? fractionPart = null; |
| | 1 | 92 | | string? exponentPart = null; |
| | | 93 | | |
| | 1 | 94 | | string[] exponentParts = formatPattern.Split('E'); |
| | 1 | 95 | | if (exponentParts.Count() == 2) |
| | 1 | 96 | | exponentPart = exponentParts[1]; |
| | 1 | 97 | | string[] fractionParts = exponentParts[0].Split(DecimalChar); |
| | 1 | 98 | | if (fractionParts.Count() == 2) |
| | 1 | 99 | | fractionPart = fractionParts[1]; |
| | 1 | 100 | | intPart = fractionParts[0]; |
| | | 101 | | |
| | | 102 | | |
| | 1 | 103 | | string regexPrefix = ""; |
| | 1 | 104 | | string regexSuffix = ""; |
| | | 105 | | |
| | 1 | 106 | | ProcessPrefixAndSuffix(ref formatPattern, ref regexPrefix, ref regexSuffix); |
| | | 107 | | |
| | 1 | 108 | | string regexIntPart = GetRegexForIntPart(intPart); |
| | 1 | 109 | | string regexFrationalPart = GetRegexForFractionalPart(fractionPart); |
| | 1 | 110 | | string regexExponentPart = GetRegexForExponentPart(exponentPart); |
| | | 111 | | |
| | 1 | 112 | | string completeRegex = $"^({regexPrefix}{regexIntPart}{regexFrationalPart}{regexExponentPart}{regexSuffix})$ |
| | | 113 | | |
| | 1 | 114 | | if (!Regex.IsMatch(strValue, completeRegex)) |
| | 1 | 115 | | ErrorFactory.ThrowDatatypeValidationError("Value not matching pattern"); |
| | | 116 | | |
| | 1 | 117 | | ProcessPercenPermileExponentPatternPresent(); |
| | | 118 | | |
| | 1 | 119 | | if (GroupChar is not null) |
| | 1 | 120 | | ProcessGroupChar(); |
| | 1 | 121 | | if (DecimalChar is not null) |
| | 1 | 122 | | ProcessDecimalChar(); |
| | | 123 | | |
| | | 124 | | try |
| | | 125 | | { |
| | 1 | 126 | | Value = BigDecimal.Parse(stringValue); |
| | 1 | 127 | | } |
| | 0 | 128 | | catch (FormatException) |
| | | 129 | | { |
| | 0 | 130 | | ErrorFactory.ThrowDatatypeValidationError(); |
| | 0 | 131 | | } |
| | | 132 | | |
| | 1 | 133 | | ResolvePercentPermileExponent(); |
| | | 134 | | |
| | 1 | 135 | | } |
| | | 136 | | |
| | | 137 | | string GetRegexForIntPart(string formatPatternIntPart) |
| | | 138 | | { |
| | 1 | 139 | | int primaryGroupingSizeIntPart = 0; |
| | 1 | 140 | | int secondaryGroupingSizeIntPart = 0; |
| | | 141 | | |
| | 1 | 142 | | GetPrimaryAndSecondaryGroupingSizes(formatPatternIntPart, GroupChar!, out primaryGroupingSizeIntPart, out se |
| | 1 | 143 | | int minDigitsCount = GetMinDigitsCount(formatPatternIntPart); |
| | | 144 | | |
| | | 145 | | |
| | 1 | 146 | | string prefixRegex = GetRegexForPrefix(formatPatternIntPart); |
| | 1 | 147 | | if (formatPatternIntPart[0] == '-' || formatPatternIntPart[0] == '+') formatPatternIntPart.Substring(1); |
| | 1 | 148 | | string digitsRegex = GetRegexForDigitsIntPart(formatPatternIntPart, minDigitsCount, GroupChar!, primaryGroup |
| | 1 | 149 | | return prefixRegex + digitsRegex; |
| | | 150 | | |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | string GetRegexForFractionalPart(string? formatPatternFractionalPart) |
| | | 154 | | { |
| | 1 | 155 | | if (formatPatternFractionalPart is null) return ""; |
| | 1 | 156 | | int primaryGroupingSizeFractionallPart = 0; |
| | 1 | 157 | | int secondaryGroupingSizeFractionalPart = 0; |
| | | 158 | | |
| | 1 | 159 | | GetPrimaryAndSecondaryGroupingSizes( |
| | 1 | 160 | | formatPatternFractionalPart, |
| | 1 | 161 | | GroupChar!, |
| | 1 | 162 | | out primaryGroupingSizeFractionallPart, |
| | 1 | 163 | | out secondaryGroupingSizeFractionalPart, |
| | 1 | 164 | | true); |
| | | 165 | | |
| | 1 | 166 | | int minDigitsCount = GetMinDigitsCount(formatPatternFractionalPart, reversed: true); |
| | | 167 | | |
| | 1 | 168 | | string digitsRegex = GetRegexForDigitsFractionPart(formatPatternFractionalPart, minDigitsCount, GroupChar!, |
| | | 169 | | |
| | 1 | 170 | | if (minDigitsCount == 0) return $"({Regex.Escape(DecimalChar) + digitsRegex})?"; |
| | 1 | 171 | | return Regex.Escape(DecimalChar) + digitsRegex; |
| | | 172 | | |
| | | 173 | | |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | string GetRegexForExponentPart(string? formatPatternExponentPart) |
| | | 177 | | { |
| | 1 | 178 | | if (formatPatternExponentPart is null) return ""; |
| | | 179 | | |
| | 1 | 180 | | string prefix = GetRegexForPrefix(formatPatternExponentPart); |
| | 1 | 181 | | int minDigits = formatPatternExponentPart.Count(c => c == '0'); |
| | 1 | 182 | | if (formatPatternExponentPart[0] == '-' || formatPatternExponentPart[0] == '+') formatPatternExponentPart.Su |
| | | 183 | | |
| | 1 | 184 | | return "E" + prefix + $"\\d{{{minDigits},}}"; |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | string GetRegexForDigitsIntPart(string formatPattern, int minDigits, string groupChar, int primaryGroupingSize = |
| | | 188 | | { |
| | 1 | 189 | | if (primaryGroupingSize == 0) return $"\\d{{{minDigits},}}"; |
| | 1 | 190 | | else if (secondaryGorupingSize == 0) |
| | | 191 | | { |
| | 1 | 192 | | int numberOfWholeGroups = minDigits / primaryGroupingSize; |
| | 1 | 193 | | int moduloWholeGroups = minDigits % primaryGroupingSize; |
| | | 194 | | |
| | 1 | 195 | | var regexBuilder = new StringBuilder(); |
| | | 196 | | |
| | 1 | 197 | | if (numberOfWholeGroups > 1) |
| | | 198 | | { |
| | 0 | 199 | | regexBuilder.Append($"({Regex.Escape(groupChar)}\\d{{{primaryGroupingSize},{primaryGroupingSize}}}){ |
| | | 200 | | } |
| | | 201 | | |
| | 1 | 202 | | if (moduloWholeGroups == 0) |
| | | 203 | | { |
| | 0 | 204 | | regexBuilder.Insert(0, $"\\d{{{primaryGroupingSize},{primaryGroupingSize}}}"); |
| | 0 | 205 | | regexBuilder.Insert(0, $"(\\d{{{primaryGroupingSize},{primaryGroupingSize}}}{Regex.Escape(groupChar) |
| | 0 | 206 | | regexBuilder.Insert(0, $"(\\d{{{1},{primaryGroupingSize}}}{Regex.Escape(groupChar)})?"); |
| | | 207 | | } |
| | | 208 | | else |
| | | 209 | | { |
| | 1 | 210 | | regexBuilder.Insert(0, $"\\d{{{moduloWholeGroups},{moduloWholeGroups}}}"); |
| | 1 | 211 | | regexBuilder.Insert(0, $"((\\d{{{1},{primaryGroupingSize}}}{Regex.Escape(groupChar)})?" + |
| | 1 | 212 | | $"(" + |
| | 1 | 213 | | $"(\\d{{{primaryGroupingSize},{primaryGroupingSize}}}{Regex.Escape(groupChar)})*" + |
| | 1 | 214 | | $"\\d{{{primaryGroupingSize - moduloWholeGroups},{primaryGroupingSize - moduloWholeGroups}}})|" |
| | 1 | 215 | | $"\\d{{{0},{primaryGroupingSize - moduloWholeGroups}}})"); |
| | | 216 | | } |
| | | 217 | | |
| | 1 | 218 | | return regexBuilder.ToString(); |
| | | 219 | | |
| | | 220 | | } |
| | | 221 | | else |
| | | 222 | | { |
| | | 223 | | string regex = ""; |
| | 1 | 224 | | var regexBuilder = new StringBuilder(); |
| | 1 | 225 | | if (primaryGroupingSize >= minDigits) |
| | | 226 | | { |
| | 1 | 227 | | if (primaryGroupingSize == minDigits) |
| | | 228 | | { |
| | 1 | 229 | | regexBuilder.Append($"\\d{{{primaryGroupingSize},{primaryGroupingSize}}}"); |
| | 1 | 230 | | regexBuilder.Insert(0, $"(\\d{{{secondaryGorupingSize},{secondaryGorupingSize}}}{Regex.Escape(gr |
| | 1 | 231 | | regexBuilder.Insert(0, $"(\\d{{{1},{secondaryGorupingSize}}}{Regex.Escape(groupChar)})?"); |
| | | 232 | | } |
| | | 233 | | else |
| | | 234 | | { |
| | 1 | 235 | | regexBuilder.Append($"\\d{{{minDigits},{minDigits}}}"); |
| | 1 | 236 | | regexBuilder.Insert(0, $"((\\d{{{1},{secondaryGorupingSize}}}{Regex.Escape(groupChar)})?((\\d{{{ |
| | | 237 | | } |
| | | 238 | | } |
| | | 239 | | else |
| | | 240 | | { |
| | 1 | 241 | | regex = $"\\d{{{primaryGroupingSize},{primaryGroupingSize}}}"; |
| | 1 | 242 | | regexBuilder.Append($"\\d{{{primaryGroupingSize},{primaryGroupingSize}}}"); |
| | | 243 | | |
| | | 244 | | |
| | 1 | 245 | | minDigits -= primaryGroupingSize; |
| | 1 | 246 | | if (minDigits > 0) |
| | | 247 | | { |
| | 1 | 248 | | regexBuilder.Insert(0, $"{Regex.Escape(groupChar)}"); |
| | | 249 | | } |
| | | 250 | | |
| | | 251 | | |
| | 1 | 252 | | int numberOfWholeGroups = minDigits / secondaryGorupingSize; |
| | 1 | 253 | | int moduloWholeGorups = minDigits % secondaryGorupingSize; |
| | | 254 | | |
| | 1 | 255 | | if (numberOfWholeGroups > 1) |
| | | 256 | | { |
| | 0 | 257 | | regexBuilder.Insert(0, $"({Regex.Escape(groupChar)}\\d{{{secondaryGorupingSize},{secondaryGorupi |
| | | 258 | | } |
| | 1 | 259 | | if (moduloWholeGorups == 0) |
| | | 260 | | { |
| | | 261 | | |
| | 1 | 262 | | regexBuilder.Insert(0, $"\\d{{{secondaryGorupingSize},{secondaryGorupingSize}}}"); |
| | 1 | 263 | | regexBuilder.Insert(0, $"(\\d{{{secondaryGorupingSize},{secondaryGorupingSize}}}{Regex.Escape(gr |
| | 1 | 264 | | regexBuilder.Insert(0, $"(\\d{{{1},{secondaryGorupingSize}}}{Regex.Escape(groupChar)})?"); |
| | | 265 | | } |
| | | 266 | | else |
| | | 267 | | { |
| | 1 | 268 | | regexBuilder.Insert(0, $"\\d{{{moduloWholeGorups},{moduloWholeGorups}}}"); |
| | 1 | 269 | | regexBuilder.Insert(0, $"((\\d{{{1},{secondaryGorupingSize}}}{Regex.Escape(groupChar)})?((\\d{{{ |
| | | 270 | | } |
| | | 271 | | |
| | | 272 | | |
| | | 273 | | } |
| | 1 | 274 | | return regexBuilder.ToString(); |
| | | 275 | | } |
| | | 276 | | } |
| | | 277 | | |
| | | 278 | | string GetRegexForDigitsFractionPart(string formatPattern, int minDigits, string groupChar, int primaryGroupingS |
| | | 279 | | { |
| | 1 | 280 | | if (minDigits == 0) minDigits = 1; |
| | | 281 | | |
| | 1 | 282 | | var regexBuilder = new StringBuilder(); |
| | 1 | 283 | | int maxDigitsCount = formatPattern.Count(c => c == '#' || c == '0'); |
| | | 284 | | |
| | 1 | 285 | | if (primaryGroupingSize == 0) |
| | | 286 | | { |
| | 1 | 287 | | regexBuilder.Append($"\\d{{{minDigits},{maxDigitsCount}}}"); |
| | | 288 | | } |
| | 1 | 289 | | else if (secondaryGroupingSize == 0) |
| | | 290 | | { |
| | 1 | 291 | | int numberOfWholeGroups = minDigits / primaryGroupingSize; |
| | 1 | 292 | | int moduloWholeGroups = minDigits % primaryGroupingSize; |
| | | 293 | | |
| | 1 | 294 | | if (numberOfWholeGroups > 0) |
| | | 295 | | { |
| | 1 | 296 | | regexBuilder.Append($"\\d{{{primaryGroupingSize},{primaryGroupingSize}}}"); |
| | 1 | 297 | | if (numberOfWholeGroups > 1) |
| | 1 | 298 | | regexBuilder.Append($"({Regex.Escape(groupChar)}){{{numberOfWholeGroups - 1},{numberOfWholeGroup |
| | | 299 | | } |
| | | 300 | | |
| | 1 | 301 | | if (moduloWholeGroups == 0) |
| | | 302 | | { |
| | 1 | 303 | | regexBuilder.Append($"(\\d{{{primaryGroupingSize},{primaryGroupingSize}}})*"); |
| | 1 | 304 | | regexBuilder.Append($"({Regex.Escape(groupChar)}\\d{{{1},{primaryGroupingSize}}})?"); |
| | | 305 | | } |
| | | 306 | | else |
| | | 307 | | { |
| | 1 | 308 | | if (numberOfWholeGroups > 0) |
| | 1 | 309 | | regexBuilder.Append($"{Regex.Escape(groupChar)}"); |
| | 1 | 310 | | regexBuilder.Append($"\\d{{{moduloWholeGroups},{moduloWholeGroups}}}"); |
| | 1 | 311 | | regexBuilder.Append("("); |
| | | 312 | | |
| | 1 | 313 | | regexBuilder.Append("("); |
| | 1 | 314 | | regexBuilder.Append($"\\d{{{primaryGroupingSize - moduloWholeGroups},{primaryGroupingSize - moduloWh |
| | 1 | 315 | | regexBuilder.Append($"({Regex.Escape(groupChar)}\\d{{{primaryGroupingSize},{primaryGroupingSize}}})* |
| | 1 | 316 | | regexBuilder.Append($"({Regex.Escape(groupChar)}\\d{{{1},{primaryGroupingSize}}})?"); |
| | 1 | 317 | | regexBuilder.Append(")|"); |
| | | 318 | | |
| | 1 | 319 | | regexBuilder.Append("("); |
| | 1 | 320 | | regexBuilder.Append($"\\d{{{0},{primaryGroupingSize - moduloWholeGroups}}}"); |
| | 1 | 321 | | regexBuilder.Append(")"); |
| | | 322 | | |
| | 1 | 323 | | regexBuilder.Append(")"); |
| | | 324 | | } |
| | | 325 | | } |
| | | 326 | | else |
| | | 327 | | { |
| | 1 | 328 | | if (primaryGroupingSize >= minDigits) |
| | | 329 | | { |
| | 1 | 330 | | if (primaryGroupingSize == minDigits) |
| | | 331 | | { |
| | 1 | 332 | | regexBuilder.Append($"\\d{{{primaryGroupingSize},{primaryGroupingSize}}}"); |
| | 1 | 333 | | regexBuilder.Append($"({Regex.Escape(groupChar)}\\d{{{secondaryGroupingSize},{secondaryGroupingS |
| | 1 | 334 | | regexBuilder.Append($"({Regex.Escape(groupChar)}\\d{{{1},{secondaryGroupingSize}}})?"); |
| | | 335 | | } |
| | | 336 | | else |
| | | 337 | | { |
| | 1 | 338 | | regexBuilder.Append($"\\d{{{minDigits},{minDigits}}}"); |
| | 1 | 339 | | regexBuilder.Append("("); |
| | 1 | 340 | | regexBuilder.Append("("); |
| | 1 | 341 | | regexBuilder.Append($"\\d{{{0},{primaryGroupingSize - minDigits}}}"); |
| | 1 | 342 | | regexBuilder.Append(")|"); |
| | 1 | 343 | | regexBuilder.Append($"\\d{{{primaryGroupingSize - minDigits},{primaryGroupingSize - minDigits}}} |
| | 1 | 344 | | regexBuilder.Append($"({Regex.Escape(groupChar)}\\d{{{secondaryGroupingSize},{secondaryGroupingS |
| | 1 | 345 | | regexBuilder.Append($"({Regex.Escape(groupChar)}\\d{{{1},{secondaryGroupingSize}}})?"); |
| | 1 | 346 | | regexBuilder.Append(")"); |
| | | 347 | | } |
| | | 348 | | } |
| | | 349 | | else |
| | | 350 | | { |
| | 1 | 351 | | regexBuilder.Append($"\\d{{{primaryGroupingSize},{primaryGroupingSize}}}"); |
| | 1 | 352 | | minDigits -= primaryGroupingSize; |
| | 1 | 353 | | if (minDigits > 0) regexBuilder.Append($"{Regex.Escape(groupChar)}"); |
| | | 354 | | |
| | 1 | 355 | | int numberOfWholeGroups = minDigits / secondaryGroupingSize; |
| | 1 | 356 | | int moduloWholeGroups = minDigits % secondaryGroupingSize; |
| | | 357 | | |
| | 1 | 358 | | if (numberOfWholeGroups > 1) |
| | | 359 | | { |
| | 0 | 360 | | regexBuilder.Append($"(\\d{{{secondaryGroupingSize},{secondaryGroupingSize}}}{Regex.Escape(group |
| | | 361 | | } |
| | | 362 | | |
| | 1 | 363 | | if (moduloWholeGroups == 0) |
| | | 364 | | { |
| | 1 | 365 | | regexBuilder.Append($"\\d{{{secondaryGroupingSize},{secondaryGroupingSize}}}"); |
| | 1 | 366 | | regexBuilder.Append($"({Regex.Escape(groupChar)}\\d{{{secondaryGroupingSize},{secondaryGroupingS |
| | 1 | 367 | | regexBuilder.Append($"({Regex.Escape(groupChar)}\\d{{{1},{secondaryGroupingSize}}})?"); |
| | | 368 | | } |
| | | 369 | | else |
| | | 370 | | { |
| | 1 | 371 | | regexBuilder.Append($"\\d{{{moduloWholeGroups},{moduloWholeGroups}}}"); |
| | 1 | 372 | | regexBuilder.Append("("); |
| | 1 | 373 | | regexBuilder.Append("("); |
| | 1 | 374 | | regexBuilder.Append($"\\d{{{primaryGroupingSize - moduloWholeGroups},{secondaryGroupingSize - mo |
| | 1 | 375 | | regexBuilder.Append($"({Regex.Escape(groupChar)}\\d{{{secondaryGroupingSize},{secondaryGroupingS |
| | 1 | 376 | | regexBuilder.Append(")|"); |
| | 1 | 377 | | regexBuilder.Append($"\\d{{{0},{secondaryGroupingSize - moduloWholeGroups}}}"); |
| | 1 | 378 | | regexBuilder.Append($"({Regex.Escape(groupChar)}\\d{{{1},{secondaryGroupingSize}}})?"); |
| | 1 | 379 | | regexBuilder.Append(")"); |
| | | 380 | | } |
| | | 381 | | } |
| | | 382 | | } |
| | | 383 | | |
| | 1 | 384 | | return regexBuilder.ToString(); |
| | | 385 | | } |
| | | 386 | | |
| | | 387 | | |
| | | 388 | | string GetRegexForPrefix(string pattern) |
| | | 389 | | { |
| | 1 | 390 | | string regex = @"[+-]?"; |
| | 1 | 391 | | if (pattern[0] == '+') regex = @"[+]"; |
| | 1 | 392 | | if (pattern[0] == '-') regex = @"[-]"; |
| | | 393 | | |
| | 1 | 394 | | return regex; |
| | | 395 | | } |
| | | 396 | | |
| | | 397 | | int GetMinDigitsCount(string pattern, bool reversed = false) |
| | | 398 | | { |
| | 1 | 399 | | StringBuilder sb = new StringBuilder(); |
| | 1 | 400 | | foreach (var c in pattern) |
| | | 401 | | { |
| | 1 | 402 | | if (c == '0' || c == '#') sb.Append(c); |
| | | 403 | | } |
| | 1 | 404 | | var clearedString = sb.ToString(); |
| | | 405 | | |
| | 1 | 406 | | if (reversed) |
| | | 407 | | { |
| | 1 | 408 | | char[] charArray = clearedString.ToCharArray(); |
| | 1 | 409 | | Array.Reverse(charArray); |
| | 1 | 410 | | clearedString = new string(charArray); |
| | | 411 | | } |
| | | 412 | | |
| | 1 | 413 | | if (clearedString.IndexOf('0') != -1) |
| | 1 | 414 | | return clearedString.Length - clearedString.IndexOf('0'); |
| | 1 | 415 | | else return 0; |
| | | 416 | | |
| | | 417 | | } |
| | | 418 | | static void GetPrimaryAndSecondaryGroupingSizes(string formatPattern, string groupChar, out int primaryGroupingS |
| | | 419 | | { |
| | 1 | 420 | | primaryGroupingSize = 0; |
| | 1 | 421 | | secondaryGroupingSize = 0; |
| | | 422 | | |
| | 1 | 423 | | if (reversed) |
| | | 424 | | { |
| | 1 | 425 | | char[] charArray = formatPattern.ToCharArray(); |
| | 1 | 426 | | Array.Reverse(charArray); |
| | 1 | 427 | | formatPattern = new string(charArray); |
| | | 428 | | } |
| | 1 | 429 | | string[] formatParts = formatPattern.Split(groupChar); |
| | 1 | 430 | | if (formatParts.Count() >= 2) |
| | 1 | 431 | | primaryGroupingSize = formatParts[formatParts.Count() - 1].Length; |
| | 1 | 432 | | if (formatParts.Count() >= 3) |
| | 1 | 433 | | secondaryGroupingSize = formatParts[formatParts.Count() - 2].Length; |
| | 1 | 434 | | } |
| | | 435 | | |
| | 1 | 436 | | void ProcessGroupChar() => stringValue = stringValue.Replace(GroupChar!, ""); |
| | | 437 | | |
| | | 438 | | void ProcessDecimalChar() |
| | | 439 | | { |
| | 1 | 440 | | if (DecimalChar != ".") |
| | 0 | 441 | | stringValue = stringValue.Replace(DecimalChar, "."); |
| | 1 | 442 | | } |
| | | 443 | | |
| | | 444 | | void ProcessPrefixAndSuffix(ref string formatPattern, ref string regexPrefix, ref string regexSuffix) |
| | | 445 | | { |
| | 1 | 446 | | if (formatPattern[0] == '%') |
| | | 447 | | { |
| | 1 | 448 | | regexPrefix = "%"; |
| | 1 | 449 | | formatPattern = formatPattern.Substring(1); |
| | | 450 | | } |
| | | 451 | | |
| | 1 | 452 | | if (formatPattern[0] == '‰') |
| | | 453 | | { |
| | 1 | 454 | | regexPrefix = "‰"; |
| | 1 | 455 | | formatPattern = formatPattern.Substring(1); |
| | | 456 | | } |
| | 1 | 457 | | if (formatPattern[formatPattern.Length - 1] == '%') |
| | | 458 | | { |
| | 1 | 459 | | regexSuffix = "%"; |
| | 1 | 460 | | formatPattern = formatPattern.Substring(0, formatPattern.Length - 1); |
| | | 461 | | } |
| | | 462 | | |
| | 1 | 463 | | if (formatPattern[formatPattern.Length - 1] == '‰') |
| | | 464 | | { |
| | 1 | 465 | | regexSuffix = "‰"; |
| | 1 | 466 | | formatPattern = formatPattern.Substring(0, formatPattern.Length - 1); |
| | | 467 | | } |
| | 1 | 468 | | } |
| | | 469 | | |
| | | 470 | | public static bool IsFormatPatternValidAndSupported(FormatDescriptor formatDescriptor) |
| | | 471 | | { |
| | | 472 | | // verify the pattern only contains allowed characters |
| | 1 | 473 | | if (formatDescriptor.pattern is null) return true; |
| | 1 | 474 | | string decimalChar = formatDescriptor.decimalChar is null ? "." : formatDescriptor.decimalChar; |
| | 1 | 475 | | string groupChar = formatDescriptor.groupChar is null ? "," : formatDescriptor.groupChar; |
| | 1 | 476 | | char[] supportedChars = new char[] { '0', '#', '%', '‰', '-', '+', 'E' }; |
| | | 477 | | |
| | 1 | 478 | | string formatPattern = formatDescriptor.pattern; |
| | 1 | 479 | | string formatPatternWithoutDecAndGrChars = formatPattern.Replace(decimalChar, "").Replace(groupChar, ""); |
| | 1 | 480 | | foreach (var c in formatPatternWithoutDecAndGrChars) |
| | | 481 | | { |
| | 1 | 482 | | if (!supportedChars.Contains(c)) return false; |
| | | 483 | | } |
| | | 484 | | |
| | | 485 | | // verify the pattern is in correct format |
| | 1 | 486 | | string correctNumericPatternRegex = |
| | 1 | 487 | | $@"^(%|‰)?[+-]?[#0]+({Regex.Escape(groupChar)}[#0]+)*({Regex.Escape(decimalChar)}[#0]+({Regex.Escape(gro |
| | | 488 | | /* |
| | | 489 | | Regex regex = new Regex($@"((E[+]?[#0]+({Regex.Escape(groupChar)}[#0]+)*)?)?(%|‰)?"); |
| | | 490 | | var match = regex.Match(formatPattern); |
| | | 491 | | */ |
| | | 492 | | |
| | 1 | 493 | | if (!Regex.IsMatch(formatPattern, correctNumericPatternRegex)) |
| | 1 | 494 | | return false; |
| | 1 | 495 | | return true; |
| | | 496 | | } |
| | | 497 | | |
| | | 498 | | void ProcessPercenPermileExponent() |
| | | 499 | | { |
| | 1 | 500 | | if (stringValue.EndsWith("%")) |
| | | 501 | | { |
| | 1 | 502 | | stringValue = stringValue.Replace("%", ""); |
| | 1 | 503 | | Percent = 0.01F; |
| | | 504 | | } |
| | 1 | 505 | | else if (stringValue.EndsWith("‰")) |
| | | 506 | | { |
| | 1 | 507 | | stringValue = stringValue.Replace("‰", ""); |
| | 1 | 508 | | Permile = 0.001F; |
| | | 509 | | } |
| | 1 | 510 | | else if (stringValue.Contains('E')) |
| | 1 | 511 | | ProcessExponent(); |
| | | 512 | | |
| | 1 | 513 | | } |
| | | 514 | | |
| | | 515 | | void ProcessPercenPermileExponentPatternPresent() |
| | | 516 | | { |
| | 1 | 517 | | if (format!.pattern!.StartsWith("%") || format!.pattern!.EndsWith("%")) |
| | | 518 | | { |
| | 1 | 519 | | stringValue = stringValue.Replace("%", ""); |
| | 1 | 520 | | Percent = 0.01F; |
| | | 521 | | } |
| | | 522 | | |
| | 1 | 523 | | if (format!.pattern!.StartsWith("‰") || format!.pattern!.EndsWith("‰")) |
| | | 524 | | { |
| | 1 | 525 | | stringValue = stringValue.Replace("‰", ""); |
| | 1 | 526 | | Permile = 0.001F; |
| | | 527 | | } |
| | 1 | 528 | | else if (format!.pattern!.Contains('E')) |
| | 1 | 529 | | ProcessExponent(); |
| | | 530 | | |
| | 1 | 531 | | } |
| | | 532 | | |
| | | 533 | | void ProcessExponent() |
| | | 534 | | { |
| | | 535 | | try |
| | | 536 | | { |
| | 1 | 537 | | string[] stringParts = stringValue.Split("E"); |
| | 1 | 538 | | if (stringParts.Length == 2) |
| | | 539 | | { |
| | 1 | 540 | | var exponentPart = stringParts[1]; |
| | 1 | 541 | | var exponentPartInt = long.Parse(exponentPart); |
| | 1 | 542 | | Exponent = BigDecimal.Pow(BigDecimal.Ten, new BigInteger(exponentPartInt)); |
| | 1 | 543 | | stringValue = stringParts[0]; |
| | | 544 | | } |
| | | 545 | | else |
| | 0 | 546 | | ErrorFactory.ThrowDatatypeValidationError(stringValue); |
| | 1 | 547 | | } |
| | 1 | 548 | | catch (Exception) |
| | | 549 | | { |
| | 1 | 550 | | ErrorFactory.ThrowDatatypeValidationError(stringValue); |
| | 0 | 551 | | } |
| | | 552 | | |
| | 1 | 553 | | } |
| | | 554 | | |
| | 1 | 555 | | protected void ResolvePercentPermileExponent() => Value = Value * Percent * Permile * Exponent; |
| | | 556 | | |
| | | 557 | | bool HandleSpecialTypes(string strValue) |
| | | 558 | | { |
| | 1 | 559 | | if (strValue == "NaN") |
| | | 560 | | { |
| | 1 | 561 | | SpecialType = SpecialTypes.NaN; |
| | 1 | 562 | | return true; |
| | | 563 | | } |
| | 1 | 564 | | if (strValue == "-INF") |
| | | 565 | | { |
| | 1 | 566 | | SpecialType = SpecialTypes.NEGATIVE_INF; |
| | 1 | 567 | | return true; |
| | | 568 | | } |
| | 1 | 569 | | if (strValue == "INF") |
| | | 570 | | { |
| | 1 | 571 | | SpecialType = SpecialTypes.POSITIVE_INF; |
| | 1 | 572 | | return true; |
| | | 573 | | } |
| | 1 | 574 | | return false; |
| | | 575 | | |
| | | 576 | | |
| | | 577 | | } |
| | | 578 | | |
| | 1 | 579 | | public override bool IsPatternValid(FormatDescriptor format) => IsFormatPatternValidAndSupported(format); |
| | | 580 | | |
| | | 581 | | } |
| | | 582 | | } |