Skip to content

Commit 54d0817

Browse files
Merge pull request #11 from FourierTransformer/os9-line-endings-fix
Mac OS9 line endings fix and BOM removal
2 parents 29675f4 + 111199c commit 54d0817

8 files changed

+40
-6
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
ftcsv, a fairly fast csv library written in pure Lua. It's been tested with LuaJIT 2.0/2.1 and Lua 5.1, 5.2, and 5.3
55

6-
It works well for CSVs that can easily be fully loaded into memory (easily up to a hundred MB). Currently, there isn't a "large" file mode with proper readers and writers for ingesting CSVs in bulk with a fixed amount of memory. It correctly handles both `\n` (LF) and `\r\n` (CRLF) line endings (ie it should work with Windows and Mac/Linux line endings) and has UTF-8 support.
6+
It works well for CSVs that can easily be fully loaded into memory (easily up to a hundred MB). Currently, there isn't a "large" file mode with proper readers and writers for ingesting CSVs in bulk with a fixed amount of memory. It correctly handles both `\n` (LF), `\r` (CR) and `\r\n` (CRLF) line endings (ie it should work with Unix, Mac OS 9, and Windows line endings), and has UTF-8 support (it will strip out BOM if it exists).
77

88

99

ftcsv-1.1.3-1.rockspec ftcsv-1.1.4-1.rockspec

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package = "ftcsv"
2-
version = "1.1.3-1"
2+
version = "1.1.4-1"
33

44
source = {
55
url = "git://github.com/FourierTransformer/ftcsv.git",
6-
tag = "1.1.3"
6+
tag = "1.1.4"
77
}
88

99
description = {

ftcsv.lua

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
local ftcsv = {
2-
_VERSION = 'ftcsv 1.1.3',
2+
_VERSION = 'ftcsv 1.1.4',
33
_DESCRIPTION = 'CSV library for Lua',
44
_URL = 'https://github.com/FourierTransformer/ftcsv',
55
_LICENSE = [[
@@ -215,7 +215,7 @@ local function parseString(inputString, inputLength, delimiter, i, headerField,
215215
-- end
216216

217217
-- newline?!
218-
elseif ((currentChar == CR and nextChar == LF) or currentChar == LF) then
218+
elseif (currentChar == CR or currentChar == LF) then
219219
if fieldsToKeep == nil or fieldsToKeep[headerField[fieldNum]] then
220220
-- create the new field
221221
field = createField(inputString, quote, fieldStart, i, doubleQuoteEscape)
@@ -347,7 +347,13 @@ function ftcsv.parse(inputFile, delimiter, options)
347347
end
348348

349349
-- parse through the headers!
350-
local headerField, i = parseString(inputString, inputLength, delimiter, 1)
350+
local startLine = 1
351+
352+
-- check for BOM
353+
if string.byte(inputString, 1) == 239 and string.byte(inputString, 2) == 187 and string.byte(inputString, 3) == 191 then
354+
startLine = 4
355+
end
356+
local headerField, i = parseString(inputString, inputLength, delimiter, startLine)
351357
i = i + 1 -- start at the next char
352358

353359
-- make sure a header isn't empty

spec/csvs/bom-os9.csv

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a,b,c1,2,34,5,ʤ

spec/csvs/os9.csv

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a,b,c1,2,34,5,ʤ

spec/json/bom-os9.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{
3+
"a": "1",
4+
"b": "2",
5+
"c": "3"
6+
},
7+
{
8+
"a": "4",
9+
"b": "5",
10+
"c": "ʤ"
11+
}
12+
]

spec/json/os9.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{
3+
"a": "1",
4+
"b": "2",
5+
"c": "3"
6+
},
7+
{
8+
"a": "4",
9+
"b": "5",
10+
"c": "ʤ"
11+
}
12+
]

spec/parse_encode_spec.lua

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ local function loadFile(textFile)
1010
end
1111

1212
local files = {
13+
"bom-os9",
1314
"comma_in_quotes",
1415
"correctness",
1516
"empty",
@@ -22,6 +23,7 @@ local files = {
2223
"json_no_newline",
2324
"newlines",
2425
"newlines_crlf",
26+
"os9",
2527
"quotes_and_newlines",
2628
"quotes_non_escaped",
2729
"simple",

0 commit comments

Comments
 (0)