thanks for the explanation. this is cool. On Sat, Jul 22, 2017 at 10:06 PM, Sven Van Caekenberghe <sven@stfx.eu> wrote:
On 22 Jul 2017, at 21:51, Stephane Ducasse <stepharo.self@gmail.com> wrote:
Hi peter Do you have an example that I understand?
In its most simple form, CSV looks like this:
a,b,c,d
However, if a field is text, it might contain a comma itself, or other problematic characters like line endings, hence there is a provision for quoting fields, as follows:
"a","b","c",d
Now, "a" could be "a1,a2" and still be one field. Embedded quotes are doubled.
Not all fields need quoting, normally only text or string fields. If you know something is an integer, you don't need quotes, like d above.
Since CSV is normally a collection of identical records, you set these options for all of them.
What Peter wanted, and now implemented himself as an option, is to make the quoting optional per field, per record, by first checking if a field contains dangerous characters and only then adding the quotes. I guess this makes most sense in the context of generating a CSV file for humans to edit.
@Svn Once the change is integrated could you add a little paragraph to the chapter?
Stef
On Sat, Jul 22, 2017 at 6:51 PM, Sven Van Caekenberghe <sven@stfx.eu> wrote:
Hi Peter,
On 22 Jul 2017, at 14:12, Peter Uhnak <i.uhnak@gmail.com> wrote:
Hi,
this is a continuation of an older thread about quoting fields only when necessary. ( http://forum.world.st/NeoCSVWriter-automatic-quotes-td4924781.html )
I've attached fileouts of NeoCSV packages with the addition (I don't know if Metacello can file-out only changesets).
The change doesn't affect the default behavior, only when explicitly requested.
Peter
I accepted your changes as such, the .mcz's were copied over to the main repositories. This is a pure & clean extension, so that is good. Thank you.
This option is always going to be slower, but the current implementation might be improved, I think.
The key test in #writeOptionalQuotedField:
{ lineEnd asString. separator asString. '"' } anySatisfy: [ :each | string includesSubstring: each ]
will be quite slow.
If we accept a little bit of (over safe) error on EOL and use any occurrence of CR or LF as needing a quote, we could switch to characters to test for. There exists a fast (primitive) test, #findFirstInString:inSet:startingAt: that can do all the testing in one go. If your version turns out to be slow, we could try that, if measurements show a difference.
Regards,
Sven