mirror of
https://github.com/FourierTransformer/ftcsv.git
synced 2024-11-19 19:54:23 +00:00
localizing some commonly used funcs
This commit is contained in:
parent
4b68b9f097
commit
f2083dd38b
34
ftcsv.lua
34
ftcsv.lua
@ -35,6 +35,10 @@ else
|
|||||||
M.load = loadstring
|
M.load = loadstring
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- perf
|
||||||
|
local sbyte = string.byte
|
||||||
|
local ssub = string.sub
|
||||||
|
|
||||||
-- luajit specific speedups
|
-- luajit specific speedups
|
||||||
-- luajit performs faster with iterating over string.byte,
|
-- luajit performs faster with iterating over string.byte,
|
||||||
-- whereas vanilla lua performs faster with string.find
|
-- whereas vanilla lua performs faster with string.find
|
||||||
@ -42,17 +46,17 @@ if type(jit) == 'table' then
|
|||||||
-- finds the end of an escape sequence
|
-- finds the end of an escape sequence
|
||||||
function M.findClosingQuote(i, inputLength, inputString, quote, doubleQuoteEscape)
|
function M.findClosingQuote(i, inputLength, inputString, quote, doubleQuoteEscape)
|
||||||
-- local doubleQuoteEscape = doubleQuoteEscape
|
-- local doubleQuoteEscape = doubleQuoteEscape
|
||||||
local currentChar, nextChar = string.byte(inputString, i), nil
|
local currentChar, nextChar = sbyte(inputString, i), nil
|
||||||
while i <= inputLength do
|
while i <= inputLength do
|
||||||
-- print(i)
|
-- print(i)
|
||||||
nextChar = string.byte(inputString, i+1)
|
nextChar = sbyte(inputString, i+1)
|
||||||
|
|
||||||
-- this one deals with " double quotes that are escaped "" within single quotes "
|
-- this one deals with " double quotes that are escaped "" within single quotes "
|
||||||
-- these should be turned into a single quote at the end of the field
|
-- these should be turned into a single quote at the end of the field
|
||||||
if currentChar == quote and nextChar == quote then
|
if currentChar == quote and nextChar == quote then
|
||||||
doubleQuoteEscape = true
|
doubleQuoteEscape = true
|
||||||
i = i + 2
|
i = i + 2
|
||||||
currentChar = string.byte(inputString, i)
|
currentChar = sbyte(inputString, i)
|
||||||
|
|
||||||
-- identifies the escape toggle
|
-- identifies the escape toggle
|
||||||
elseif currentChar == quote and nextChar ~= quote then
|
elseif currentChar == quote and nextChar ~= quote then
|
||||||
@ -72,8 +76,8 @@ else
|
|||||||
local firstChar, iChar = nil, nil
|
local firstChar, iChar = nil, nil
|
||||||
repeat
|
repeat
|
||||||
firstCharIndex, i = inputString:find('".?', i+1)
|
firstCharIndex, i = inputString:find('".?', i+1)
|
||||||
firstChar = string.byte(inputString, firstCharIndex)
|
firstChar = sbyte(inputString, firstCharIndex)
|
||||||
iChar = string.byte(inputString, i)
|
iChar = sbyte(inputString, i)
|
||||||
-- nextChar = string.byte(inputString, i+1)
|
-- nextChar = string.byte(inputString, i+1)
|
||||||
-- print("HI", offset, i)
|
-- print("HI", offset, i)
|
||||||
-- print(firstChar, iChar)
|
-- print(firstChar, iChar)
|
||||||
@ -110,11 +114,11 @@ local function createNewField(inputString, quote, fieldStart, i, line, fieldNum,
|
|||||||
if fieldsToKeep == nil or fieldsToKeep[fieldNum] then
|
if fieldsToKeep == nil or fieldsToKeep[fieldNum] then
|
||||||
-- print(fieldsToKeep)
|
-- print(fieldsToKeep)
|
||||||
-- print("b4", i, fieldNum, line[fieldNum])
|
-- print("b4", i, fieldNum, line[fieldNum])
|
||||||
if string.byte(inputString, i-1) == quote then
|
if sbyte(inputString, i-1) == quote then
|
||||||
-- print("Skipping last \"")
|
-- print("Skipping last \"")
|
||||||
line[fieldNum] = string.sub(inputString, fieldStart, i-2)
|
line[fieldNum] = ssub(inputString, fieldStart, i-2)
|
||||||
else
|
else
|
||||||
line[fieldNum] = string.sub(inputString, fieldStart, i-1)
|
line[fieldNum] = ssub(inputString, fieldStart, i-1)
|
||||||
end
|
end
|
||||||
-- print("aft", i, fieldNum, line[fieldNum])
|
-- print("aft", i, fieldNum, line[fieldNum])
|
||||||
-- remove the double quotes (if they existed)
|
-- remove the double quotes (if they existed)
|
||||||
@ -150,7 +154,7 @@ function ftcsv.parse(inputFile, delimiter, options)
|
|||||||
|
|
||||||
-- delimiter MUST be one character
|
-- delimiter MUST be one character
|
||||||
assert(#delimiter == 1 and type(delimiter) == "string", "the delimiter must be of string type and exactly one character")
|
assert(#delimiter == 1 and type(delimiter) == "string", "the delimiter must be of string type and exactly one character")
|
||||||
local delimiterByte = string.byte(delimiter)
|
local delimiterByte = sbyte(delimiter)
|
||||||
|
|
||||||
-- OPTIONS yo
|
-- OPTIONS yo
|
||||||
local header = true
|
local header = true
|
||||||
@ -187,9 +191,9 @@ function ftcsv.parse(inputFile, delimiter, options)
|
|||||||
inputString = loadFile(inputFile)
|
inputString = loadFile(inputFile)
|
||||||
end
|
end
|
||||||
|
|
||||||
local CR = string.byte("\r")
|
local CR = sbyte("\r")
|
||||||
local LF = string.byte("\n")
|
local LF = sbyte("\n")
|
||||||
local quote = string.byte("\"")
|
local quote = sbyte("\"")
|
||||||
local doubleQuoteEscape = false
|
local doubleQuoteEscape = false
|
||||||
local fieldStart = 1
|
local fieldStart = 1
|
||||||
local fieldNum = 1
|
local fieldNum = 1
|
||||||
@ -201,12 +205,12 @@ function ftcsv.parse(inputFile, delimiter, options)
|
|||||||
local i = 1
|
local i = 1
|
||||||
|
|
||||||
-- keep track of my chars!
|
-- keep track of my chars!
|
||||||
local currentChar, nextChar = string.byte(inputString, i), nil
|
local currentChar, nextChar = sbyte(inputString, i), nil
|
||||||
|
|
||||||
while i <= inputLength do
|
while i <= inputLength do
|
||||||
-- go by two chars at a time! currentChar is set at the bottom.
|
-- go by two chars at a time! currentChar is set at the bottom.
|
||||||
-- currentChar = string.byte(inputString, i)
|
-- currentChar = string.byte(inputString, i)
|
||||||
nextChar = string.byte(inputString, i+1)
|
nextChar = sbyte(inputString, i+1)
|
||||||
-- print(i, string.char(currentChar), string.char(nextChar))
|
-- print(i, string.char(currentChar), string.char(nextChar))
|
||||||
|
|
||||||
-- empty string
|
-- empty string
|
||||||
@ -291,7 +295,7 @@ function ftcsv.parse(inputFile, delimiter, options)
|
|||||||
|
|
||||||
i = i + 1 + skipChar
|
i = i + 1 + skipChar
|
||||||
if (skipChar > 0) then
|
if (skipChar > 0) then
|
||||||
currentChar = string.byte(inputString, i)
|
currentChar = sbyte(inputString, i)
|
||||||
else
|
else
|
||||||
currentChar = nextChar
|
currentChar = nextChar
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user