Permalink
Browse files

imbalanced quotes raise an exception

  • Loading branch information...
1 parent 04962b2 commit 7d49abc7d9cc55c3c308622753ac0bdd5d81a27e @jondistad jondistad committed Apr 3, 2011
Showing with 13 additions and 6 deletions.
  1. +6 −4 ext/bamfcsv/bamfcsv_ext.c
  2. +7 −2 spec/lib/bamfcsv_spec.rb
View
@@ -102,7 +102,7 @@ void finalize_cell(struct s_Cell *cell, char *cur, int quote_count) {
VALUE build_matrix(char *buf, int bufsize) {
int str_start = 0;
int num_rows = 1;
- int quote_count = 0, quotes_matched = 0;
+ int quote_count = 0, quotes_matched = 1;
struct s_Row *first_row = alloc_row();
struct s_Row *cur_row = first_row;
@@ -116,15 +116,15 @@ VALUE build_matrix(char *buf, int bufsize) {
for (cur = buf; cur < buf+bufsize; cur++) {
- int quotes_matched = !(quote_count & 1); /* count is even */
-
if (*cur == '"') {
if (0 == quote_count && cur_cell->start != cur)
rb_raise(BAMFCSV_MalformedCSVError_class, "Illegal quoting on line %d.", num_rows);
else
++quote_count;
}
+ quotes_matched = !(quote_count & 1); /* count is even */
+
if (quotes_matched) {
if (*cur == ',') {
@@ -157,7 +157,9 @@ VALUE build_matrix(char *buf, int bufsize) {
}
- if (cur_row->cell_count == 0) { /* Ended with newline */
+ if (!quotes_matched)
+ rb_raise(BAMFCSV_MalformedCSVError_class, "Illegal quoting on line %d.", num_rows);
+ else if (cur_row->cell_count == 0) { /* Ended with newline */
num_rows--;
} else { /* No newline before EOF */
finalize_cell(cur_cell, cur, quote_count);
View
@@ -81,8 +81,13 @@
expect { BAMFCSV.parse(' ""') }.should raise_error(BAMFCSV::MalformedCSVError)
end
- it "raises BAMFCSV::MalformedCSVError when a quoted cell is not closed at its end"
- it "raises BAMFCSV::MalformedCSVError when quoted cell is closed before its end"
+ it "raises BAMFCSV::MalformedCSVError when a quoted cell is not closed at its end" do
+ expect { BAMFCSV.parse('"') }.should raise_error(BAMFCSV::MalformedCSVError)
+ end
+
+ it "raises BAMFCSV::MalformedCSVError when quoted cell is closed before its end" do
+ expect { BAMFCSV.parse('"" ') }.should raise_error(BAMFCSV::MalformedCSVError)
+ end
end
end
end

0 comments on commit 7d49abc

Please sign in to comment.