Skip to content

Instantly share code, notes, and snippets.

@thedmitriyk
Last active November 3, 2016 10:44
Show Gist options
  • Save thedmitriyk/6009335 to your computer and use it in GitHub Desktop.
Save thedmitriyk/6009335 to your computer and use it in GitHub Desktop.
Flickr Base58 encoder and decoder written in Scala.
object FlickrBase58Coder {
val alpha = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"
val base = alpha.length
def apply(encodedInput: String) = decode(encodedInput)
def apply(decodedInput: Long) = encode(decodedInput)
def encode(input: String): String = encode(input.toLong)
def encode(input: Long) = {
def enc(in: Long, acc: String): String = if (in < 1) acc else enc(in / base, alpha((in % base).toInt) + acc)
enc(input, "")
}
def decode: PartialFunction[String, Long] = {
case s: String if s.length == 0 => 0
case s: String if s.head != '1' => {
val in = s.reverse
def dec(idx: Int, acc: BigInt): Long = if (idx == in.length) acc.toLong else dec(idx + 1, acc + alpha.indexOf(in(idx)) * BigInt(base).pow(idx))
dec(0, 0)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment