censive 1.0.4 → 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.
- checksums.yaml +4 -4
- data/lib/censive.rb +43 -33
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8f5885eeeeab9f57546128e03407fbb1546137c3dfe19edc1a8dc8e08c95ad50
|
4
|
+
data.tar.gz: ed4b2f1ac55b826534c5bf9d854feca86cd2b4ef3cca9a2fab9e1993f3b2d6a4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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
|
-
#
|
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,19 +29,26 @@ require "stringio"
|
|
29
29
|
require "strscan"
|
30
30
|
|
31
31
|
class Censive < StringScanner
|
32
|
-
VERSION="1.0
|
32
|
+
VERSION="1.1.0"
|
33
33
|
|
34
34
|
attr :encoding, :out, :rows
|
35
35
|
|
36
|
-
def self.
|
37
|
-
new(...).
|
36
|
+
def self.read(...)
|
37
|
+
new(...).read
|
38
38
|
end
|
39
39
|
|
40
|
-
def self.
|
40
|
+
def self.write(obj=nil, **opts, &code)
|
41
41
|
case obj
|
42
|
-
when String
|
43
|
-
|
44
|
-
|
42
|
+
when String
|
43
|
+
if block_given?
|
44
|
+
File.open(obj, "w") {|io| new(out: io, **opts, &code) }
|
45
|
+
else
|
46
|
+
new(out: File.open(obj, "w"), **opts)
|
47
|
+
end
|
48
|
+
when StringIO, IO, nil
|
49
|
+
new(out: obj, **opts, &code)
|
50
|
+
else
|
51
|
+
abort "#{File.basename($0)}: #{self}.write can't use #{obj.class} objects"
|
45
52
|
end
|
46
53
|
end
|
47
54
|
|
@@ -117,9 +124,9 @@ class Censive < StringScanner
|
|
117
124
|
super()
|
118
125
|
end
|
119
126
|
|
120
|
-
# ==[
|
127
|
+
# ==[ Reader ]==
|
121
128
|
|
122
|
-
def
|
129
|
+
def read
|
123
130
|
@rows = []
|
124
131
|
while row = next_row
|
125
132
|
@rows << row
|
@@ -182,28 +189,19 @@ class Censive < StringScanner
|
|
182
189
|
end
|
183
190
|
|
184
191
|
def each
|
185
|
-
@rows or
|
192
|
+
@rows or read
|
186
193
|
@rows.each {|row| yield row }
|
187
194
|
end
|
188
195
|
|
189
|
-
|
196
|
+
# ==[ Writer ]==
|
197
|
+
|
198
|
+
def write(*args, **opts, &code)
|
190
199
|
if args.empty? && opts.empty?
|
191
200
|
block_given? ? each(&code) : each {|row| @out << row }
|
192
201
|
elsif block_given?
|
193
|
-
Censive.
|
194
|
-
else
|
195
|
-
Censive.writer(*args, **opts) {|csv| each {|row| csv << row }}
|
196
|
-
end
|
197
|
-
end
|
198
|
-
|
199
|
-
# ==[ Helpers ]==
|
200
|
-
|
201
|
-
# returns 2 (must be quoted and escaped), 1 (must be quoted), 0 (neither)
|
202
|
-
def grok(str)
|
203
|
-
if idx = str&.index(@escapes)
|
204
|
-
$1 ? 2 : str.index(@quote, idx) ? 2 : 1
|
202
|
+
Censive.write(*args, **opts, &code)
|
205
203
|
else
|
206
|
-
|
204
|
+
Censive.write(*args, **opts) {|csv| each {|row| csv << row }}
|
207
205
|
end
|
208
206
|
end
|
209
207
|
|
@@ -216,7 +214,7 @@ class Censive < StringScanner
|
|
216
214
|
s,q = @sep, @quote
|
217
215
|
out = case @mode
|
218
216
|
when :compact
|
219
|
-
case @excel ? 2 :
|
217
|
+
case @excel ? 2 : quote_type(row.join)
|
220
218
|
when 0
|
221
219
|
row
|
222
220
|
when 1
|
@@ -226,7 +224,7 @@ class Censive < StringScanner
|
|
226
224
|
else
|
227
225
|
row.map do |col|
|
228
226
|
@excel && col =~ @leadzero ? "=#{q}#{col}#{q}" :
|
229
|
-
case
|
227
|
+
case quote_type(col)
|
230
228
|
when 0 then col
|
231
229
|
when 1 then "#{q}#{col}#{q}"
|
232
230
|
else "#{q}#{col.gsub(q, @esc)}#{q}"
|
@@ -246,6 +244,21 @@ class Censive < StringScanner
|
|
246
244
|
@out << out + @rowsep
|
247
245
|
end
|
248
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
|
+
|
249
262
|
def stats
|
250
263
|
wide = string.size.to_s.size
|
251
264
|
puts "%#{wide}d rows" % @rows.size
|
@@ -254,9 +267,6 @@ class Censive < StringScanner
|
|
254
267
|
puts "%#{wide}d bytes" % string.size
|
255
268
|
end
|
256
269
|
|
257
|
-
def bomb(msg)
|
258
|
-
abort "\n#{File.basename($0)}: #{msg} at character #{pos} near '#{string[pos-4,7]}'"
|
259
|
-
end
|
260
270
|
end
|
261
271
|
|
262
272
|
if __FILE__ == $0
|
@@ -274,7 +284,7 @@ if __FILE__ == $0
|
|
274
284
|
# out = csv.export
|
275
285
|
# puts out.out
|
276
286
|
|
277
|
-
puts Censive.
|
287
|
+
puts Censive.read(str, excel: true, relax: true).write
|
278
288
|
end
|
279
289
|
|
280
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
|
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-
|
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
|