Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues#99 Use Seq[String] for interface instead of List[String] #114

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/main/scala/com/github/tototoshi/csv/CSVParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ object CSVParser {
/**
* {{{
* scala> com.github.tototoshi.csv.CSVParser.parse("a,b,c", '\\', ',', '"')
* res0: Option[List[String]] = Some(List(a, b, c))
* res0: Option[Seq[String]] = Some(List(a, b, c))
*
* scala> com.github.tototoshi.csv.CSVParser.parse("\"a\",\"b\",\"c\"", '\\', ',', '"')
* res1: Option[List[String]] = Some(List(a, b, c))
* res1: Option[Seq[String]] = Some(List(a, b, c))
* }}}
*/
def parse(input: String, escapeChar: Char, delimiter: Char, quoteChar: Char): Option[List[String]] = {
def parse(input: String, escapeChar: Char, delimiter: Char, quoteChar: Char): Option[Seq[String]] = {
val buf: Array[Char] = input.toCharArray
var fields: Vector[String] = Vector()
var field = new StringBuilder
Expand Down Expand Up @@ -299,7 +299,7 @@ object CSVParser {

class CSVParser(format: CSVFormat) extends Serializable {

def parseLine(input: String): Option[List[String]] = {
def parseLine(input: String): Option[Seq[String]] = {
val parsedResult = CSVParser.parse(input, format.escapeChar, format.delimiter, format.quoteChar)
if (parsedResult == Some(List("")) && format.treatEmptyLineAsNil) Some(Nil)
else parsedResult
Expand Down
12 changes: 6 additions & 6 deletions src/main/scala/com/github/tototoshi/csv/CSVReader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class CSVReader protected (private val lineReader: LineReader)(implicit format:

private val parser = new CSVParser(format)

def readNext(): Option[List[String]] = {
def readNext(): Option[Seq[String]] = {

@scala.annotation.tailrec
def parseNext(lineReader: LineReader, leftOver: Option[String] = None): Option[List[String]] = {
def parseNext(lineReader: LineReader, leftOver: Option[String] = None): Option[Seq[String]] = {

val nextLine = lineReader.readLineWithTerminator()
if (nextLine == null) {
Expand Down Expand Up @@ -86,18 +86,18 @@ class CSVReader protected (private val lineReader: LineReader)(implicit format:

def toStreamWithHeaders: Stream[Map[String, String]] = iteratorWithHeaders.toStream

def toStream: Stream[List[String]] =
def toStream: Stream[Seq[String]] =
Stream.continually(readNext()).takeWhile(_.isDefined).map(_.get)

def all(): List[List[String]] = {
def all(): Seq[Seq[String]] = {
toStream.toList
}

def allWithHeaders(): List[Map[String, String]] = {
def allWithHeaders(): Seq[Map[String, String]] = {
allWithOrderedHeaders._2
}

def allWithOrderedHeaders(): (List[String], List[Map[String, String]]) = {
def allWithOrderedHeaders(): (Seq[String], Seq[Map[String, String]]) = {
val headers = readNext()
val data = headers.map(headers => {
val lines = all()
Expand Down
26 changes: 13 additions & 13 deletions src/test/scala/com/github/tototoshi/csv/CSVReaderSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CSVReaderSpec extends FunSpec with Matchers with Using {
describe("CSVReader") {

it("should be constructed with java.io.File") {
var res: List[String] = Nil
var res: Seq[String] = Nil
using(CSVReader.open(new File("src/test/resources/simple.csv"))) { reader =>
reader foreach { fields =>
res = res ++ fields
Expand All @@ -23,7 +23,7 @@ class CSVReaderSpec extends FunSpec with Matchers with Using {
}

it("should be constructed with filename") {
var res: List[String] = Nil
var res: Seq[String] = Nil
using(CSVReader.open("src/test/resources/simple.csv")) { reader =>
reader foreach { fields =>
res = res ++ fields
Expand Down Expand Up @@ -83,7 +83,7 @@ class CSVReaderSpec extends FunSpec with Matchers with Using {
}

it("read simple CSV from file") {
var res: List[String] = Nil
var res: Seq[String] = Nil
using(CSVReader.open(new FileReader("src/test/resources/simple.csv"))) { reader =>
reader foreach { fields =>
res = res ++ fields
Expand All @@ -94,7 +94,7 @@ class CSVReaderSpec extends FunSpec with Matchers with Using {

it("read simple CSV string") {
val csvString = "a,b,c\nd,e,f\n"
var res: List[String] = Nil
var res: Seq[String] = Nil
using(CSVReader.open(new StringReader(csvString))) { reader =>
reader foreach { fields =>
res = res ++ fields
Expand All @@ -104,7 +104,7 @@ class CSVReaderSpec extends FunSpec with Matchers with Using {
}

it("issue #22") {
var res: List[String] = Nil
var res: Seq[String] = Nil
using(CSVReader.open("src/test/resources/issue22.csv")) { reader =>
reader foreach { fields =>
res = res ++ fields
Expand All @@ -113,7 +113,7 @@ class CSVReaderSpec extends FunSpec with Matchers with Using {
}

it("issue #32") {
var res: List[String] = Nil
var res: Seq[String] = Nil
using(CSVReader.open("src/test/resources/issue32.csv")(new DefaultCSVFormat {
override val escapeChar: Char = '\\'
})) { reader =>
Expand All @@ -124,7 +124,7 @@ class CSVReaderSpec extends FunSpec with Matchers with Using {
}

it("should read csv file whose escape char is backslash") {
var res: List[String] = Nil
var res: Seq[String] = Nil
implicit val format = new DefaultCSVFormat {
override val escapeChar: Char = '\\'
}
Expand All @@ -137,7 +137,7 @@ class CSVReaderSpec extends FunSpec with Matchers with Using {
}

it("read simple CSV file with empty quoted fields") {
var res: List[String] = Nil
var res: Seq[String] = Nil
using(CSVReader.open("src/test/resources/issue30.csv")) { reader =>
reader foreach { fields =>
res = res ++ fields
Expand All @@ -147,7 +147,7 @@ class CSVReaderSpec extends FunSpec with Matchers with Using {
}

it("should read a file starting with BOM") {
var res: List[String] = Nil
var res: Seq[String] = Nil
using(CSVReader.open("src/test/resources/bom.csv")) { reader =>
reader foreach { fields =>
res = res ++ fields
Expand All @@ -165,7 +165,7 @@ class CSVReaderSpec extends FunSpec with Matchers with Using {
}

it("read CSV file including escaped fields") {
var res: List[String] = Nil
var res: Seq[String] = Nil
using(CSVReader.open(new FileReader("src/test/resources/escape.csv"))) { reader =>
reader foreach { fields =>
res = res ++ fields
Expand All @@ -175,7 +175,7 @@ class CSVReaderSpec extends FunSpec with Matchers with Using {
}

it("should correctly parse fields with line breaks enclosed in double quotes") {
var res: List[Seq[String]] = Nil
var res: Seq[Seq[String]] = Nil
using(CSVReader.open(new FileReader("src/test/resources/line-breaks.csv"))) { reader =>
reader foreach { fields =>
res = res :+ fields
Expand All @@ -187,10 +187,10 @@ class CSVReaderSpec extends FunSpec with Matchers with Using {

it("read TSV from file") {
implicit val format = new TSVFormat {}
var res: List[Seq[String]] = Nil
var res: Seq[Seq[String]] = Nil
using(CSVReader.open(new FileReader("src/test/resources/simple.tsv"))(format)) { reader =>
reader.foreach { fields =>
res = res ::: fields :: Nil
res = res :+ fields
}
}
res(0) should be(List("a", "b", "c"))
Expand Down