StringHash.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // Copyright (c) 2008-2020 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/StringHashRegister.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. const StringMap* hashReverseMap = nullptr;
  35. // Hide static global variables in functions to ensure initialization order.
  36. static StringHashRegister& GetGlobalStringHashRegister()
  37. {
  38. static StringHashRegister stringHashRegister(true /*thread safe*/ );
  39. hashReverseMap = &stringHashRegister.GetInternalMap();
  40. return stringHashRegister;
  41. }
  42. #endif
  43. const StringHash StringHash::ZERO;
  44. StringHash::StringHash(const char* str) noexcept :
  45. value_(Calculate(str))
  46. {
  47. #ifdef URHO3D_HASH_DEBUG
  48. Urho3D::GetGlobalStringHashRegister().RegisterString(*this, str);
  49. #endif
  50. }
  51. StringHash::StringHash(const String& str) noexcept :
  52. value_(Calculate(str.CString()))
  53. {
  54. #ifdef URHO3D_HASH_DEBUG
  55. Urho3D::GetGlobalStringHashRegister().RegisterString(*this, str.CString());
  56. #endif
  57. }
  58. unsigned StringHash::Calculate(const char* str, unsigned hash)
  59. {
  60. if (!str)
  61. return hash;
  62. while (*str)
  63. {
  64. hash = SDBMHash(hash, (unsigned char)*str++);
  65. }
  66. return hash;
  67. }
  68. StringHashRegister* StringHash::GetGlobalStringHashRegister()
  69. {
  70. #ifdef URHO3D_HASH_DEBUG
  71. return &Urho3D::GetGlobalStringHashRegister();
  72. #else
  73. return nullptr;
  74. #endif
  75. }
  76. String StringHash::ToString() const
  77. {
  78. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  79. sprintf(tempBuffer, "%08X", value_);
  80. return String(tempBuffer);
  81. }
  82. String StringHash::Reverse() const
  83. {
  84. #ifdef URHO3D_HASH_DEBUG
  85. return Urho3D::GetGlobalStringHashRegister().GetStringCopy(*this);
  86. #else
  87. return String::EMPTY;
  88. #endif
  89. }
  90. }