Skip to content

Instantly share code, notes, and snippets.

@seralf
Created October 19, 2013 22:42
Show Gist options
  • Save seralf/7062433 to your computer and use it in GitHub Desktop.
Save seralf/7062433 to your computer and use it in GitHub Desktop.
Simple draft idea for implementing a facility object for entity char replacement in HTML text
// idea for mapping: TODO
val mapping = """
{ %7B
} %7D
( %28
) %29
< %3C
> %3E
? %3F
/ %2F
\ %22
' %27
, %2C
: %3A
= %3D
& %26
\r %0D
\n %0A
\s+ +
"""
def replace(text: String, mapping: String, delimiter: String) = {
val res = for (l <- mapping.lines.toList; if (!l.trim().equals(""))) yield {
val e = l.split(delimiter)
(if (e { 0 }.startsWith("\\") && e { 0 }.length > 1) e { 0 } else Pattern.quote(e { 0 }), e { 1 })
}
def repl(txt: String, sbs: List[(String, String)]): String = sbs match {
case Nil => txt
case c :: tail => repl(txt.replaceAll(c._1, c._2), tail)
}
repl(text, res)
}
val boh = replace("ciao\nmondo vediamo se {in qualche modo} funziona!", mapping, "\t")
println(boh)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment