StringHash.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // Copyright (c) 2008-2018 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Math/MathDefs.h"
  24. #include "../Math/StringHash.h"
  25. #include "../Container/HashMap.h"
  26. #include "../Core/Mutex.h"
  27. #include "../IO/Log.h"
  28. #include <cstdio>
  29. #include "../DebugNew.h"
  30. namespace Urho3D
  31. {
  32. #ifdef URHO3D_HASH_DEBUG
  33. // Expose map to let Visual Studio debugger access it if Urho3D is linked statically.
  34. StringMap* hashReverseMap = nullptr;
  35. // Hide static global variables in functions to ensure initialization order.
  36. // @{
  37. static Mutex& GetHashReverseMapMutex()
  38. {
  39. static Mutex mutex;
  40. return mutex;
  41. }
  42. static StringMap& GetHashReverseMap()
  43. {
  44. static StringMap map;
  45. hashReverseMap = &map;
  46. return map;
  47. }
  48. // @}
  49. static void AddStringHash(const StringHash& hash, const char* string)
  50. {
  51. Mutex& guard = GetHashReverseMapMutex();
  52. guard.Acquire();
  53. StringMap& map = GetHashReverseMap();
  54. auto iter = map.Find(hash);
  55. if (iter == map.End())
  56. {
  57. map.Populate(hash, string);
  58. }
  59. else if (iter->second_.Compare(string, false) != 0)
  60. {
  61. URHO3D_LOGWARNINGF("String hash collision detected! Both \"%s\" and \"%s\" have hash #%s",
  62. string, iter->second_.CString(), hash.ToString().CString());
  63. }
  64. guard.Release();
  65. }
  66. static String ReverseStringHash(const StringHash& hash)
  67. {
  68. Mutex& guard = GetHashReverseMapMutex();
  69. guard.Acquire();
  70. const StringMap& map = GetHashReverseMap();
  71. auto iter = map.Find(hash);
  72. String result = iter == map.End() ? String::EMPTY : iter->second_;
  73. guard.Release();
  74. return result;
  75. }
  76. #else
  77. static void AddStringHash(const StringHash& /*hash*/, const char* /*string*/)
  78. {
  79. // Do nothing
  80. }
  81. static String ReverseStringHash(const StringHash& /*hash*/)
  82. {
  83. // Do nothing
  84. return String::EMPTY;
  85. }
  86. #endif
  87. const StringHash StringHash::ZERO;
  88. StringHash::StringHash(const char* str) noexcept :
  89. value_(Calculate(str))
  90. {
  91. AddStringHash(*this, str);
  92. }
  93. StringHash::StringHash(const String& str) noexcept :
  94. value_(Calculate(str.CString()))
  95. {
  96. AddStringHash(*this, str.CString());
  97. }
  98. unsigned StringHash::Calculate(const char* str, unsigned hash)
  99. {
  100. if (!str)
  101. return hash;
  102. while (*str)
  103. {
  104. // Perform the actual hashing as case-insensitive
  105. char c = *str;
  106. hash = SDBMHash(hash, (unsigned char)tolower(c));
  107. ++str;
  108. }
  109. return hash;
  110. }
  111. const StringMap* StringHash::GetHashReverseMap()
  112. {
  113. #ifdef URHO3D_HASH_DEBUG
  114. return &Urho3D::GetHashReverseMap();
  115. #else
  116. return nullptr;
  117. #endif
  118. }
  119. String StringHash::ToString() const
  120. {
  121. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  122. sprintf(tempBuffer, "%08X", value_);
  123. return String(tempBuffer);
  124. }
  125. String StringHash::Reverse() const
  126. {
  127. return ReverseStringHash(*this);
  128. }
  129. }