Function Signature
Parameters
The raw CSV string to parse
Returns
A two-dimensional array where each inner array represents a row of CSV data, and each string represents a cell value
Description
TheparseCsv function parses a CSV (Comma-Separated Values) string into a structured two-dimensional array. It handles:
- Quoted fields: Values enclosed in double quotes can contain commas and newlines
- Escape sequences: Double quotes within quoted fields are escaped as
""(two consecutive quotes) - Line endings: Supports both
\nand\r\nline endings - Whitespace trimming: Automatically trims whitespace from unquoted cell values
Example Usage
Implementation Details
The parser uses a state machine approach with aninQuotes flag to track whether the current position is inside a quoted field. This allows it to correctly handle:
- Commas inside quoted strings (not treated as delimiters)
- Newlines inside quoted strings (not treated as row separators)
- Escaped double quotes (
""becomes") - Mixed quoted and unquoted fields in the same row