XML unencoding in Scala



I've coded a function for XML unencoding in Scala. This function should replace encoded sequences as &amp;, &lt;, etc. with &, <, etc.



def unencode(encoding: Seq[(Char, String)])(inp: String): String =
if (inp == "") inp else {
val (c, s) =
encoding.find {case (k, v) => inp.startsWith(v)}
.map {case (k, v) => (k, inp.drop(v.size))}
.getOrElse (inp.head, inp.tail)
c + unencode(encoding)(s)
}


(It's just an exercise. I would probably use commons_lang)


Does it make sense ? How would you fix/improve it ?


No comments:

Post a Comment