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