pw_tokenizer: C version of hash function
Change-Id: I4b62699dca1dcb5f9477a0338fff1cf32bec6c43
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/22780
Reviewed-by: Keir Mierle <keir@google.com>
Commit-Queue: Wyatt Hepler <hepler@google.com>
diff --git a/pw_tokenizer/BUILD b/pw_tokenizer/BUILD
index 431aa4b..350d463 100644
--- a/pw_tokenizer/BUILD
+++ b/pw_tokenizer/BUILD
@@ -27,6 +27,7 @@
name = "pw_tokenizer",
srcs = [
"encode_args.cc",
+ "hash.cc",
"public/pw_tokenizer/config.h",
"public/pw_tokenizer/internal/argument_types.h",
"public/pw_tokenizer/internal/argument_types_macro_4_byte.h",
diff --git a/pw_tokenizer/BUILD.gn b/pw_tokenizer/BUILD.gn
index 06011be..4efa585 100644
--- a/pw_tokenizer/BUILD.gn
+++ b/pw_tokenizer/BUILD.gn
@@ -55,6 +55,7 @@
]
sources = [
"encode_args.cc",
+ "hash.cc",
"public/pw_tokenizer/internal/argument_types.h",
"public/pw_tokenizer/internal/argument_types_macro_4_byte.h",
"public/pw_tokenizer/internal/argument_types_macro_8_byte.h",
diff --git a/pw_tokenizer/CMakeLists.txt b/pw_tokenizer/CMakeLists.txt
index 060a798..05b713a 100644
--- a/pw_tokenizer/CMakeLists.txt
+++ b/pw_tokenizer/CMakeLists.txt
@@ -15,6 +15,7 @@
pw_add_module_library(pw_tokenizer
SOURCES
encode_args.cc
+ hash.cc
tokenize.cc
PUBLIC_DEPS
pw_polyfill.overrides
diff --git a/pw_tokenizer/hash.cc b/pw_tokenizer/hash.cc
new file mode 100644
index 0000000..8cb3f26
--- /dev/null
+++ b/pw_tokenizer/hash.cc
@@ -0,0 +1,28 @@
+// Copyright 2020 The Pigweed Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+#include "pw_tokenizer/hash.h"
+
+namespace pw {
+namespace tokenizer {
+
+extern "C" uint32_t pw_tokenizer_65599FixedLengthHash(const char* string,
+ size_t string_length,
+ size_t hash_length) {
+ return PwTokenizer65599FixedLengthHash(
+ std::string_view(string, string_length), hash_length);
+}
+
+} // namespace tokenizer
+} // namespace pw
diff --git a/pw_tokenizer/hash_test.cc b/pw_tokenizer/hash_test.cc
index 2870542..de6681e 100644
--- a/pw_tokenizer/hash_test.cc
+++ b/pw_tokenizer/hash_test.cc
@@ -110,7 +110,13 @@
PwTokenizer65599FixedLengthHash( \
std::string_view(string_literal, sizeof(string_literal) - 1), \
sizeof(string_literal) - 1) == Hash(string_literal), \
- "Hash function mismatch!")
+ "Hash function mismatch!"); \
+ EXPECT_EQ(PwTokenizer65599FixedLengthHash( \
+ std::string_view(string_literal, sizeof(string_literal) - 1), \
+ sizeof(string_literal) - 1), \
+ pw_tokenizer_65599FixedLengthHash(string_literal, \
+ sizeof(string_literal) - 1, \
+ sizeof(string_literal) - 1))
TEST(HashMacro, Empty) { TEST_SUPPORTED_HASHES(""); }
diff --git a/pw_tokenizer/public/pw_tokenizer/hash.h b/pw_tokenizer/public/pw_tokenizer/hash.h
index c3e5414..2cd8cf3 100644
--- a/pw_tokenizer/public/pw_tokenizer/hash.h
+++ b/pw_tokenizer/public/pw_tokenizer/hash.h
@@ -13,8 +13,11 @@
// the License.
#pragma once
-#include <cstddef>
-#include <cstdint>
+#include <stddef.h>
+#include <stdint.h>
+
+#ifdef __cplusplus
+
#include <string_view>
#include "pw_preprocessor/compiler.h"
@@ -100,3 +103,11 @@
}
} // namespace pw::tokenizer
+
+#endif // __cplusplus
+
+// C version of the fixed-length hash. Can be used to calculate hashes
+// equivalent to the hashing macros at runtime in C.
+PW_EXTERN_C uint32_t pw_tokenizer_65599FixedLengthHash(const char* string,
+ size_t string_length,
+ size_t hash_length);