censive 0.26 → 0.27

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 +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: dc8277614e4b254907b5d127eddae62757c4522785a9564ace4207e0bda1c1f6
4
+ data.tar.gz: 47049fc05225b53909c827b359d2201d9e0b915a21cd604625cb1cd3d2f1b1d5
5
5
  SHA512:
6
- metadata.gz: abc657f4df8f53d133008c653ab0fc632dd586443ece89a6961cec22c1794adc36d1bac76fbfd77480e83e5ae46dd2ee5943368d7c05bf07066ed6a6ff688a99
7
- data.tar.gz: dbd98b250d85730a299b965cd73defc4b5ef17e3372ec5c78f5b84a42f43cd7196e4fe471c0ef0e316fb8e3e5880779976f31cc1479d96ec0ec937c0b01df2a8
6
+ metadata.gz: d904a4b70401c772e14dac76ebd8a62f88e5d6a3efd49916733eea049d613c29d3f23703a458893fd86391c668a713baadb242bee83f3909d7e18a609bd1081a
7
+ data.tar.gz: 11ea3270797dd9a9f4f994599d9324c0b23955fb8c86a01cf56378a07d94818cf922bf00f556f2100da27660412b29279b7222834dcc3ff59037fda0db920443
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.27"
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.27'
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