summaryrefslogtreecommitdiff
path: root/source/core/slang-digest-util.cpp
diff options
context:
space:
mode:
authorlucy96chen <47800040+lucy96chen@users.noreply.github.com>2022-10-17 17:38:59 -0700
committerGitHub <noreply@github.com>2022-10-17 17:38:59 -0700
commit8add41a6e37994577d928bc312801ddfa1c33173 (patch)
tree6ca5ef639a22c4e37c7287df1877cb5bf7ce691c /source/core/slang-digest-util.cpp
parent09408e32d7c0ccebf38fe31b5d2ddf4b1cd128e4 (diff)
Shader cache index implementation (#2452)
Diffstat (limited to 'source/core/slang-digest-util.cpp')
-rw-r--r--source/core/slang-digest-util.cpp66
1 files changed, 66 insertions, 0 deletions
diff --git a/source/core/slang-digest-util.cpp b/source/core/slang-digest-util.cpp
new file mode 100644
index 000000000..d235f2cf0
--- /dev/null
+++ b/source/core/slang-digest-util.cpp
@@ -0,0 +1,66 @@
+// string-digest-util.cpp
+#include "slang-digest-util.h"
+
+#include "../core/slang-basic.h"
+#include "../core/slang-digest-builder.h"
+#include "../core/slang-md5.h"
+#include "../core/slang-char-util.h"
+
+namespace Slang
+{
+
+/*static*/ Digest DigestUtil::computeDigestForStringSlice(UnownedStringSlice text)
+{
+ DigestBuilder builder;
+ builder.addToDigest(text);
+ return builder.finalize();
+}
+
+/*static*/ Digest DigestUtil::combine(const Digest& digestA, const Digest& digestB)
+{
+ DigestBuilder builder;
+ builder.addToDigest(digestA);
+ builder.addToDigest(digestB);
+ return builder.finalize();
+}
+
+/*static*/ String DigestUtil::toString(const Digest& digest)
+{
+ StringBuilder hashString;
+
+ uint8_t* uint8Hash = (uint8_t*)digest.values;
+
+ for (Index i = 0; i < 16; ++i)
+ {
+ auto hashSegmentString = String(uint8Hash[i], 16);
+
+ if (hashSegmentString.getLength() == 1)
+ {
+ hashString.append("0");
+ }
+ hashString.append(hashSegmentString.getBuffer());
+ }
+
+ return hashString;
+}
+
+/*static*/ Digest DigestUtil::fromString(UnownedStringSlice hashString)
+{
+ uint8_t uint8Hash[16];
+
+ // When the hash is converted to a String, ReverseInternalAscii is called
+ // at the very end. Since there is no way to get a char* for hashString to pass
+ // to ReverseInternalAscii to flip the string back, we instead loop starting from
+ // the end and work backwards towards the beginning.
+ for (Index i = 0; i < 16; i++)
+ {
+ uint8Hash[i] = (uint8_t)CharUtil::getHexDigitValue(hashString[i * 2]) * 16
+ + (uint8_t)CharUtil::getHexDigitValue(hashString[i * 2 + 1]);
+ }
+
+ Digest digest;
+ memcpy(digest.values, uint8Hash, 16);
+ return digest;
+}
+
+}