writeCsvTo: stream
1 to: size do: [ :row |
1 to: size do: [ :column |
column ~= 1 ifTrue: [ stream nextPut: $, ].
stream print: (self at: row at: column) ].
stream nextPut: Character lf ]
And this is the read loop:
readCsvFrom: stream
| numberParser |
numberParser := SqNumberParser on: stream.
1 to: size do: [ :row |
1 to: size do: [ :column |
self at: row at: column put: numberParser nextNumber.
column ~= size ifTrue: [ stream peekFor: $, ] ].
row ~= size ifTrue: [ stream peekFor: Character lf ] ]
I am of course cheating a little bit, but should your CSV file be different, I am sure you can adapt the code (for example to deal with quoting). I am also advancing the stream under the SqNumberParser to avoid allocation a new one every time. I think this code generates little garbage.
What do you think ?
Sven