Skip to content

Commit a6856fb

Browse files
authored
Merge pull request #71 from close2/CL/issue70
fix: if inside string ignore text-delimiters
2 parents c0aebba + fcb4f98 commit a6856fb

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 6.0.0
2+
If inside an unquoted string, text-delimiters are ignored instead of swallowed.
3+
This (partially?) fixes issue #70.
4+
Example: `"A B", "C, D"` will now produce `[["A B",' "C',' D"']]` instead of `[["A B",' C',' D']]`.
5+
16
# 5.1.1
27
Fix warnings. (Thanks https://github.com/thumbert for the bug report)
38

lib/src/csv_parser.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ part 'csv_argument_errors.dart';
8989
//
9090
// We have a counter for every delimiter. Before adding a character _c_ to the
9191
// StringBuffer which represents the current field, we find out if _c_ is part
92-
// a delimiter.
92+
// of a delimiter.
9393
// Assume all counters are 0 (the start condition). If any delimiter starts
9494
// with _c_ we increment the corresponding counter:
9595
// if (delim[delimCounter] == _c_) delimCounter++;
@@ -199,7 +199,8 @@ class CsvParser {
199199
/// non 0 counter and take a substring of the corresponding string.
200200
late StringBuffer _matchedChars;
201201

202-
/// If [allowInvalid] is true we only use the user supplied value if it isn't null.
202+
/// If [allowInvalid] is true we only use the user supplied value if it isn't
203+
/// null.
203204
static String? _argValue(
204205
bool? allowInvalid, String? userValue, String defaultValue,
205206
{String? userValue2}) {
@@ -297,10 +298,10 @@ class CsvParser {
297298
final onlyTextEndDelimiterMatches =
298299
_insideQuotedString && !_previousWasTextEndDelimiter;
299300

300-
// never look for a start text delimiter inside a quoted string.
301+
// never look for a start text delimiter inside a string.
301302
// (even if _previousWasTextEndDelimiter)
302303
final matchTextDelimiters =
303-
!_insideQuotedString && (!matching || _matchingTextDelimiter > 0);
304+
!_insideString && (!matching || _matchingTextDelimiter > 0);
304305

305306
final matchTextEndDelimiters =
306307
_insideQuotedString && (!matching || _matchingTextEndDelimiter > 0);

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: csv
2-
version: 5.1.1
2+
version: 6.0.0
33
description: |-
44
A codec to transform between a string and a list of values.
55

test/csv_to_list_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,4 +601,16 @@ void main_converter() {
601601
['', 'faa4aag', 'haa5.6']
602602
]));
603603
});
604+
605+
test('Issue #70', () {
606+
final input = '"A B", "C, D"\r\n';
607+
final parsed = CsvToListConverter().convert(input);
608+
expect(parsed, equals([["A B",' "C',' D"']]));
609+
});
610+
611+
test('Quotes inside', () {
612+
final input = 'A"B,C"D';
613+
final parsed = CsvToListConverter().convert(input);
614+
expect(parsed, equals([['A"B', 'C"D']]));
615+
});
604616
}

0 commit comments

Comments
 (0)