| 1 | #include "Library/Base/HashCodeUtil.h" |
|---|---|
| 2 | |
| 3 | #include <cctype> |
| 4 | #include <cstdio> |
| 5 | #include <cstring> |
| 6 | |
| 7 | namespace al { |
| 8 | s32 calcHashCode(const char* str) { |
| 9 | if (str[0] == '\0') |
| 10 | return 0; |
| 11 | |
| 12 | s32 hashCode = 0; |
| 13 | for (s32 i = 0; str[i] != '\0'; i++) |
| 14 | hashCode = (hashCode * 0x1f) + str[i]; |
| 15 | |
| 16 | return hashCode; |
| 17 | } |
| 18 | |
| 19 | s32 calcHashCodeLower(const char* str) { |
| 20 | s32 hashCode = 0; |
| 21 | for (s32 i = 0; str[i] != '\0'; i++) |
| 22 | hashCode = (hashCode * 0x1f) + tolower(str[i]); |
| 23 | |
| 24 | return hashCode; |
| 25 | } |
| 26 | |
| 27 | s32 calcHashCodeFmt(const char* format, std::va_list argv) { |
| 28 | char buf[0x100]; |
| 29 | vsnprintf(buf, 0x100, format, argv); |
| 30 | |
| 31 | return calcHashCode(str: buf); |
| 32 | } |
| 33 | |
| 34 | s32 calcHashCodeFmt(const char* format, ...) { |
| 35 | std::va_list argv; |
| 36 | |
| 37 | va_start(argv, format); |
| 38 | s32 result = calcHashCodeFmt(format, argv); |
| 39 | va_end(argv); |
| 40 | |
| 41 | return result; |
| 42 | } |
| 43 | |
| 44 | const char* getBaseName(const char* name) { |
| 45 | const char* baseName = strrchr(s: name, c: '/'); |
| 46 | |
| 47 | return baseName != nullptr ? baseName + 1 : name; |
| 48 | } |
| 49 | } // namespace al |
| 50 |