diff --git a/firmware/util/efilib.cpp b/firmware/util/efilib.cpp index 9a49334f08..4563bbe934 100644 --- a/firmware/util/efilib.cpp +++ b/firmware/util/efilib.cpp @@ -236,8 +236,6 @@ float atoff(const char *param) { return integerPart + decimal / divider; } -#define TO_LOWER(x) (((x)>='A' && (x)<='Z') ? (x) - 'A' + 'a' : (x)) - bool strEqualCaseInsensitive(const char *str1, const char *str2) { int len1 = strlen(str1); int len2 = strlen(str2); @@ -257,6 +255,19 @@ int mytolower(const char c) { return TO_LOWER(c); } + +int djb2lowerCase(const char *str) { + unsigned long hash = 5381; + int c; + + while (c = *str++) { + c = TO_LOWER(c); + hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ + } + + return hash; +} + bool strEqual(const char *str1, const char *str2) { // todo: there must be a standard function?! int len1 = strlen(str1); diff --git a/firmware/util/efilib.h b/firmware/util/efilib.h index a6b96a6805..e540fb76fb 100644 --- a/firmware/util/efilib.h +++ b/firmware/util/efilib.h @@ -12,6 +12,9 @@ #include #endif +#define TO_LOWER(x) (((x)>='A' && (x)<='Z') ? (x) - 'A' + 'a' : (x)) +int djb2lowerCase(const char *str); + #define _MAX_FILLER 11 // http://en.wikipedia.org/wiki/Endianness diff --git a/java_tools/configuration_definition/src/main/java/com/rusefi/output/HashUtil.java b/java_tools/configuration_definition/src/main/java/com/rusefi/output/HashUtil.java new file mode 100644 index 0000000000..aca5f0adb7 --- /dev/null +++ b/java_tools/configuration_definition/src/main/java/com/rusefi/output/HashUtil.java @@ -0,0 +1,17 @@ +package com.rusefi.output; + +public class HashUtil { + public static int hash(String string) { + return djb2lowerCase(string); + } + + public static int djb2lowerCase(String string) { + int hash = 5381; + + for (int i = 0; i < string.length(); i++) { + char c = Character.toLowerCase(string.charAt(i)); + hash = ((hash << 5) + hash) + (byte) c/* hash * 33 + c */; + } + return hash; + } +} diff --git a/java_tools/configuration_definition/src/test/java/com/rusefi/test/HashTest.java b/java_tools/configuration_definition/src/test/java/com/rusefi/test/HashTest.java new file mode 100644 index 0000000000..0b16ec5ee2 --- /dev/null +++ b/java_tools/configuration_definition/src/test/java/com/rusefi/test/HashTest.java @@ -0,0 +1,16 @@ +package com.rusefi.test; + +import com.rusefi.output.HashUtil; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class HashTest { + @Test + public void testdjb2() { + assertEquals(HashUtil.djb2lowerCase("Hello1"), 30950378); + assertEquals(HashUtil.djb2lowerCase("Hello2"), 30950379); + assertEquals(HashUtil.djb2lowerCase("HELLO2"), 30950379); + } + +} diff --git a/unit_tests/tests/util/test_hash.cpp b/unit_tests/tests/util/test_hash.cpp new file mode 100644 index 0000000000..5983c0f99f --- /dev/null +++ b/unit_tests/tests/util/test_hash.cpp @@ -0,0 +1,8 @@ +#include "pch.h" +//#include "efilib.h" + +TEST(util, hash) { + ASSERT_EQ(djb2lowerCase("Hello1"), 30950378); + ASSERT_EQ(djb2lowerCase("Hello2"), 30950379); + ASSERT_EQ(djb2lowerCase("HELLO2"), 30950379); +} diff --git a/unit_tests/tests/util/test_util.mk b/unit_tests/tests/util/test_util.mk index 2bfd5f43ad..e54a9337e3 100644 --- a/unit_tests/tests/util/test_util.mk +++ b/unit_tests/tests/util/test_util.mk @@ -3,6 +3,7 @@ CPPSRC += $(PROJECT_DIR)/../unit_tests/tests/util/test_buffered_writer.cpp \ $(PROJECT_DIR)/../unit_tests/tests/util/test_error_accumulator.cpp \ $(PROJECT_DIR)/../unit_tests/tests/util/test_exp_average.cpp \ - + $(PROJECT_DIR)/../unit_tests/tests/util/test_hash.cpp \ + INCDIR += $(PROJECT_DIR)/controllers/system \ No newline at end of file