dsv 0.14.2 → 0.15.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: b69244ec05a6f3303e048cf5f4866b026c04497cce84c1281261223f46cae932
4
- data.tar.gz: 1f2b028014b57beed86a853ec8a36c2b0a53b3db0b0e88fcabf77645b091f329
3
+ metadata.gz: 4914f31a3cf3aa41ed6dd8ff27b3d8b7e47910548b4d1f17dc8e6e45b5dd4722
4
+ data.tar.gz: 483c3d575cbe4dab8e18c2898f7e62e594d67846d55edd984c08261e98611efb
5
5
  SHA512:
6
- metadata.gz: 9353eb0403013111353b18ec8b39c7f6c035e20b204f7c99287377f1429196b94cfa6c9fcad0dac4e8d8a1782c58ad3de65666936b95ed05a4951e6bf43a47ce
7
- data.tar.gz: 1e1ee800d9651f883ef517cf9ac074a80e8a8731c98fec4cb05a06f65e27974b4af1ce426cf828d00926d369c88fd30f45f54c6e0e257d38ada1ff1abfabc72f
6
+ metadata.gz: 3af32b101d386980e623d9eced7e9e4fd882fb5185b56960888023ef72d4115e6ca669cf3199952c2f5599387826e31a3d2712250600d7a893d841580559bd2f
7
+ data.tar.gz: d94eecf097300e5d1008bd62e55b6f24aeb23830bb864e572f774313b347f0efa3c0fe715a3833e2e8d439daefd849f3a9b981a63db2e3aff1f3e48b816f9f57
data/CHANGELOG CHANGED
@@ -2,6 +2,16 @@
2
2
 
3
3
  ## 20261126
4
4
 
5
+ 0.15.0: ~ DSV#read: Under :single, every field is read as single-quoted.
6
+
7
+ 1. ~ split_row(): Under :single, :single_quoted and :single_quotes, every field is read as single-quoted, and a row with a field which isn't raises UnquotedField. They had been read as unspecified.
8
+ 2. ~ split_wholly_quoted(), reassemble_wholly_quoted_fields(), unescaped_fields(), quoted_separator(), trailing_quotes(): Each takes the quote, of which quoted_separator() makes a separator once for each. A :double read of quoted fields takes about a sixth less time, the quote no longer being made from a literal for each field.
9
+ 3. ~ test/dsv_test.rb: Reading under :single is tested, reading back what it writes and a mode set after construction included.
10
+ 4. ~ README.md: Reading under :single is described.
11
+ 5. ~ TODO: The job for :single is gone.
12
+ 6. ~ ROADMAP.md: filter is 0.16.0, the declarations 0.17.0, without :single, and streaming 0.18.0.
13
+ 7. ~ DSV::VERSION: /0.14.2/0.15.0/
14
+
5
15
  0.14.2: ~ DSV.new: A path or text no longer has its header read twice.
6
16
 
7
17
  1. ~ initialize(): A String source is opened by DSV::File.source or DSV::String.source, rather than taken from a DSV::File or DSV::String built and thrown away, whose own initialize had already read the header.
data/README.md CHANGED
@@ -94,9 +94,10 @@ Unspecified, quoting follows RFC 4180: a quoted field may hold the separator or
94
94
  ```ruby
95
95
  DSV.read('plain.csv', headers: true, quote: :none) # no field is quoted; quotes are data
96
96
  DSV.read('quoted.csv', headers: true, quote: :double) # every field is quoted, as DSV writes them
97
+ DSV.read('single.csv', headers: true, quote: :single) # every field is quoted with a single quote
97
98
  ```
98
99
 
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
+ 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. Under `:single` every field is single-quoted and held to as under `:double`, so a double quote is data; with no mode named, a single quote is data.
100
101
 
101
102
  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
103
 
data/TODO CHANGED
@@ -11,39 +11,38 @@
11
11
  4. :unquoted, the name of the mode reading no quotes, :none kept as an alias.
12
12
  5. The reader chosen once, when the quote mode is set, as row_renderer is for writing.
13
13
  6. .read and .open taking a path, and .parse text; a path which does not exist raising, and nothing inferred from whether a file exists.
14
- 7. :single read as :double is, with a single quote.
15
- 8. attributes= setting columns, attributes always worked out from columns; rows read afterwards keyed by those names.
14
+ 7. attributes= setting columns, attributes always worked out from columns; rows read afterwards keyed by those names.
16
15
 
17
16
  ## Streaming
18
17
 
19
- 9. each yields each row as it is read.
20
- 10. each returns its receiver.
21
- 11. rows no longer filled by each.
22
- 12. Chunks from each_slice.
23
- 13. The source read once, start to end, with no rewind; the header from the first row read.
24
- 14. The line of an error found by seeking only where the source can.
25
- 15. A rewind left to r+ rewriting and to first_row called alone.
18
+ 8. each yields each row as it is read.
19
+ 9. each returns its receiver.
20
+ 10. rows no longer filled by each.
21
+ 11. Chunks from each_slice.
22
+ 12. The source read once, start to end, with no rewind; the header from the first row read.
23
+ 13. The line of an error found by seeking only where the source can.
24
+ 14. A rewind left to r+ rewriting and to first_row called alone.
26
25
 
27
26
  ## Checked and measured
28
27
 
29
- 16. 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.
30
- 17. A benchmark script against stdlib CSV, and the README's speed sentence written from its output.
31
- 18. The README showing every mode by example, and a table matching each to stdlib CSV's and Python's settings.
28
+ 15. 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.
29
+ 16. A benchmark script against stdlib CSV, and the README's speed sentence written from its output.
30
+ 17. The README showing every mode by example, and a table matching each to stdlib CSV's and Python's settings.
32
31
 
33
32
  ## Speed, results unchanged
34
33
 
35
- 19. A per-row fast path for the default: split, the fields bare or wrapped in two quotes unwrapped, any other row read in full.
36
- 20. as_array: returning the split fields, rather than building a Hash and then an Array from it.
37
- 21. Rows built by zip.to_h.
38
- 22. The scanner-based parser, at the tag scanner-parser-20261006, judged against the string operations used now.
34
+ 18. A per-row fast path for the default: split, the fields bare or wrapped in two quotes unwrapped, any other row read in full.
35
+ 19. as_array: returning the split fields, rather than building a Hash and then an Array from it.
36
+ 20. Rows built by zip.to_h.
37
+ 21. The scanner-based parser, at the tag scanner-parser-20261006, judged against the string operations used now.
39
38
 
40
39
  ## Features
41
40
 
42
- 23. :rfc_4180, a quote mode reading what RFC 4180 allows as :unspecified does and raising on what it forbids.
43
- 24. field_count:, :header or an Integer, off by default; a row of another width raising.
44
- 25. Writing with minimal quoting: a field quoted only where it holds a separator, a quote or a row terminator.
45
- 26. A line number in each parse error, and line_numbers:.
46
- 27. headers?, the instance-level header_row renamed, header_row kept as an alias.
47
- 28. <<, to append a row, and generate, to build a string.
48
- 29. Symbols and Strings interchangeable for column names, in a selection, a columns: list and a row's keys.
49
- 30. Duplicate headers by option: all kept as an Array, the default; indexed, a, a.1; first; last; error.
41
+ 22. :rfc_4180, a quote mode reading what RFC 4180 allows as :unspecified does and raising on what it forbids.
42
+ 23. field_count:, :header or an Integer, off by default; a row of another width raising.
43
+ 24. Writing with minimal quoting: a field quoted only where it holds a separator, a quote or a row terminator.
44
+ 25. A line number in each parse error, and line_numbers:.
45
+ 26. headers?, the instance-level header_row renamed, header_row kept as an alias.
46
+ 27. <<, to append a row, and generate, to build a string.
47
+ 28. Symbols and Strings interchangeable for column names, in a selection, a columns: list and a row's keys.
48
+ 29. Duplicate headers by option: all kept as an Array, the default; indexed, a, a.1; first; last; error.
data/lib/DSV/VERSION.rb CHANGED
@@ -2,5 +2,5 @@
2
2
  # DSV::VERSION
3
3
 
4
4
  class DSV
5
- VERSION = '0.14.2'
5
+ VERSION = '0.15.0'
6
6
  end
data/lib/dsv.rb CHANGED
@@ -392,7 +392,9 @@ class DSV
392
392
  when :none, :unquoted
393
393
  split_on(row, @column_separator)
394
394
  when :double, :double_quoted, :double_quotes
395
- split_wholly_quoted(row)
395
+ split_wholly_quoted(row, '"')
396
+ when :single, :single_quoted, :single_quotes
397
+ split_wholly_quoted(row, "'")
396
398
  else
397
399
  split_quoted(row)
398
400
  end
@@ -428,17 +430,17 @@ class DSV
428
430
  end
429
431
  end
430
432
 
431
- # 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.
432
- def split_wholly_quoted(row)
433
- raise UnquotedField.new(:double) unless row.start_with?('"')
434
- fields = split_on(row.delete_prefix('"'), quoted_separator)
435
- return reassemble_wholly_quoted_fields(row) unless fields.last && trailing_quotes(fields.last, -1).odd?
436
- fields[-1] = fields.last.delete_suffix('"')
437
- unescaped_fields(fields) || reassemble_wholly_quoted_fields(row)
433
+ # The contract of :double and :single, every field quoted with the quote given: 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 the quote raises UnquotedField, at the cost of one byte compared. nil where the last field does not close.
434
+ def split_wholly_quoted(row, quote)
435
+ raise UnquotedField.new(quote == "'" ? :single : :double) unless row.start_with?(quote)
436
+ fields = split_on(row.delete_prefix(quote), quoted_separator(quote))
437
+ return reassemble_wholly_quoted_fields(row, quote) unless fields.last && trailing_quotes(fields.last, -1, quote.getbyte(0)).odd?
438
+ fields[-1] = fields.last.delete_suffix(quote)
439
+ unescaped_fields(fields, quote) || reassemble_wholly_quoted_fields(row, quote)
438
440
  end
439
441
 
440
- # 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.
441
- def reassemble_wholly_quoted_fields(row)
442
+ # A wholly quoted row 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.
443
+ def reassemble_wholly_quoted_fields(row, quote)
442
444
  pieces, separators = (
443
445
  if @column_separator.is_a?(::Regexp)
444
446
  pieces_and_separators(row, @column_separator)
@@ -448,40 +450,42 @@ class DSV
448
450
  )
449
451
  fields = []
450
452
  buffer = nil
453
+ quote_byte, escaped = quote.getbyte(0), quote * 2
451
454
  pieces.each_with_index do |piece, i|
452
- if buffer && trailing_quotes(piece, -1).even?
455
+ if buffer && trailing_quotes(piece, -1, quote_byte).even?
453
456
  buffer << separator_before(i, separators) << piece
454
457
  elsif buffer
455
- fields << (buffer << separator_before(i, separators) << piece.delete_suffix('"')).gsub('""', '"')
458
+ fields << (buffer << separator_before(i, separators) << piece.delete_suffix(quote)).gsub(escaped, quote)
456
459
  buffer = nil
457
- elsif !piece.start_with?('"')
458
- raise UnquotedField.new(:double)
459
- elsif trailing_quotes(piece, 0).even?
460
- buffer = +piece.delete_prefix('"')
460
+ elsif !piece.start_with?(quote)
461
+ raise UnquotedField.new(quote == "'" ? :single : :double)
462
+ elsif trailing_quotes(piece, 0, quote_byte).even?
463
+ buffer = +piece.delete_prefix(quote)
461
464
  else
462
- fields << piece.delete_prefix('"').delete_suffix('"').gsub('""', '"')
465
+ fields << piece.delete_prefix(quote).delete_suffix(quote).gsub(escaped, quote)
463
466
  end
464
467
  end
465
468
  fields unless buffer
466
469
  end
467
470
 
468
471
  # 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.
469
- def unescaped_fields(fields)
472
+ def unescaped_fields(fields, quote)
473
+ escaped = quote * 2
470
474
  fields.collect do |field|
471
- next field unless field.include?('"')
472
- unescaped = field.gsub('""', '"')
473
- return nil unless field.count('"') == 2 * unescaped.count('"')
475
+ next field unless field.include?(quote)
476
+ unescaped = field.gsub(escaped, quote)
477
+ return nil unless field.count(quote) == 2 * unescaped.count(quote)
474
478
  unescaped
475
479
  end
476
480
  end
477
481
 
478
- # Quote, separator, quote: what divides the fields of a wholly quoted row.
479
- def quoted_separator
480
- @quoted_separator ||= (
482
+ # Quote, separator, quote: what divides the fields of a wholly quoted row, made once for each quote.
483
+ def quoted_separator(quote)
484
+ (@quoted_separators ||= {})[quote] ||= (
481
485
  if @column_separator.is_a?(::Regexp)
482
- ::Regexp.new('"(?:' + @column_separator.source + ')"', @column_separator.options)
486
+ ::Regexp.new(quote + '(?:' + @column_separator.source + ')' + quote, @column_separator.options)
483
487
  else
484
- '"' + @column_separator + '"'
488
+ quote + @column_separator + quote
485
489
  end
486
490
  )
487
491
  end
@@ -507,10 +511,10 @@ class DSV
507
511
  fields unless buffer
508
512
  end
509
513
 
510
- # How many quotes a piece ends with, counting back to but not including the byte given, so that a field's opening quote is not taken for part of its closing. Read by byte, a quote being 34 in any ASCII-compatible encoding, so that nothing is allocated per character.
511
- def trailing_quotes(piece, floor)
514
+ # How many quotes a piece ends with, counting back to but not including the byte given, so that a field's opening quote is not taken for part of its closing. Read by byte, a double quote being 34 and a single quote 39 in any ASCII-compatible encoding, so that nothing is allocated per character.
515
+ def trailing_quotes(piece, floor, quote_byte = 34)
512
516
  i = piece.bytesize - 1
513
- i -= 1 while i > floor && piece.getbyte(i) == 34
517
+ i -= 1 while i > floor && piece.getbyte(i) == quote_byte
514
518
  piece.bytesize - 1 - i
515
519
  end
516
520
 
data/test/dsv_test.rb CHANGED
@@ -682,6 +682,40 @@ describe DSV do
682
682
  output = written(headers: true, columns: ['a', 'b']){|csv| csv.rows = [{'a' => "x\ny", 'b' => '2'}]; csv.write}
683
683
  _(DSV.read(output, headers: true, quote: :double)).must_equal [{'a' => "x\ny", 'b' => '2'}]
684
684
  end
685
+
686
+ it "reads under quote: :single as under :double, with a single quote" do
687
+ _(DSV.parse_line("'a,b','c'", quote: :single)).must_equal ['a,b', 'c']
688
+ _(DSV.read("'a','b'\n'it''s','2'\n", headers: true, quote: :single)).must_equal [{'a' => "it's", 'b' => '2'}]
689
+ _(DSV.read("'a'',''b','c'\n", quote: :single_quoted)).must_equal [{0 => "a','b", 1 => 'c'}]
690
+ _(DSV.read("'x\ny','2'\n", quote: 'single')).must_equal [{0 => "x\ny", 1 => '2'}]
691
+ _(DSV.read("'a'; 'b'\n'x; y';'2'\n", headers: true, quote: :single_quotes, column_separator: /;\s*/)).must_equal [{'a' => 'x; y', 'b' => '2'}]
692
+ end
693
+
694
+ it "reads a double quote as data under quote: :single, and a single quote as data with no mode named" do
695
+ _(DSV.parse_line("'\"a\"','b'", quote: :single)).must_equal ['"a"', 'b']
696
+ _(DSV.parse_line("'a','b'")).must_equal ["'a'", "'b'"]
697
+ end
698
+
699
+ it "raises UnquotedField on a row under quote: :single with a field not quoted, naming its line and the mode" do
700
+ _{DSV.parse_line("O'Brien,'x'", quote: :single)}.must_raise DSV::UnquotedField
701
+ _{DSV.read("'a',b,'c'\n", quote: :single)}.must_raise DSV::UnquotedField
702
+ _{DSV.read("\"a\",\"b\"\n", quote: :single)}.must_raise DSV::UnquotedField
703
+ error = _{DSV.read("'a','b'\n'y',3\n", quote: :single)}.must_raise DSV::UnquotedField
704
+ _(error.line).must_equal 2
705
+ _(error.mode).must_equal :single
706
+ _{DSV.read("'x','abc\n", quote: :single)}.must_raise DSV::UnterminatedQuote
707
+ end
708
+
709
+ it "reads back what it writes under quote: :single" do
710
+ output = written(headers: true, columns: ['a', 'b'], quote: :single){|csv| csv.rows = [{'a' => "it's, here", 'b' => "x\ny"}]; csv.write}
711
+ _(DSV.read(output, headers: true, quote: :single)).must_equal [{'a' => "it's, here", 'b' => "x\ny"}]
712
+ end
713
+
714
+ it "reads with the quote of a mode set after construction" do
715
+ csv = DSV.new("'a','b'\n")
716
+ csv.quote = :single
717
+ _(csv.read).must_equal [{0 => 'a', 1 => 'b'}]
718
+ end
685
719
  end
686
720
 
687
721
  # Input which is not RFC 4180, read to the default rule: a quote is structure only where it opens a field, and a field it opens which does not close raises UnterminatedQuote.
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.14.2
4
+ version: 0.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoran