zsv 1.4.3 → 1.4.4

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.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +140 -2
  3. data/README.md +129 -275
  4. data/ext/zsv/ZSV_UPSTREAM +7 -0
  5. data/ext/zsv/common.h +0 -12
  6. data/ext/zsv/extconf.rb +70 -122
  7. data/ext/zsv/options.c +63 -62
  8. data/ext/zsv/options.h +8 -11
  9. data/ext/zsv/parser.c +390 -201
  10. data/ext/zsv/parser.h +35 -22
  11. data/ext/zsv/row.c +75 -28
  12. data/ext/zsv/row.h +15 -9
  13. data/ext/zsv/upstream.rb +55 -0
  14. data/ext/zsv/vendor/zsv/LICENSE +21 -0
  15. data/ext/zsv/vendor/zsv/VENDORED_FROM +2 -0
  16. data/ext/zsv/vendor/zsv/configure +1212 -0
  17. data/ext/zsv/vendor/zsv/include/zsv/api.h +375 -0
  18. data/ext/zsv/vendor/zsv/include/zsv/common.h +397 -0
  19. data/ext/zsv/vendor/zsv/include/zsv/ext/implementation.h +62 -0
  20. data/ext/zsv/vendor/zsv/include/zsv/ext/implementation_private.h +113 -0
  21. data/ext/zsv/vendor/zsv/include/zsv/ext/sheet.h +80 -0
  22. data/ext/zsv/vendor/zsv/include/zsv/ext.h +333 -0
  23. data/ext/zsv/vendor/zsv/include/zsv/utils/appname.h +51 -0
  24. data/ext/zsv/vendor/zsv/include/zsv/utils/arg.h +139 -0
  25. data/ext/zsv/vendor/zsv/include/zsv/utils/cache.h +49 -0
  26. data/ext/zsv/vendor/zsv/include/zsv/utils/clock.h +36 -0
  27. data/ext/zsv/vendor/zsv/include/zsv/utils/compiler.h +69 -0
  28. data/ext/zsv/vendor/zsv/include/zsv/utils/db.h +19 -0
  29. data/ext/zsv/vendor/zsv/include/zsv/utils/dirs.h +149 -0
  30. data/ext/zsv/vendor/zsv/include/zsv/utils/dl.h +22 -0
  31. data/ext/zsv/vendor/zsv/include/zsv/utils/emcc/fs_api.h +28 -0
  32. data/ext/zsv/vendor/zsv/include/zsv/utils/err.h +22 -0
  33. data/ext/zsv/vendor/zsv/include/zsv/utils/file-mem.h +18 -0
  34. data/ext/zsv/vendor/zsv/include/zsv/utils/file.h +125 -0
  35. data/ext/zsv/vendor/zsv/include/zsv/utils/jq.h +65 -0
  36. data/ext/zsv/vendor/zsv/include/zsv/utils/json.h +19 -0
  37. data/ext/zsv/vendor/zsv/include/zsv/utils/mem.h +19 -0
  38. data/ext/zsv/vendor/zsv/include/zsv/utils/memmem.h +13 -0
  39. data/ext/zsv/vendor/zsv/include/zsv/utils/os.h +145 -0
  40. data/ext/zsv/vendor/zsv/include/zsv/utils/output.h +34 -0
  41. data/ext/zsv/vendor/zsv/include/zsv/utils/overwrite.h +71 -0
  42. data/ext/zsv/vendor/zsv/include/zsv/utils/overwrite_writer.h +53 -0
  43. data/ext/zsv/vendor/zsv/include/zsv/utils/prop.h +119 -0
  44. data/ext/zsv/vendor/zsv/include/zsv/utils/rng.h +35 -0
  45. data/ext/zsv/vendor/zsv/include/zsv/utils/signal.h +25 -0
  46. data/ext/zsv/vendor/zsv/include/zsv/utils/sql.h +13 -0
  47. data/ext/zsv/vendor/zsv/include/zsv/utils/string.h +173 -0
  48. data/ext/zsv/vendor/zsv/include/zsv/utils/utf8.h +41 -0
  49. data/ext/zsv/vendor/zsv/include/zsv/utils/win/dl.h +25 -0
  50. data/ext/zsv/vendor/zsv/include/zsv/utils/writer.h +109 -0
  51. data/ext/zsv/vendor/zsv/include/zsv/zsv_export.h +33 -0
  52. data/ext/zsv/vendor/zsv/include/zsv.h.in +18 -0
  53. data/ext/zsv/vendor/zsv/src/Makefile +238 -0
  54. data/ext/zsv/vendor/zsv/src/vector_delim.c +60 -0
  55. data/ext/zsv/vendor/zsv/src/zsv.c +622 -0
  56. data/ext/zsv/vendor/zsv/src/zsv_internal.c +789 -0
  57. data/ext/zsv/vendor/zsv/src/zsv_scan_delim.c +285 -0
  58. data/ext/zsv/vendor/zsv/src/zsv_scan_delim_fast.c +741 -0
  59. data/ext/zsv/vendor/zsv/src/zsv_scan_fixed.c +88 -0
  60. data/ext/zsv/vendor/zsv/src/zsv_scan_simd_avx2.h +89 -0
  61. data/ext/zsv/vendor/zsv/src/zsv_scan_simd_neon.h +80 -0
  62. data/ext/zsv/vendor/zsv/src/zsv_scan_simd_sse2.h +93 -0
  63. data/ext/zsv/vendor/zsv/src/zsv_scan_simd_wasm.h +79 -0
  64. data/ext/zsv/vendor/zsv/src/zsv_strencode.c +84 -0
  65. data/ext/zsv/zsv_ext.c +176 -105
  66. data/lib/zsv/errors.rb +32 -0
  67. data/lib/zsv/input.rb +88 -0
  68. data/lib/zsv/options.rb +132 -0
  69. data/lib/zsv/strict_validator.rb +124 -0
  70. data/lib/zsv/version.rb +1 -1
  71. data/lib/zsv.rb +137 -35
  72. metadata +82 -11
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8826bd0bbac979e171b7b490065814b7b0f8baa1225d657c32f171db94e75e9e
4
- data.tar.gz: ab5cdf28dae9d799c0f493912671129915332641baea47e8ac54b10606e4dcf6
3
+ metadata.gz: 80777f16398f7ec2992d76b49a67f1be772a1eb03f8219684bcaeffc2a2094ae
4
+ data.tar.gz: 64bb251b0db24011d8632ae7f76ae39af9200d38403f78f49f4f3200ca9c01fa
5
5
  SHA512:
6
- metadata.gz: 44ffa9413a2ae0c4dc15f14f25f50a6f77274b1ab111c8e89cecf49c32d0e2d42852add435095d0ebaff60854326882775f3e9e260b09028122e85f97c168832
7
- data.tar.gz: 339f0d40fef118b0457e299e019f324be068eec7386ac872a4e924bded86d9e59c40b326d8541ac125ee13ed14785c84e53867b33f96dd659b7f04da2b2f4c3a
6
+ metadata.gz: c9fa2228e696cddee3c0c9955f8012ae4095ec68f27528e0815c24998ef24589107fd6aea6433d0b6831fd149800e310750968283198ddf1b82595e7b225889c
7
+ data.tar.gz: 29d1ae51f114c31354c0eb318060a790ebc954da5d0b14efe54c77f240409590e03e34fa88876e13e08c4bed41855d34f34b87d0f5903c92dbf2fa185ea7a2e8
data/CHANGELOG.md CHANGED
@@ -2,8 +2,146 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
4
 
5
- The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
- and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
6
+
7
+ Versioning: gem `X.Y.Z` ships the zsv C library `X.Y.Z` (a gem-only release
8
+ adds a fourth segment, `X.Y.Z.N`), so the version number follows zsv, not
9
+ [Semantic Versioning](https://semver.org/spec/v2.0.0.html). A release can
10
+ therefore change behaviour without a major bump: every such change is listed
11
+ under **Changed — breaking**, with what to do before upgrading.
12
+
13
+ ## [Unreleased]
14
+
15
+ ## [1.4.4] - 2026-11-23
16
+
17
+ ### Added
18
+ - IO sources: `ZSV::Parser.new(io)`, `ZSV.parse_enum(io)` read any object
19
+ responding to `#read` (File, StringIO, pipes, sockets) as the rows are
20
+ consumed. Fields carry the IO's external encoding.
21
+ - `ZSV.parse_line(line, **options)`, like `CSV.parse_line`.
22
+ - `ZSV::Parser.from_string(csv, **options)` and
23
+ `ZSV::Parser.from_path(path, **options)`: build a parser without the
24
+ String-is-data-or-path guess of `ZSV::Parser.new`.
25
+ - `headers:` accepts a String (the header names as one CSV line) and
26
+ `:first_row` (same as `true`), as in `CSV`.
27
+ - `ZSV.open` and `ZSV.foreach` accept a read mode: `"r"`, `"rb"`, `"rt"`, and
28
+ `"r:<encoding>"` (`"r:bom|utf-8"`, `"r:iso-8859-1"`). Write modes raise
29
+ `ArgumentError`.
30
+ - Strict mode: `liberal_parsing: false` validates the input like `CSV` does
31
+ and raises `ZSV::MalformedCSVError` (quoting errors) or
32
+ `ZSV::InvalidEncodingError` (bytes invalid in the source encoding), both
33
+ with `#line_number` (alias `#lineno`). It reads the whole input before the
34
+ first row and is slower than the default lenient mode.
35
+ - `max_row_size` (default 262144 bytes) and `max_columns` (default 1024)
36
+ options: the limits past which a row raises instead of being cut.
37
+ - Precompiled gems for x86_64 and aarch64 Linux (glibc and musl) and x86_64
38
+ and arm64 macOS, published with each release: no C compiler needed there.
39
+ - Offline install: the zsv source ships inside the gem, pinned by SHA-256
40
+ (see Security / Packaging). `gem install` never touches the network.
41
+
42
+ ### Changed — breaking, read before upgrading
43
+ - **Unknown options raise `ArgumentError`** (`unknown keyword: :converters`).
44
+ They were silently ignored before. Upgrade: remove options ZSV does not
45
+ implement (`converters`, `strip`, `skip_blanks`, `nil_value`, ...; the
46
+ list is in `docs/API_REFERENCE.md`) and do that work on the rows.
47
+ - **`quote_char` other than `'"'` and `row_sep` other than `:auto`, `"\n"`,
48
+ `"\r\n"` or `"\r"` raise `ArgumentError`.** They were accepted and had no
49
+ effect. Upgrade: drop them; zsv only quotes with `"` and auto-detects the
50
+ three line endings.
51
+ - **`liberal_parsing: false` now validates**: it is slower and raises on
52
+ malformed quoting or invalid encoding, where it used to be ignored (rows
53
+ were always parsed leniently). Upgrade: omit the option, or pass
54
+ `liberal_parsing: true`, to keep the old lenient behaviour and speed.
55
+ - **Field encoding follows the source**: the String's encoding for
56
+ `ZSV.parse`, the IO's external encoding for IOs, `encoding:` or the mode's
57
+ encoding for files (UTF-8 when none is given). Fields were always tagged
58
+ UTF-8 before, so a binary String (`File.binread`, `"..".b`) now yields
59
+ BINARY (ASCII-8BIT) fields. Upgrade: `force_encoding('UTF-8')` the input,
60
+ or pass `encoding: 'UTF-8'`.
61
+ - **A blank line is an empty row `[]`, as in `CSV`**: `ZSV.parse("a\n\nb\n")`
62
+ is `[["a"], [], ["b"]]`, was `[["a"], [""], ["b"]]`. With headers it is a
63
+ Hash with every header mapped to `nil` (was `{first_header => ""}`); blank
64
+ lines before the header row are skipped. A line holding only `""` is now
65
+ `[""]` (it was dropped). Upgrade: `reject(&:empty?)` if you counted on
66
+ non-empty rows.
67
+ - **Header rows match `CSV`**: a row shorter than the headers has `nil` for
68
+ the missing fields (the keys were absent), and when a header name repeats
69
+ its first column wins (the last one did). Upgrade: `row.compact` for the
70
+ old missing-key shape; rename duplicate headers to read every column.
71
+ - **How `ZSV::Parser.new` reads its argument**: a `Pathname` or other
72
+ `#to_path` object that is not an open stream is a path (it was read as an
73
+ IO and looped forever); a String holding a line ending or a NUL byte, or
74
+ empty, is data; otherwise an existing file of that name is a path (a file
75
+ with a comma in its name is found); otherwise a String containing
76
+ `col_sep` is data (`"a,b"`, `"a;b"` with `col_sep: ";"`); anything else is
77
+ a missing path (`Errno::ENOENT`). Upgrade: use `ZSV::Parser.from_string` or
78
+ `from_path` (or `ZSV.parse` / `ZSV.open`) to choose explicitly.
79
+ - **`skip_lines` must be a non-negative Integer**; a Regexp, a String or a
80
+ negative number raises `ArgumentError` (a negative value was ignored).
81
+ Upgrade: skip matching rows yourself (`each.reject { ... }`).
82
+ - **Over-long and over-wide rows raise `ZSV::MalformedCSVError`** instead of
83
+ being returned cut short: a row longer than `max_row_size` (default
84
+ 256 KiB, the old buffer size) and a row with more than `max_columns`
85
+ (default 1024) fields, whose extra fields were dropped. Upgrade: pass a
86
+ larger `max_row_size:` / `max_columns:` for such files.
87
+ - **A source build targets the baseline CPU** (SSE2 on x86_64, NEON on
88
+ aarch64; zsv's SIMD scanner stays on) instead of the build machine's
89
+ (`-march=native`, AVX2). A binary built in a Docker image on one CPU used
90
+ to die with SIGILL on another. Upgrade: nothing to do; to build for the
91
+ local CPU when the code only runs where it was built,
92
+ `gem install zsv --platform ruby -- --enable-native` (or `ZSV_NATIVE=1`).
93
+ On the bundled benchmarks the portable and native builds were within run-to-run noise of each other (x86_64, Ruby 4.0); the Benchmarks workflow reports current numbers.
94
+
95
+ ### Changed
96
+ - Upgraded the bundled zsv C library from 1.4.3 to 1.4.4. Parser-library
97
+ changes are small: malformed-UTF-8 replacement now works on targets where
98
+ `char` is unsigned (e.g. gcc on ARM Linux), plus a new optional per-cell
99
+ malformed-UTF-8 handler this gem does not use yet. Most of the release is the
100
+ zsv CLI. See the
101
+ [zsv 1.4.4 release notes](https://github.com/liquidaty/zsv/releases/tag/v1.4.4).
102
+
103
+ ### Fixed
104
+ - `ZSV.parse` could abort the whole Ruby process (`[BUG] Aborted`).
105
+ - A row longer than the read buffer (256 KiB by default) was returned cut
106
+ short and the rest of it dropped, with no error. A row of up to
107
+ `max_row_size` bytes now always parses completely, whatever `buffer_size`
108
+ is, and a longer row raises `ZSV::MalformedCSVError` with its line number
109
+ (`#line_number`).
110
+ - A row with more than 1024 fields lost the extra fields silently. It now
111
+ raises `ZSV::MalformedCSVError`; `max_columns` raises the limit.
112
+ - `ZSV::Parser#initialize` given a `Pathname` read it as an IO, and
113
+ `Pathname#read(n)` returns the first bytes of the file on every call, so the
114
+ rows repeated forever. A path-like object (`#to_path`, not an open stream) is
115
+ now opened as a path; a `File` is still read from its current position.
116
+ - `ZSV::Parser#each` and `#each_row` returned `nil` with a block; they return
117
+ the parser, like `CSV#each`.
118
+ - Calling `#initialize` again on a parser leaked the first parser's state; it
119
+ now raises `RuntimeError` and leaks nothing.
120
+ - Ruby code run from zsv's callbacks (an IO's `#read`, building rows) could
121
+ raise through zsv's C frames. It now runs under `rb_protect` and the
122
+ exception is re-raised once zsv has returned and the parser is released.
123
+ - The parser now supports GC compaction (its stored objects are marked
124
+ movable and updated), never calls into Ruby while being freed, calls
125
+ `zsv_finish` exactly once at end of input (never from `close` or free) and
126
+ checks its status, and leaks no `FILE *`, zsv parser or memory when options,
127
+ `#read`, the block or a second `#initialize` raise.
128
+ - A parser is no longer re-entered from its own callback or another thread:
129
+ that raises `ZSV::Error` instead of corrupting zsv's state.
130
+ - zsv's warnings no longer print to stderr.
131
+
132
+ ### Security / Packaging
133
+ - `gem install` no longer downloads anything. The zsv source is vendored in the
134
+ gem (`ext/zsv/vendor/zsv`, with zsv's MIT `LICENSE`) and pinned by SHA-256 in
135
+ `ext/zsv/ZSV_UPSTREAM`. Before, `extconf.rb` fetched an unverified tarball
136
+ from GitHub at install time, and retried with TLS verification switched off
137
+ when verification failed.
138
+ - Precompiled platform gems are cross-built with `rake gem:native`
139
+ (rake-compiler-dock) for the baseline CPU of each architecture.
140
+
141
+ ### Tooling
142
+ - `rake zsv:vendor`, `rake zsv:verify` and `rake "zsv:bump[X.Y.Z]"` manage the
143
+ vendored zsv. `bin/zsv-upstream-check` reports whether a newer zsv release
144
+ exists (exit 10 when one does).
7
145
 
8
146
  ## [1.4.3] - 2026-06-26
9
147
 
data/README.md CHANGED
@@ -1,349 +1,203 @@
1
- # ZSV - SIMD-Accelerated CSV Parser for Ruby ⚡
1
+ # zsv
2
2
 
3
- A drop-in replacement for Ruby's CSV stdlib that uses the [zsv](https://github.com/liquidaty/zsv) C library for 5-6x performance improvements via SIMD optimizations.
3
+ [![CI](https://github.com/sebyx07/zsv-ruby/actions/workflows/ci.yml/badge.svg)](https://github.com/sebyx07/zsv-ruby/actions/workflows/ci.yml)
4
+ [![Gem Version](https://img.shields.io/gem/v/zsv)](https://rubygems.org/gems/zsv)
5
+ [![Gem Downloads](https://img.shields.io/gem/dt/zsv)](https://rubygems.org/gems/zsv)
6
+ [![License](https://img.shields.io/github/license/sebyx07/zsv-ruby)](LICENSE)
7
+ [![Maintained by developerz.ai](https://img.shields.io/badge/maintained%20by-developerz.ai-6f42c1)](https://developerz.ai)
4
8
 
5
- > 🤖 Built with [Claude Code](https://claude.com/claude-code)
9
+ A fast CSV parser for Ruby with the reading API of the `CSV` standard library. The parsing is done by [zsv](https://github.com/liquidaty/zsv), a SIMD-accelerated C library, through a native extension.
6
10
 
7
- ## 📚 Documentation
8
-
9
- - [Quick Start Guide](docs/QUICKSTART.md) - Get started in 5 minutes
10
- - [API Reference](docs/API_REFERENCE.md) - Complete API documentation
11
- - [Verification Report](docs/VERIFICATION.md) - Test results and metrics
11
+ ```ruby
12
+ require 'zsv'
12
13
 
13
- ## ✨ Features
14
+ ZSV.foreach('data.csv', headers: true) do |row|
15
+ puts row['name']
16
+ end
17
+ ```
14
18
 
15
- - **Blazing Fast**: 5-6x faster than Ruby's CSV stdlib thanks to SIMD optimizations
16
- - **Memory Efficient**: Streaming parser that doesn't load entire files into memory
17
- - **API Compatible**: Familiar interface matching Ruby's CSV class
18
- - **Native Extension**: Direct C integration for minimal overhead
19
- - **Ruby 3.3+**: Modern Ruby support with proper encoding handling
19
+ - **Familiar**: `ZSV.foreach`, `ZSV.parse`, `ZSV.read`, `ZSV.open`, `ZSV::Parser#shift` / `#each`, with the option names `CSV` uses.
20
+ - **Streaming**: rows are produced as the input is read; a file is never loaded whole.
21
+ - **Checked against `CSV`**: the test suite compares rows with the standard library's for the same input and option. The [intended differences](#differences-from-csv) are few and listed.
22
+ - **Self-contained**: the zsv source ships inside the gem, pinned by SHA-256. Installing downloads nothing beyond the gem and needs no system library.
20
23
 
21
- ## 📦 Installation
24
+ It parses; it does not write CSV. For generating CSV, converters, or `CSV::Table`, keep using `csv`.
22
25
 
23
- Add to your Gemfile:
26
+ ## Install
24
27
 
25
28
  ```ruby
29
+ # Gemfile
26
30
  gem 'zsv'
27
31
  ```
28
32
 
29
- Or install directly:
33
+ or `gem install zsv`.
30
34
 
31
- ```bash
32
- gem install zsv
33
- ```
35
+ - **Precompiled gems** are published, from 1.4.4 on, for Linux (x86_64 and aarch64, glibc and musl) and macOS (x86_64 and arm64): no compiler needed. They are built for the baseline CPU of each architecture (SSE2 / NEON).
36
+ - **Everywhere else** the source gem compiles on install. That needs a C compiler and `make`, and nothing from the network: the zsv C source is inside the gem. A source build also targets the baseline CPU (SSE2 / NEON), so a binary built on one machine (a Docker image built in CI, for example) runs on any other machine of the same architecture. When the code runs only where it was built, you can build for that machine's CPU instead (`-march=native`):
34
37
 
35
- The gem will automatically download and compile zsv 1.4.3 during installation.
38
+ ```bash
39
+ gem install zsv --platform ruby -- --enable-native
40
+ # or ZSV_NATIVE=1 gem install zsv --platform ruby; with Bundler:
41
+ bundle config set --local build.zsv --enable-native
42
+ bundle config set --local force_ruby_platform true
43
+ ```
36
44
 
37
- ## 🚀 Usage
45
+ Supported Rubies and platforms are the ones CI tests: see the matrix in [`.github/workflows/ci.yml`](.github/workflows/ci.yml). The Ruby floor is `required_ruby_version` in [`zsv.gemspec`](zsv.gemspec). Windows is not supported yet.
38
46
 
39
- ### Basic Parsing
47
+ **Versioning.** The gem version is the version of the zsv library it wraps: gem `1.4.4` ships zsv `1.4.4`. A gem-only release adds a fourth segment (`1.4.4.1`).
40
48
 
41
- ```ruby
42
- require 'zsv'
49
+ ## Usage
43
50
 
44
- # Parse entire file
45
- rows = ZSV.read("data.csv")
46
- # => [["a", "b", "c"], ["1", "2", "3"]]
51
+ ### Files
47
52
 
48
- # Stream rows (memory efficient)
49
- ZSV.foreach("large_file.csv") do |row|
50
- puts row.inspect
51
- end
53
+ ```ruby
54
+ # Stream rows; nothing is kept
55
+ ZSV.foreach('data.csv') { |row| p row } # row is an Array of Strings
52
56
 
53
- # Parse string
54
- rows = ZSV.parse("a,b,c\n1,2,3\n")
57
+ # Without a block: an Enumerator
58
+ ZSV.foreach('data.csv').with_index { |row, i| ... }
59
+ ZSV.foreach('data.csv').lazy.select { |row| row[2] == 'paid' }.first(10)
60
+
61
+ # Everything at once
62
+ rows = ZSV.read('data.csv') # => [["a", "b"], ["1", "2"]]
55
63
  ```
56
64
 
57
- ### Headers Mode
65
+ ### Strings
58
66
 
59
67
  ```ruby
60
- # Use first row as headers
61
- ZSV.foreach("data.csv", headers: true) do |row|
62
- puts row["name"] # Hash access
63
- end
64
-
65
- # Provide custom headers
66
- ZSV.foreach("data.csv", headers: ["id", "name", "email"]) do |row|
67
- puts row["name"]
68
- end
68
+ ZSV.parse("a,b\n1,2\n") # => [["a", "b"], ["1", "2"]]
69
+ ZSV.parse_line('a,"b,c"') # => ["a", "b,c"]
69
70
  ```
70
71
 
71
- ### Parser Instance
72
+ ### Headers
72
73
 
73
74
  ```ruby
74
- # Create parser
75
- parser = ZSV.open("data.csv", headers: true)
76
-
77
- # Read rows one at a time
78
- row = parser.shift
79
- row = parser.shift
80
-
81
- # Iterate all rows
82
- parser.each do |row|
83
- puts row
84
- end
85
-
86
- # Rewind to beginning
87
- parser.rewind
88
-
89
- # Clean up
90
- parser.close
75
+ ZSV.parse("name,age\nAda,36\n", headers: true)
76
+ # => [{"name" => "Ada", "age" => "36"}]
91
77
 
92
- # Or use block form (auto-closes)
93
- ZSV.open("data.csv") do |parser|
94
- parser.each { |row| puts row }
95
- end
78
+ # Supply the headers yourself (the first row is then data)
79
+ ZSV.foreach('data.csv', headers: %w[id name email]) { |row| row['email'] }
96
80
  ```
97
81
 
98
- ### Enumerable Methods
82
+ With headers each row is a plain `Hash`, not a `CSV::Row`.
99
83
 
100
- The parser includes `Enumerable`, so you can use `map`, `select`, `find`, etc.:
84
+ ### A parser object
101
85
 
102
86
  ```ruby
103
- # Transform rows
104
- names = ZSV.open("users.csv", headers: true) do |parser|
105
- parser.map { |row| row["name"].upcase }
106
- end
107
-
108
- # Filter rows
109
- adults = ZSV.open("users.csv", headers: true) do |parser|
110
- parser.select { |row| row["age"].to_i >= 18 }
87
+ ZSV.open('data.csv', headers: true) do |csv| # closed when the block returns
88
+ first = csv.shift # one row, or nil at the end
89
+ csv.headers # => ["name", "age"] (nil until the first row is read)
90
+ csv.each { |row| ... } # the rest
91
+ csv.rewind
111
92
  end
112
93
 
113
- # Find first match
114
- admin = ZSV.open("users.csv", headers: true) do |parser|
115
- parser.find { |row| row["role"] == "admin" }
116
- end
94
+ parser = ZSV::Parser.new(io) # any object with #read; or a path, a Pathname, CSV data
95
+ parser.each_slice(1_000) { |batch| ... } # Enumerable
96
+ parser.close
117
97
  ```
118
98
 
99
+ `ZSV::Parser.new` guesses whether a String is CSV data or a path (see the [API reference](docs/API_REFERENCE.md)); `ZSV::Parser.from_string` and `ZSV::Parser.from_path` do not guess.
100
+
119
101
  ### Options
120
102
 
121
- All parsing methods accept these options:
103
+ The common ones; the complete list, with defaults and every accepted value, is in [`docs/API_REFERENCE.md`](docs/API_REFERENCE.md).
122
104
 
123
- | Option | Type | Default | Description |
124
- |--------|------|---------|-------------|
125
- | `headers` | Boolean/Array | `false` | Use first row as headers or provide custom headers |
126
- | `col_sep` | String | `","` | Column delimiter (single character) |
127
- | `quote_char` | String | `"\""` | Quote character (single character) |
128
- | `skip_lines` | Integer | `0` | Number of lines to skip at start |
129
- | `encoding` | Encoding | `UTF-8` | Source encoding |
130
- | `liberal_parsing` | Boolean | `false` | Handle malformed CSV gracefully |
131
- | `buffer_size` | Integer | `262144` | Buffer size in bytes (256KB default) |
105
+ | Option | Meaning |
106
+ |---|---|
107
+ | `headers` | `true`: the first row names the fields. An `Array` or a CSV line: your own names |
108
+ | `col_sep` | Column separator, one ASCII character (`"\t"`, `";"`, `"|"`) |
109
+ | `skip_lines` | Number of leading rows to drop |
110
+ | `liberal_parsing` | `false`: raise `ZSV::MalformedCSVError` on malformed input, as `CSV` does by default |
111
+ | `encoding` | Encoding the returned Strings are labelled with (no transcoding) |
132
112
 
133
113
  ```ruby
134
- # Tab-separated values
135
- ZSV.foreach("data.tsv", col_sep: "\t") { |row| puts row }
136
-
137
- # Pipe-separated values
138
- ZSV.parse("a|b|c\n1|2|3", col_sep: "|")
139
-
140
- # Skip header comment lines
141
- ZSV.foreach("data.csv", skip_lines: 2) { |row| puts row }
114
+ ZSV.foreach('data.tsv', col_sep: "\t", headers: true) { |row| ... }
142
115
  ```
143
116
 
144
- ## ⚡ Performance
117
+ An option the gem does not implement raises `ArgumentError`; nothing is silently ignored.
145
118
 
146
- Benchmarks comparing ZSV vs Ruby CSV stdlib (Ruby 3.4.7):
119
+ ### Errors
147
120
 
148
- ```
149
- === Small file (1K rows, 5 cols) ===
150
- CSV (stdlib): 163.4 i/s
151
- ZSV: 1,013.7 i/s - 6.20x faster
121
+ `ZSV::Error` is the base class. `ZSV::MalformedCSVError` and `ZSV::InvalidEncodingError` mirror `CSV`'s. A missing file raises the usual `Errno::ENOENT`.
152
122
 
153
- === Medium file (10K rows, 10 cols) ===
154
- CSV (stdlib): 10.3 i/s
155
- ZSV: 54.5 i/s - 5.27x faster
123
+ ## Differences from `CSV`
156
124
 
157
- === Large file (100K rows, 10 cols) ===
158
- CSV (stdlib): 1.1 i/s
159
- ZSV: 5.3 i/s - 5.00x faster
125
+ The suite compares ZSV's rows with `CSV`'s for the same input. These differences are intended:
160
126
 
161
- === With headers (10K rows) ===
162
- CSV (stdlib): 7.8 i/s
163
- ZSV: 33.8 i/s - 4.33x faster
164
- ```
127
+ | | `CSV` | `ZSV` |
128
+ |---|---|---|
129
+ | Malformed input | raises by default (`liberal_parsing: false`) | **lenient by default**: zsv never rejects input. Pass `liberal_parsing: false` to validate like `CSV` and raise `ZSV::MalformedCSVError` |
130
+ | Empty unquoted field (`a,,b`) | `nil` | `""` |
131
+ | Row with headers | `CSV::Row`; `CSV.parse` returns a `CSV::Table` | a plain `Hash`; `ZSV.parse` returns an `Array` of them. With duplicate header names the first one wins, as in `CSV::Row#to_h` |
132
+ | Field Strings | mutable | frozen |
133
+ | UTF-8 BOM | kept unless `encoding: 'bom|utf-8'` | always stripped |
134
+ | `col_sep` | any String | one ASCII character |
135
+ | `quote_char` | any character | only `"` |
136
+ | `row_sep` | any String | always auto-detected (`\n`, `\r\n`, `\r`) |
137
+ | `skip_lines` | a pattern matching lines to skip | a number of leading rows to drop |
138
+ | `encoding` | can transcode (`'iso-8859-1:utf-8'`) | labels the returned Strings; no transcoding |
139
+ | Very wide rows | no limit | more than `max_columns` fields (1024 unless raised) raises `ZSV::MalformedCSVError` rather than dropping data |
140
+ | Very long rows | no limit | a row over `max_row_size` bytes (256 KiB unless raised) raises `ZSV::MalformedCSVError` rather than being cut short |
141
+ | Converters, `CSV::Table`, writing | yes | no: unsupported options raise `ArgumentError` |
165
142
 
166
- ### Memory Usage
143
+ [`docs/API_REFERENCE.md`](docs/API_REFERENCE.md) is the authoritative list.
167
144
 
168
- ZSV uses significantly less memory than Ruby's CSV stdlib:
145
+ ## Performance
169
146
 
170
- ```
171
- === Memory Usage (100K rows) ===
172
- CSV stdlib: 56.8 MB
173
- ZSV: 9.9 MB - 82.6% less memory
147
+ zsv scans input with SIMD instructions and the extension builds Ruby rows directly in C, so parsing is faster than the pure-Ruby `CSV`. How much depends on the shape of the data (row width, quoting), the options (rows as Hashes cost more than rows as Arrays), the CPU and the Ruby version, so this README does not carry a headline number.
174
148
 
175
- === String Allocations (10K rows) ===
176
- CSV stdlib: 116,144 strings
177
- ZSV: 50,005 strings - 56.9% fewer allocations
178
- ```
149
+ **Current numbers are produced by CI**: the [Benchmarks workflow](https://github.com/sebyx07/zsv-ruby/actions/workflows/benchmark.yml) runs on every push to `main`, on x86_64 and arm64 Linux, and prints the full output in each run's summary, with the Ruby version and commit.
179
150
 
180
- ZSV achieves **~6x lower memory usage** through frozen strings and efficient C-level memory management.
151
+ As a guide, across the maintainers' runs of `bundle exec rake bench` on x86_64 Linux, `ZSV.foreach` has typically been 3 to 4 times faster than `CSV.foreach`: a little over 2x on very wide rows, up to about 11 to 12x on heavily quoted data. `liberal_parsing: false` adds a validation pass in Ruby and lands near 2x. ZSV makes about half the allocations and holds a parsed file in noticeably less memory. These are ranges from a few machines, not a promise: the CI summaries and your own run are the reference.
181
152
 
182
- Run benchmarks yourself:
153
+ Reproduce it on your own data and hardware:
183
154
 
184
155
  ```bash
185
- bundle exec rake bench
186
- bundle exec ruby benchmark/memory_bench.rb
156
+ git clone https://github.com/sebyx07/zsv-ruby && cd zsv-ruby
157
+ bin/setup
158
+ bundle exec rake bench # benchmark/parse_bench.rb and benchmark/memory_bench.rb
159
+ BENCH_TIME=10 BENCH_JSON=parse.json bundle exec ruby benchmark/parse_bench.rb # longer runs, JSON in benchmark/results/
187
160
  ```
188
161
 
189
- ## API Reference
190
-
191
- ### Module Methods
192
-
193
- #### `ZSV.foreach(path, **options) { |row| }`
194
-
195
- Stream rows from a CSV file. Returns an Enumerator if no block given.
196
-
197
- #### `ZSV.parse(string, **options) -> Array`
198
-
199
- Parse CSV string and return all rows as an array.
200
-
201
- #### `ZSV.read(path, **options) -> Array`
202
-
203
- Read entire CSV file into an array.
204
-
205
- #### `ZSV.open(path, mode="r", **options) -> Parser`
162
+ Both sides of every comparison do the same work on the same generated, fixed-seed input, and the scripts abort if the two parsers disagree on the rows.
206
163
 
207
- Open a CSV file and return a Parser instance. If a block is given, the parser is automatically closed after the block completes.
164
+ ## Documentation
208
165
 
209
- #### `ZSV.new(io, **options) -> Parser`
166
+ - [Quick start](docs/QUICKSTART.md)
167
+ - [API reference](docs/API_REFERENCE.md): every method, option, error and difference from `CSV`
168
+ - [`llms.txt`](llms.txt): the same reference condensed into one file for AI coding agents
169
+ - [Changelog](CHANGELOG.md)
210
170
 
211
- Create a Parser from any IO-like object.
171
+ ## AI agents
212
172
 
213
- ### Parser Instance Methods
173
+ **This project is maintained by [developerz.ai](https://developerz.ai).** Its maintainer agent triages issues, reviews every pull request, merges every pull request once its author marks it ready and CI passes, watches project health daily, follows new releases of the zsv C library, and opens release PRs. The policy is in [`.dz/maintainer/maintainer.yml`](.dz/maintainer/maintainer.yml). Breaking changes stay with the owner.
214
174
 
215
- #### `#shift -> Array|Hash|nil`
175
+ - **Using zsv in your app?** Point your agent at [`llms.txt`](llms.txt), the complete reference in one file (methods, options, differences from `CSV`, pitfalls). Paste this into your app's `CLAUDE.md` / `AGENTS.md`:
176
+ ```markdown
177
+ ## CSV parsing
178
+ CSV files are read with the zsv gem (`ZSV.foreach`, `ZSV.parse`), not Ruby's CSV. Before editing that code read
179
+ https://github.com/sebyx07/zsv-ruby/blob/main/llms.txt
180
+ ```
181
+ - **Working on this repo?** [`CLAUDE.md`](CLAUDE.md) is the agent-instructions file (`AGENTS.md` is a symlink to it): invariants, commands, code map, how to verify a change, the release lane. [`ext/CLAUDE.md`](ext/CLAUDE.md) holds the rules for the C extension.
182
+ - **Claude Code**: commands in `.claude/commands/` (`/planx`, `/feature`, `/release`), skills in `.claude/skills/` (`verify-change`, `upgrade-zsv`, `debug-gc`), and a read-only reviewer subagent in `.claude/agents/` (`c-safety-reviewer`).
183
+ - **How the gem follows zsv**: once a month an agent runs `bin/zsv-upstream-check`; when zsv has a new release it reads the upstream changes, re-vendors the source, runs the suite, the sanitizers and the benchmarks before and after, and opens `chore: zsv X.Y.Z`. The next weekly release check then publishes gem `X.Y.Z`. The brief is [`.dz/schedules/upstream-zsv.md`](.dz/schedules/upstream-zsv.md).
216
184
 
217
- Read and return the next row. Returns `nil` at EOF.
218
-
219
- #### `#each { |row| } -> self`
220
-
221
- Iterate over all rows. Returns Enumerator without block.
222
-
223
- #### `#rewind -> nil`
224
-
225
- Reset parser to the beginning (file-based parsers only).
226
-
227
- #### `#close -> nil`
228
-
229
- Close parser and release resources.
230
-
231
- #### `#headers -> Array|nil`
232
-
233
- Return headers if header mode is enabled.
234
-
235
- #### `#closed? -> Boolean`
236
-
237
- Check if parser is closed.
238
-
239
- #### `#read -> Array`
240
-
241
- Read all remaining rows into an array.
242
-
243
- ### Exception Classes
244
-
245
- - `ZSV::Error` - Base exception class
246
- - `ZSV::MalformedCSVError` - Raised on CSV parsing errors
247
- - `ZSV::InvalidEncodingError` - Raised on encoding issues
248
-
249
- ## Architecture
250
-
251
- The gem follows SOLID principles with clear separation of concerns:
252
-
253
- ```
254
- ext/zsv/
255
- ├── zsv_ext.c # Main extension entry point, Ruby API
256
- ├── parser.c/h # Parser state management and zsv wrapper
257
- ├── row.c/h # Row building and conversion (arrays/hashes)
258
- ├── options.c/h # Option parsing and validation
259
- └── common.h # Shared types and macros
260
- ```
261
-
262
- ### Design Principles
263
-
264
- 1. **Single Responsibility**: Each C module handles one concern
265
- 2. **Streaming First**: Never load entire files into memory
266
- 3. **Zero-Copy Where Possible**: Minimize data copying
267
- 4. **Proper Resource Management**: RAII-style cleanup with Ruby GC
268
-
269
- ## 🛠️ Development
270
-
271
- ```bash
272
- # Clone and setup
273
- git clone https://github.com/sebyx07/zsv-ruby.git
274
- cd zsv-ruby
275
- bundle install
276
-
277
- # Compile extension
278
- bundle exec rake compile
279
-
280
- # Run tests
281
- bundle exec rake spec
282
-
283
- # Run benchmarks
284
- bundle exec rake bench
285
-
286
- # Clean build artifacts
287
- bundle exec rake clean
288
- ```
289
-
290
- ### Running Tests
185
+ ## Development
291
186
 
292
187
  ```bash
293
- bundle exec rspec
188
+ bin/setup # Ruby, gems, compile the extension (offline after the gems)
189
+ bin/check # the gate CI runs: repo guards, RuboCop, clang-format, compile, RSpec
190
+ bin/check fast # inner loop
191
+ bin/check asan # the suite under AddressSanitizer + UBSan (Linux)
192
+ bin/console # IRB with ZSV and CSV loaded
294
193
  ```
295
194
 
296
- The test suite includes:
297
-
298
- - Basic parsing tests
299
- - Header mode tests
300
- - Custom delimiter tests
301
- - Error handling tests
302
- - Memory leak detection
303
- - API compatibility tests
304
-
305
- ## Compatibility
306
-
307
- - **Ruby**: 3.3+ required (tested on 3.3, 3.4, 4.0)
308
- - **Platforms**: Linux, macOS (ARM and x86)
309
- - **ZSV**: Compiles against zsv 1.4.3
310
-
311
- ## 🤝 Contributing
312
-
313
- 1. Fork the repository
314
- 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
315
- 3. Write tests for your changes
316
- 4. Ensure tests pass (`bundle exec rake spec`)
317
- 5. Commit your changes (`git commit -am 'Add amazing feature'`)
318
- 6. Push to the branch (`git push origin feature/amazing-feature`)
319
- 7. Open a Pull Request
320
-
321
- ## License
322
-
323
- MIT License - see LICENSE file for details.
324
-
325
- ## 🙏 Credits
326
-
327
- - Built on [zsv](https://github.com/liquidaty/zsv) by liquidaty
328
- - Inspired by Ruby's CSV stdlib
329
- - SIMD optimizations courtesy of zsv's excellent engineering
330
- - Developed with [Claude Code](https://claude.com/claude-code)
331
-
332
- ## 🗺️ Roadmap
195
+ See [CONTRIBUTING.md](CONTRIBUTING.md). Security reports go through [SECURITY.md](SECURITY.md) (GitHub private vulnerability reporting), never a public issue. Releases are cut by merging a version bump: [RELEASE.md](RELEASE.md).
333
196
 
334
- ### Phase 1: Core Parser (Current)
335
- - [x] Basic parsing (foreach, parse, read)
336
- - [x] Header mode
337
- - [x] Custom delimiters
338
- - [x] File and string input
197
+ ## License and credits
339
198
 
340
- ### Phase 2: CSV Stdlib Compatibility
341
- - [ ] Type converters (`:numeric`, `:date`, `:date_time`)
342
- - [ ] Header converters (`:downcase`, `:symbol`)
343
- - [ ] `unconverted_fields` option
199
+ MIT, see [LICENSE](LICENSE). The bundled [zsv](https://github.com/liquidaty/zsv) library is by Liquidaty and also MIT-licensed; its license ships with the gem at `ext/zsv/vendor/zsv/LICENSE`.
344
200
 
345
- ## 💬 Support
201
+ ## Star history
346
202
 
347
- - **Issues**: [GitHub Issues](https://github.com/sebyx07/zsv-ruby/issues)
348
- - **Discussions**: [GitHub Discussions](https://github.com/sebyx07/zsv-ruby/discussions)
349
- - **Upstream zsv**: [zsv repository](https://github.com/liquidaty/zsv)
203
+ [![Star History Chart](https://api.star-history.com/svg?repos=sebyx07/zsv-ruby&type=Date)](https://star-history.com/#sebyx07/zsv-ruby&Date)
@@ -0,0 +1,7 @@
1
+ # The zsv C library release this gem is built from (gem X.Y.Z wraps zsv X.Y.Z).
2
+ # Read by ext/zsv/upstream.rb (Rakefile, extconf.rb) and bin/zsv-upstream-check.
3
+ #
4
+ # To upgrade: `bundle exec rake "zsv:bump[NEW_VERSION]"`, which rewrites this
5
+ # file with the new tarball's SHA-256 and re-vendors ext/zsv/vendor/zsv.
6
+ version=1.4.4
7
+ sha256=99486b6901d6b576bf15afceb0bcc888ceeb3f555a6d489547eefe67eff52079