-
Notifications
You must be signed in to change notification settings - Fork 11
/
csv.pl
176 lines (161 loc) · 5.76 KB
/
csv.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use v6;
class Text::CSV {
has Str $.quote_char is rw = '"';
has Str $.escape_char is rw = '"';
has Str $.sep_char is rw = ',';
has Str $.eol is rw; # = ($*IN.newline),
has Bool $.always_quote is rw;
has Bool $.quote_space is rw = True;
has Bool $.quote_null is rw = True;
has Bool $.quote_binary is rw = True;
has Bool $.binary is rw;
has Bool $.keep_meta_info is rw;
has Bool $.allow_loose_quotes is rw;
has Bool $.allow_loose_escapes is rw;
has Bool $.allow_whitespace is rw;
has Bool $.blank_is_undef is rw;
has Bool $.empty_is_undef is rw;
has Bool $.verbatim is rw;
has Bool $.auto_diag is rw;
has @!fields;
method parse(Str:D $line){
enum State <Start Data QuotedData Finish Escape>;
my State $state = State::Start;
my State $saved_state;
my Str $field;
my Int $index;
my Str $input;
my sub append(Str:D $char){
$field ~= $char;
}
my sub non_nil{
$field ~= '';
}
my sub store(){
@!fields.push($field);
$field = Nil;
}
my sub push_state(State $ns){
$saved_state = $state;
$state = $ns;
}
my sub pop_state(){
$state = $saved_state;
}
my sub parse_error(Str:D $reason, *@args){
my $msg = $reason.sprintf(@args);
die "$msg\n$line\n" ~ ' ' x $index ~ "^\n";
}
@!fields = ();
my Int $last=$line.chars;
$index=0;
while $index < $last { # 30% faster than 0..^$line.chars ...
#for 0..^$line.chars -> Int:D $lindex {
# $index = $lindex;
$input = $line.substr($index,1);
given $state {
when State::Start {
given $input {
when $!sep_char { store; }
when $!quote_char { non_nil; $state = State::QuotedData; }
#when $!escape_char { $state = State::Data; push_state(State::Escape); }
default { append($_); $state = State::Data; }
}
}
when State::Data {
given $input {
when $!sep_char { store; $state = State::Start; }
#when $!escape_char { push_state(State::Escape); }
when $!quote_char { parse_error("Halfway quoting is forbidden"); }
default { append($_); }
}
}
when State::QuotedData {
given $input {
when $!quote_char { $state = State::Finish; }
#when $!escape_char { push_state(State::Escape); }
default { append($_); }
}
}
when State::Escape {
given $input {
when $!sep_char|$!quote_char|$!escape_char { append($_); pop_state; }
default { parse_error("Illegally escaped character"); }
}
}
when State::Finish {
given $input {
when $!sep_char { store; $state = State::Start; }
default { parse_error("Seperator ('%s') expected", $!sep_char); }
}
}
default { say "What state?", $_ }
}
++$index;
}
given $state {
when State::Start|State::Finish|State::Data { store }
default { parse_error("Inproper state to end the line (%s)", $state); }
}
}
method getline(){
@!fields;
}
}
sub MAIN(
Str :$quote_char = '"',
Str :$escape_char = '"',
Str :$sep_char = ',',
Str :$eol, # = ($*IN.newline),
Bool :$always_quote,
Bool :$quote_space = True,
Bool :$quote_null = True,
Bool :$quote_binary = True,
Bool :$binary,
Bool :$keep_meta_info,
Bool :$allow_loose_quotes,
Bool :$allow_loose_escapes,
Bool :$allow_whitespace,
Bool :$blank_is_undef,
Bool :$empty_is_undef,
Bool :$verbatim,
Bool :$auto_diag,
) {
my $csv_parser = Text::CSV.new
:$quote_char
:$escape_char
:$sep_char
:$eol
:$always_quote
:$quote_space
:$quote_null
:$quote_binary
:$binary
:$keep_meta_info
:$allow_loose_quotes
:$allow_loose_escapes
:$allow_whitespace
:$blank_is_undef
:$empty_is_undef
:$verbatim
:$auto_diag
;
#my Str $sep ='"';
#say $csv_parser.perl;
$csv_parser.parse(q/ab,cde,"q",/);
say $csv_parser.getline().perl;
$csv_parser.sep_char='e';
$csv_parser.parse(q/ab,cde"q"e/);
say $csv_parser.getline().perl;
#$csv_parser.parse(q/ab,cde"q"eaa"aaarghh/);
#$csv_parser.parse(q/ab,cde"q"e"aaarghh/);
$csv_parser.sep_char=',';
my $sum = 0;
for lines() :eager {
$csv_parser.parse($_);
my @r = $csv_parser.getline();
#say @r.perl;
$sum += +@r;
}
say $sum;
}