censive 1.0.5 → 1.1.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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/censive.rb +34 -31
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a6f156186b0612f198563d23cd09fa0cf65d823a92b031bd71a3e22335163b73
4
- data.tar.gz: 6ac6ba05c9ee8cb52d1a3c5573dde803144651c7b4a2f3af9d83596d7365bf1c
3
+ metadata.gz: 8f5885eeeeab9f57546128e03407fbb1546137c3dfe19edc1a8dc8e08c95ad50
4
+ data.tar.gz: ed4b2f1ac55b826534c5bf9d854feca86cd2b4ef3cca9a2fab9e1993f3b2d6a4
5
5
  SHA512:
6
- metadata.gz: a078837538b6693a92586ebc997e0c7564952509991997215f7e0994d5ebaf29fdde4732feb72a2f418c4d2927115f23e1a7948e9f98a071438bff4c7fa8d255
7
- data.tar.gz: 3c28503ad4dc72fbcc0140d61e5d94d6ae44d9deb70842b40bd23406438f7de6b3a2557a39271f463ed1acf93f968e5543b8481d2b02b458a04a3389fa7f6e1f
6
+ metadata.gz: bf304f19bcabdbff428cfbc021807228291eded005c30bb3578bf2530f97dfb73b7fd08f684f39cb2eb5bd159c24ecedc8d6d8a768dd3dba50c3462ff87dc597
7
+ data.tar.gz: 02fddd62e35fd145ed34d34524c038b3397508eb8bfc81ce66dad2cfd4b4ea4bea931f1685f1f8ae81cdaac027740c5ef73f422c80c566974121ceaa3ed4bb87
data/lib/censive.rb CHANGED
@@ -4,14 +4,14 @@
4
4
  # censive - A quick and lightweight CSV handling library for Ruby
5
5
  #
6
6
  # Author: Steve Shreeve (steve.shreeve@gmail.com)
7
- # Date: June 16, 2023
7
+ # Date: June 28, 2023
8
8
  #
9
9
  # https://crystal-lang.org/api/1.7.2/CSV.html (Crystal's CSV library)
10
10
  # https://github.com/ruby/strscan/blob/master/ext/strscan/strscan.c
11
11
  #
12
- # Thanks to Sutou Kouhei (kou) for his excellent advice on using scan
12
+ # Thanks to Sutou Kouhei (kou) for his excellent advice on using scan better
13
13
  # ============================================================================
14
- # GOALS:
14
+ # HIGHLIGHTS:
15
15
  # 1. Faster than Ruby's default CSV library
16
16
  # 2. Lightweight code with streamlined and optimized logic
17
17
  # 3. Support most non-compliant CSV variations (@excel, @relax, etc)
@@ -29,15 +29,15 @@ require "stringio"
29
29
  require "strscan"
30
30
 
31
31
  class Censive < StringScanner
32
- VERSION="1.0.5"
32
+ VERSION="1.1.0"
33
33
 
34
34
  attr :encoding, :out, :rows
35
35
 
36
- def self.parse(...)
37
- new(...).parse
36
+ def self.read(...)
37
+ new(...).read
38
38
  end
39
39
 
40
- def self.writer(obj=nil, **opts, &code)
40
+ def self.write(obj=nil, **opts, &code)
41
41
  case obj
42
42
  when String
43
43
  if block_given?
@@ -48,7 +48,7 @@ class Censive < StringScanner
48
48
  when StringIO, IO, nil
49
49
  new(out: obj, **opts, &code)
50
50
  else
51
- abort "#{File.basename($0)}: invalid #{obj.class} object in writer"
51
+ abort "#{File.basename($0)}: #{self}.write can't use #{obj.class} objects"
52
52
  end
53
53
  end
54
54
 
@@ -124,9 +124,9 @@ class Censive < StringScanner
124
124
  super()
125
125
  end
126
126
 
127
- # ==[ Parser ]==
127
+ # ==[ Reader ]==
128
128
 
129
- def parse
129
+ def read
130
130
  @rows = []
131
131
  while row = next_row
132
132
  @rows << row
@@ -189,28 +189,19 @@ class Censive < StringScanner
189
189
  end
190
190
 
191
191
  def each
192
- @rows or parse
192
+ @rows or read
193
193
  @rows.each {|row| yield row }
194
194
  end
195
195
 
196
- def to_csv(*args, **opts, &code)
196
+ # ==[ Writer ]==
197
+
198
+ def write(*args, **opts, &code)
197
199
  if args.empty? && opts.empty?
198
200
  block_given? ? each(&code) : each {|row| @out << row }
199
201
  elsif block_given?
200
- Censive.writer(*args, **opts, &code)
201
- else
202
- Censive.writer(*args, **opts) {|csv| each {|row| csv << row }}
203
- end
204
- end
205
-
206
- # ==[ Helpers ]==
207
-
208
- # returns 2 (must be quoted and escaped), 1 (must be quoted), 0 (neither)
209
- def grok(str)
210
- if idx = str&.index(@escapes)
211
- $1 ? 2 : str.index(@quote, idx) ? 2 : 1
202
+ Censive.write(*args, **opts, &code)
212
203
  else
213
- 0
204
+ Censive.write(*args, **opts) {|csv| each {|row| csv << row }}
214
205
  end
215
206
  end
216
207
 
@@ -223,7 +214,7 @@ class Censive < StringScanner
223
214
  s,q = @sep, @quote
224
215
  out = case @mode
225
216
  when :compact
226
- case @excel ? 2 : grok(row.join)
217
+ case @excel ? 2 : quote_type(row.join)
227
218
  when 0
228
219
  row
229
220
  when 1
@@ -233,7 +224,7 @@ class Censive < StringScanner
233
224
  else
234
225
  row.map do |col|
235
226
  @excel && col =~ @leadzero ? "=#{q}#{col}#{q}" :
236
- case grok(col)
227
+ case quote_type(col)
237
228
  when 0 then col
238
229
  when 1 then "#{q}#{col}#{q}"
239
230
  else "#{q}#{col.gsub(q, @esc)}#{q}"
@@ -253,6 +244,21 @@ class Censive < StringScanner
253
244
  @out << out + @rowsep
254
245
  end
255
246
 
247
+ # ==[ Helpers ]==
248
+
249
+ def bomb(msg)
250
+ abort "\n#{File.basename($0)}: #{msg} at character #{pos} near '#{string[pos-4,7]}'"
251
+ end
252
+
253
+ # returns 2 (must be quoted and escaped), 1 (must be quoted), 0 (neither)
254
+ def quote_type(str)
255
+ if idx = str&.index(@escapes)
256
+ $1 ? 2 : str.index(@quote, idx) ? 2 : 1
257
+ else
258
+ 0
259
+ end
260
+ end
261
+
256
262
  def stats
257
263
  wide = string.size.to_s.size
258
264
  puts "%#{wide}d rows" % @rows.size
@@ -261,9 +267,6 @@ class Censive < StringScanner
261
267
  puts "%#{wide}d bytes" % string.size
262
268
  end
263
269
 
264
- def bomb(msg)
265
- abort "\n#{File.basename($0)}: #{msg} at character #{pos} near '#{string[pos-4,7]}'"
266
- end
267
270
  end
268
271
 
269
272
  if __FILE__ == $0
@@ -281,7 +284,7 @@ if __FILE__ == $0
281
284
  # out = csv.export
282
285
  # puts out.out
283
286
 
284
- puts Censive.parse(str, excel: true, relax: true).to_csv
287
+ puts Censive.read(str, excel: true, relax: true).write
285
288
  end
286
289
 
287
290
  __END__
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: censive
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Shreeve
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-27 00:00:00.000000000 Z
11
+ date: 2023-06-28 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A quick and lightweight CSV handling library for Ruby
14
14
  email: steve.shreeve@gmail.com