diff options
Diffstat (limited to 'lib/pure/parsecsv.nim')
-rw-r--r-- | lib/pure/parsecsv.nim | 40 |
1 files changed, 20 insertions, 20 deletions
diff --git a/lib/pure/parsecsv.nim b/lib/pure/parsecsv.nim index 117d75cfa..af51e1201 100644 --- a/lib/pure/parsecsv.nim +++ b/lib/pure/parsecsv.nim @@ -8,7 +8,7 @@ # ## This module implements a simple high performance `CSV`:idx: -## (`comma separated value`:idx:) parser. +## (`comma separated value`:idx:) parser. ## ## Example: How to use the parser ## ============================== @@ -43,7 +43,7 @@ type {.deprecated: [TCsvRow: CsvRow, TCsvParser: CsvParser, EInvalidCsv: CsvError].} -proc raiseEInvalidCsv(filename: string, line, col: int, +proc raiseEInvalidCsv(filename: string, line, col: int, msg: string) {.noreturn.} = var e: ref CsvError new(e) @@ -60,13 +60,13 @@ proc open*(my: var CsvParser, input: Stream, filename: string, ## for nice error messages. The parser's behaviour can be controlled by ## the diverse optional parameters: ## - `separator`: character used to separate fields - ## - `quote`: Used to quote fields containing special characters like + ## - `quote`: Used to quote fields containing special characters like ## `separator`, `quote` or new-line characters. '\0' disables the parsing ## of quotes. - ## - `escape`: removes any special meaning from the following character; + ## - `escape`: removes any special meaning from the following character; ## '\0' disables escaping; if escaping is disabled and `quote` is not '\0', ## two `quote` characters are parsed one literal `quote` character. - ## - `skipInitialSpace`: If true, whitespace immediately following the + ## - `skipInitialSpace`: If true, whitespace immediately following the ## `separator` is ignored. lexbase.open(my, input) my.filename = filename @@ -77,13 +77,13 @@ proc open*(my: var CsvParser, input: Stream, filename: string, my.row = @[] my.currRow = 0 -proc parseField(my: var CsvParser, a: var string) = +proc parseField(my: var CsvParser, a: var string) = var pos = my.bufpos var buf = my.buf if my.skipWhite: while buf[pos] in {' ', '\t'}: inc(pos) setLen(a, 0) # reuse memory - if buf[pos] == my.quote and my.quote != '\0': + if buf[pos] == my.quote and my.quote != '\0': inc(pos) while true: var c = buf[pos] @@ -91,7 +91,7 @@ proc parseField(my: var CsvParser, a: var string) = my.bufpos = pos # can continue after exception? error(my, pos, my.quote & " expected") break - elif c == my.quote: + elif c == my.quote: if my.esc == '\0' and buf[pos+1] == my.quote: add(a, my.quote) inc(pos, 2) @@ -103,11 +103,11 @@ proc parseField(my: var CsvParser, a: var string) = inc(pos, 2) else: case c - of '\c': + of '\c': pos = handleCR(my, pos) buf = my.buf add(a, "\n") - of '\l': + of '\l': pos = handleLF(my, pos) buf = my.buf add(a, "\n") @@ -123,11 +123,11 @@ proc parseField(my: var CsvParser, a: var string) = inc(pos) my.bufpos = pos -proc processedRows*(my: var CsvParser): int = +proc processedRows*(my: var CsvParser): int = ## returns number of the processed rows return my.currRow -proc readRow*(my: var CsvParser, columns = 0): bool = +proc readRow*(my: var CsvParser, columns = 0): bool = ## reads the next row; if `columns` > 0, it expects the row to have ## exactly this many columns. Returns false if the end of the file ## has been encountered else true. @@ -140,13 +140,13 @@ proc readRow*(my: var CsvParser, columns = 0): bool = my.row[col] = "" parseField(my, my.row[col]) inc(col) - if my.buf[my.bufpos] == my.sep: + if my.buf[my.bufpos] == my.sep: inc(my.bufpos) else: case my.buf[my.bufpos] - of '\c', '\l': + of '\c', '\l': # skip empty lines: - while true: + while true: case my.buf[my.bufpos] of '\c': my.bufpos = handleCR(my, my.bufpos) of '\l': my.bufpos = handleLF(my, my.bufpos) @@ -154,15 +154,15 @@ proc readRow*(my: var CsvParser, columns = 0): bool = of '\0': discard else: error(my, my.bufpos, my.sep & " expected") break - + setLen(my.row, col) result = col > 0 - if result and col != columns and columns > 0: - error(my, oldpos+1, $columns & " columns expected, but found " & + if result and col != columns and columns > 0: + error(my, oldpos+1, $columns & " columns expected, but found " & $col & " columns") inc(my.currRow) - -proc close*(my: var CsvParser) {.inline.} = + +proc close*(my: var CsvParser) {.inline.} = ## closes the parser `my` and its associated input stream. lexbase.close(my) |