Swiftpack.co -  msalmonse/CSVParser as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
msalmonse/CSVParser
CSV parser
.package(url: "https://github.com/msalmonse/CSVParser.git", from: "1.1.1")

CSVParser

This package parses CSV files following RFC-4180. It is much more tolerant than described there but that shouldn't be a problem. The state machine is described here

API

csvParser can be called as:
csvParse(csvText, separatedBy: ",", to: &csv, leavingWhiteSpace: false) or:
csvParse(csvURL, separatedBy: ",", to: &csv, leavingWhiteSpace: false)

Where csvText is the csv data as a string, csvURL is the location of a csv file and csv is an array of arrays of string that will be filled with the fields from the input. separatedBy can be used to change the field separator from ,. If leavingWhiteSpace is set to true then leading and trailing whitespace are left untrimmed.

Speed

100,000 lines of 7 fields of random numbers takes about .4 seconds to process on my M1 mini, leaving the spaces adds about .2 seconds. A simple split of the text on newlines and then commas using components is also about .4 seconds, YMMV.

Limitations

  1. The input and output must fit in memory at the same time.
  2. The column seperator mustn't be a part of a Unicode sequence e.g. 1️⃣ consists of 3 characters, 1, VS-16 and ⃣ (the enclosing keycap). If for some reason you wanted to use 1 as the field separator and 1️⃣ was in one of the fields then your text isn't going to look right. If you stick to comma, tab or, semicolon there shouldn't be a problem.

GitHub

link
Stars: 0
Last commit: 2 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Related Packages

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API