censive 0.26 → 0.28

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/censive.rb +47 -7
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 352fd46b3f3fe35f80d3ccdb10349112d5880007810265eaf05eaeaae77d13d4
4
- data.tar.gz: 7cf15c90f45741185c681d962d0dcf0b95c0fed08ad40d0d98823d12bbc388dd
3
+ metadata.gz: d9d7a9c43e8371994aba6118bc34e34eeb250a3f289e4a3dfe724cb6f9e42cb1
4
+ data.tar.gz: 84bde447c2e0de60e03a17afc3d266f4018ce7aad5dd0f690d3d52438e0fd343
5
5
  SHA512:
6
- metadata.gz: abc657f4df8f53d133008c653ab0fc632dd586443ece89a6961cec22c1794adc36d1bac76fbfd77480e83e5ae46dd2ee5943368d7c05bf07066ed6a6ff688a99
7
- data.tar.gz: dbd98b250d85730a299b965cd73defc4b5ef17e3372ec5c78f5b84a42f43cd7196e4fe471c0ef0e316fb8e3e5880779976f31cc1479d96ec0ec937c0b01df2a8
6
+ metadata.gz: 5eeb09213826f6cd2821d83c0559034163ac701a2cbd3ef190df3dbfde31167d529f51e30363e585b65fc9a7dd29259b14d2957e7ff2f299878de24e3710e2ce
7
+ data.tar.gz: 4e54a08d30e433234caa04d94bfcbfef2bb2b9f2b36a4a2f6248bab77dd5fbf410f2fef69ab43c7b2c76a8e79d9d50780d55396aabc9c8dab876db820370e9e1
data/lib/censive.rb CHANGED
@@ -4,7 +4,7 @@
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: Feb 17, 2023
7
+ # Date: Mar 20, 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
@@ -28,7 +28,7 @@
28
28
  require "strscan"
29
29
 
30
30
  class Censive < StringScanner
31
- VERSION="0.26"
31
+ VERSION="0.28"
32
32
 
33
33
  attr :encoding, :out, :rows
34
34
 
@@ -93,14 +93,15 @@ class Censive < StringScanner
93
93
  @seq = [@sep, @eq].join # used for parsing in excel mode
94
94
 
95
95
  # regexes
96
- @eoc = /(?=#{"\\" + @sep}|#{@cr}|#{@lf}|\z)/o # end of cell
96
+ xsep = Regexp.escape(@sep) # may need to be escaped
97
+ @eoc = /(?=#{"\\" + xsep}|#{@cr}|#{@lf}|\z)/o # end of cell
97
98
  @eol = /#{@cr}#{@lf}?|#{@lf}/o # end of line
98
- @escapes = /(#{@quote})|#{"\\"+@sep}|#{@cr}|#{@lf}/o
99
- @quotable = /#{"\\"+@sep}|#{@cr}|#{@lf}/o
99
+ @escapes = /(#{@quote})|#{xsep}|#{@cr}|#{@lf}/o
100
+ @quotable = /#{xsep}|#{@cr}|#{@lf}/o
100
101
  @quotes = /#{@quote}/o
101
- @seps = /#{@sep}+/o
102
+ @seps = /#{xsep}+/o
102
103
  @quoted = @excel ? /(?:=)?#{@quote}/o : @quote
103
- @unquoted = /[^#{@sep}#{@cr}#{@lf}][^#{@quote}#{@cr}#{@lf}]*/o
104
+ @unquoted = /[^#{xsep}#{@cr}#{@lf}][^#{@quote}#{@cr}#{@lf}]*/o
104
105
  @leadzero = /\A0\d*\z/
105
106
 
106
107
  yield self if block_given?
@@ -252,3 +253,42 @@ class Censive < StringScanner
252
253
  abort "\n#{File.basename($0)}: #{msg} at character #{pos} near '#{string[pos-4,7]}'"
253
254
  end
254
255
  end
256
+
257
+ if __FILE__ == $0
258
+ str = DATA.gets("\n\n").chomp
259
+ # str = File.read(ARGV.first || "lc-2023.csv")
260
+ # str = File.open("KEN_ALL.CSV", "r:cp932").read
261
+
262
+ # require "stringio"
263
+ # csv = Censive.new(str, excel: true, relax: true)
264
+ # out = "" # StringIO.new
265
+ # csv.export(out: out) # (excel: true) # sep: "|")
266
+ # puts out # .string
267
+
268
+ # csv = Censive.new(str, excel: true, relax: true, out: "")
269
+ # out = csv.export
270
+ # puts out.out
271
+
272
+ puts Censive.parse(str, excel: true, relax: true).to_csv
273
+ end
274
+
275
+ __END__
276
+ "AAA "BBB",CCC,"DDD"
277
+
278
+ "CHUI, LOK HANG "BENNY",224325325610,="001453","Hemoglobin A1c",=""
279
+ "Don",="007",10,"Ed"
280
+ Name,Age,,,Shoe,,,
281
+ "Alice",27,5
282
+ Bob,33,10 1/2
283
+ Charlie or "Chuck",=B2 + B3,9
284
+ Subtotal,=sum(B2:B5),="01234"
285
+ A,B,C,D
286
+ A,B,"C",D
287
+ A,B,C",D
288
+ A,B,"C",D
289
+ 08/27/2022,="73859443",="04043260",,"Crossover @ Mathilda","CHO, JOELLE "JOJO"",08/19/2022
290
+ 123,
291
+ 123,"CHO, JOELLE "JOJO"",456
292
+ 123,"CHO, JOELLE ""JOJO""",456
293
+ =,=x,x=,="x",="","","=",123,0123,="123",="0123"
294
+ ,=x,x=,x,,,,,,=,,123,="0123",123,,="0123"
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: '0.26'
4
+ version: '0.28'
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-03-15 00:00:00.000000000 Z
11
+ date: 2023-03-20 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