Skip to content

Instantly share code, notes, and snippets.

@darkfrog26
Created June 7, 2018 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darkfrog26/2dfe3fc7dcddcd9a72c5b7c55e3a669f to your computer and use it in GitHub Desktop.
Save darkfrog26/2dfe3fc7dcddcd9a72c5b7c55e3a669f to your computer and use it in GitHub Desktop.
private val groupCharacters = Set('(', '[', '{', '"')
private val groupEndings = Map('(' -> ')', '[' -> ']', '{' -> '}', '"' -> '"')
private def parseWords(s: String): List[String] = {
val b = new StringBuilder
var open = List.empty[Char]
val words = ListBuffer.empty[String]
s.foreach {
case ' ' if open.isEmpty => {
if (b.nonEmpty) {
words += b.toString()
b.clear()
}
}
case c if open.nonEmpty && groupEndings(open.head) == c => {
b.append(c)
open = open.tail
}
case c if groupCharacters.contains(c) => {
b.append(c)
open = c :: open
}
case c => b.append(c)
}
if (b.nonEmpty) {
words += b.toString()
}
words.toList
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment