Add support for any SV like TSV
This commit is contained in:
@@ -211,7 +211,7 @@ end
|
||||
---
|
||||
--- Requirements/assumptions:
|
||||
--- - Input is a single string containing the entire CSV content.
|
||||
--- - Field separators are commas (,).
|
||||
--- - Field separators are specified by delimiter parameter (default: comma).
|
||||
--- - Newlines between rows may be "\n" or "\r\n". "\r\n" is treated as one line break.
|
||||
--- - Fields may be quoted with double quotes (").
|
||||
--- - Inside quoted fields, doubled quotes ("") represent a literal quote character.
|
||||
@@ -221,9 +221,13 @@ end
|
||||
--- - Empty fields and empty rows are preserved.
|
||||
--- - The final row is emitted even if the text does not end with a newline.
|
||||
---
|
||||
--- Returns:
|
||||
--- - A table (array) of rows; each row is a table (array) of string fields.
|
||||
function fromCSV(csv)
|
||||
--- @param csv string The CSV text to parse.
|
||||
--- @param delimiter string? The field delimiter (default: ",").
|
||||
--- @return table A table (array) of rows; each row is a table (array) of string fields.
|
||||
function fromCSV(csv, delimiter)
|
||||
if delimiter == nil then
|
||||
delimiter = ","
|
||||
end
|
||||
local rows = {}
|
||||
local fields = {}
|
||||
local field = {}
|
||||
@@ -243,7 +247,7 @@ function fromCSV(csv)
|
||||
if c == '"' then
|
||||
state = STATE_IN_QUOTES
|
||||
i = i + 1
|
||||
elseif c == ',' then
|
||||
elseif c == delimiter then
|
||||
table.insert(fields, table.concat(field))
|
||||
field = {}
|
||||
i = i + 1
|
||||
@@ -274,7 +278,7 @@ function fromCSV(csv)
|
||||
table.insert(field, '"')
|
||||
state = STATE_IN_QUOTES
|
||||
i = i + 1
|
||||
elseif c == ',' then
|
||||
elseif c == delimiter then
|
||||
table.insert(fields, table.concat(field))
|
||||
field = {}
|
||||
state = STATE_DEFAULT
|
||||
@@ -310,13 +314,17 @@ end
|
||||
--- Requirements:
|
||||
--- - Input is a table (array) of rows, where each row is a table (array) of field values.
|
||||
--- - Field values are converted to strings using tostring().
|
||||
--- - Fields are quoted if they contain commas, newlines, or double quotes.
|
||||
--- - Fields are quoted if they contain the delimiter, newlines, or double quotes.
|
||||
--- - Double quotes inside quoted fields are doubled ("").
|
||||
--- - Fields are joined with commas; rows are joined with newlines.
|
||||
--- - Fields are joined with the specified delimiter; rows are joined with newlines.
|
||||
---
|
||||
--- @param rows table Array of rows, where each row is an array of field values.
|
||||
--- @param delimiter string? The field delimiter (default: ",").
|
||||
--- @return string CSV-formatted text.
|
||||
function toCSV(rows)
|
||||
function toCSV(rows, delimiter)
|
||||
if delimiter == nil then
|
||||
delimiter = ","
|
||||
end
|
||||
local rowStrings = {}
|
||||
|
||||
for _, row in ipairs(rows) do
|
||||
@@ -326,7 +334,7 @@ function toCSV(rows)
|
||||
local fieldStr = tostring(field)
|
||||
local needsQuoting = false
|
||||
|
||||
if fieldStr:find(',') or fieldStr:find('\n') or fieldStr:find('\r') or fieldStr:find('"') then
|
||||
if fieldStr:find(delimiter) or fieldStr:find('\n') or fieldStr:find('\r') or fieldStr:find('"') then
|
||||
needsQuoting = true
|
||||
end
|
||||
|
||||
@@ -338,7 +346,7 @@ function toCSV(rows)
|
||||
table.insert(fieldStrings, fieldStr)
|
||||
end
|
||||
|
||||
table.insert(rowStrings, table.concat(fieldStrings, ','))
|
||||
table.insert(rowStrings, table.concat(fieldStrings, delimiter))
|
||||
end
|
||||
|
||||
return table.concat(rowStrings, '\n')
|
||||
@@ -668,8 +676,8 @@ STRING FUNCTIONS:
|
||||
format(s, ...) - Formats string using Lua string.format
|
||||
trim(s) - Removes leading/trailing whitespace
|
||||
strsplit(inputstr, sep) - Splits string by separator (default: whitespace)
|
||||
fromCSV(csv) - Parses CSV text into rows of fields
|
||||
toCSV(rows) - Converts table of rows to CSV text format
|
||||
fromCSV(csv, delimiter) - Parses CSV text into rows of fields (delimiter defaults to ",")
|
||||
toCSV(rows, delimiter) - Converts table of rows to CSV text format (delimiter defaults to ",")
|
||||
num(str) - Converts string to number (returns 0 if invalid)
|
||||
str(num) - Converts number to string
|
||||
is_number(str) - Returns true if string is numeric
|
||||
|
||||
Reference in New Issue
Block a user