private void DetectDelimiter() { var text = new string(buffer, 0, charsRead); while (text.Length > 0) { var index = text.IndexOf(newLine); var line = index > -1 ? text.Substring(0, index + newLine.Length) : text; var delimiterCounts = new Dictionary <string, int>(); foreach (var delimiter in delimiterValues) { // Escape regex special chars to use as regex pattern. var pattern = Regex.Replace(delimiter, @"([.$^{\[(|)*+?\\])", "\\$1"); delimiterCounts[delimiter] = Regex.Matches(line, pattern).Count; } var maxCount = delimiterCounts.OrderByDescending(c => c.Value).First(); if (maxCount.Value > 0) { delimiter = maxCount.Key; delimiterFirstChar = delimiter[0]; configuration.Validate(); break; } text = index > -1 ? text.Substring(index + newLine.Length) : string.Empty; } }
/// <summary> /// Initializes a new instance of the <see cref="CsvWriter"/> class. /// </summary> /// <param name="writer">The writer.</param> /// <param name="configuration">The configuration.</param> public CsvWriter(TextWriter writer, CsvConfiguration configuration) { configuration.Validate(); this.writer = writer; Configuration = configuration; context = new CsvContext(this); typeConverterCache = context.TypeConverterCache; recordManager = new Lazy <RecordManager>(() => ObjectResolver.Current.Resolve <RecordManager>(this)); comment = configuration.Comment; bufferSize = configuration.BufferSize; delimiter = configuration.Delimiter; cultureInfo = configuration.CultureInfo; dynamicPropertySort = configuration.DynamicPropertySort; escapeDelimiterString = new string(configuration.Delimiter.SelectMany(c => new[] { configuration.Escape, c }).ToArray()); escapeNewlineString = new string(configuration.NewLine.SelectMany(c => new[] { configuration.Escape, c }).ToArray()); escapeQuoteString = new string(new[] { configuration.Escape, configuration.Quote }); hasHeaderRecord = configuration.HasHeaderRecord; includePrivateMembers = configuration.IncludePrivateMembers; injectionCharacters = configuration.InjectionCharacters; injectionEscapeCharacter = configuration.InjectionEscapeCharacter; leaveOpen = configuration.LeaveOpen; mode = configuration.Mode; newLine = configuration.NewLine; quote = configuration.Quote; quoteString = configuration.Quote.ToString(); sanitizeForInjection = configuration.SanitizeForInjection; shouldQuote = configuration.ShouldQuote; trimOptions = configuration.TrimOptions; buffer = new char[bufferSize]; }
/// <summary> /// Initializes a new instance of the <see cref="CsvParser"/> class. /// </summary> /// <param name="reader">The reader.</param> /// <param name="configuration">The configuration.</param> public CsvParser(TextReader reader, CsvConfiguration configuration) { configuration.Validate(); this.reader = reader; Configuration = configuration; Context = new CsvContext(this); allowComments = configuration.AllowComments; badDataFound = configuration.BadDataFound; bufferSize = configuration.BufferSize; cacheFields = configuration.CacheFields; comment = configuration.Comment; countBytes = configuration.CountBytes; delimiter = configuration.Delimiter; delimiterFirstChar = configuration.Delimiter[0]; encoding = configuration.Encoding; escape = configuration.Escape; ignoreBlankLines = configuration.IgnoreBlankLines; isNewLineSet = configuration.IsNewLineSet; leaveOpen = configuration.LeaveOpen; lineBreakInQuotedFieldIsBadData = configuration.LineBreakInQuotedFieldIsBadData; newLine = configuration.NewLine; newLineFirstChar = configuration.NewLine[0]; mode = configuration.Mode; processFieldBufferSize = configuration.ProcessFieldBufferSize; quote = configuration.Quote; whiteSpaceChars = configuration.WhiteSpaceChars; trimOptions = configuration.TrimOptions; buffer = new char[bufferSize]; processFieldBuffer = new char[processFieldBufferSize]; fields = new Field[128]; }
private void DetectDelimiter() { var text = new string(buffer, 0, charsRead); var delimiterCounts = new Dictionary <string, int>(); foreach (var delimiter in delimiterValues) { // Escape every character to use as regex pattern. var pattern = Regex.Replace(delimiter, "(.)", "\\$1"); delimiterCounts[delimiter] = Regex.Matches(text, pattern).Count; } var maxCount = delimiterCounts.OrderByDescending(c => c.Value).First(); if (maxCount.Value > 0) { delimiter = maxCount.Key; delimiterFirstChar = delimiter[0]; configuration.Validate(); } }