fastest-csv 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +68 -0
- data/Rakefile +2 -0
- data/ext/csv_parser/extconf.rb +9 -0
- data/ext/csv_parser/parser.c +94 -0
- data/fastest-csv.gemspec +19 -0
- data/lib/fastest-csv.rb +1 -0
- data/lib/fastest-csv/version.rb +3 -0
- data/lib/fastest_csv.rb +91 -0
- data/test/tc_csv_parsing.rb +126 -0
- data/test/tc_interface.rb +119 -0
- metadata +61 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 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,68 @@
|
|
1
|
+
# FastestCSV
|
2
|
+
|
3
|
+
Fastest CSV class for MRI Ruby. Faster than faster_csv and fasterer-csv.
|
4
|
+
|
5
|
+
Uses native C code to parse CSV lines. Not (yet) compatible with JRuby.
|
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
|
+
Originally developed to parse large CSV log files from PowerMTA.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
gem 'fastest-csv'
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install fastest-csv
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
Parse single line
|
30
|
+
|
31
|
+
FastestCSV.parse_line("one,two,three")
|
32
|
+
=> ["one", "two", "three"]
|
33
|
+
|
34
|
+
"one,two,three".parse_csv
|
35
|
+
=> ["one", "two", "three"]
|
36
|
+
|
37
|
+
Parse file without header
|
38
|
+
|
39
|
+
FastestCSV.foreach("path/to/file.csv") do |row|
|
40
|
+
while row = csv.shift
|
41
|
+
#
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
Parse file with header
|
46
|
+
|
47
|
+
FastestCSV.open("path/to/file.csv") do |csv|
|
48
|
+
fields = csv.shift
|
49
|
+
while values = csv.shift
|
50
|
+
#
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
Parse file in array of arrays
|
55
|
+
|
56
|
+
rows = FastestCSV.read("path/to/file.csv")
|
57
|
+
|
58
|
+
Parse string in array of arrays
|
59
|
+
|
60
|
+
rows = FastestCSV.parse(csv_data)
|
61
|
+
|
62
|
+
## Contributing
|
63
|
+
|
64
|
+
1. Fork it
|
65
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
66
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
67
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
68
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,94 @@
|
|
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
|
+
/* default allocated size is 16 */
|
13
|
+
#define DEF_ARRAY_LEN 32
|
14
|
+
|
15
|
+
static VALUE cFastestCSV;
|
16
|
+
|
17
|
+
static VALUE parse_line(VALUE self, VALUE str)
|
18
|
+
{
|
19
|
+
if (NIL_P(str))
|
20
|
+
return Qnil;
|
21
|
+
|
22
|
+
const char *ptr = RSTRING_PTR(str);
|
23
|
+
int len = (int) RSTRING_LEN(str); /* cast to prevent warning in 64-bit OS */
|
24
|
+
|
25
|
+
if (len == 0)
|
26
|
+
return Qnil;
|
27
|
+
|
28
|
+
VALUE array = rb_ary_new2(DEF_ARRAY_LEN);
|
29
|
+
char value[len]; /* field value, no longer than line */
|
30
|
+
int state = 0;
|
31
|
+
int index = 0;
|
32
|
+
int i;
|
33
|
+
char c;
|
34
|
+
for (i = 0; i < len; i++)
|
35
|
+
{
|
36
|
+
c = ptr[i];
|
37
|
+
switch (c)
|
38
|
+
{
|
39
|
+
case ',':
|
40
|
+
if (state == 0) {
|
41
|
+
rb_ary_push(array, (index == 0 ? Qnil: rb_str_new(value, index)));
|
42
|
+
index = 0;
|
43
|
+
}
|
44
|
+
else if (state == 1) {
|
45
|
+
value[index++] = c;
|
46
|
+
}
|
47
|
+
else if (state == 2) {
|
48
|
+
rb_ary_push(array, rb_str_new(value, index));
|
49
|
+
index = 0;
|
50
|
+
state = 0; /* outside quoted */
|
51
|
+
}
|
52
|
+
break;
|
53
|
+
case '"':
|
54
|
+
if (state == 0) {
|
55
|
+
state = 1; /* in quoted */
|
56
|
+
}
|
57
|
+
else if (state == 1) {
|
58
|
+
state = 2; /* quote in quoted */
|
59
|
+
}
|
60
|
+
else if (state == 2) {
|
61
|
+
value[index++] = c; /* escaped quote */
|
62
|
+
state = 1; /* in quoted */
|
63
|
+
}
|
64
|
+
break;
|
65
|
+
case 13: /* \r */
|
66
|
+
case 10: /* \n */
|
67
|
+
if (state == 1) { /* quoted */
|
68
|
+
value[index++] = c;
|
69
|
+
}
|
70
|
+
else {
|
71
|
+
/* only do first line */
|
72
|
+
i = len;
|
73
|
+
}
|
74
|
+
/* else eat it ??? or return so far */
|
75
|
+
break;
|
76
|
+
default:
|
77
|
+
value[index++] = c;
|
78
|
+
}
|
79
|
+
}
|
80
|
+
|
81
|
+
if (state == 0) {
|
82
|
+
rb_ary_push(array, (index == 0 ? Qnil: rb_str_new(value, index)));
|
83
|
+
}
|
84
|
+
else if (state == 2) {
|
85
|
+
rb_ary_push(array, rb_str_new(value, index));
|
86
|
+
}
|
87
|
+
return array;
|
88
|
+
}
|
89
|
+
|
90
|
+
void Init_csv_parser()
|
91
|
+
{
|
92
|
+
cFastestCSV = rb_define_class("FastestCSV", rb_cObject);
|
93
|
+
rb_define_singleton_method(cFastestCSV, "parse_line", parse_line, 1);
|
94
|
+
}
|
data/fastest-csv.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/fastest-csv/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Maarten Oelering"]
|
6
|
+
gem.email = ["maarten@brightcode.nl"]
|
7
|
+
gem.description = %q{Fastest standard CSV parser for MRI Ruby}
|
8
|
+
gem.summary = %q{Fastest standard CSV parser for MRI Ruby}
|
9
|
+
gem.homepage = "https://github.com/brightcode/fastest-csv"
|
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 = "fastest-csv"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = FastestCSV::VERSION
|
17
|
+
|
18
|
+
gem.extensions = ['ext/csv_parser/extconf.rb']
|
19
|
+
end
|
data/lib/fastest-csv.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'fastest_csv'
|
data/lib/fastest_csv.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'csv_parser'
|
2
|
+
require 'stringio'
|
3
|
+
|
4
|
+
class FastestCSV
|
5
|
+
|
6
|
+
# This method opens an accounting file and passes each record to the provided +block+.
|
7
|
+
def self.foreach(path, &block)
|
8
|
+
open(path) do |reader|
|
9
|
+
reader.each(&block)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# This method opens a csv file. It will pass a Reader object to the provided block,
|
14
|
+
# or return a Reader object when no block is provided.
|
15
|
+
def self.open(path, mode = "rb")
|
16
|
+
csv = new(File.open(path, mode))
|
17
|
+
if block_given?
|
18
|
+
begin
|
19
|
+
yield csv
|
20
|
+
ensure
|
21
|
+
csv.close
|
22
|
+
end
|
23
|
+
else
|
24
|
+
csv
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.read(path)
|
29
|
+
open(path, "rb") { |csv| csv.read }
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.readlines(path)
|
33
|
+
read(path)
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.parse(data, &block)
|
37
|
+
csv = new(StringIO.new(data))
|
38
|
+
if block.nil?
|
39
|
+
begin
|
40
|
+
csv.read
|
41
|
+
ensure
|
42
|
+
csv.close
|
43
|
+
end
|
44
|
+
else
|
45
|
+
csv.each(&block)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def initialize(io)
|
50
|
+
@io = io
|
51
|
+
end
|
52
|
+
|
53
|
+
def each
|
54
|
+
while row = shift
|
55
|
+
yield row
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def read
|
60
|
+
table = Array.new
|
61
|
+
each {|row| table << row}
|
62
|
+
table
|
63
|
+
end
|
64
|
+
alias_method :readlines, :read
|
65
|
+
|
66
|
+
def shift
|
67
|
+
if line = @io.gets
|
68
|
+
FastestCSV.parse_line(line)
|
69
|
+
else
|
70
|
+
nil
|
71
|
+
end
|
72
|
+
end
|
73
|
+
alias_method :gets, :shift
|
74
|
+
alias_method :readline, :shift
|
75
|
+
|
76
|
+
def close
|
77
|
+
@io.close
|
78
|
+
end
|
79
|
+
|
80
|
+
def closed?
|
81
|
+
@io.closed?
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
class String
|
86
|
+
# Equivalent to <tt>FasterCSV::parse_line(self, options)</tt>.
|
87
|
+
def parse_csv
|
88
|
+
FastestCSV.parse_line(self)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
@@ -0,0 +1,126 @@
|
|
1
|
+
#
|
2
|
+
# Tests copied from faster_csv by James Edward Gray II
|
3
|
+
#
|
4
|
+
|
5
|
+
require 'test/unit'
|
6
|
+
require 'fastest_csv'
|
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
|
+
FastestCSV.parse_line(ex) )
|
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, FastestCSV.parse_line(csv_test.first))
|
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, FastestCSV.parse_line(csv_test.first))
|
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, FastestCSV.parse_line(edge_case.first))
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_james_edge_cases
|
99
|
+
# A read at eof? should return nil.
|
100
|
+
assert_equal(nil, FastestCSV.parse_line(""))
|
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, FastestCSV.parse_line("\n1,2,3\n"))
|
107
|
+
assert_equal([nil], FastestCSV.parse_line("\n1,2,3\n"))
|
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, FastestCSV.parse_line(edge_case.first))
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
#
|
2
|
+
# Tests copied from faster_csv by James Edward Gray II
|
3
|
+
#
|
4
|
+
|
5
|
+
require 'test/unit'
|
6
|
+
require 'fastest_csv'
|
7
|
+
|
8
|
+
class TestFastestCSVInterface < 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
|
+
FastestCSV.foreach(@path) do |row|
|
29
|
+
assert_equal(@expected.shift, row)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_open_and_close
|
34
|
+
csv = FastestCSV.open(@path, "r+")
|
35
|
+
assert_not_nil(csv)
|
36
|
+
assert_instance_of(FastestCSV, csv)
|
37
|
+
assert_equal(false, csv.closed?)
|
38
|
+
csv.close
|
39
|
+
assert(csv.closed?)
|
40
|
+
|
41
|
+
ret = FastestCSV.open(@path) do |csv|
|
42
|
+
assert_instance_of(FastestCSV, 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
|
+
FastestCSV.parse(data) )
|
53
|
+
|
54
|
+
FastestCSV.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, FastestCSV.parse_line("")) # to signal eof
|
74
|
+
#assert_equal(Array.new, FastestCSV.parse_line("\n1,2,3"))
|
75
|
+
assert_equal([nil], FastestCSV.parse_line("\n1,2,3"))
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_read_and_readlines
|
79
|
+
assert_equal( @expected,
|
80
|
+
FastestCSV.read(@path) )
|
81
|
+
assert_equal( @expected,
|
82
|
+
FastestCSV.readlines(@path))
|
83
|
+
|
84
|
+
|
85
|
+
data = FastestCSV.open(@path) do |csv|
|
86
|
+
csv.read
|
87
|
+
end
|
88
|
+
assert_equal(@expected, data)
|
89
|
+
data = FastestCSV.open(@path) do |csv|
|
90
|
+
csv.readlines
|
91
|
+
end
|
92
|
+
assert_equal(@expected, data)
|
93
|
+
end
|
94
|
+
|
95
|
+
#def test_table
|
96
|
+
# table = FastestCSV.table(@path)
|
97
|
+
# assert_instance_of(FastestCSV::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
|
+
FastestCSV.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
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fastest-csv
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Maarten Oelering
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-28 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Fastest standard CSV parser for MRI Ruby
|
15
|
+
email:
|
16
|
+
- maarten@brightcode.nl
|
17
|
+
executables: []
|
18
|
+
extensions:
|
19
|
+
- ext/csv_parser/extconf.rb
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- ext/csv_parser/extconf.rb
|
28
|
+
- ext/csv_parser/parser.c
|
29
|
+
- fastest-csv.gemspec
|
30
|
+
- lib/fastest-csv.rb
|
31
|
+
- lib/fastest-csv/version.rb
|
32
|
+
- lib/fastest_csv.rb
|
33
|
+
- test/tc_csv_parsing.rb
|
34
|
+
- test/tc_interface.rb
|
35
|
+
homepage: https://github.com/brightcode/fastest-csv
|
36
|
+
licenses: []
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.8.24
|
56
|
+
signing_key:
|
57
|
+
specification_version: 3
|
58
|
+
summary: Fastest standard CSV parser for MRI Ruby
|
59
|
+
test_files:
|
60
|
+
- test/tc_csv_parsing.rb
|
61
|
+
- test/tc_interface.rb
|