From: James Edward Gray II Date: 2006-04-28T00:35:19+09:00 Subject: Re: Q about the FasterCSV On Apr 27, 2006, at 10:28 AM, Dave Burt wrote: > Let's choose option 1. Ruby lets you modify classes from libraries. > Let's call this "lenient_and_still_a_little_bit_faster_csv.rb": > > require 'faster_csv' > class FasterCSV > # Pre-compiles parsers and stores them by name for access during > # reads, just like the official FasterCSV version, BUT the central > # parser allows arbitrary whitespace before and after the column > # separator. > def init_parsers( options ) > # prebuild Regexps for faster parsing > @parsers = { > :leading_fields => > /\A#{Regexp.escape(@col_sep)}+/, # for empty leading > fields You should modify the above line too. It takes both to correctly parse some lines: /\A\s*#{Regexp.escape(@col_sep)}+/ > :csv_row => > ### The Primary Parser ### > / \G(?:^|#{Regexp.escape(@col_sep)}) # anchor the match > \s* # <----- # ignore some > whitespace > (?: "((?>[^"]*)(?>""[^"]*)*)" # find quoted fields > | # ... or ... > ([^"#{Regexp.escape(@col_sep)}]*) # unquoted fields > )/x, > ### End Primary Parser ### > :line_end => > /#{Regexp.escape(@row_sep)}\Z/ # safer than chomp!() > } > end > end > > All that code except for the line consisting entirely of "\s*" was > taken > from FasterCSV 0.2.0, and I should have asked Gray Productions for > permission to republish it, but I don't think Mr. Gray will mind this > particular use of his excellent work. Looks good to me. Just don't hold your breath waiting on the patch... ;) James Edward Gray II