Skip to content

Commit

Permalink
Use MurmurHash3 to count id in Zsh - avoid using java methods from Me…
Browse files Browse the repository at this point in the history
…ssageDigest
  • Loading branch information
lwronski committed Nov 25, 2021
1 parent 50aa34a commit 3e1229b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ object Bash {
}

private def escape(s: String): String =
s.replace("\"", "\\\"").replace("`", "\\`").linesIterator.toStream.headOption.getOrElse("")
s.replace("\"", "\\\"").replace("`", "\\`").split("\r?\n").headOption.getOrElse("")
def print(items: Seq[CompletionItem]): String = {
val newLine = System.lineSeparator()
val b = new StringBuilder
Expand Down
25 changes: 8 additions & 17 deletions core/shared/src/main/scala/caseapp/core/complete/Zsh.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package caseapp.core.complete

import java.math.BigInteger
import java.nio.charset.StandardCharsets
import java.security.MessageDigest

import dataclass.data

import scala.collection.mutable
import scala.util.hashing.MurmurHash3

object Zsh {

Expand All @@ -24,18 +19,14 @@ object Zsh {
|}
|""".stripMargin

private def md5(content: Iterator[String]): String = {
val md = MessageDigest.getInstance("MD5")
for (s <- content) md.update(s.getBytes(StandardCharsets.UTF_8))
val digest = md.digest()
val res = new BigInteger(1, digest).toString(16)
if (res.length < 32)
("0" * (32 - res.length)) + res
else
res
private def hash(content: Iterator[String]): String = {
val sb = new StringBuilder()
for (s <- content) sb.append(s.getBytes(StandardCharsets.UTF_8))

MurmurHash3.arrayHash(sb.result().getBytes).toString
}
private def escape(s: String): String =
s.replace("'", "\\'").replace("`", "\\`").linesIterator.toStream.headOption.getOrElse("")
s.replace("'", "\\'").replace("`", "\\`").split("\r?\n").headOption.getOrElse("")
private def defs(item: CompletionItem): Seq[String] = {
val (options, arguments) = item.values.partition(_.startsWith("-"))
val optionsOutput =
Expand All @@ -59,7 +50,7 @@ object Zsh {
private def render(commands: Seq[String]): String =
if (commands.isEmpty) "_files" + System.lineSeparator()
else {
val id = md5(commands.iterator)
val id = hash(commands.iterator)
s"""local -a args$id
|args$id=(
|${commands.mkString(System.lineSeparator())}
Expand Down

0 comments on commit 3e1229b

Please sign in to comment.