|
@@ -2763,12 +2763,13 @@ double String::to_float() const {
|
|
}
|
|
}
|
|
|
|
|
|
uint32_t String::hash(const char *p_cstr) {
|
|
uint32_t String::hash(const char *p_cstr) {
|
|
|
|
+ // static_cast: avoid negative values on platforms where char is signed.
|
|
uint32_t hashv = 5381;
|
|
uint32_t hashv = 5381;
|
|
- uint32_t c = *p_cstr++;
|
|
|
|
|
|
+ uint32_t c = static_cast<uint8_t>(*p_cstr++);
|
|
|
|
|
|
while (c) {
|
|
while (c) {
|
|
hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
|
|
hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
|
|
- c = *p_cstr++;
|
|
|
|
|
|
+ c = static_cast<uint8_t>(*p_cstr++);
|
|
}
|
|
}
|
|
|
|
|
|
return hashv;
|
|
return hashv;
|
|
@@ -2777,28 +2778,35 @@ uint32_t String::hash(const char *p_cstr) {
|
|
uint32_t String::hash(const char *p_cstr, int p_len) {
|
|
uint32_t String::hash(const char *p_cstr, int p_len) {
|
|
uint32_t hashv = 5381;
|
|
uint32_t hashv = 5381;
|
|
for (int i = 0; i < p_len; i++) {
|
|
for (int i = 0; i < p_len; i++) {
|
|
- hashv = ((hashv << 5) + hashv) + p_cstr[i]; /* hash * 33 + c */
|
|
|
|
|
|
+ // static_cast: avoid negative values on platforms where char is signed.
|
|
|
|
+ hashv = ((hashv << 5) + hashv) + static_cast<uint8_t>(p_cstr[i]); /* hash * 33 + c */
|
|
}
|
|
}
|
|
|
|
|
|
return hashv;
|
|
return hashv;
|
|
}
|
|
}
|
|
|
|
|
|
uint32_t String::hash(const wchar_t *p_cstr, int p_len) {
|
|
uint32_t String::hash(const wchar_t *p_cstr, int p_len) {
|
|
|
|
+ // Avoid negative values on platforms where wchar_t is signed. Account for different sizes.
|
|
|
|
+ using wide_unsigned = std::conditional<sizeof(wchar_t) == 2, uint16_t, uint32_t>::type;
|
|
|
|
+
|
|
uint32_t hashv = 5381;
|
|
uint32_t hashv = 5381;
|
|
for (int i = 0; i < p_len; i++) {
|
|
for (int i = 0; i < p_len; i++) {
|
|
- hashv = ((hashv << 5) + hashv) + p_cstr[i]; /* hash * 33 + c */
|
|
|
|
|
|
+ hashv = ((hashv << 5) + hashv) + static_cast<wide_unsigned>(p_cstr[i]); /* hash * 33 + c */
|
|
}
|
|
}
|
|
|
|
|
|
return hashv;
|
|
return hashv;
|
|
}
|
|
}
|
|
|
|
|
|
uint32_t String::hash(const wchar_t *p_cstr) {
|
|
uint32_t String::hash(const wchar_t *p_cstr) {
|
|
|
|
+ // Avoid negative values on platforms where wchar_t is signed. Account for different sizes.
|
|
|
|
+ using wide_unsigned = std::conditional<sizeof(wchar_t) == 2, uint16_t, uint32_t>::type;
|
|
|
|
+
|
|
uint32_t hashv = 5381;
|
|
uint32_t hashv = 5381;
|
|
- uint32_t c = *p_cstr++;
|
|
|
|
|
|
+ uint32_t c = static_cast<wide_unsigned>(*p_cstr++);
|
|
|
|
|
|
while (c) {
|
|
while (c) {
|
|
hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
|
|
hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
|
|
- c = *p_cstr++;
|
|
|
|
|
|
+ c = static_cast<wide_unsigned>(*p_cstr++);
|
|
}
|
|
}
|
|
|
|
|
|
return hashv;
|
|
return hashv;
|