nesquikcsv 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +20 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +37 -0
- data/Rakefile +17 -0
- data/ext/csv_parser/extconf.rb +15 -0
- data/ext/csv_parser/parser.c +101 -0
- data/lib/csv_parser.so +0 -0
- data/lib/nesquikcsv/version.rb +3 -0
- data/lib/nesquikcsv.rb +116 -0
- data/nesquikcsv.gemspec +23 -0
- data/test/tc_csv_parsing.rb +126 -0
- data/test/tc_interface.rb +128 -0
- metadata +74 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 77bd305b5beeb6d2d9c5aa453d46f1eacce9f930
|
4
|
+
data.tar.gz: 23a2e272068835c70087cfeb4fa9a7aaa28e4c26
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ca390b23677ba83564fcccd47c4a339f5b8372da9bd6b69f1824509917becc9f189f60829bcca11935f7a3c7a886ae7eec7f48bcc99907d1930e8451ce8b23bb
|
7
|
+
data.tar.gz: f9f44de8d2b0b07d3cc38e9bd82f09c384cfa9947d87adfae976ba4aeb88442f0d368935e9d8092ba36f352373732ed7997599c87f7e1023255ad5ef6823b7db
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012, 2013 Maarten Oelering
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# NesquikCSV
|
2
|
+
|
3
|
+
Fork of the Fastest-CSV gem.
|
4
|
+
|
5
|
+
Uses native C code to parse CSV lines in MRI Ruby.
|
6
|
+
|
7
|
+
Supports standard CSV according to RFC4180. Not the so-called "csv" from Excel.
|
8
|
+
|
9
|
+
The interface is a subset of the CSV interface in Ruby 1.9.3. The options parameter is not supported.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'nesquikcsv'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install nesquikcsv
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
Parse single line
|
28
|
+
|
29
|
+
NesquikCSV.parse_line("one,two,three", "UTF-8")
|
30
|
+
=> ["one", "two", "three"]
|
31
|
+
|
32
|
+
Parse string in array of arrays
|
33
|
+
|
34
|
+
# Defaults to UTF-8
|
35
|
+
rows = NesquikCSV.parse(csv_data)
|
36
|
+
# Explicitly
|
37
|
+
rows = NesquikCSV.parse(csv_data, "UTF-8")
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
|
4
|
+
spec = Gem::Specification.load('nesquikcsv.gemspec')
|
5
|
+
|
6
|
+
require 'rake/extensiontask'
|
7
|
+
Rake::ExtensionTask.new('csv_parser', spec)
|
8
|
+
|
9
|
+
require 'rake/testtask'
|
10
|
+
Rake::TestTask.new do |t|
|
11
|
+
t.libs << "test"
|
12
|
+
t.test_files = FileList['test/tc_*.rb']
|
13
|
+
#test.libs << 'lib' << 'test'
|
14
|
+
#test.pattern = 'test/**/test_*.rb'
|
15
|
+
#test.verbose = true
|
16
|
+
end
|
17
|
+
|
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
|
3
|
+
require 'mkmf'
|
4
|
+
extension_name = 'csv_parser'
|
5
|
+
#dir_config(extension_name)
|
6
|
+
|
7
|
+
if RUBY_VERSION =~ /1.8/ then
|
8
|
+
$CPPFLAGS += " -DRUBY_18"
|
9
|
+
end
|
10
|
+
|
11
|
+
#if CONFIG["arch"] =~ /mswin32|mingw/
|
12
|
+
# $CFLAGS += " -march=i686"
|
13
|
+
#end
|
14
|
+
|
15
|
+
create_makefile(extension_name)
|
@@ -0,0 +1,101 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) Maarten Oelering, BrightCode BV
|
3
|
+
*/
|
4
|
+
|
5
|
+
#include "ruby.h"
|
6
|
+
#ifdef RUBY_18
|
7
|
+
#include "rubyio.h"
|
8
|
+
#else
|
9
|
+
#include "ruby/io.h"
|
10
|
+
#endif
|
11
|
+
|
12
|
+
#define DEF_ARRAY_LEN 32
|
13
|
+
|
14
|
+
#define UNQUOTED 0
|
15
|
+
#define IN_QUOTED 1
|
16
|
+
#define QUOTE_IN_QUOTED 2
|
17
|
+
|
18
|
+
static VALUE mCsvParser;
|
19
|
+
|
20
|
+
static VALUE parse_line(VALUE self, VALUE str, VALUE encoding)
|
21
|
+
{
|
22
|
+
if (NIL_P(str))
|
23
|
+
return Qnil;
|
24
|
+
|
25
|
+
const char *ptr = RSTRING_PTR(str);
|
26
|
+
int len = (int) RSTRING_LEN(str); /* cast to prevent warning in 64-bit OS */
|
27
|
+
|
28
|
+
if (len == 0)
|
29
|
+
return Qnil;
|
30
|
+
|
31
|
+
VALUE array = rb_ary_new2(DEF_ARRAY_LEN); /* default allocated size is 16 */
|
32
|
+
char value[len]; /* field value, no longer than line */
|
33
|
+
int state = 0;
|
34
|
+
int index = 0;
|
35
|
+
int i;
|
36
|
+
char c;
|
37
|
+
|
38
|
+
/* Grab encoding to be used for string malloc */
|
39
|
+
rb_encoding* rb_encoding = rb_enc_find(RSTRING_PTR(encoding));
|
40
|
+
if(rb_encoding == NULL)
|
41
|
+
return Qnil;
|
42
|
+
|
43
|
+
for (i = 0; i < len; i++)
|
44
|
+
{
|
45
|
+
c = ptr[i];
|
46
|
+
switch (c)
|
47
|
+
{
|
48
|
+
case ',':
|
49
|
+
if (state == UNQUOTED) {
|
50
|
+
rb_ary_push(array, (index == 0 ? Qnil: rb_str_new(value, index)));
|
51
|
+
index = 0;
|
52
|
+
}
|
53
|
+
else if (state == IN_QUOTED) {
|
54
|
+
value[index++] = c;
|
55
|
+
}
|
56
|
+
else if (state == QUOTE_IN_QUOTED) {
|
57
|
+
rb_ary_push(array, rb_str_new(value, index));
|
58
|
+
index = 0;
|
59
|
+
state = UNQUOTED;
|
60
|
+
}
|
61
|
+
break;
|
62
|
+
case '"':
|
63
|
+
if (state == UNQUOTED) {
|
64
|
+
state = IN_QUOTED;
|
65
|
+
}
|
66
|
+
else if (state == 1) {
|
67
|
+
state = QUOTE_IN_QUOTED;
|
68
|
+
}
|
69
|
+
else if (state == QUOTE_IN_QUOTED) {
|
70
|
+
value[index++] = c; /* escaped quote */
|
71
|
+
state = IN_QUOTED;
|
72
|
+
}
|
73
|
+
break;
|
74
|
+
case 13: /* \r */
|
75
|
+
case 10: /* \n */
|
76
|
+
if (state == IN_QUOTED) {
|
77
|
+
value[index++] = c;
|
78
|
+
}
|
79
|
+
else {
|
80
|
+
i = len; /* only parse first line if multiline */
|
81
|
+
}
|
82
|
+
break;
|
83
|
+
default:
|
84
|
+
value[index++] = c;
|
85
|
+
}
|
86
|
+
}
|
87
|
+
|
88
|
+
if (state == UNQUOTED) {
|
89
|
+
rb_ary_push(array, (index == 0 ? Qnil: rb_enc_str_new(value, index, rb_encoding)));
|
90
|
+
}
|
91
|
+
else if (state == QUOTE_IN_QUOTED) {
|
92
|
+
rb_ary_push(array, rb_enc_str_new(value, index, rb_encoding));
|
93
|
+
}
|
94
|
+
return array;
|
95
|
+
}
|
96
|
+
|
97
|
+
void Init_csv_parser()
|
98
|
+
{
|
99
|
+
mCsvParser = rb_define_module("CsvParser");
|
100
|
+
rb_define_module_function(mCsvParser, "parse_line", parse_line, 2);
|
101
|
+
}
|
data/lib/csv_parser.so
ADDED
Binary file
|
data/lib/nesquikcsv.rb
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
# This loads either csv_parser.so, csv_parser.bundle or
|
2
|
+
# csv_parser.jar, depending on your Ruby platform and OS
|
3
|
+
require 'csv_parser'
|
4
|
+
require 'stringio'
|
5
|
+
|
6
|
+
# Fast CSV parser using native code
|
7
|
+
class NesquikCSV
|
8
|
+
include Enumerable
|
9
|
+
|
10
|
+
# Pass each line of the specified +path+ as array to the provided +block+
|
11
|
+
def self.foreach(path, &block)
|
12
|
+
open(path) do |reader|
|
13
|
+
reader.each(&block)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# Opens a csv file. Pass a FastestCSV instance to the provided block,
|
18
|
+
# or return it when no block is provided
|
19
|
+
def self.open(path, mode = "rb")
|
20
|
+
csv = new(File.open(path, mode))
|
21
|
+
if block_given?
|
22
|
+
begin
|
23
|
+
yield csv
|
24
|
+
ensure
|
25
|
+
csv.close
|
26
|
+
end
|
27
|
+
else
|
28
|
+
csv
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Read all lines from the specified +path+ into an array of arrays
|
33
|
+
def self.read(path)
|
34
|
+
open(path, "rb") { |csv| csv.read }
|
35
|
+
end
|
36
|
+
|
37
|
+
# Alias for read
|
38
|
+
def self.readlines(path)
|
39
|
+
read(path)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Read all lines from the specified String into an array of arrays
|
43
|
+
def self.parse(data, &block)
|
44
|
+
csv = new(StringIO.new(data))
|
45
|
+
if block.nil?
|
46
|
+
begin
|
47
|
+
csv.read
|
48
|
+
ensure
|
49
|
+
csv.close
|
50
|
+
end
|
51
|
+
else
|
52
|
+
csv.each(&block)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.parse_line(line, encoding)
|
57
|
+
CsvParser.parse_line(line, encoding)
|
58
|
+
end
|
59
|
+
|
60
|
+
# Create new NesquikCSV wrapping the specified IO object
|
61
|
+
def initialize(io)
|
62
|
+
@io = io
|
63
|
+
end
|
64
|
+
|
65
|
+
# Read from the wrapped IO passing each line as array to the specified block
|
66
|
+
def each
|
67
|
+
if block_given?
|
68
|
+
while row = shift
|
69
|
+
yield row
|
70
|
+
end
|
71
|
+
else
|
72
|
+
to_enum # return enumerator
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# Read all remaining lines from the wrapped IO into an array of arrays
|
77
|
+
def read
|
78
|
+
table = Array.new
|
79
|
+
each {|row| table << row}
|
80
|
+
table
|
81
|
+
end
|
82
|
+
alias_method :readlines, :read
|
83
|
+
|
84
|
+
# Rewind the underlying IO object and reset line counter
|
85
|
+
def rewind
|
86
|
+
@io.rewind
|
87
|
+
end
|
88
|
+
|
89
|
+
# Read next line from the wrapped IO and return as array or nil at EOF
|
90
|
+
def shift(encoding='UTF-8')
|
91
|
+
if line = @io.gets
|
92
|
+
CsvParser.parse_line(line, encoding)
|
93
|
+
else
|
94
|
+
nil
|
95
|
+
end
|
96
|
+
end
|
97
|
+
alias_method :gets, :shift
|
98
|
+
alias_method :readline, :shift
|
99
|
+
|
100
|
+
# Close the wrapped IO
|
101
|
+
def close
|
102
|
+
@io.close
|
103
|
+
end
|
104
|
+
|
105
|
+
def closed?
|
106
|
+
@io.closed?
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
class String
|
111
|
+
# Equivalent to <tt>FasterCSV::parse_line(self)</tt>
|
112
|
+
def parse_csv
|
113
|
+
CsvParser.parse_line(self)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
data/nesquikcsv.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/nesquikcsv/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Juan Martty"]
|
6
|
+
gem.email = ["null.terminated.string@gmail.com"]
|
7
|
+
gem.description = %q{Fastest-CSV fork with encoding support}
|
8
|
+
gem.summary = %q{Fastest-CSV fork with encoding support}
|
9
|
+
gem.homepage = "https://github.com/jmartty/nesquikcsv"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
#gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "nesquikcsv"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = NesquikCSV::VERSION
|
17
|
+
|
18
|
+
gem.extensions = ['ext/csv_parser/extconf.rb']
|
19
|
+
|
20
|
+
gem.add_development_dependency "rake-compiler"
|
21
|
+
|
22
|
+
gem.license = 'MIT'
|
23
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
#
|
2
|
+
# Tests copied from faster_csv by James Edward Gray II
|
3
|
+
#
|
4
|
+
|
5
|
+
require 'test/unit'
|
6
|
+
require 'nesquikcsv'
|
7
|
+
|
8
|
+
#
|
9
|
+
# Following tests are my interpretation of the
|
10
|
+
# {CSV RCF}[http://www.ietf.org/rfc/rfc4180.txt]. I only deviate from that
|
11
|
+
# document in one place (intentionally) and that is to make the default row
|
12
|
+
# separator <tt>$/</tt>.
|
13
|
+
#
|
14
|
+
class TestCSVParsing < Test::Unit::TestCase
|
15
|
+
|
16
|
+
def test_mastering_regex_example
|
17
|
+
ex = %Q{Ten Thousand,10000, 2710 ,,"10,000","It's ""10 Grand"", baby",10K}
|
18
|
+
assert_equal( [ "Ten Thousand", "10000", " 2710 ", nil, "10,000",
|
19
|
+
"It's \"10 Grand\", baby", "10K" ],
|
20
|
+
CsvParser.parse_line(ex, "UTF-8") )
|
21
|
+
end
|
22
|
+
|
23
|
+
# Pulled from: http://www.ruby-lang.org/cgi-bin/cvsweb.cgi/ruby/test/csv/test_csv.rb?rev=1.12.2.2;content-type=text%2Fplain
|
24
|
+
def test_std_lib_csv
|
25
|
+
[ ["\t", ["\t"]],
|
26
|
+
["foo,\"\"\"\"\"\",baz", ["foo", "\"\"", "baz"]],
|
27
|
+
["foo,\"\"\"bar\"\"\",baz", ["foo", "\"bar\"", "baz"]],
|
28
|
+
["\"\"\"\n\",\"\"\"\n\"", ["\"\n", "\"\n"]],
|
29
|
+
["foo,\"\r\n\",baz", ["foo", "\r\n", "baz"]],
|
30
|
+
["\"\"", [""]],
|
31
|
+
["foo,\"\"\"\",baz", ["foo", "\"", "baz"]],
|
32
|
+
["foo,\"\r.\n\",baz", ["foo", "\r.\n", "baz"]],
|
33
|
+
["foo,\"\r\",baz", ["foo", "\r", "baz"]],
|
34
|
+
["foo,\"\",baz", ["foo", "", "baz"]],
|
35
|
+
["\",\"", [","]],
|
36
|
+
["foo", ["foo"]],
|
37
|
+
[",,", [nil, nil, nil]],
|
38
|
+
[",", [nil, nil]],
|
39
|
+
["foo,\"\n\",baz", ["foo", "\n", "baz"]],
|
40
|
+
["foo,,baz", ["foo", nil, "baz"]],
|
41
|
+
["\"\"\"\r\",\"\"\"\r\"", ["\"\r", "\"\r"]],
|
42
|
+
["\",\",\",\"", [",", ","]],
|
43
|
+
["foo,bar,", ["foo", "bar", nil]],
|
44
|
+
[",foo,bar", [nil, "foo", "bar"]],
|
45
|
+
["foo,bar", ["foo", "bar"]],
|
46
|
+
[";", [";"]],
|
47
|
+
["\t,\t", ["\t", "\t"]],
|
48
|
+
["foo,\"\r\n\r\",baz", ["foo", "\r\n\r", "baz"]],
|
49
|
+
["foo,\"\r\n\n\",baz", ["foo", "\r\n\n", "baz"]],
|
50
|
+
["foo,\"foo,bar\",baz", ["foo", "foo,bar", "baz"]],
|
51
|
+
[";,;", [";", ";"]] ].each do |csv_test|
|
52
|
+
assert_equal(csv_test.last, CsvParser.parse_line(csv_test.first, "UTF-8"))
|
53
|
+
end
|
54
|
+
|
55
|
+
[ ["foo,\"\"\"\"\"\",baz", ["foo", "\"\"", "baz"]],
|
56
|
+
["foo,\"\"\"bar\"\"\",baz", ["foo", "\"bar\"", "baz"]],
|
57
|
+
["foo,\"\r\n\",baz", ["foo", "\r\n", "baz"]],
|
58
|
+
["\"\"", [""]],
|
59
|
+
["foo,\"\"\"\",baz", ["foo", "\"", "baz"]],
|
60
|
+
["foo,\"\r.\n\",baz", ["foo", "\r.\n", "baz"]],
|
61
|
+
["foo,\"\r\",baz", ["foo", "\r", "baz"]],
|
62
|
+
["foo,\"\",baz", ["foo", "", "baz"]],
|
63
|
+
["foo", ["foo"]],
|
64
|
+
[",,", [nil, nil, nil]],
|
65
|
+
[",", [nil, nil]],
|
66
|
+
["foo,\"\n\",baz", ["foo", "\n", "baz"]],
|
67
|
+
["foo,,baz", ["foo", nil, "baz"]],
|
68
|
+
["foo,bar", ["foo", "bar"]],
|
69
|
+
["foo,\"\r\n\n\",baz", ["foo", "\r\n\n", "baz"]],
|
70
|
+
["foo,\"foo,bar\",baz", ["foo", "foo,bar", "baz"]] ].each do |csv_test|
|
71
|
+
assert_equal(csv_test.last, CsvParser.parse_line(csv_test.first, "UTF-8"))
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# From: http://ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-core/6496
|
76
|
+
def test_aras_edge_cases
|
77
|
+
[ [%Q{a,b}, ["a", "b"]],
|
78
|
+
[%Q{a,"""b"""}, ["a", "\"b\""]],
|
79
|
+
[%Q{a,"""b"}, ["a", "\"b"]],
|
80
|
+
[%Q{a,"b"""}, ["a", "b\""]],
|
81
|
+
[%Q{a,"\nb"""}, ["a", "\nb\""]],
|
82
|
+
[%Q{a,"""\nb"}, ["a", "\"\nb"]],
|
83
|
+
[%Q{a,"""\nb\n"""}, ["a", "\"\nb\n\""]],
|
84
|
+
[%Q{a,"""\nb\n""",\nc}, ["a", "\"\nb\n\"", nil]],
|
85
|
+
[%Q{a,,,}, ["a", nil, nil, nil]],
|
86
|
+
[%Q{,}, [nil, nil]],
|
87
|
+
[%Q{"",""}, ["", ""]],
|
88
|
+
[%Q{""""}, ["\""]],
|
89
|
+
[%Q{"""",""}, ["\"",""]],
|
90
|
+
[%Q{,""}, [nil,""]],
|
91
|
+
[%Q{,"\r"}, [nil,"\r"]],
|
92
|
+
[%Q{"\r\n,"}, ["\r\n,"]],
|
93
|
+
[%Q{"\r\n,",}, ["\r\n,", nil]] ].each do |edge_case|
|
94
|
+
assert_equal(edge_case.last, CsvParser.parse_line(edge_case.first, "UTF-8"))
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_james_edge_cases
|
99
|
+
# A read at eof? should return nil.
|
100
|
+
assert_equal(nil, CsvParser.parse_line("", "UTF-8"))
|
101
|
+
#
|
102
|
+
# With CSV it's impossible to tell an empty line from a line containing a
|
103
|
+
# single +nil+ field. The standard CSV library returns <tt>[nil]</tt>
|
104
|
+
# in these cases, but <tt>Array.new</tt> makes more sense to me.
|
105
|
+
#
|
106
|
+
#assert_equal(Array.new, NesquikCSV.parse_line("\n1,2,3\n"))
|
107
|
+
assert_equal([nil], CsvParser.parse_line("\n1,2,3\n", "UTF-8"))
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_rob_edge_cases
|
111
|
+
[ [%Q{"a\nb"}, ["a\nb"]],
|
112
|
+
[%Q{"\n\n\n"}, ["\n\n\n"]],
|
113
|
+
[%Q{a,"b\n\nc"}, ['a', "b\n\nc"]],
|
114
|
+
[%Q{,"\r\n"}, [nil,"\r\n"]],
|
115
|
+
[%Q{,"\r\n."}, [nil,"\r\n."]],
|
116
|
+
[%Q{"a\na","one newline"}, ["a\na", 'one newline']],
|
117
|
+
[%Q{"a\n\na","two newlines"}, ["a\n\na", 'two newlines']],
|
118
|
+
[%Q{"a\r\na","one CRLF"}, ["a\r\na", 'one CRLF']],
|
119
|
+
[%Q{"a\r\n\r\na","two CRLFs"}, ["a\r\n\r\na", 'two CRLFs']],
|
120
|
+
[%Q{with blank,"start\n\nfinish"\n}, ['with blank', "start\n\nfinish"]],
|
121
|
+
].each do |edge_case|
|
122
|
+
assert_equal(edge_case.last, CsvParser.parse_line(edge_case.first, "UTF-8"))
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
end
|
@@ -0,0 +1,128 @@
|
|
1
|
+
#
|
2
|
+
# Tests copied from faster_csv by James Edward Gray II
|
3
|
+
#
|
4
|
+
|
5
|
+
require 'test/unit'
|
6
|
+
require 'nesquikcsv'
|
7
|
+
|
8
|
+
class TestNesquikCSVInterface < Test::Unit::TestCase
|
9
|
+
|
10
|
+
def setup
|
11
|
+
@path = File.join(File.dirname(__FILE__), "temp_test_data.csv")
|
12
|
+
|
13
|
+
File.open(@path, "w") do |file|
|
14
|
+
file << "1,2,3\r\n"
|
15
|
+
file << "4,5\r\n"
|
16
|
+
end
|
17
|
+
|
18
|
+
@expected = [%w{1 2 3}, %w{4 5}]
|
19
|
+
end
|
20
|
+
|
21
|
+
def teardown
|
22
|
+
File.unlink(@path)
|
23
|
+
end
|
24
|
+
|
25
|
+
### Test Read Interface ###
|
26
|
+
|
27
|
+
def test_foreach
|
28
|
+
NesquikCSV.foreach(@path) do |row|
|
29
|
+
assert_equal(@expected.shift, row)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_open_and_close
|
34
|
+
csv = NesquikCSV.open(@path, "r+")
|
35
|
+
assert_not_nil(csv)
|
36
|
+
assert_instance_of(NesquikCSV, csv)
|
37
|
+
assert_equal(false, csv.closed?)
|
38
|
+
csv.close
|
39
|
+
assert(csv.closed?)
|
40
|
+
|
41
|
+
ret = NesquikCSV.open(@path) do |csv|
|
42
|
+
assert_instance_of(NesquikCSV, csv)
|
43
|
+
"Return value."
|
44
|
+
end
|
45
|
+
assert(csv.closed?)
|
46
|
+
assert_equal("Return value.", ret)
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_parse
|
50
|
+
data = File.read(@path)
|
51
|
+
assert_equal( @expected,
|
52
|
+
NesquikCSV.parse(data) )
|
53
|
+
|
54
|
+
NesquikCSV.parse(data) do |row|
|
55
|
+
assert_equal(@expected.shift, row)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
#def test_parse_line
|
60
|
+
# row = FasterCSV.parse_line("1;2;3", :col_sep => ";")
|
61
|
+
# assert_not_nil(row)
|
62
|
+
# assert_instance_of(Array, row)
|
63
|
+
# assert_equal(%w{1 2 3}, row)
|
64
|
+
#
|
65
|
+
# # shortcut interface
|
66
|
+
# row = "1;2;3".parse_csv(:col_sep => ";")
|
67
|
+
# assert_not_nil(row)
|
68
|
+
# assert_instance_of(Array, row)
|
69
|
+
# assert_equal(%w{1 2 3}, row)
|
70
|
+
#end
|
71
|
+
|
72
|
+
def test_parse_line_with_empty_lines
|
73
|
+
assert_equal(nil, NesquikCSV.parse_line("", "UTF-8")) # to signal eof
|
74
|
+
#assert_equal(Array.new, NesquikCSV.parse_line("\n1,2,3"))
|
75
|
+
assert_equal([nil], NesquikCSV.parse_line("\n1,2,3", "UTF-8"))
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_read_and_readlines
|
79
|
+
assert_equal( @expected,
|
80
|
+
NesquikCSV.read(@path) )
|
81
|
+
assert_equal( @expected,
|
82
|
+
NesquikCSV.readlines(@path))
|
83
|
+
|
84
|
+
|
85
|
+
data = NesquikCSV.open(@path) do |csv|
|
86
|
+
csv.read
|
87
|
+
end
|
88
|
+
assert_equal(@expected, data)
|
89
|
+
data = NesquikCSV.open(@path) do |csv|
|
90
|
+
csv.readlines
|
91
|
+
end
|
92
|
+
assert_equal(@expected, data)
|
93
|
+
end
|
94
|
+
|
95
|
+
#def test_table
|
96
|
+
# table = NesquikCSV.table(@path)
|
97
|
+
# assert_instance_of(NesquikCSV::Table, table)
|
98
|
+
# assert_equal([[:"1", :"2", :"3"], [4, 5, nil]], table.to_a)
|
99
|
+
#end
|
100
|
+
|
101
|
+
def test_shift # aliased as gets() and readline()
|
102
|
+
NesquikCSV.open(@path, "r+") do |csv|
|
103
|
+
assert_equal(@expected.shift, csv.shift)
|
104
|
+
assert_equal(@expected.shift, csv.shift)
|
105
|
+
assert_equal(nil, csv.shift)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_long_line # ruby's regex parser may have problems with long rows
|
110
|
+
File.unlink(@path)
|
111
|
+
|
112
|
+
long_field_length = 2800
|
113
|
+
File.open(@path, "w") do |file|
|
114
|
+
file << "1,2,#{'3' * long_field_length}\r\n"
|
115
|
+
end
|
116
|
+
@expected = [%w{1 2} + ['3' * long_field_length]]
|
117
|
+
test_shift
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_enumerable
|
121
|
+
NesquikCSV.open(@path) do |csv|
|
122
|
+
assert(csv.include?(["1", "2", "3"]))
|
123
|
+
csv.rewind
|
124
|
+
assert_equal([["1", "2", "3"], ["4", "5"]], csv.to_a)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nesquikcsv
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Juan Martty
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake-compiler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Fastest-CSV fork with encoding support
|
28
|
+
email:
|
29
|
+
- null.terminated.string@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions:
|
32
|
+
- ext/csv_parser/extconf.rb
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- .gitignore
|
36
|
+
- Gemfile
|
37
|
+
- LICENSE
|
38
|
+
- README.md
|
39
|
+
- Rakefile
|
40
|
+
- ext/csv_parser/extconf.rb
|
41
|
+
- ext/csv_parser/parser.c
|
42
|
+
- lib/csv_parser.so
|
43
|
+
- lib/nesquikcsv.rb
|
44
|
+
- lib/nesquikcsv/version.rb
|
45
|
+
- nesquikcsv.gemspec
|
46
|
+
- test/tc_csv_parsing.rb
|
47
|
+
- test/tc_interface.rb
|
48
|
+
homepage: https://github.com/jmartty/nesquikcsv
|
49
|
+
licenses:
|
50
|
+
- MIT
|
51
|
+
metadata: {}
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 2.2.2
|
69
|
+
signing_key:
|
70
|
+
specification_version: 4
|
71
|
+
summary: Fastest-CSV fork with encoding support
|
72
|
+
test_files:
|
73
|
+
- test/tc_csv_parsing.rb
|
74
|
+
- test/tc_interface.rb
|