
io.lemonlabs.uri.encoding.UriEncoder.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scala-uri_sjs1_2.12 Show documentation
Show all versions of scala-uri_sjs1_2.12 Show documentation
Simple scala library for building and parsing URIs
The newest version!
package io.lemonlabs.uri.encoding
trait UriEncoder extends Product with Serializable {
def shouldEncode(ch: Char): Boolean
def encodeChar(ch: Char): String
def encode(s: String, charset: String): String = {
val bytes = s.getBytes(charset)
encode(bytes, charset)
}
def encode(bytes: Array[Byte], charset: String): String = {
val chars = bytes.map(_.toChar)
val encChars = chars.flatMap(ch => {
if (shouldEncode(ch)) {
encodeChar(ch).getBytes(charset)
} else {
Array(ch.toByte)
}
})
new String(encChars, charset)
}
def +(other: UriEncoder) = ChainedUriEncoder(this :: other :: Nil)
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy