Created
June 12, 2019 03:56
-
-
Save winguse/6876703c4eaac038022e3af68d632284 to your computer and use it in GitHub Desktop.
A simple CSV parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Application { | |
case class CsvLine(values: List[String] = Nil, inQuote: Boolean = false, buff: List[Char] = Nil) | |
@tailrec | |
def parseCsvLine(line: CsvLine, remains: List[Char]): List[String] = (line, remains) match { | |
case (CsvLine(values, false, buff), Nil) => | |
values :+ buff.mkString | |
case (CsvLine(values, true, buff), '"' :: Nil) => | |
values :+ buff.mkString | |
case (CsvLine(values, false, buff), ',' :: tail) => | |
parseCsvLine(CsvLine(values :+ buff.mkString, inQuote = false, Nil), tail) | |
case (CsvLine(values, false, Nil), '"' :: tail) => | |
parseCsvLine(CsvLine(values, inQuote = true, Nil), tail) | |
case (CsvLine(values, true, buff), '"' :: ',' :: tail) => | |
parseCsvLine(CsvLine(values :+ buff.mkString, inQuote = false, Nil), tail) | |
case (CsvLine(values, true, buff), '"' :: '"' :: tail) => | |
parseCsvLine(CsvLine(values, inQuote = true, buff :+ '"'), tail) | |
case (CsvLine(values, inQuote, buff), c :: tail) => | |
parseCsvLine(CsvLine(values, inQuote, buff :+ c), tail) | |
case v => | |
throw new Error(s"error state: $v") | |
} | |
def processString(input: String): List[String] = { | |
parseCsvLine(CsvLine(), input.toList) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.scalatest.{Matchers, WordSpec} | |
class ApplicationSpec extends WordSpec with Matchers { | |
"Application.processString" should { | |
"process csv line correctly" in { | |
Application.processString("a,b,c") shouldEqual Seq("a", "b", "c") | |
Application.processString("aa,bb,cc") shouldEqual Seq("aa", "bb", "cc") | |
Application.processString("aa,,cc") shouldEqual Seq("aa", "", "cc") | |
Application.processString("aa,,") shouldEqual Seq("aa", "", "") | |
Application.processString(",,") shouldEqual Seq("", "", "") | |
Application.processString("") shouldEqual Seq("") | |
Application.processString("\"a\",b,c") shouldEqual Seq("a", "b", "c") | |
Application.processString("\"a\"\"\",b,c") shouldEqual Seq("a\"", "b", "c") | |
Application.processString("""a,"b, d",c""") shouldEqual Seq("a", "b, d", "c") | |
Application.processString("""a,"b, "","" d",c""") shouldEqual Seq("a", "b, \",\" d", "c") | |
Application.processString("""a,b,"c"""") shouldEqual Seq("a", "b", "c") | |
Application.processString("""a,b,"c, """") shouldEqual Seq("a", "b", "c, ") | |
Application.processString("""a,b,"c,"" """") shouldEqual Seq("a", "b", "c,\" ") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment