dsv.rb 0.12.13 → 0.12.15

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: 0ca4abca7cb6bd9076317aadd692522f0c182e5b59a3fb446f03914de15f9cb4
4
- data.tar.gz: e1000934e68b46f82f268963ef9658f45d64f7aeb53a91e4601ce5de45995608
3
+ metadata.gz: 17e552f03171a7acc7b504ca028f8e861356fd3db7e9c632cdbb599f71ee82b0
4
+ data.tar.gz: 757077fc667f36bfc8b89f3988f08e229ceb6185d8803f6c13e78878754fbd27
5
5
  SHA512:
6
- metadata.gz: b6869a34fc41967008c873dd888a34fa82f08791ab180a7271a4c68f0c1ee7d6f0e1fdf79a0729711afa4fe6e3c812a4877ff47f5cba6fc5843c03ef5ee7279c
7
- data.tar.gz: fead5bc4cb84b50a9e0698442fc4f7964e31a6cfd3ea4bacfea16c32780180711d57ffe77c9be7556001f3302a5413da52b78b75bf4a3eb8fda5b541a4981d13
6
+ metadata.gz: 8783be7839a75cd0cdc4b437116c608ccae9f318968530551ea6d5c11827f833ef8d588e8da4ff593237953dbc7e3c500f7593cc98836d6d1e98314eb4e02312
7
+ data.tar.gz: 7ad81b76e4fa176c16d241db9c02ecb1b1b1ca5df8c4ed3a62e90d91da12a77aaf435ad0a7ae6e9a64a72e5760784c6a9eb85689a6f680e1ba392b1e0e41d9d1
data/CHANGELOG CHANGED
@@ -1,5 +1,31 @@
1
1
  # dsv/CHANGELOG
2
2
 
3
+ ## 20261120
4
+
5
+ 0.12.15: normalised_mode, row_renderer and write_row are laid out as SimpleCSV 0.11.5 has them.
6
+
7
+ 1. ~ normalised_mode(): Each when and its result are joined by ;, not then.
8
+ 2. ~ write_row(): The values are assigned from an if wrapped in parentheses, rather than one hanging from the assignment.
9
+ 3. ~ row_renderer(): Each quote mode has a branch of its own, giving its renderer.
10
+ 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.
11
+ 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.
12
+ 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.
13
+ 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.
14
+ 8. ~ lib/DSV/VERSION.rb: /0.12.14/0.12.15/.
15
+
16
+ ## 20261108
17
+
18
+ 0.12.14: A space as the column separator splits on each space.
19
+
20
+ 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.
21
+ 2. ~ split_quoted(): A String separator is split on by split_on().
22
+ 3. ~ test/dsv_test.rb: A space separator is tested, under each quote mode, and with a tab.
23
+ 4. ~ README.md: A String separator is split on as given.
24
+ 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.
25
+ 6. ~ ROADMAP.md: Each release is stated as what it does and why; what is undecided is moved to PROPOSED.md.
26
+ 7. + PROPOSED.md: What is undecided, under a heading for each theme or change.
27
+ 8. ~ lib/DSV/VERSION.rb: /0.12.13/0.12.14/.
28
+
3
29
  ## 20261103
4
30
 
5
31
  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
 
data/TODO CHANGED
@@ -1,43 +1,39 @@
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. filter, a class-level alias of select. (0.12.x)
4
+ 2. The internals private. (0.12.x)
5
+ 3. split_quoted() renamed quoted_fields(), and split_double_quoted() double_quoted_fields(). (0.12.x)
6
+ 4. An unknown quote mode, file mode or option raises ArgumentError. (0.13.0)
7
+ 5. :unspecified, the name of the default. (0.13.0)
8
+ 6. :unquoted, the name of the mode reading no quotes, :none kept as an alias. (0.13.0)
9
+ 7. The reader chosen once, when the quote mode is set, as row_renderer is for writing. (0.13.0)
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. (0.13.0)
11
+ 9. Under :double, a row not starting with a quote raises, the header included. (0.13.0)
12
+ 10. Under :double, a bare field in a row read in full raises. (0.13.0)
13
+ 11. :single read as :double is, with a single quote, once :double raises. (0.13.0)
14
+ 12. each yields each row as it is read. (0.14.0)
15
+ 13. each returns its receiver. (0.14.0)
16
+ 14. rows no longer filled by each. (0.14.0)
17
+ 15. Chunks from each_slice. (0.14.0)
18
+ 16. The source read once, start to end, with no rewind; the header from the first row read. (0.14.0)
19
+ 17. The line of an error found by seeking only where the source can. (0.14.0)
20
+ 18. A rewind left to r+ rewriting and to first_row called alone. (0.14.0)
21
+ 19. 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. (1.0)
22
+ 20. A benchmark script against stdlib CSV. (1.0)
23
+ 21. README: the speed sentence written from the benchmark's output. (1.0)
24
+ 22. README: every mode by example. (1.0)
25
+ 23. README: a table matching each mode to stdlib CSV's and Python's settings. (1.0)
26
+ 24. A per-row fast path for the default: split, the fields bare or wrapped in two quotes unwrapped, any other row read in full. (1.x)
27
+ 25. as_array: returning the split fields, rather than building a Hash and then an Array from it. (1.x)
28
+ 26. Rows built by zip.to_h. (1.x)
29
+ 27. The scanner-based parser, at the tag scanner-parser-20261006, judged against the string operations used now. (1.x)
30
+ 28. :rfc_4180, a quote mode reading what RFC 4180 allows as :unspecified does and raising on what it forbids. (1.x)
31
+ 29. field_count:, :header or an Integer, off by default: a row of another width raises. (1.x)
32
+ 30. Writing with minimal quoting: a field quoted only where it holds a separator, a quote or a row terminator. (1.x)
33
+ 31. A line number in each parse error, found only when the error is raised, where the source can seek. (1.x)
34
+ 32. line_numbers:, off by default, counting lines as they are read, for any source, a pipe included. (1.x)
35
+ 33. headers?, the instance-level header_row renamed, header_row kept as an alias. (1.x)
36
+ 34. <<, to append a row. (1.x)
37
+ 35. generate, to build a string. (1.x)
38
+ 36. Symbols and Strings interchangeable for column names, in a selection, a columns: list and a row's keys. (1.x)
39
+ 37. Duplicate headers by option: all kept as an Array, the default; indexed, a, a.1; first; last; error. (1.x)
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.12.15'
6
6
  end
data/lib/dsv.rb CHANGED
@@ -22,11 +22,11 @@ class DSV
22
22
  # A mode as Ruby's File spells it, from any of the spellings the library accepts, a Symbol included; unspecified is r.
23
23
  def normalised_mode(mode)
24
24
  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'
25
+ when 'r', 'r+', 'w', 'w+', 'a', 'a+'; mode.to_s
26
+ when 'read_only', 'read-only', 'readonly'; 'r'
27
+ when 'rw', 'read_write', 'read-write', 'readwrite'; 'r+'
28
+ when 'write_only', 'write-only', 'writeonly'; 'w'
29
+ when 'append'; 'a'
30
30
  else 'r'
31
31
  end
32
32
  end
@@ -281,9 +281,13 @@ class DSV
281
281
  end
282
282
  end
283
283
 
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.
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. A single space is split on as / /, since String#split takes ' ' to mean runs of whitespace, any leading run dropped.
285
285
  def split_on(row, separator)
286
- separator.is_a?(::Regexp) ? pieces_and_separators(row, separator).first : row.split(separator, -1)
286
+ case separator
287
+ when ::Regexp; pieces_and_separators(row, separator).first
288
+ when ' '; row.split(/ /, -1)
289
+ else row.split(separator, -1)
290
+ end
287
291
  end
288
292
 
289
293
  # 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,7 +307,7 @@ class DSV
303
307
  if @column_separator.is_a?(::Regexp)
304
308
  reassemble_quoted_fields(*pieces_and_separators(row, @column_separator))
305
309
  else
306
- reassemble_quoted_fields(row.split(@column_separator, -1))
310
+ reassemble_quoted_fields(split_on(row, @column_separator))
307
311
  end
308
312
  end
309
313
 
@@ -450,13 +454,15 @@ end
450
454
  # 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
455
  def write_row(row, *selected_columns)
452
456
  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
457
+ values = (
458
+ if !selected_columns.empty?
459
+ selected_columns.collect{|column| row[column]}
460
+ elsif attributes
461
+ values_in_order(row)
462
+ else
463
+ row.values
464
+ end
465
+ )
460
466
  write_values(values)
461
467
  end
462
468
  alias_method :write_csv_row, :write_row
@@ -474,20 +480,14 @@ end
474
480
 
475
481
  # 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
482
  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}
483
+ separator = writable_column_separator
484
+ case @quote&.to_sym
485
+ when :none, :unquoted; unquoted_row_renderer(separator)
486
+ when :spacey_none, :spacey_unquoted; unquoted_row_renderer(separator + ' ')
487
+ when :single; quoted_row_renderer("'", separator)
488
+ when :spacey_single; quoted_row_renderer("'", separator + ' ')
489
+ when :spacey_double; quoted_row_renderer('"', separator + ' ')
490
+ else quoted_row_renderer('"', separator)
491
491
  end
492
492
  end
493
493
 
@@ -563,4 +563,15 @@ end
563
563
  end
564
564
  end
565
565
 
566
+ private
567
+
568
+ def quoted_row_renderer(quote, separator)
569
+ doubled_quote = quote * 2
570
+ ->(values){values.collect{|value| quote + value.to_s.gsub(quote, doubled_quote) + quote}.join(separator) + @row_separator}
571
+ end
572
+
573
+ def unquoted_row_renderer(separator)
574
+ ->(values){values.join(separator) + @row_separator}
575
+ end
576
+
566
577
  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_equal [{0 => 'a', 1 => '', 2 => 'b'}]
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dsv.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.13
4
+ version: 0.12.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoran