From 8c712eb1cba8f511169d6b05bc832e116b540ab8 Mon Sep 17 00:00:00 2001 From: Shakil Thakur Date: Mon, 14 Mar 2016 20:12:45 -0500 Subject: [PATCH] added support for lua 5.1 and 5.3 and have setup .travis.yml --- .travis.yml | 30 ++++++++ ...1.0.0-1.rockspec => ftcsv-1.0.1-1.rockspec | 6 +- ftcsv.lua | 69 ++++++++++++------- 3 files changed, 78 insertions(+), 27 deletions(-) create mode 100644 .travis.yml rename ftcsv-1.0.0-1.rockspec => ftcsv-1.0.1-1.rockspec (93%) diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..8320624 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,30 @@ +language: python +sudo: false + +env: + - LUA="lua=5.1" + - LUA="lua=5.2" + - LUA="lua=5.3" + - LUA="luajit=2.0" + - LUA="luajit=2.1" + +before_install: + - pip install hererocks + - hererocks lua_install -r^ --$LUA + - export PATH=$PATH:$PWD/lua_install/bin # Add directory with all installed binaries to PATH + +install: + - luarocks install busted + - luarocks install luacov + - luarocks install luacov-coveralls + +script: + - busted --verbose --coverage + +after_success: + - luacov-coveralls --exclude $TRAVIS_BUILD_DIR/lua_install + +branches: + except: + - gh-pages + diff --git a/ftcsv-1.0.0-1.rockspec b/ftcsv-1.0.1-1.rockspec similarity index 93% rename from ftcsv-1.0.0-1.rockspec rename to ftcsv-1.0.1-1.rockspec index 70f5dea..73d264d 100644 --- a/ftcsv-1.0.0-1.rockspec +++ b/ftcsv-1.0.1-1.rockspec @@ -1,9 +1,9 @@ package = "ftcsv" -version = "1.0.0-1" +version = "1.0.1-1" source = { url = "git://github.com/FourierTransformer/ftcsv.git", - tag = "1.0.0" + tag = "1.0.1" } description = { @@ -22,7 +22,7 @@ description = { } dependencies = { - "lua >= 5.1, <5.3", + "lua >= 5.1, <5.4", } build = { diff --git a/ftcsv.lua b/ftcsv.lua index 29d5b0e..7f824f2 100644 --- a/ftcsv.lua +++ b/ftcsv.lua @@ -1,17 +1,39 @@ ---------------- --- ## ftcsv, a fairly fast csv library written in pure lua --- --- It works well for CSVs that can easily be fully loaded into memory (easily --- up to a hundred MBs). Currently, there isn't a "large" file mode with --- proper readers and writers for ingesting CSVs in bulk with a fixed amount --- of memory --- --- @author Shakil Thakur --- @copyright 2016 --- @license MIT ---------------- +local ftcsv = { + _VERSION = 'ftcsv 1.0.1', + _DESCRIPTION = 'CSV library for Lua', + _URL = 'https://github.com/FourierTransformer/ftcsv', + _LICENSE = [[ + The MIT License (MIT) -local ftcsv = {} + Copyright (c) 2016 Shakil Thakur + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + ]] +} + +-- lua 5.1 compat +local M = {} +if type(jit) == 'table' or _ENV then + M.load = _G.load +else + M.load = loadstring +end -- load an entire file into memory local function loadFile(textFile) @@ -24,7 +46,7 @@ end -- finds the end of an escape sequence local function findClosingQuote(i, inputLength, inputString, quote, doubleQuoteEscape) - local doubleQuoteEscape = doubleQuoteEscape + -- local doubleQuoteEscape = doubleQuoteEscape while i <= inputLength do -- print(i) local currentChar = string.byte(inputString, i) @@ -94,7 +116,7 @@ function ftcsv.parse(inputFile, delimiter, options) -- delimiter MUST be one character assert(#delimiter == 1 and type(delimiter) == "string", "the delimiter must be of string type and exactly one character") - local delimiter = string.byte(delimiter) + local delimiterByte = string.byte(delimiter) -- OPTIONS yo local header = true @@ -173,7 +195,7 @@ function ftcsv.parse(inputFile, delimiter, options) -- end -- create some fields if we can! - elseif currentChar == delimiter then + elseif currentChar == delimiterByte then -- for that first field if not headerSet and lineNum == 1 then headerField[fieldNum] = fieldNum @@ -201,8 +223,8 @@ function ftcsv.parse(inputFile, delimiter, options) if lineNum == 1 and not headerSet then if ofieldsToKeep ~= nil then fieldsToKeep = {} - for i = 1, #ofieldsToKeep do - fieldsToKeep[ofieldsToKeep[i]] = true + for j = 1, #ofieldsToKeep do + fieldsToKeep[ofieldsToKeep[j]] = true end end if header then @@ -254,11 +276,11 @@ function ftcsv.parse(inputFile, delimiter, options) -- clean up last line if it's weird (this happens when there is a CRLF newline at end of file) -- doing a count gets it to pick up the oddballs local finalLineCount = 0 - for _, value in pairs(outResults[lineNum]) do + for _, _ in pairs(outResults[lineNum]) do finalLineCount = finalLineCount + 1 end local initialLineCount = 0 - for _, value in pairs(outResults[1]) do + for _, _ in pairs(outResults[1]) do initialLineCount = initialLineCount + 1 end -- print("Final/Initial", finalLineCount, initialLineCount) @@ -282,7 +304,6 @@ end local function writer(inputTable, dilimeter, headers) -- they get re-created here if they need to be escaped so lua understands it based on how -- they came in - local headers = headers for i = 1, #headers do if inputTable[1][headers[i]] == nil then error("the field '" .. headers[i] .. "' doesn't exist in the table") @@ -306,13 +327,13 @@ local function writer(inputTable, dilimeter, headers) state.tableSize = #inputTable state.delimitField = delimitField - return load(outputFunc), state, 0 + return M.load(outputFunc), state, 0 end -- takes the values from the headers in the first row of the input table local function extractHeaders(inputTable) - headers = {} + local headers = {} for key, _ in pairs(inputTable[1]) do headers[#headers+1] = key end @@ -330,7 +351,6 @@ function ftcsv.encode(inputTable, delimiter, options) -- dilimeter MUST be one character assert(#delimiter == 1 and type(delimiter) == "string", "the delimiter must be of string type and exactly one character") - local delimiter = delimiter -- grab the headers from the options if they are there local headers = nil @@ -364,3 +384,4 @@ function ftcsv.encode(inputTable, delimiter, options) end return ftcsv +