fast-csv
2026-11-29
Fast, RFC 4180 compliant CSV parser
fast-csv
A fast, RFC 4180 compliant CSV parser for Common Lisp with 100% test coverage.
Purpose
fast-csv provides a high-performance CSV parser with strict RFC 4180 compliance. It is designed for applications requiring both speed and correctness when processing CSV data.
Features
- 100% test coverage (59 tests, 75 checks passing)
- Complete RFC 4180 compliance - Strict adherence to the standard
- Fast string-based parsing - Minimal memory allocation
- Streaming support - Handle large files via
parse-csv-stream - Simple API - Three main functions for common use cases
- Comprehensive error detection - Validates malformed CSV input
- Production ready - Behavior matches established libraries (fare-csv)
Installation
Manual Installation
- Place in your Quicklisp local-projects directory:
cd ~/quicklisp/local-projects/ # Copy or clone fast-csv here
- Load the system:
(ql:quickload :fast-csv)
Dependencies
No external dependencies required. Uses only Common Lisp standard library.
Quick Start
Basic Usage
(use-package :fast-csv) ;; Parse from string (parse-csv-string "Name,Age,City Alice,30,NYC Bob,25,LA") ;; => (("Name" "Age" "City") ("Alice" "30" "NYC") ("Bob" "25" "LA")) ;; Parse from file (handles large files efficiently with streaming) (parse-csv-file #P"data.csv") ;; Parse from stream (for custom streams or row-by-row processing) (with-open-file (stream "data.csv") (parse-csv-stream stream :row-fn (lambda (row) (process-row row))))
Handling Special Cases
;; Quoted fields with commas (parse-csv-string "\"Smith, John\",Engineer,50000") ;; => (("Smith, John" "Engineer" "50000")) ;; Escaped quotes (double-quote convention) (parse-csv-string "\"He said \"\"Hello\"\"\"") ;; => (("He said \"Hello\"")) ;; Multi-line quoted fields (parse-csv-string "\"Line 1 Line 2\",Field2") ;; => (("Line 1\nLine 2" "Field2")) ;; Empty fields (parse-csv-string "a,,c") ;; => (("a" "" "c")) ;; CRLF line endings (Windows) (parse-csv-string "a,b\r\nc,d") ;; => (("a" "b") ("c" "d"))
API Reference
Core Functions
parse-csv-string (string)
Parse CSV from a string.
Parameters:
string- CSV data as a string
Returns: List of rows, where each row is a list of strings
Example:
(parse-csv-string "a,b,c\nd,e,f") ;; => (("a" "b" "c") ("d" "e" "f"))
parse-csv-file (pathname)
Parse CSV from a file. Uses streaming internally for efficient memory usage.
Parameters:
pathname- Path to CSV file
Returns: List of rows
Example:
(parse-csv-file #P"data.csv") ;; Works efficiently even with large files
Note: For row-by-row processing without accumulating all rows in memory,
use parse-csv-stream with the :row-fn parameter.
parse-csv-stream (stream &key row-fn)
Parse CSV from a stream with optional row-by-row processing.
Parameters:
stream- Input streamrow-fn- Optional function called for each row (for streaming mode)
Returns:
- List of rows (accumulate mode, default)
- Number of rows processed (streaming mode with row-fn)
Accumulate mode (loads all rows into memory):
(with-open-file (s "data.csv") (parse-csv-stream s)) ;; => (("name" "age") ("Alice" "30"))
Streaming mode (constant memory, for large files):
(with-open-file (s "large.csv") (parse-csv-stream s :row-fn (lambda (row) (process-row row)))) ;; => 1000000 (number of rows processed)
validate-csv-string (string)
Validate CSV string and return list of all errors found.
Parameters:
string- CSV data as a string
Returns: List of error plists with :line, :column, :position, :error keys.
Empty list means no errors found.
Example:
(validate-csv-string "a,b,c\n1,2,3") ;; => NIL (no errors) (validate-csv-string "a,b\nc\"d,e\n\"unclosed") ;; => ((:LINE 2 :COLUMN 2 :POSITION 5 :ERROR "Unexpected quote in unquoted field") ;; (:LINE 3 :COLUMN 9 :POSITION 13 :ERROR "Unclosed quote at end of input"))
validate-csv-file (pathname)
Validate CSV file and return list of all errors found.
Parameters:
pathname- Path to CSV file
Returns: List of error plists (same format as validate-csv-string)
Example:
(validate-csv-file #P"data.csv") ;; => NIL (no errors) or list of errors with line/column information
Use case: Validate CSV files before processing to find all errors at once.
Write Functions
write-csv-string (rows)
Write CSV rows to a string.
Parameters:
rows- List of rows, where each row is a list of strings
Returns: CSV formatted string
Example:
(write-csv-string '(("name" "age") ("Alice" "30") ("Bob" "25"))) ;; => "name,age\nAlice,30\nBob,25"
write-csv-stream (rows stream)
Write CSV rows to a stream.
Parameters:
rows- List of rows, where each row is a list of stringsstream- Output stream
Example:
(with-open-file (s "output.csv" :direction :output :if-exists :supersede) (write-csv-stream '(("a" "b") ("c" "d")) s))
write-csv-file (rows pathname)
Write CSV rows to a file.
Parameters:
rows- List of rows, where each row is a list of stringspathname- Path to output file
Example:
(write-csv-file '(("name" "age") ("Alice" "30")) #P"output.csv")
Note: Write functions automatically quote fields containing commas, quotes, or newlines per RFC 4180.
RFC 4180 Compliance
fast-csv strictly implements RFC 4180, the CSV format specification:
Supported Features
- Line endings: CRLF (standard) and LF (common)
- Field delimiters: Comma (
,) - Quote character: Double-quote (
") - Quote escaping: Double-quote convention (
""→") - Multi-line fields: Newlines allowed within quoted fields
- Empty fields: Properly handled
- Trailing newlines: Correctly ignored
Error Detection
The parser validates input and signals errors for:
- Unclosed quotes
- Quotes in unquoted fields
- Text after closing quote (before delimiter)
;; These will signal errors: (parse-csv-string "\"unclosed) ; Unclosed quote (parse-csv-string "a\"b,c") ; Quote in unquoted field (parse-csv-string "\"a\"b,c") ; Text after closing quote
Running Tests
To run the comprehensive test suite:
;; Load the library and tests (ql:quickload :fast-csv/test) ;; Run all tests (fiveam:run! :fast-csv)
Expected Output
Running test suite FAST-CSV
Running test SIMPLE-FIELDS .
Running test QUOTED-FIELDS .
...
Did 75 checks.
Pass: 75 (100%)
Skip: 0 (0%)
Fail: 0 (0%)
Testing & Compliance
This library has been extensively tested with:
- 59 tests passing (75 checks, 100% coverage)
- Complete RFC 4180 compliance
- Behavior verified against fare-csv in RFC 4180 mode
- Test categories:
- Basic parsing (quoted/unquoted fields)
- Edge cases (empty fields, single fields, trailing commas)
- Quote handling (escaped quotes, quotes at boundaries)
- Whitespace (spaces, tabs preserved in fields)
- Line endings (LF, CRLF, mixed, trailing)
- Complex scenarios (multi-line, special characters)
- Real-world examples (headers, data with quotes)
- RFC 4180 strict compliance (ABNF grammar, malformed input)
Performance Characteristics
fast-csv is optimized for speed with true streaming support:
- Buffered I/O: 64KB read buffer eliminates char-by-char overhead
- Row-by-row streaming: Constant memory usage regardless of file size
- Minimal allocation: Reusable buffers, copy-on-finalize
- Inline state machine: No function call overhead
- Character-level parsing: Direct iteration without intermediate structures
Benchmark Results
Read Performance
Tested on real-world datasets with streaming mode and buffered I/O:
100K rows (12 MB) - Sales Records:
| Library | Time (avg) | Relative Speed |
|---|---|---|
| fast-csv | 167 ms | 1.00x (baseline) |
| fare-csv | 566 ms | 3.39x slower |
| cl-csv | 2.81 s | 16.86x slower |
839K rows (162 MB) - Asteroid Dataset:
| Library | Time (avg) | Relative Speed |
|---|---|---|
| fast-csv | 1.98 s | 1.00x (baseline) |
| fare-csv | 7.10 s | 3.59x slower |
| cl-csv | 44.70 s | 22.59x slower |
fast-csv consistently delivers 3-4x performance advantage over fare-csv and 17-23x over cl-csv while maintaining strict RFC 4180 compliance.
Read throughput: ~420,000 rows/second on typical hardware.
Write Performance
Tested on generated data (100K rows, 10 columns):
| Library | Time (avg) | Relative Speed |
|---|---|---|
| fast-csv | 66 ms | 1.00x (baseline) |
| fare-csv | 149 ms | 2.26x slower |
| cl-csv | 385 ms | 5.83x slower |
fast-csv is 2.3x faster than fare-csv and 5.8x faster than cl-csv for writing.
Write throughput: ~1,515,000 rows/second on typical hardware.
Running Benchmarks
(ql:quickload :fast-csv/benchmark) ;; Read benchmarks (fast-csv/benchmark:run-benchmarks "/path/to/your/file.csv") ;; Write benchmarks (fast-csv/benchmark:run-write-benchmarks :rows 100000 :cols 10) ;; Quick single-run benchmark (fast-csv/benchmark:quick-benchmark "/path/to/your/file.csv")
Note: All libraries use streaming mode for fair comparison and constant memory usage. Benchmark system optionally depends on fare-csv and cl-csv. If not installed, it will only benchmark fast-csv.
When to Use
Best for:
- Large CSV files (streaming support with constant memory)
- Performance-critical applications
- Strict RFC 4180 compliance required
- Integration with streaming data pipelines
Consider alternatives for:
- Non-standard CSV variants (TSV, custom delimiters)
Why Choose fast-csv?
- Correctness: 100% test coverage with strict RFC 4180 compliance
- Performance: Optimized for speed with minimal allocation
- Simplicity: Clean API with three main functions
- Reliability: Behavior matches established libraries
- No dependencies: Uses only Common Lisp standard library
License
MIT License
Contact
Author: Hajovonta
Email: hajovonta@protonmail.com
Contributing
Contributions welcome! This library achieves 100% test coverage with complete RFC 4180 compliance.
Current Status
- 43/43 tests passing (100%)
- All RFC 4180 features implemented
- Behavior verified against fare-csv
Need help? Check out the RFC 4180 specification or open an issue for support.