Created
June 7, 2018 18:14
-
-
Save darkfrog26/2dfe3fc7dcddcd9a72c5b7c55e3a669f to your computer and use it in GitHub Desktop.
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
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