|
1 | 1 | local ftcsv = {
|
2 |
| - _VERSION = 'ftcsv 1.0.1', |
| 2 | + _VERSION = 'ftcsv 1.0.2', |
3 | 3 | _DESCRIPTION = 'CSV library for Lua',
|
4 | 4 | _URL = 'https://github.com/FourierTransformer/ftcsv',
|
5 | 5 | _LICENSE = [[
|
@@ -204,94 +204,90 @@ function ftcsv.parse(inputFile, delimiter, options)
|
204 | 204 | local currentChar, nextChar = string.byte(inputString, i), nil
|
205 | 205 |
|
206 | 206 | while i <= inputLength do
|
207 |
| - -- go by two chars at a time! |
| 207 | + -- go by two chars at a time! currentChar is set at the bottom. |
208 | 208 | -- currentChar = string.byte(inputString, i)
|
209 | 209 | nextChar = string.byte(inputString, i+1)
|
210 | 210 | -- print(i, string.char(currentChar), string.char(nextChar))
|
211 | 211 |
|
212 |
| - -- keeps track of characters to "skip" while going through the encoding process |
213 |
| - -- if skipChar == 0 then |
214 |
| - |
215 |
| - -- empty string |
216 |
| - if currentChar == quote and nextChar == quote then |
217 |
| - -- print("EMPTY STRING") |
218 |
| - skipChar = 1 |
219 |
| - fieldStart = i + 2 |
220 |
| - -- print("fs+2:", fieldStart) |
221 |
| - |
222 |
| - -- identifies the escape toggle |
223 |
| - elseif currentChar == quote and nextChar ~= quote then |
224 |
| - -- print("ESCAPE TOGGLE") |
225 |
| - fieldStart = i + 1 |
226 |
| - i, doubleQuoteEscape = M.findClosingQuote(i+1, inputLength, inputString, quote, doubleQuoteEscape) |
227 |
| - -- print("I VALUE", i, doubleQuoteEscape) |
228 |
| - skipChar = 1 |
229 |
| - -- end |
230 |
| - |
231 |
| - -- create some fields if we can! |
232 |
| - elseif currentChar == delimiterByte then |
233 |
| - -- for that first field |
234 |
| - if not headerSet and lineNum == 1 then |
235 |
| - headerField[fieldNum] = fieldNum |
236 |
| - end |
237 |
| - -- create the new field |
238 |
| - -- print(headerField[fieldNum]) |
239 |
| - doubleQuoteEscape = createNewField(inputString, quote, fieldStart, i, outResults[lineNum], headerField[fieldNum], doubleQuoteEscape, fieldsToKeep) |
240 |
| - |
241 |
| - fieldNum = fieldNum + 1 |
242 |
| - fieldStart = i + 1 |
243 |
| - -- print("fs+1:", fieldStart) |
244 |
| - -- end |
245 |
| - |
246 |
| - -- newline?! |
247 |
| - elseif ((currentChar == CR and nextChar == LF) or currentChar == LF) then |
248 |
| - -- keep track of headers |
249 |
| - if not headerSet and lineNum == 1 then |
250 |
| - headerField[fieldNum] = fieldNum |
251 |
| - end |
| 212 | + -- empty string |
| 213 | + if currentChar == quote and nextChar == quote then |
| 214 | + -- print("EMPTY STRING") |
| 215 | + skipChar = 1 |
| 216 | + fieldStart = i + 2 |
| 217 | + -- print("fs+2:", fieldStart) |
| 218 | + |
| 219 | + -- identifies the escape toggle |
| 220 | + elseif currentChar == quote and nextChar ~= quote then |
| 221 | + -- print("ESCAPE TOGGLE") |
| 222 | + fieldStart = i + 1 |
| 223 | + i, doubleQuoteEscape = M.findClosingQuote(i+1, inputLength, inputString, quote, doubleQuoteEscape) |
| 224 | + -- print("I VALUE", i, doubleQuoteEscape) |
| 225 | + skipChar = 1 |
| 226 | + |
| 227 | + -- create some fields if we can! |
| 228 | + elseif currentChar == delimiterByte then |
| 229 | + -- for that first field |
| 230 | + if not headerSet and lineNum == 1 then |
| 231 | + headerField[fieldNum] = fieldNum |
| 232 | + end |
| 233 | + -- create the new field |
| 234 | + -- print(headerField[fieldNum]) |
| 235 | + doubleQuoteEscape = createNewField(inputString, quote, fieldStart, i, outResults[lineNum], headerField[fieldNum], doubleQuoteEscape, fieldsToKeep) |
| 236 | + |
| 237 | + fieldNum = fieldNum + 1 |
| 238 | + fieldStart = i + 1 |
| 239 | + -- print("fs+1:", fieldStart) |
| 240 | + -- end |
| 241 | + |
| 242 | + -- newline?! |
| 243 | + elseif ((currentChar == CR and nextChar == LF) or currentChar == LF) then |
| 244 | + -- keep track of headers |
| 245 | + if not headerSet and lineNum == 1 then |
| 246 | + headerField[fieldNum] = fieldNum |
| 247 | + end |
252 | 248 |
|
253 |
| - -- create the new field |
254 |
| - doubleQuoteEscape = createNewField(inputString, quote, fieldStart, i, outResults[lineNum], headerField[fieldNum], doubleQuoteEscape, fieldsToKeep) |
| 249 | + -- create the new field |
| 250 | + doubleQuoteEscape = createNewField(inputString, quote, fieldStart, i, outResults[lineNum], headerField[fieldNum], doubleQuoteEscape, fieldsToKeep) |
255 | 251 |
|
256 |
| - -- if we have headers then we gotta do something about it |
257 |
| - if lineNum == 1 and not headerSet then |
258 |
| - if ofieldsToKeep ~= nil then |
259 |
| - fieldsToKeep = {} |
260 |
| - for j = 1, #ofieldsToKeep do |
261 |
| - fieldsToKeep[ofieldsToKeep[j]] = true |
262 |
| - end |
| 252 | + -- if we have headers then we gotta do something about it |
| 253 | + if lineNum == 1 and not headerSet then |
| 254 | + if ofieldsToKeep ~= nil then |
| 255 | + fieldsToKeep = {} |
| 256 | + for j = 1, #ofieldsToKeep do |
| 257 | + fieldsToKeep[ofieldsToKeep[j]] = true |
263 | 258 | end
|
264 |
| - if header then |
265 |
| - headerField, lineNum, headerSet = createHeaders(outResults[lineNum], rename) |
266 |
| - else |
267 |
| - -- files without headers, but with a rename need to be handled too! |
268 |
| - if #rename > 0 then |
269 |
| - for j = 1, math.max(#rename, #headerField) do |
270 |
| - headerField[j] = rename[j] |
271 |
| - -- this is an odd case of where there are certain fields to be kept |
272 |
| - if fieldsToKeep == nil or fieldsToKeep[rename[j]] then |
273 |
| - outResults[1][rename[j]] = outResults[1][j] |
274 |
| - end |
275 |
| - -- print("J", j) |
276 |
| - outResults[1][j] = nil |
| 259 | + end |
| 260 | + if header then |
| 261 | + headerField, lineNum, headerSet = createHeaders(outResults[lineNum], rename) |
| 262 | + else |
| 263 | + -- files without headers, but with a rename need to be handled too! |
| 264 | + if #rename > 0 then |
| 265 | + for j = 1, math.max(#rename, #headerField) do |
| 266 | + headerField[j] = rename[j] |
| 267 | + -- this is an odd case of where there are certain fields to be kept |
| 268 | + if fieldsToKeep == nil or fieldsToKeep[rename[j]] then |
| 269 | + outResults[1][rename[j]] = outResults[1][j] |
277 | 270 | end
|
| 271 | + -- print("J", j) |
| 272 | + outResults[1][j] = nil |
278 | 273 | end
|
279 | 274 | end
|
280 | 275 | end
|
| 276 | + end |
281 | 277 |
|
282 |
| - -- incrememnt for new line |
283 |
| - lineNum = lineNum + 1 |
284 |
| - outResults[lineNum] = {} |
285 |
| - fieldNum = 1 |
286 |
| - fieldStart = i + 1 |
| 278 | + -- incrememnt for new line |
| 279 | + lineNum = lineNum + 1 |
| 280 | + outResults[lineNum] = {} |
| 281 | + fieldNum = 1 |
| 282 | + fieldStart = i + 1 |
| 283 | + -- print("fs:", fieldStart) |
| 284 | + if (currentChar == CR and nextChar == LF) then |
| 285 | + -- print("CRLF DETECTED") |
| 286 | + skipChar = 1 |
| 287 | + fieldStart = fieldStart + 1 |
287 | 288 | -- print("fs:", fieldStart)
|
288 |
| - if (currentChar == CR and nextChar == LF) then |
289 |
| - -- print("CRLF DETECTED") |
290 |
| - skipChar = 1 |
291 |
| - fieldStart = fieldStart + 1 |
292 |
| - -- print("fs:", fieldStart) |
293 |
| - end |
294 | 289 | end
|
| 290 | + end |
295 | 291 |
|
296 | 292 | i = i + 1 + skipChar
|
297 | 293 | if (skipChar > 0) then
|
|
0 commit comments