All Downloads are FREE. Search and download functionalities are using the official Maven repository.

spice.net.Decoder.scala Maven / Gradle / Ivy

There is a newer version: 0.7.2
Show newest version
package spice.net

import java.util.regex.{Matcher, Pattern}
import scala.util.matching.Regex

object Decoder {
  private val encodedRegex = """%([a-zA-Z0-9]{2})""".r

  def apply(s: String): String = {
    val cleaned = s.replace("\\", "\\\\")
    try {
      encodedRegex.replaceAllIn(cleaned, (m: Regex.Match) => {
        val g = m.group(1)
        val code = Integer.parseInt(g, 16)
        val c = code.toChar
        if (c == '\\') {
          "\\\\"
        } else {
          Matcher.quoteReplacement(c.toString)
        }
      })
    } catch {
      case t: Throwable => throw new RuntimeException(s"Failed to decode: [$cleaned]", t)
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy