-
-
Notifications
You must be signed in to change notification settings - Fork 237
/
StringUtils.scala
37 lines (34 loc) · 1018 Bytes
/
StringUtils.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// (C) Uri Wilensky. https://github.com/NetLogo/NetLogo
package org.nlogo.api
// This object holds methods that are used specifically with NetLogo strings.
object StringUtils {
def escapeString(s: String): String =
s.flatMap{
case '\n' => "\\n"
case '\r' => "\\r"
case '\t' => "\\t"
case '\\' => "\\\\"
case '\"' => "\\\""
case c => c.toString
}
def unEscapeString(s: String): String = {
val (stuff, more) = s.span(_ != '\\')
if(more.size < 2)
s
else
stuff + unescape(more(1)) + unEscapeString(more.drop(2))
}
def unescape(c: Char): Char =
c match {
case 'n' => '\n'
case 'r' => '\r'
case 't' => '\t'
case '\\' => '\\'
case '"' => '"'
case _ =>
// kludge alert: Tokenizer expects exactly this exception, so it
// can detect the error and recover when it's in "robust" mode - ST 9/22/10
throw new IllegalArgumentException(
"invalid escape sequence: \\" + c)
}
}