dsv 0.12.13 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 290a72f198f3953590ac7477955d635a7a7f93f99c69571646af95ce952201b8
4
- data.tar.gz: 4cbcef66327426c841bc2f899f0e365e988c0b9011f56bf1b73a0551637a7429
3
+ metadata.gz: db9ee29841ef9fa86e047c7c2793bc249fb6a565bc6f76f3a4fb0f24e12ee4b9
4
+ data.tar.gz: 8cd6112162fd005f994a1d2db8699e791e547d0e8ee1f633628ecd246b490cac
5
5
  SHA512:
6
- metadata.gz: be78ef602e4f7857fe2af664b57d45c002f5c4cb93b9879dc492fa62da287bb6907ade74b6812f94abe53d6ef340b94810e4df2e5b0acb9f0879bd08de71c967
7
- data.tar.gz: 517e620882451b2c8ad5356e92c1b163d8ad697246bb7a0c64ec0ec84e8f50190b44b98e0171874ba011f2be30a0a82ee25b1b0ab6e341a4252a8632b5d2bf77
6
+ metadata.gz: 7645c70c8ac8664ad7febcfe16db321a90ef334946535cf45a63582417cbde9d6353b4cd4fb991daa177b97ac4cd8f74d5e28ed92d422ba283c29676d036ea8b
7
+ data.tar.gz: 2f932b86ad2fa36ed622b13cb88d5e425d51075b75f1c4a4f05e73eb47c03d18da52379b7618b8c5152d227b74ba8294da9b404772d5cf8696abf1bc4e78a86e
data/CHANGELOG CHANGED
@@ -1,5 +1,48 @@
1
1
  # dsv/CHANGELOG
2
2
 
3
+ ## 20261124
4
+
5
+ 0.13.0: Under :double, a row which breaks it raises, where from 0.12.5 to 0.12.15 it was read as unspecified.
6
+
7
+ 1. ~ split_row(): Under :double, a row with a field which is not quoted raises DSV::UnquotedField. 0.12.5 item 2 read such a row as unspecified: a change of design, which cost :double a scan of each unquoted row and a count of each field holding a quote so as to know when to fall back, and so a minor, released as a patch beside a fix.
8
+ 2. ~ split_double_quoted(): A row not starting with a quote raises UnquotedField, an unquoted header included, which every release before split plainly. The row is read in full only where its last field seems open or the split leaves a lone quote in a field.
9
+ 3. + reassemble_wholly_quoted_fields(): A row under :double read in full, a field which is not quoted raising UnquotedField.
10
+ 4. + lib/DSV/Error.rb, + lib/DSV/UnexpectedRow.rb, + lib/DSV/UnquotedField.rb: DSV::Error is the base of what dsv raises about the data; UnexpectedRow, beneath it, a row which contradicts its mode, with its line and mode; UnquotedField, beneath that, a field with no quotes under a wholly quoted mode.
11
+ 5. ~ lib/DSV/UnterminatedQuote.rb: UnterminatedQuote is a DSV::Error.
12
+ 6. ~ row_fields(), parse_row(), columns: An UnexpectedRow is given the line its row began on.
13
+ 7. ~ test/dsv_test.rb: Rows which break :double raise, with their line and mode, an unquoted header included; a field holding quote-separator-quote and an empty last field are read under :double; the errors' classes are tested.
14
+ 8. ~ README.md: A row which breaks :double raises UnquotedField, where it was read as with no mode named. The errors are listed.
15
+ 9. ~ TODO: Only the jobs in hand are kept, in order and without their releases; the rest are in ROADMAP.md. The readers are no longer renamed as nouns, split_double_quoted() alone becoming split_wholly_quoted(). Jobs are added for write keeping a repeated name, and for attributes= setting columns.
16
+ 10. ~ ROADMAP.md: Speed is given as the reason for the modes and for no fallback. 0.13.0 undoes the fallback, naming 0.12.5's errors; 0.14.0 makes the internals private; the declarations are 0.15.0 and streaming 0.16.0. Path or text is no longer a theme, and speed against other languages is one.
17
+ 11. ~ PROPOSED.md: The exception raised is settled. :collect and :infer are asked after, and the version rule of filter alone. The quote character notes what a quote longer than a byte would cost.
18
+ 12. ~ lib/DSV/VERSION.rb: /0.12.15/0.13.0/.
19
+
20
+ ## 20261120
21
+
22
+ 0.12.15: normalised_mode, row_renderer and write_row are laid out as SimpleCSV 0.11.5 has them.
23
+
24
+ 1. ~ normalised_mode(): Each when and its result are joined by ;, not then.
25
+ 2. ~ write_row(): The values are assigned from an if wrapped in parentheses, rather than one hanging from the assignment.
26
+ 3. ~ row_renderer(): Each quote mode has a branch of its own, giving its renderer.
27
+ 4. + quoted_row_renderer(), + unquoted_row_renderer(): The renderers, private. Each lambda holds its quote and separator, since literals inside it would be allocated for every field.
28
+ 5. ~ TODO: - 1, 2. The fallback under :double is undone by 0.13.0's declarations, not by a patch back to 0.12.4, whose reading misread rows silently. :unspecified, not :mixed, names the default. Jobs are added: :unquoted as the name of the mode reading no quotes; the reader chosen once, brought forward from 1.x; and .read and .parse taking a path and text apart, into which the String under a write mode taken as a path is folded. :single moves to 0.13.0, once :double raises, since no release has read it.
29
+ 6. ~ ROADMAP.md: The design philosophy opens with fail fast, otherwise go fast, and the default is the one reading which interprets. The layout is done, :double back to 0.12.4's reading is no longer listed under 0.12.x, and :single moves to 0.13.0. :unspecified names the default. 0.13.0 lists :unquoted, the reader chosen once and the source given as a path or text, and 2.0 is gone.
30
+ 7. ~ PROPOSED.md: The version rule is no longer asked of :single, an addition. :unspecified replaces :mixed in the examples. Path or text asks only how DSV.new is told which a String is.
31
+ 8. ~ lib/DSV/VERSION.rb: /0.12.14/0.12.15/.
32
+
33
+ ## 20261108
34
+
35
+ 0.12.14: A space as the column separator splits on each space.
36
+
37
+ 1. ~ split_on(): A single space is split on as / /. String#split had taken ' ' as runs of whitespace, dropping any leading run, so empty fields were lost and a tab split as a space.
38
+ 2. ~ split_quoted(): A String separator is split on by split_on().
39
+ 3. ~ test/dsv_test.rb: A space separator is tested, under each quote mode, and with a tab.
40
+ 4. ~ README.md: A String separator is split on as given.
41
+ 5. ~ TODO: One job to a line, each marked with its release, the reasons moved to ROADMAP.md and what is undecided to PROPOSED.md. The space separator is done, and the string predicates were done in 0.12.0. The layout of normalised_mode, row_renderer and write_row, and the renaming of split_quoted() and split_double_quoted(), are added.
42
+ 6. ~ ROADMAP.md: Each release is stated as what it does and why; what is undecided is moved to PROPOSED.md.
43
+ 7. + PROPOSED.md: What is undecided, under a heading for each theme or change.
44
+ 8. ~ lib/DSV/VERSION.rb: /0.12.13/0.12.14/.
45
+
3
46
  ## 20261103
4
47
 
5
48
  0.12.13: reading honours quote:, given as a String or to DSV.parse_line.
data/README.md CHANGED
@@ -85,7 +85,7 @@ DSV.read('people.txt', headers: true, column_separator: /,\s*/)
85
85
  DSV.read('people.csv', headers: true, row_separator: "\r\n")
86
86
  ```
87
87
 
88
- `col_sep:` and `row_sep:` are accepted as spellings of those. Writing takes the same two options, so a file round-trips through whatever it was read with. A Regexp says what may be read as a separator, not what to write, so writing with one raises `ArgumentError`; give a String to write.
88
+ A String is split on exactly as given: a space splits on each space, two in a row leaving an empty field between them, rather than on runs of whitespace as `String#split(' ')` does. `col_sep:` and `row_sep:` are accepted as spellings of those. Writing takes the same two options, so a file round-trips through whatever it was read with. A Regexp says what may be read as a separator, not what to write, so writing with one raises `ArgumentError`; give a String to write.
89
89
 
90
90
  ### Quoting
91
91
 
@@ -96,10 +96,12 @@ DSV.read('plain.csv', headers: true, quote: :none) # no field is quoted; quot
96
96
  DSV.read('quoted.csv', headers: true, quote: :double) # every field is quoted, as DSV writes them
97
97
  ```
98
98
 
99
- Under `:double` a row that is wholly unquoted, as a header row often is, is split plainly, and a row that is neither wholly quoted nor wholly unquoted is read as it would be with no mode named, so naming the mode never costs a correct read.
99
+ A named mode is held to: under `:double` every field of every row is quoted, the header's included. A row with a field which isn't raises `DSV::UnquotedField`, whose `line` is the line its row began on and `mode` the mode in force. To read a file whose quoting varies, name no mode.
100
100
 
101
101
  Input which isn't RFC 4180 is read to one rule. A quote opens a field only at the start of one, so a quote anywhere else, as in `12" pizza`, is data. A quoted field closes only at a quote followed by the separator or the end of the row, so one which doesn't close by the end of the source, as `"abc` or `"ab"c` doesn't, raises `DSV::UnterminatedQuote`, whose `line` is the line its row began on.
102
102
 
103
+ Every error about the data is a `DSV::Error`: `UnterminatedQuote`, and `UnexpectedRow`, a row which contradicts its mode, of which `UnquotedField` is one kind. A mistake in the options is an `ArgumentError`.
104
+
103
105
  ### Writing
104
106
 
105
107
  Set the columns, give the rows as Hashes, and write:
data/TODO CHANGED
@@ -1,43 +1,19 @@
1
1
  # TODO
2
2
 
3
- ## 0.12.x, fixes
4
-
5
- 1. A space as the column separator splits on each space, not as String#split(' ') does.
6
- 2. :double back to 0.12.4's reading, undoing 0.12.5 item 2: a row not starting with a quote split plainly, one starting with a quote split on quote-separator-quote, and one which leaves a lone quote in a field read in full as wholly quoted. The README back to 0.12.3's wording.
7
- 3. :single read, as :double is with a single quote.
8
- 4. filter, a class-level alias of select.
9
- 5. A String under a write mode taken as a path, so that a file not yet there can be written by name.
10
- 6. The internals private.
11
-
12
- ## 0.13.0, modes as declarations
13
-
14
- 7. An unknown quote mode, file mode or option raises ArgumentError, where each is ignored today.
15
- 8. :mixed, the name of the default.
16
- 9. A mode raises on a contradiction which costs nothing to see, every row alike, header included: under :double, a row not starting with a quote, and a bare field in a row read in full. Declaring :mixed reads such a row.
17
-
18
- ## 0.14.0, streaming
19
-
20
- 10. each yields each row as it is read and returns its receiver, so that detect and break stop reading and memory does not grow with the file; rows is no longer filled by each. Chunks come from each_slice. A source read once, start to end, with no rewind, so that a pipe or $stdin can be read: the header from the first row read, and the line of an error found by seeking only where the source can; a rewind left to r+ rewriting and first_row called alone.
21
-
22
- ## 1.0
23
-
24
- 11. A corpus in the tests, a day of NEMWEB's dispatch files trimmed and the RFC 4180 cases, each checked against stdlib CSV's rows.
25
- 12. A benchmark script against stdlib CSV, and the README's speed sentence written from its output.
26
- 13. README: every mode by example, and a table matching each to stdlib CSV's and Python's settings.
27
-
28
- ## 1.x, speed, results unchanged
29
-
30
- 14. A per-row fast path for the default: split, the fields bare or wrapped in two quotes unwrapped, any other row read in full.
31
- 15. as_array: returning the split fields, rather than building a Hash and then an Array from it.
32
- 16. The splitter chosen once, at construction, as row_renderer is for writing.
33
- 17. Two string predicates in place of four regex matches per piece; rows built by zip.to_h; the scanner-based parser, at the tag scanner-parser-20261006, judged against the rest.
34
-
35
- ## 1.x, features
36
-
37
- 18. :rfc_4180, a quote mode reading what RFC 4180 allows as :mixed does and raising on what it forbids.
38
- 19. field_count:, :header or an Integer, off by default: a row of another width raises.
39
- 20. Writing: minimal quoting, a field quoted only where it holds a separator, a quote or a row terminator; a row terminator inside a field kept, quoted, or folded to a space; the encoding declared and a transcode offered with any loss marked.
40
- 21. A line number in each parse error, found only when the error is raised, as UnterminatedQuote finds its, where the source can seek. line_numbers:, off by default, counts lines as it reads instead, for any source a pipe included: the read loop chosen once, at construction, as row_renderer is for writing, so that without it no row pays for the count.
41
- 22. The instance-level header_row renamed headers?, kept as an alias; << to append a row and generate to build a string.
42
- 23. Symbols and Strings interchangeable for column names, in a selection, a columns: list and a row's keys.
43
- 24. The duplicate-header modes as an option: keep all as an Array, the default; index, a, a.1; first; last; error.
3
+ 1. write with no columns defined keeping a repeated name, each of its values in a field of its own, where the Array is written whole into one field.
4
+ 2. filter, a class-level alias of select.
5
+ 3. The internals private, and split_double_quoted() renamed split_wholly_quoted().
6
+ 4. An unknown quote mode, file mode or option raises ArgumentError.
7
+ 5. :unspecified, the name of the default.
8
+ 6. :unquoted, the name of the mode reading no quotes, :none kept as an alias.
9
+ 7. The reader chosen once, when the quote mode is set, as row_renderer is for writing.
10
+ 8. .read and .open taking a path, and .parse text; a path which does not exist raising, and nothing inferred from whether a file exists.
11
+ 9. :single read as :double is, with a single quote.
12
+ 10. attributes= setting columns, attributes always worked out from columns; rows read afterwards keyed by those names.
13
+ 11. each yields each row as it is read.
14
+ 12. each returns its receiver.
15
+ 13. rows no longer filled by each.
16
+ 14. Chunks from each_slice.
17
+ 15. The source read once, start to end, with no rewind; the header from the first row read.
18
+ 16. The line of an error found by seeking only where the source can.
19
+ 17. A rewind left to r+ rewriting and to first_row called alone.
data/lib/DSV/Error.rb ADDED
@@ -0,0 +1,8 @@
1
+ # DSV/Error.rb
2
+ # DSV::Error
3
+
4
+ class DSV
5
+ # The base of what dsv raises about the data, so that one rescue catches all of it.
6
+ class Error < ::StandardError
7
+ end
8
+ end
@@ -0,0 +1,20 @@
1
+ # DSV/UnexpectedRow.rb
2
+ # DSV::UnexpectedRow
3
+
4
+ class DSV
5
+ # A row which contradicts its declared quote mode; mode is the mode in force, and line the one on which the row began, given by the reader once it knows it.
6
+ class UnexpectedRow < Error
7
+ attr_reader :mode
8
+ attr_accessor :line
9
+
10
+ def initialize(mode, line = nil)
11
+ @mode = mode
12
+ @line = line
13
+ super()
14
+ end
15
+
16
+ def message
17
+ "The row beginning on line #{@line} does not keep to quote: #{@mode.inspect}."
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,11 @@
1
+ # DSV/UnquotedField.rb
2
+ # DSV::UnquotedField
3
+
4
+ class DSV
5
+ # A field with no quotes, under a quote mode which declares every field quoted.
6
+ class UnquotedField < UnexpectedRow
7
+ def message
8
+ "A field in the row beginning on line #{@line} is not quoted, as quote: #{@mode.inspect} declares every field to be."
9
+ end
10
+ end
11
+ end
@@ -3,7 +3,7 @@
3
3
 
4
4
  class DSV
5
5
  # A quoted field which does not close by the end of the source; line is the one on which its row began.
6
- class UnterminatedQuote < ::StandardError
6
+ class UnterminatedQuote < Error
7
7
  attr_reader :line
8
8
 
9
9
  def initialize(line)
data/lib/DSV/VERSION.rb CHANGED
@@ -2,5 +2,5 @@
2
2
  # DSV::VERSION
3
3
 
4
4
  class DSV
5
- VERSION = '0.12.13'
5
+ VERSION = '0.13.0'
6
6
  end
data/lib/dsv.rb CHANGED
@@ -8,7 +8,10 @@ require 'stringio'
8
8
  require 'DSV/VERSION'
9
9
  require 'DSV/File'
10
10
  require 'DSV/String'
11
+ require 'DSV/Error'
11
12
  require 'DSV/UnterminatedQuote'
13
+ require 'DSV/UnexpectedRow'
14
+ require 'DSV/UnquotedField'
12
15
 
13
16
  class DSV
14
17
 
@@ -22,11 +25,11 @@ class DSV
22
25
  # A mode as Ruby's File spells it, from any of the spellings the library accepts, a Symbol included; unspecified is r.
23
26
  def normalised_mode(mode)
24
27
  case mode.to_s
25
- when 'r', 'r+', 'w', 'w+', 'a', 'a+' then mode.to_s
26
- when 'read_only', 'read-only', 'readonly' then 'r'
27
- when 'rw', 'read_write', 'read-write', 'readwrite' then 'r+'
28
- when 'write_only', 'write-only', 'writeonly' then 'w'
29
- when 'append' then 'a'
28
+ when 'r', 'r+', 'w', 'w+', 'a', 'a+'; mode.to_s
29
+ when 'read_only', 'read-only', 'readonly'; 'r'
30
+ when 'rw', 'read_write', 'read-write', 'readwrite'; 'r+'
31
+ when 'write_only', 'write-only', 'writeonly'; 'w'
32
+ when 'append'; 'a'
30
33
  else 'r'
31
34
  end
32
35
  end
@@ -224,6 +227,9 @@ class DSV
224
227
  nil
225
228
  end
226
229
  )
230
+ rescue UnexpectedRow => error
231
+ error.line = 1
232
+ raise
227
233
  end
228
234
 
229
235
  def columns=(*column_order)
@@ -268,22 +274,26 @@ class DSV
268
274
  value.nil? || value.empty?
269
275
  end
270
276
 
271
- # One row into its fields, by the quote mode: :none splits; :double splits a row as it promises, wholly quoted or wholly unquoted, and one that is neither as unspecified would; unspecified splits and reassembles a quoted field that held the separator, a doubled quote inside it reading as one. nil where a quoted field in the row is still open at its end. Direct string operations throughout; the scanner-based parser is on the branch scanner-parser.
277
+ # One row into its fields, by the quote mode: :none splits; :double splits a row as it promises, every field quoted, and raises UnquotedField on one which is not; unspecified splits and reassembles a quoted field that held the separator, a doubled quote inside it reading as one. nil where a quoted field in the row is still open at its end. Direct string operations throughout; the scanner-based parser is on the branch scanner-parser.
272
278
  def split_row(raw_row)
273
279
  row = raw_row.chomp(@row_separator)
274
280
  case @quote&.to_sym
275
281
  when :none, :unquoted
276
282
  split_on(row, @column_separator)
277
283
  when :double, :double_quoted, :double_quotes
278
- split_double_quoted(row) || split_quoted(row)
284
+ split_double_quoted(row)
279
285
  else
280
286
  split_quoted(row)
281
287
  end
282
288
  end
283
289
 
284
- # A row split on a separator, String or Regexp; a Regexp's matches divide it, since String#split would also return any groups it captures.
290
+ # A row split on a separator, String or Regexp; a Regexp's matches divide it, since String#split would also return any groups it captures. A single space is split on as / /, since String#split takes ' ' to mean runs of whitespace, any leading run dropped.
285
291
  def split_on(row, separator)
286
- separator.is_a?(::Regexp) ? pieces_and_separators(row, separator).first : row.split(separator, -1)
292
+ case separator
293
+ when ::Regexp; pieces_and_separators(row, separator).first
294
+ when ' '; row.split(/ /, -1)
295
+ else row.split(separator, -1)
296
+ end
287
297
  end
288
298
 
289
299
  # A row's pieces between a Regexp's matches, with the text of each match, so that a quoted field may be joined back with the separator it held.
@@ -303,20 +313,48 @@ class DSV
303
313
  if @column_separator.is_a?(::Regexp)
304
314
  reassemble_quoted_fields(*pieces_and_separators(row, @column_separator))
305
315
  else
306
- reassemble_quoted_fields(row.split(@column_separator, -1))
316
+ reassemble_quoted_fields(split_on(row, @column_separator))
307
317
  end
308
318
  end
309
319
 
310
- # The :double contract: a wholly quoted row split on quote-separator-quote, a wholly unquoted one plainly; nil for a row that is neither, or whose last field does not close.
320
+ # The :double contract, every field quoted: the row split on quote-separator-quote, a closed last field ending in an odd run of quotes, counted from the last piece, whose opening quote the split has already taken. Read in full only where the last field seems open, as it is where it goes on to the next line, or where the split leaves a lone quote in a field, as a field holding quote-separator-quote does. A row not starting with a quote raises UnquotedField, at the cost of one byte compared. nil where the last field does not close.
311
321
  def split_double_quoted(row)
312
- if row.start_with?('"')
313
- unescaped_fields(split_on(row.delete_prefix('"').delete_suffix('"'), quoted_separator)) if trailing_quotes(row, 0).odd?
314
- else
315
- split_on(row, @column_separator) unless row.include?('"')
322
+ raise UnquotedField.new(:double) unless row.start_with?('"')
323
+ fields = split_on(row.delete_prefix('"'), quoted_separator)
324
+ return reassemble_wholly_quoted_fields(row) unless fields.last && trailing_quotes(fields.last, -1).odd?
325
+ fields[-1] = fields.last.delete_suffix('"')
326
+ unescaped_fields(fields) || reassemble_wholly_quoted_fields(row)
327
+ end
328
+
329
+ # A row under :double whose split left a lone quote in a field, read in full as unspecified reads one, except that a field which is not quoted raises UnquotedField. nil where a quoted field is still open after the last piece.
330
+ def reassemble_wholly_quoted_fields(row)
331
+ pieces, separators = (
332
+ if @column_separator.is_a?(::Regexp)
333
+ pieces_and_separators(row, @column_separator)
334
+ else
335
+ [split_on(row, @column_separator), nil]
336
+ end
337
+ )
338
+ fields = []
339
+ buffer = nil
340
+ pieces.each_with_index do |piece, i|
341
+ if buffer && trailing_quotes(piece, -1).even?
342
+ buffer << separator_before(i, separators) << piece
343
+ elsif buffer
344
+ fields << (buffer << separator_before(i, separators) << piece.delete_suffix('"')).gsub('""', '"')
345
+ buffer = nil
346
+ elsif !piece.start_with?('"')
347
+ raise UnquotedField.new(:double)
348
+ elsif trailing_quotes(piece, 0).even?
349
+ buffer = +piece.delete_prefix('"')
350
+ else
351
+ fields << piece.delete_prefix('"').delete_suffix('"').gsub('""', '"')
352
+ end
316
353
  end
354
+ fields unless buffer
317
355
  end
318
356
 
319
- # The fields of a row split as wholly quoted, a doubled quote read as one; nil where a field holds a lone quote, the mark of a row that was not. A field has twice the quotes it has unescaped exactly when every quote in it is one of a doubled pair.
357
+ # The fields of a row split as wholly quoted, a doubled quote read as one; nil where a field holds a lone quote, which the split alone cannot place. A field has twice the quotes it has unescaped exactly when every quote in it is one of a doubled pair.
320
358
  def unescaped_fields(fields)
321
359
  fields.collect do |field|
322
360
  next field unless field.include?('"')
@@ -370,10 +408,13 @@ class DSV
370
408
  separators ? separators[i - 1] : @column_separator
371
409
  end
372
410
 
373
- # A row's fields, the next line taken into the row while a quoted field in it is open, a quoted field being able to hold the row separator; a field still open at the end of the source raises UnterminatedQuote.
411
+ # A row's fields, the next line taken into the row while a quoted field in it is open, a quoted field being able to hold the row separator; a field still open at the end of the source raises UnterminatedQuote. A row which contradicts its mode is given its line here, where the line can be found, and only then.
374
412
  def row_fields(raw_row)
375
413
  raw_row << @source.gets(@row_separator) until (fields = split_row(raw_row)) || @source.eof?
376
414
  fields || raise(UnterminatedQuote.new(line_of(raw_row)))
415
+ rescue UnexpectedRow => error
416
+ error.line = line_of(raw_row)
417
+ raise
377
418
  end
378
419
 
379
420
  # The line on which a row read up to the source's position began, counted only when an error wants it, by reading the source again up to the row, so that no row which closes pays for it.
@@ -385,6 +426,9 @@ class DSV
385
426
 
386
427
  def parse_row(raw_row, *selected_columns)
387
428
  parse_fields(split_row(raw_row) || raise(UnterminatedQuote.new(1)), *selected_columns)
429
+ rescue UnexpectedRow => error
430
+ error.line = 1
431
+ raise
388
432
  end
389
433
 
390
434
  def parse_fields(fields, *selected_columns)
@@ -450,13 +494,15 @@ end
450
494
  # A nil value is an empty field, so the columns after it keep their places; with no columns defined a row's values are written in the row's own order.
451
495
  def write_row(row, *selected_columns)
452
496
  selected_columns.flatten!
453
- values = if !selected_columns.empty?
454
- selected_columns.collect{|column| row[column]}
455
- elsif attributes
456
- values_in_order(row)
457
- else
458
- row.values
459
- end
497
+ values = (
498
+ if !selected_columns.empty?
499
+ selected_columns.collect{|column| row[column]}
500
+ elsif attributes
501
+ values_in_order(row)
502
+ else
503
+ row.values
504
+ end
505
+ )
460
506
  write_values(values)
461
507
  end
462
508
  alias_method :write_csv_row, :write_row
@@ -474,20 +520,14 @@ end
474
520
 
475
521
  # Chosen once, at the first write, from the quote mode: how a row of values is rendered, with the instance's separators, a quote inside a quoted value doubled as RFC 4180 has it. The spacey modes put a space after each separator, as Array#to_csv_row did.
476
522
  def row_renderer
477
- quote, spacey = case @quote&.to_sym
478
- when :none, :unquoted then [nil, false]
479
- when :spacey_none, :spacey_unquoted then [nil, true]
480
- when :single then ["'", false]
481
- when :spacey_single then ["'", true]
482
- when :spacey_double then ['"', true]
483
- else ['"', false]
484
- end
485
- joiner = spacey ? writable_column_separator + ' ' : writable_column_separator
486
- if quote
487
- doubled = quote * 2
488
- ->(values){values.collect{|value| quote + value.to_s.gsub(quote, doubled) + quote}.join(joiner) + @row_separator}
489
- else
490
- ->(values){values.join(joiner) + @row_separator}
523
+ separator = writable_column_separator
524
+ case @quote&.to_sym
525
+ when :none, :unquoted; unquoted_row_renderer(separator)
526
+ when :spacey_none, :spacey_unquoted; unquoted_row_renderer(separator + ' ')
527
+ when :single; quoted_row_renderer("'", separator)
528
+ when :spacey_single; quoted_row_renderer("'", separator + ' ')
529
+ when :spacey_double; quoted_row_renderer('"', separator + ' ')
530
+ else quoted_row_renderer('"', separator)
491
531
  end
492
532
  end
493
533
 
@@ -563,4 +603,15 @@ end
563
603
  end
564
604
  end
565
605
 
606
+ private
607
+
608
+ def quoted_row_renderer(quote, separator)
609
+ doubled_quote = quote * 2
610
+ ->(values){values.collect{|value| quote + value.to_s.gsub(quote, doubled_quote) + quote}.join(separator) + @row_separator}
611
+ end
612
+
613
+ def unquoted_row_renderer(separator)
614
+ ->(values){values.join(separator) + @row_separator}
615
+ end
616
+
566
617
  end # class DSV
data/test/dsv_test.rb CHANGED
@@ -517,6 +517,18 @@ describe DSV do
517
517
  _(DSV.read("a::b\n1::2\n", headers: true, column_separator: '::')).must_equal [{'a' => '1', 'b' => '2'}]
518
518
  end
519
519
 
520
+ it "splits on each space where the separator is a space, an empty field between two and at either end" do
521
+ _(DSV.read(" a b \n", quote: :none, column_separator: ' ')).must_equal [{0 => '', 1 => 'a', 2 => '', 3 => 'b', 4 => ''}]
522
+ _(DSV.read("a b\n", column_separator: ' ')).must_equal [{0 => 'a', 1 => '', 2 => 'b'}]
523
+ _(DSV.read("\"x y\" 2\n", column_separator: ' ')).must_equal [{0 => 'x y', 1 => '', 2 => '2'}]
524
+ _(DSV.read("\"a\" \"\" \"b\"\n", quote: :double, column_separator: ' ')).must_equal [{0 => 'a', 1 => '', 2 => 'b'}]
525
+ _{DSV.read("a b\n", quote: :double, column_separator: ' ')}.must_raise DSV::UnquotedField
526
+ end
527
+
528
+ it "does not split on a tab where the separator is a space" do
529
+ _(DSV.read("a\tb c\n", column_separator: ' ')).must_equal [{0 => "a\tb", 1 => 'c'}]
530
+ end
531
+
520
532
  it "reads a regular expression separator" do
521
533
  _(DSV.read("a, b\n1,2\n", headers: true, column_separator: /,\s*/)).must_equal [{'a' => '1', 'b' => '2'}]
522
534
  end
@@ -575,9 +587,10 @@ describe DSV do
575
587
  _(DSV.read("\"a\",\"b\"\n\"x\",\"2\"\n", headers: true, quote: 'double')).must_equal [{'a' => 'x', 'b' => '2'}]
576
588
  end
577
589
 
578
- it "strips the quotes under quote: :double, a row being wholly quoted or, as a header row often is, wholly unquoted" do
579
- _(DSV.read("a,b\n\"x\",\"2\"\n", headers: true, quote: :double)).must_equal [{'a' => 'x', 'b' => '2'}]
590
+ it "strips the quotes under quote: :double, every row wholly quoted, the header included" do
591
+ _(DSV.read("\"a\",\"b\"\n\"x\",\"2\"\n", headers: true, quote: :double)).must_equal [{'a' => 'x', 'b' => '2'}]
580
592
  _(DSV.read("\"a\",\"b\"\n\"say \"\"hi\"\"\",\"2\"\n", headers: true, quote: :double)).must_equal [{'a' => 'say "hi"', 'b' => '2'}]
593
+ _(DSV.read("\"a\",\"\"\n", quote: :double)).must_equal [{0 => 'a', 1 => ''}]
581
594
  end
582
595
 
583
596
  it "keeps every position of a repeated header name in columns, the name at each position in attributes" do
@@ -615,19 +628,41 @@ describe DSV do
615
628
  end
616
629
 
617
630
  it "keeps a separator inside a quoted field under quote: :double" do
618
- _(DSV.read("a,b\n\"x, y\",\"2\"\n", headers: true, quote: :double)).must_equal [{'a' => 'x, y', 'b' => '2'}]
631
+ _(DSV.read("\"a\",\"b\"\n\"x, y\",\"2\"\n", headers: true, quote: :double)).must_equal [{'a' => 'x, y', 'b' => '2'}]
632
+ end
633
+
634
+ it "reads a field holding quote-separator-quote under quote: :double, the row read in full" do
635
+ _(DSV.read("\"a\"\",\"\"b\",\"c\"\n", quote: :double)).must_equal [{0 => 'a","b', 1 => 'c'}]
636
+ end
637
+
638
+ it "raises UnquotedField on a row under quote: :double with a field not quoted, where 0.12.5 to 0.12.15 read it as unspecified" do
639
+ _{DSV.read("\"a\",b,\"c\"\n", quote: :double)}.must_raise DSV::UnquotedField
640
+ _{DSV.read("1,\"x\"\n", quote: :double)}.must_raise DSV::UnquotedField
641
+ _{DSV.read("\"a\",\"b\",c\n", quote: :double)}.must_raise DSV::UnquotedField
642
+ _{DSV.read("\"a\",,\"c\"\n", quote: :double)}.must_raise DSV::UnquotedField
643
+ _{DSV.read("a,b\n\"x\",\"2\"\n", headers: true, quote: :double)}.must_raise DSV::UnquotedField
644
+ _{DSV.parse_line('"a",b', quote: :double)}.must_raise DSV::UnquotedField
645
+ end
646
+
647
+ it "names the line and the mode of a row which breaks quote: :double" do
648
+ error = _{DSV.read("\"a\",\"b\"\n\"x\",\"2\"\n\"y\",3\n", quote: :double)}.must_raise DSV::UnquotedField
649
+ _(error.line).must_equal 3
650
+ _(error.mode).must_equal :double
651
+ _(error.message).must_include 'line 3'
652
+ error = _{DSV.read("a,b\n\"x\",\"2\"\n", headers: true, quote: :double)}.must_raise DSV::UnquotedField
653
+ _(error.line).must_equal 1
619
654
  end
620
655
 
621
- it "reads a row that is neither wholly quoted nor wholly unquoted under quote: :double as it would unspecified" do
622
- _(DSV.read("\"a\",b,\"c\"\n", quote: :double)).must_equal [{0 => 'a', 1 => 'b', 2 => 'c'}]
623
- _(DSV.read("1,\"x\"\n", quote: :double)).must_equal [{0 => '1', 1 => 'x'}]
624
- _(DSV.read("\"a\",\"b\",c\n", quote: :double)).must_equal [{0 => 'a', 1 => 'b', 2 => 'c'}]
625
- _(DSV.read("a,b\n1,\"x, y\"\n", headers: true, quote: :double)).must_equal [{'a' => '1', 'b' => 'x, y'}]
656
+ it "raises errors about the data under DSV::Error, UnquotedField under UnexpectedRow" do
657
+ _(DSV::UnquotedField.ancestors).must_include DSV::UnexpectedRow
658
+ _(DSV::UnexpectedRow.ancestors).must_include DSV::Error
659
+ _(DSV::UnterminatedQuote.ancestors).must_include DSV::Error
660
+ _{DSV.read("\"a\",b\n", quote: :double)}.must_raise DSV::Error
626
661
  end
627
662
 
628
663
  it "reads a quoted field holding the row separator as one field across lines under quote: :double, as DSV writes it" do
629
664
  _(DSV.read("\"x\ny\",\"2\"\n\"z\",\"3\"\n", quote: :double)).must_equal [{0 => "x\ny", 1 => '2'}, {0 => 'z', 1 => '3'}]
630
- _(DSV.read("1,\"x\ny\"\n", quote: :double)).must_equal [{0 => '1', 1 => "x\ny"}]
665
+ _(DSV.read("\"a\",\"x\n,y\",\"z\"\n", quote: :double)).must_equal [{0 => 'a', 1 => "x\n,y", 2 => 'z'}]
631
666
  output = written(headers: true, columns: ['a', 'b']){|csv| csv.rows = [{'a' => "x\ny", 'b' => '2'}]; csv.write}
632
667
  _(DSV.read(output, headers: true, quote: :double)).must_equal [{'a' => "x\ny", 'b' => '2'}]
633
668
  end
@@ -642,18 +677,19 @@ describe DSV do
642
677
 
643
678
  it "reads a stray quote in an unquoted field as data, without taking in the next line" do
644
679
  _(DSV.read("12\" pizza,9.99\nnext,row\n", as_array: true)).must_equal [['12" pizza', '9.99'], ['next', 'row']]
645
- _(DSV.read("12\" pizza,9.99\nnext,row\n", as_array: true, quote: :double)).must_equal [['12" pizza', '9.99'], ['next', 'row']]
680
+ _{DSV.read("12\" pizza,9.99\nnext,row\n", as_array: true, quote: :double)}.must_raise DSV::UnquotedField
646
681
  end
647
682
 
648
683
  it "reads a quoted field across lines after a stray quote in the same row" do
649
684
  _(DSV.read("a\"b,\"c\nd\"\n", as_array: true)).must_equal [['a"b', "c\nd"]]
650
- _(DSV.read("a\"b,\"c\nd\"\n", as_array: true, quote: :double)).must_equal [['a"b', "c\nd"]]
685
+ _{DSV.read("a\"b,\"c\nd\"\n", as_array: true, quote: :double)}.must_raise DSV::UnquotedField
651
686
  end
652
687
 
653
688
  it "raises UnterminatedQuote on a quoted field which does not close" do
654
689
  _{DSV.read("\"abc,d\n")}.must_raise DSV::UnterminatedQuote
655
690
  _{DSV.read("\"ab\"c,d\n")}.must_raise DSV::UnterminatedQuote
656
- _{DSV.read("x,\"abc\n", quote: :double)}.must_raise DSV::UnterminatedQuote
691
+ _{DSV.read("\"x\",\"abc\n", quote: :double)}.must_raise DSV::UnterminatedQuote
692
+ _{DSV.read("\"x\",\"y\"\",\"\"\n", quote: :double)}.must_raise DSV::UnterminatedQuote
657
693
  _{DSV.parse_line('x,"abc')}.must_raise DSV::UnterminatedQuote
658
694
  end
659
695
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dsv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.13
4
+ version: 0.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoran
@@ -65,8 +65,11 @@ files:
65
65
  - Rakefile
66
66
  - TODO
67
67
  - dsv.gemspec
68
+ - lib/DSV/Error.rb
68
69
  - lib/DSV/File.rb
69
70
  - lib/DSV/String.rb
71
+ - lib/DSV/UnexpectedRow.rb
72
+ - lib/DSV/UnquotedField.rb
70
73
  - lib/DSV/UnterminatedQuote.rb
71
74
  - lib/DSV/VERSION.rb
72
75
  - lib/dsv.rb