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
data/ext/zsv/common.h CHANGED
@@ -14,21 +14,9 @@ extern VALUE eZSVError;
14
14
  extern VALUE eMalformedCSVError;
15
15
  extern VALUE eInvalidEncodingError;
16
16
 
17
- /* Error handling macros */
18
- #define RAISE_ZSV_ERROR(msg) rb_raise(eZSVError, "%s", (msg))
19
- #define RAISE_MALFORMED_CSV(msg) rb_raise(eMalformedCSVError, "%s", (msg))
20
- #define RAISE_INVALID_ENCODING(msg) rb_raise(eInvalidEncodingError, "%s", (msg))
21
-
22
17
  /* Memory management macros - use Ruby's built-in */
23
18
  #define ZSV_ALLOC(type) RB_ALLOC(type)
24
19
  #define ZSV_ALLOC_N(type, n) RB_ALLOC_N(type, n)
25
20
  #define ZSV_REALLOC_N(var, type, n) RB_REALLOC_N(var, type, n)
26
21
 
27
- /* Debug logging (disabled in production) */
28
- #ifdef DEBUG
29
- #define LOG_DEBUG(fmt, ...) fprintf(stderr, "[ZSV DEBUG] " fmt "\n", ##__VA_ARGS__)
30
- #else
31
- #define LOG_DEBUG(fmt, ...) ((void)0)
32
- #endif
33
-
34
22
  #endif /* ZSV_RUBY_COMMON_H */
data/ext/zsv/extconf.rb CHANGED
@@ -1,154 +1,102 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'mkmf'
4
- require 'net/http'
5
- require 'uri'
6
4
  require 'fileutils'
7
- require 'rubygems/package'
8
- require 'zlib'
9
- require 'openssl'
10
-
11
- # ZSV version to compile against
12
- ZSV_VERSION = '1.4.3' # zsv C library version (not gem version)
13
- ZSV_URL = "https://github.com/liquidaty/zsv/archive/refs/tags/v#{ZSV_VERSION}.tar.gz".freeze
14
- # Use absolute path relative to the original extconf.rb location
15
- EXTCONF_DIR = File.expand_path(__dir__)
16
- VENDOR_DIR = File.join(EXTCONF_DIR, 'vendor')
17
- ZSV_DIR = File.join(VENDOR_DIR, "zsv-#{ZSV_VERSION}")
18
-
19
- def download_file(url, destination, redirect_limit = 10, verify_ssl = true)
20
- abort('Too many redirects') if redirect_limit.zero?
21
-
22
- uri = URI.parse(url)
23
- http = Net::HTTP.new(uri.host, uri.port)
24
-
25
- if uri.scheme == 'https'
26
- http.use_ssl = true
27
- http.verify_mode = verify_ssl ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
28
- http.ca_file = ENV['SSL_CERT_FILE'] if ENV['SSL_CERT_FILE'] && verify_ssl
29
- end
30
-
31
- request = Net::HTTP::Get.new(uri.request_uri)
32
-
33
- begin
34
- response = http.request(request)
35
- rescue OpenSSL::SSL::SSLError => e
36
- if verify_ssl
37
- # Retry without SSL verification (GitHub is trusted)
38
- warn "SSL verification failed (#{e.message}), retrying without verification..."
39
- return download_file(url, destination, redirect_limit, false)
40
- end
41
- raise
42
- end
43
-
44
- case response
45
- when Net::HTTPRedirection
46
- download_file(response['location'], destination, redirect_limit - 1, verify_ssl)
47
- when Net::HTTPSuccess
48
- File.binwrite(destination, response.body)
49
- else
50
- abort("Failed to download: #{response.code} #{response.message}")
5
+ require 'shellwords'
6
+ require_relative 'upstream'
7
+
8
+ # Builds the zsv C library from the source vendored in ext/zsv/vendor/zsv (see
9
+ # ZSV_UPSTREAM and `rake zsv:vendor`), then links it statically into the
10
+ # extension. No network access happens here.
11
+ #
12
+ # The default build targets the baseline CPU of the architecture (SSE2 on
13
+ # x86_64, NEON on aarch64), so a binary built on one machine (a Docker image
14
+ # built in CI, say) runs on any other machine of that architecture instead of
15
+ # dying with SIGILL.
16
+ #
17
+ # Options (`gem install zsv -- --enable-native`, or ZSV_NATIVE=1):
18
+ # --enable-native build for the build machine's CPU (-march=native,
19
+ # AVX2 when it has it). Only for binaries that run
20
+ # where they were built.
21
+ # --enable-cross-build set by the Rakefile for rake-compiler-dock builds of
22
+ # precompiled gems; always baseline, ignores
23
+ # --enable-native.
24
+
25
+ def vendored_zsv_dir
26
+ unless ZSVUpstream.vendored_current?
27
+ abort <<~MSG
28
+ The zsv #{ZSVUpstream.version} C source is missing from #{ZSVUpstream::VENDOR_DIR}
29
+ (or was vendored from a different ZSV_UPSTREAM pin).
30
+ In a git checkout run `bundle exec rake zsv:vendor`; released gems always include it.
31
+ MSG
51
32
  end
33
+ ZSVUpstream::VENDOR_DIR
52
34
  end
53
35
 
54
- def extract_tar_gz(tarball, destination)
55
- Gem::Package::TarReader.new(Zlib::GzipReader.open(tarball)) do |tar|
56
- tar.each do |entry|
57
- dest_path = File.join(destination, entry.full_name)
58
-
59
- if entry.directory?
60
- FileUtils.mkdir_p(dest_path)
61
- elsif entry.file?
62
- FileUtils.mkdir_p(File.dirname(dest_path))
63
- File.binwrite(dest_path, entry.read)
64
- FileUtils.chmod(entry.header.mode, dest_path)
65
- end
66
- end
67
- end
36
+ def cross_compiling?
37
+ enable_config('cross-build', false)
68
38
  end
69
39
 
70
- def download_and_extract_zsv
71
- return if File.directory?(ZSV_DIR)
72
-
73
- puts "Downloading zsv #{ZSV_VERSION}..."
74
- FileUtils.mkdir_p(VENDOR_DIR)
75
-
76
- tarball = File.join(VENDOR_DIR, 'zsv.tar.gz')
77
- download_file(ZSV_URL, tarball)
78
-
79
- puts 'Extracting zsv...'
80
- extract_tar_gz(tarball, VENDOR_DIR)
81
- FileUtils.rm_f(tarball)
40
+ def native_build?
41
+ !cross_compiling? && enable_config('native', ENV['ZSV_NATIVE'] == '1')
42
+ end
82
43
 
83
- abort('zsv directory not found after extraction') unless File.directory?(ZSV_DIR)
84
- puts "zsv #{ZSV_VERSION} downloaded successfully"
44
+ def configure_args
45
+ # --minimal drops ZSV_EXTRAS (row limits, progress callbacks, sidecar
46
+ # overwrite files), which this gem does not use and which needs sqlite3.h
47
+ # from upstream's app/ tree, deliberately not vendored.
48
+ args = %w[--minimal=yes]
49
+ # SSE2 (x86_64) and NEON (aarch64) are part of the baseline ABI and still
50
+ # select zsv's SIMD scanner; only AVX/AVX2 and -march=native are dropped.
51
+ args += %w[--arch=none --force-avx2=no --force-avx=no] unless native_build?
52
+ args
85
53
  end
86
54
 
87
- def build_zsv
88
- puts 'Building zsv library...'
55
+ def build_env
56
+ env = { 'CC' => RbConfig::CONFIG['CC'], 'AR' => RbConfig::CONFIG['AR'] }
57
+ env['CROSS_COMPILING'] = 'yes' if cross_compiling?
58
+ env.compact
59
+ end
89
60
 
90
- # Build zsv static library
91
- Dir.chdir(ZSV_DIR) do
92
- # Configure zsv
93
- system('./configure') or abort('Failed to configure zsv') unless File.exist?('config.mk')
61
+ def run!(env, *cmd)
62
+ puts "#{env.map { |k, v| "#{k}=#{v.shellescape}" }.join(' ')} #{cmd.shelljoin}"
63
+ system(env, *cmd) or abort("zsv build failed: #{cmd.shelljoin}")
64
+ end
94
65
 
95
- # Build the library
96
- Dir.chdir('src') do
97
- system('make', 'build') or abort('Failed to build zsv library')
98
- end
66
+ # Builds a private copy so the vendored tree stays pristine and builds for
67
+ # several Rubies or platforms never share a config.mk.
68
+ def build_zsv(source_dir, build_dir)
69
+ FileUtils.rm_rf(build_dir)
70
+ FileUtils.cp_r(source_dir, build_dir)
71
+ env = build_env
72
+ Dir.chdir(build_dir) do
73
+ run!(env, './configure', *configure_args)
74
+ run!(env, find_executable('gmake') || 'make', '-C', 'src', 'build', "VERSION=#{ZSVUpstream.version}")
99
75
  end
100
-
101
- puts 'zsv library built successfully'
76
+ Dir.glob(File.join(build_dir, 'build', '*', 'rel', '*', 'lib', 'libzsv.a')).first or
77
+ abort("libzsv.a not found under #{build_dir}/build")
102
78
  end
103
79
 
104
- # Download and build zsv
105
- download_and_extract_zsv
106
- build_zsv
107
-
108
- # Determine build directory based on platform
109
- platform_dir = if RUBY_PLATFORM =~ /darwin/
110
- 'Darwin'
111
- elsif RUBY_PLATFORM =~ /linux/
112
- 'Linux'
113
- else
114
- 'generic'
115
- end
116
-
117
- # Find the built library - compiler name varies (gcc, gcc-14, clang, etc.)
118
- # Search for libzsv.a in the build directory
119
- build_rel_dir = File.join(ZSV_DIR, 'build', platform_dir, 'rel')
120
- zsv_lib = Dir.glob(File.join(build_rel_dir, '*', 'lib', 'libzsv.a')).first
80
+ zsv_build_dir = File.join(Dir.pwd, 'zsv-build')
81
+ puts "Building zsv #{ZSVUpstream.version} (#{native_build? ? 'native CPU' : 'portable baseline CPU'})"
82
+ zsv_lib = build_zsv(vendored_zsv_dir, zsv_build_dir)
83
+ include_dir = File.join(zsv_build_dir, 'include')
121
84
 
122
- abort("libzsv.a not found in #{build_rel_dir}/*/lib/") unless zsv_lib && File.exist?(zsv_lib)
123
-
124
- zsv_lib_dir = File.dirname(zsv_lib)
125
-
126
- # Add zsv include path
127
- include_dir = File.join(ZSV_DIR, 'include')
128
-
129
- # Add compiler and linker flags
130
85
  $INCFLAGS << " -I#{include_dir}"
131
86
  $CFLAGS << ' -std=c99 -Wall -Wextra'
132
- $CFLAGS << ' -O3' # Optimization level
133
-
134
- # Configure include and lib paths
135
- dir_config('zsv', include_dir, zsv_lib_dir)
87
+ $CFLAGS << ' -O3'
136
88
 
137
- # Find zsv header
138
89
  abort("zsv.h not found in #{include_dir}") unless have_header('zsv.h')
139
90
 
140
- # Link the static library
141
91
  $LOCAL_LIBS << " #{zsv_lib}"
142
92
 
143
- # Platform-specific adjustments
144
- if RUBY_PLATFORM =~ /darwin/
93
+ if RUBY_PLATFORM.include?('darwin')
145
94
  $LDFLAGS << ' -framework Foundation'
146
- elsif RUBY_PLATFORM =~ /linux/
95
+ elsif RUBY_PLATFORM.include?('linux')
147
96
  $LIBS << ' -lpthread -lm'
148
97
  end
149
98
 
150
- # Check for Ruby 3.2+ hash capacity preallocation
99
+ # Ruby 3.2+ hash capacity preallocation
151
100
  have_func('rb_hash_new_capa')
152
101
 
153
- # Create Makefile
154
102
  create_makefile('zsv/zsv')
data/ext/zsv/options.c CHANGED
@@ -1,5 +1,6 @@
1
1
  /* Options parsing and conversion implementation */
2
2
  #include "options.h"
3
+ #include "options_internal.h"
3
4
 
4
5
  /* Symbol constants for option keys */
5
6
  static ID id_headers;
@@ -7,45 +8,47 @@ static ID id_col_sep;
7
8
  static ID id_quote_char;
8
9
  static ID id_skip_lines;
9
10
  static ID id_encoding;
10
- static ID id_liberal_parsing;
11
11
  static ID id_buffer_size;
12
-
13
- void zsv_options_init_symbols(void)
14
- {
15
- id_headers = rb_intern("headers");
16
- id_col_sep = rb_intern("col_sep");
17
- id_quote_char = rb_intern("quote_char");
18
- id_skip_lines = rb_intern("skip_lines");
19
- id_encoding = rb_intern("encoding");
20
- id_liberal_parsing = rb_intern("liberal_parsing");
21
- id_buffer_size = rb_intern("buffer_size");
22
- }
12
+ static ID id_max_columns;
13
+ static ID id_max_row_size;
23
14
 
24
15
  void zsv_options_init(zsv_ruby_options_t *opts)
25
16
  {
26
17
  opts->delimiter = ',';
27
- opts->quote_char = '"';
28
18
  opts->headers = false;
29
19
  opts->header_array = Qnil;
30
20
  opts->skip_lines = 0;
31
- opts->liberal_parsing = false;
32
21
  opts->encoding = rb_utf8_encoding();
33
- opts->buffer_size = 256 * 1024; /* 256KB default */
22
+ opts->buffer_size = ZSV_DEFAULT_SCANNER_BUFFSIZE;
23
+ opts->max_columns = ZSV_MAX_COLS_DEFAULT;
24
+ opts->max_row_size = ZSV_RUBY_DEFAULT_MAX_ROW_SIZE;
34
25
  }
35
26
 
36
- static VALUE get_option(VALUE opts_hash, ID key, VALUE default_value)
27
+ static VALUE get_option(VALUE opts_hash, ID key)
37
28
  {
38
- if (NIL_P(opts_hash)) {
39
- return default_value;
29
+ return rb_hash_lookup(opts_hash, ID2SYM(key));
30
+ }
31
+
32
+ static char parse_single_char(VALUE value, const char *name)
33
+ {
34
+ StringValue(value);
35
+ if (RSTRING_LEN(value) != 1) {
36
+ rb_raise(rb_eArgError, "%s must be a single character", name);
40
37
  }
38
+ return RSTRING_PTR(value)[0];
39
+ }
41
40
 
42
- VALUE val = rb_hash_lookup(opts_hash, ID2SYM(key));
43
- return NIL_P(val) ? default_value : val;
41
+ static unsigned parse_positive_uint(VALUE value, const char *name)
42
+ {
43
+ unsigned long n = NUM2ULONG(value);
44
+ if (n == 0 || n > UINT_MAX) {
45
+ rb_raise(rb_eArgError, "%s must be between 1 and %u", name, UINT_MAX);
46
+ }
47
+ return (unsigned)n;
44
48
  }
45
49
 
46
50
  void zsv_options_parse(VALUE opts_hash, zsv_ruby_options_t *opts)
47
51
  {
48
- /* Initialize with defaults */
49
52
  zsv_options_init(opts);
50
53
 
51
54
  if (NIL_P(opts_hash)) {
@@ -54,72 +57,70 @@ void zsv_options_parse(VALUE opts_hash, zsv_ruby_options_t *opts)
54
57
 
55
58
  Check_Type(opts_hash, T_HASH);
56
59
 
57
- /* Parse col_sep */
58
- VALUE col_sep = get_option(opts_hash, id_col_sep, Qnil);
60
+ VALUE col_sep = get_option(opts_hash, id_col_sep);
59
61
  if (!NIL_P(col_sep)) {
60
- Check_Type(col_sep, T_STRING);
61
- if (RSTRING_LEN(col_sep) != 1) {
62
- rb_raise(rb_eArgError, "col_sep must be a single character");
62
+ opts->delimiter = parse_single_char(col_sep, "col_sep");
63
+ /* zsv silently replaces these delimiters with ',' */
64
+ if (opts->delimiter == '\n' || opts->delimiter == '\r' || opts->delimiter == '"') {
65
+ rb_raise(rb_eArgError, "col_sep cannot be a newline or a double quote");
63
66
  }
64
- opts->delimiter = RSTRING_PTR(col_sep)[0];
65
67
  }
66
68
 
67
- /* Parse quote_char */
68
- VALUE quote_char = get_option(opts_hash, id_quote_char, Qnil);
69
- if (!NIL_P(quote_char)) {
70
- Check_Type(quote_char, T_STRING);
71
- if (RSTRING_LEN(quote_char) != 1) {
72
- rb_raise(rb_eArgError, "quote_char must be a single character");
73
- }
74
- opts->quote_char = RSTRING_PTR(quote_char)[0];
69
+ /* zsv only supports '"' as the quote character */
70
+ VALUE quote_char = get_option(opts_hash, id_quote_char);
71
+ if (!NIL_P(quote_char) && parse_single_char(quote_char, "quote_char") != '"') {
72
+ rb_raise(rb_eArgError, "quote_char must be '\"' (zsv does not support other quotes)");
75
73
  }
76
74
 
77
- /* Parse headers */
78
- VALUE headers = get_option(opts_hash, id_headers, Qfalse);
79
- if (TYPE(headers) == T_ARRAY) {
75
+ VALUE headers = get_option(opts_hash, id_headers);
76
+ if (RB_TYPE_P(headers, T_ARRAY)) {
80
77
  opts->headers = true;
81
78
  opts->header_array = headers;
82
79
  } else {
83
80
  opts->headers = RTEST(headers);
84
81
  }
85
82
 
86
- /* Parse skip_lines */
87
- VALUE skip_lines = get_option(opts_hash, id_skip_lines, INT2FIX(0));
88
- opts->skip_lines = NUM2INT(skip_lines);
89
-
90
- /* Parse liberal_parsing */
91
- VALUE liberal = get_option(opts_hash, id_liberal_parsing, Qfalse);
92
- opts->liberal_parsing = RTEST(liberal);
83
+ VALUE skip_lines = get_option(opts_hash, id_skip_lines);
84
+ if (!NIL_P(skip_lines)) {
85
+ opts->skip_lines = NUM2INT(skip_lines);
86
+ if (opts->skip_lines < 0) {
87
+ rb_raise(rb_eArgError, "skip_lines must not be negative");
88
+ }
89
+ }
93
90
 
94
- /* Parse encoding */
95
- VALUE encoding = get_option(opts_hash, id_encoding, Qnil);
91
+ VALUE encoding = get_option(opts_hash, id_encoding);
96
92
  if (!NIL_P(encoding)) {
97
93
  opts->encoding = rb_to_encoding(encoding);
98
94
  }
99
95
 
100
- /* Parse buffer_size */
101
- VALUE buffer_size = get_option(opts_hash, id_buffer_size, Qnil);
96
+ VALUE buffer_size = get_option(opts_hash, id_buffer_size);
102
97
  if (!NIL_P(buffer_size)) {
103
98
  opts->buffer_size = NUM2SIZET(buffer_size);
99
+ if (opts->buffer_size == 0) {
100
+ rb_raise(rb_eArgError, "buffer_size must be positive");
101
+ }
104
102
  }
105
- }
106
103
 
107
- void zsv_options_free(zsv_ruby_options_t *opts)
108
- {
109
- (void)opts; /* Currently no dynamic allocations to free */
110
- }
104
+ VALUE max_columns = get_option(opts_hash, id_max_columns);
105
+ if (!NIL_P(max_columns)) {
106
+ opts->max_columns = parse_positive_uint(max_columns, "max_columns");
107
+ }
111
108
 
112
- void zsv_options_apply(zsv_parser parser, zsv_ruby_options_t *opts)
113
- {
114
- /* Note: zsv_set_delimiter doesn't exist in zsv API */
115
- /* The delimiter is set during parser creation via zsv_opts */
116
- /* Additional zsv-specific option application would go here */
117
- (void)parser; /* Suppress unused parameter warning */
118
- (void)opts;
109
+ VALUE max_row_size = get_option(opts_hash, id_max_row_size);
110
+ if (!NIL_P(max_row_size)) {
111
+ opts->max_row_size = parse_positive_uint(max_row_size, "max_row_size");
112
+ }
119
113
  }
120
114
 
121
115
  /* Initialize symbols (call from Init_zsv) */
122
116
  void Init_zsv_options(void)
123
117
  {
124
- zsv_options_init_symbols();
118
+ id_headers = rb_intern("headers");
119
+ id_col_sep = rb_intern("col_sep");
120
+ id_quote_char = rb_intern("quote_char");
121
+ id_skip_lines = rb_intern("skip_lines");
122
+ id_encoding = rb_intern("encoding");
123
+ id_buffer_size = rb_intern("buffer_size");
124
+ id_max_columns = rb_intern("max_columns");
125
+ id_max_row_size = rb_intern("max_row_size");
125
126
  }
data/ext/zsv/options.h CHANGED
@@ -4,28 +4,25 @@
4
4
 
5
5
  #include "common.h"
6
6
 
7
+ /* Longest row (bytes, without its line ending) parsed by default */
8
+ #define ZSV_RUBY_DEFAULT_MAX_ROW_SIZE (256 * 1024)
9
+
7
10
  /* Parser options structure */
8
11
  typedef struct {
9
12
  char delimiter;
10
- char quote_char;
11
13
  bool headers;
12
- VALUE header_array; /* Custom headers as Ruby array */
14
+ VALUE header_array; /* Custom headers as Ruby array (marked by the parser) */
13
15
  int skip_lines;
14
- bool liberal_parsing;
15
16
  rb_encoding *encoding;
16
- size_t buffer_size;
17
+ size_t buffer_size; /* Requested read size; the zsv buffer may be larger */
18
+ unsigned max_columns; /* Most fields in a row; a wider row raises */
19
+ unsigned max_row_size; /* Most bytes in a row; a longer row raises */
17
20
  } zsv_ruby_options_t;
18
21
 
19
22
  /* Initialize options with defaults */
20
23
  void zsv_options_init(zsv_ruby_options_t *opts);
21
24
 
22
- /* Parse Ruby hash into options structure */
25
+ /* Parse Ruby hash into options structure (raises on invalid options) */
23
26
  void zsv_options_parse(VALUE opts_hash, zsv_ruby_options_t *opts);
24
27
 
25
- /* Free any allocated option resources */
26
- void zsv_options_free(zsv_ruby_options_t *opts);
27
-
28
- /* Apply options to zsv parser */
29
- void zsv_options_apply(zsv_parser parser, zsv_ruby_options_t *opts);
30
-
31
28
  #endif /* ZSV_RUBY_OPTIONS_H */