|
| 1 | +namespace CsvHelper.Configuration; |
| 2 | + |
| 3 | +/// <summary> |
| 4 | +/// Options for parsing CSV data. |
| 5 | +/// </summary> |
| 6 | +public record CsvParserOptions : CsvOptions |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// A value indicating whether fields should be cached for improved performance. |
| 10 | + /// </summary> |
| 11 | + public bool CacheFields { get; init; } |
| 12 | + |
| 13 | + /// <summary> |
| 14 | + /// Strategy use to find special characters in the CSV data. |
| 15 | + /// </summary> |
| 16 | + public ParsingStrategy? Strategy { get; init; } |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Creates a new instance of <see cref="CsvSerializerOptions"/> using common values from this instance. |
| 20 | + /// </summary> |
| 21 | + public CsvSerializerOptions ToSerializerOptions() |
| 22 | + { |
| 23 | + return (CsvSerializerOptions)CopyTo(new CsvSerializerOptions()); |
| 24 | + } |
| 25 | + |
| 26 | + internal StringCreator StringCreator = (chars, i) |
| 27 | +#if NET8_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER |
| 28 | + => new string(chars); |
| 29 | +#else |
| 30 | + => chars.ToString(); |
| 31 | +#endif |
| 32 | + |
| 33 | + internal int ParsedRowsSize = 8; |
| 34 | + |
| 35 | + internal int ParsedFieldsSize = 8 * 32; |
| 36 | + |
| 37 | + internal CsvModeParse ModeParse = Rfc4180Mode.Parse; |
| 38 | + |
| 39 | + internal ICsvParsingStrategy ParsingStrategyImplementation = new ThrowStrategy(); |
| 40 | + |
| 41 | + internal char[] ValidSpecialCharsForIntrinsics = Enumerable |
| 42 | + .Range(32, 127 - 32) |
| 43 | + .Select(i => (char)i) |
| 44 | + .ToList() |
| 45 | + .Append('\t') |
| 46 | + .Append('\r') |
| 47 | + .Append('\n') |
| 48 | + .ToArray(); |
| 49 | + |
| 50 | + internal override void Validate() |
| 51 | + { |
| 52 | + base.Validate(); |
| 53 | + |
| 54 | + // IndexOfAny does not use Vector256, so any char is valid for a special char. |
| 55 | + if (Strategy == ParsingStrategy.IndexOfAny) |
| 56 | + { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + if (!ValidSpecialCharsForIntrinsics.Contains(Delimiter)) |
| 61 | + { |
| 62 | + ThrowInvalidSpecialCharsForIntrinsics(nameof(Delimiter)); |
| 63 | + } |
| 64 | + |
| 65 | + if (!ValidSpecialCharsForIntrinsics.Contains(Escape)) |
| 66 | + { |
| 67 | + ThrowInvalidSpecialCharsForIntrinsics(nameof(Escape)); |
| 68 | + } |
| 69 | + |
| 70 | + if (NewLine.HasValue && !ValidSpecialCharsForIntrinsics.Contains(NewLine.Value)) |
| 71 | + { |
| 72 | + ThrowInvalidSpecialCharsForIntrinsics(nameof(NewLine)); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private void ThrowInvalidSpecialCharsForIntrinsics(string property) |
| 77 | + { |
| 78 | + var validChars = string.Join(", ", ValidSpecialCharsForIntrinsics.Select(c => $"'{c}'")); |
| 79 | + var message = $"{property} must be in the valid range of characters." + |
| 80 | + Environment.NewLine + |
| 81 | + $"You can use {nameof(ReplaceTextReader)} to replace invalid chars with valid chars." + |
| 82 | + Environment.NewLine + |
| 83 | + $"You can also use parsing strategy {nameof(ParsingStrategy.IndexOfAny)} because it does not suffer this limitation, though parsing will be slower." + |
| 84 | + Environment.NewLine + |
| 85 | + $"Valid chars: {validChars}"; |
| 86 | + |
| 87 | + throw new ConfigurationException(message); |
| 88 | + } |
| 89 | +} |
0 commit comments