WASM: Implement string hashcode

This commit is contained in:
Igor Laevsky
2021-07-30 18:49:40 +03:00
committed by TeamCityServer
parent c526145a48
commit af865544ff
3 changed files with 15 additions and 4 deletions
@@ -56,8 +56,16 @@ public class String internal constructor(internal val chars: CharArray) : Compar
public override fun toString(): String = this
// TODO: this is not nice
public override fun hashCode(): Int = 10
private var cachedHash: Int = 0
public override fun hashCode(): Int {
if (cachedHash != 0 || this.isEmpty())
return cachedHash
var hash = 0
for (c in chars)
hash = 31 * hash + c.toInt()
cachedHash = hash
return cachedHash
}
}
internal fun stringLiteral(startAddr: Int, length: Int) = String(unsafeRawMemoryToCharArray(startAddr, length))