Skip to content

Instantly share code, notes, and snippets.

@Jire
Last active February 16, 2016 21:50
Show Gist options
  • Save Jire/3f55c99d6f56ba0d4bce to your computer and use it in GitHub Desktop.
Save Jire/3f55c99d6f56ba0d4bce to your computer and use it in GitHub Desktop.
@file:JvmName("Stringa")
package org.jire.stringa
import java.util.*
private val intToString = HashMap<Int, String>(8192)
private val arrayToString = HashMap<Int, Int>(8192)
fun String.stringa() {
val hash = hashCode()
if (!intToString.containsKey(hash)) {
val array = toByteArray()
intToString.put(hash, this)
arrayToString.put(array.hashCode(), hash)
}
}
fun Array<Byte>.stringa(): String {
val hash = arrayToString[this.hashCode()]
if (hash != null) return intToString[hash]!!
var string = ""
for (b in this) string += b.toChar()
string.stringa()
return string
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment