StringHash.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Exception.h"
  25. #include "Hash.h"
  26. #include "Log.h"
  27. #include "StringUtils.h"
  28. #include <cstring>
  29. #include <map>
  30. #include <string>
  31. #include "DebugNew.h"
  32. #include <ctype.h>
  33. std::map<StringHash, std::string> hashes;
  34. std::map<ShortStringHash, std::string> shortHashes;
  35. StringHash::StringHash(const char* str) :
  36. mData(calculate(str))
  37. {
  38. }
  39. StringHash::StringHash(const std::string& str) :
  40. mData(calculate(str.c_str()))
  41. {
  42. }
  43. unsigned StringHash::calculate(const char* str)
  44. {
  45. unsigned hash = 0;
  46. if (!str)
  47. return hash;
  48. while (*str)
  49. {
  50. // Perform the actual hashing as case-insensitive
  51. char c = *str;
  52. updateHash(hash, tolower(c));
  53. ++str;
  54. }
  55. return hash;
  56. }
  57. ShortStringHash::ShortStringHash(const char* str) :
  58. mData(calculate(str))
  59. {
  60. }
  61. ShortStringHash::ShortStringHash(const std::string& str) :
  62. mData(calculate(str.c_str()))
  63. {
  64. }
  65. unsigned short ShortStringHash::calculate(const char* str)
  66. {
  67. return StringHash::calculate(str);
  68. }
  69. void registerHash(const std::string& str, bool throwIfCollision)
  70. {
  71. StringHash hash(str);
  72. // Check for collision
  73. std::map<StringHash, std::string>::const_iterator i = hashes.find(hash);
  74. if ((i != hashes.end()) && (toLower(i->second) != toLower(str)))
  75. {
  76. std::string errorMsg = "Hash collision - " + str + " vs " + i->second;
  77. if (throwIfCollision)
  78. SAFE_EXCEPTION(errorMsg)
  79. else
  80. LOGERROR(errorMsg);
  81. }
  82. hashes[hash] = str;
  83. }
  84. const std::string& hashToString(StringHash hash)
  85. {
  86. return hashes[hash];
  87. }
  88. void registerShortHash(const std::string& str, bool throwIfCollision)
  89. {
  90. ShortStringHash hash(str);
  91. // Check for collision
  92. std::map<ShortStringHash, std::string>::const_iterator i = shortHashes.find(hash);
  93. if ((i != shortHashes.end()) && (toLower(i->second) != toLower(str)))
  94. {
  95. std::string errorMsg = "Short hash collision - " + str + " vs " + i->second;
  96. if (throwIfCollision)
  97. SAFE_EXCEPTION(errorMsg)
  98. else
  99. LOGERROR(errorMsg);
  100. }
  101. shortHashes[hash] = str;
  102. }
  103. const std::string& shortHashToString(ShortStringHash hash)
  104. {
  105. return shortHashes[hash];
  106. }