From: "Ara.T.Howard" Date: 2005-10-30T09:06:58+09:00 Subject: Re: csv.rb a start on refactoring. On Sun, 30 Oct 2005, James Edward Gray II wrote: > On Oct 29, 2005, at 12:11 PM, Ara.T.Howard wrote: > >> ========================================== >> CSV2[8] => FAILED (RuntimeError) >> ========================================== >> input: >> "a,,," >> csv: >> ["a", "", "", ""] >> expected: >> ["a", nil, nil, nil] >> ========================================== > > Which one of those is more correct must be debatable. I didn't see a concept > of nil in the RCF document and nil stringifies to "", of course. Regardless, > it's a simple fix: > > module CSV2 > def self::parse_line( line ) > csv = Array.new > line.scan(/\G(?:^|,)(?:"((?>[^"]*)(?>""[^"]*)*)"|([^",]*))/) do > csv << if $1.nil? > if $2 == "" > nil > else > $2 > end > else > $1.gsub('""', '"') > end > end > csv > end > end > > That gives us two less errors. i think you need both - otherwise a,,, and a,"","","" cannot be told apart - but they are clearly different... debateable for sure. the rfc is on your side here it seems: field = (escaped / non-escaped) escaped = DQUOTE *(TEXTDATA / COMMA / CR / LF / 2DQUOTE) DQUOTE non-escaped = *TEXTDATA so '""' and '' are the same in the rfc. seems an over-sight though since it's impossible to mark 'empty' differently than 'missing'. in any case it'd probably be good to maintain backwards compat here so as not to break any apps - in that case nil would win. >> ========================================== >> CSV2[9] => FAILED (RuntimeError) >> ========================================== >> input: >> "," >> csv: >> [""] >> expected: >> [nil, nil] >> ========================================== > > You're input there was strip()ed. It was actually " , ". The expression > obviously fails this case either way, but in the RCF I read: > > "4... Spaces are considered part of a field and should not be ignored." i stripped input simply so i could write %w( , ) instead of %w(,) which is a little cryptic. i did mean for it to be ',' - no spaces. sorry if that was confusing. note that each and every input is stripped by design - but only for clarity. > I would interpret that to mean that the correct solution is [" ", " "]. Can > you please explain why that's wrong? taking into account the stripping above a correct solution should be [nil, nil] or possibly ["", ""] but it should surely be two fields either way. i guess we are saying the same thing here... so, except for blowing up on trailing commas the re seems to be pretty dang close. impressive. cheers. -a -- =============================================================================== | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | anything that contradicts experience and logic should be abandoned. | -- h.h. the 14th dalai lama ===============================================================================