BsUUID.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Prerequisites/BsPrerequisitesUtil.h"
  4. #include "Utility/BsUUID.h"
  5. #include "Utility/BsPlatformUtility.h"
  6. #include <chrono>
  7. using namespace std::chrono;
  8. namespace bs
  9. {
  10. std::array<UINT8, 256> literalToHex()
  11. {
  12. std::array<UINT8, 256> output;
  13. output.fill(-1);
  14. output['0'] = 0;
  15. output['1'] = 1;
  16. output['2'] = 2;
  17. output['3'] = 3;
  18. output['4'] = 4;
  19. output['5'] = 5;
  20. output['6'] = 6;
  21. output['7'] = 7;
  22. output['8'] = 8;
  23. output['9'] = 9;
  24. output['a'] = 10;
  25. output['b'] = 11;
  26. output['c'] = 12;
  27. output['d'] = 13;
  28. output['e'] = 14;
  29. output['f'] = 15;
  30. return output;
  31. }
  32. static const std::array<char, 16> HEX_TO_LITERAL = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
  33. static const std::array<UINT8, 256> LITERAL_TO_HEX = literalToHex();
  34. UUID UUID::EMPTY;
  35. UUID::UUID(const String& uuid)
  36. {
  37. memset(mData, 0, sizeof(mData));
  38. if (uuid.size() < 36)
  39. return;
  40. UINT32 idx = 0;
  41. // First group: 8 digits
  42. for(INT32 i = 7; i >= 0; --i)
  43. {
  44. char charVal = (char)tolower(uuid[idx++]);
  45. UINT8 hexVal = LITERAL_TO_HEX[charVal];
  46. mData[0] |= hexVal << (i * 4);
  47. }
  48. idx++;
  49. // Second group: 4 digits
  50. for(INT32 i = 7; i >= 4; --i)
  51. {
  52. char charVal = (char)tolower(uuid[idx++]);
  53. UINT8 hexVal = LITERAL_TO_HEX[charVal];
  54. mData[1] |= hexVal << (i * 4);
  55. }
  56. idx++;
  57. // Third group: 4 digits
  58. for(INT32 i = 3; i >= 0; --i)
  59. {
  60. char charVal = (char)tolower(uuid[idx++]);
  61. UINT8 hexVal = LITERAL_TO_HEX[charVal];
  62. mData[1] |= hexVal << (i * 4);
  63. }
  64. idx++;
  65. // Fourth group: 4 digits
  66. for(INT32 i = 7; i >= 4; --i)
  67. {
  68. char charVal = (char)tolower(uuid[idx++]);
  69. UINT8 hexVal = LITERAL_TO_HEX[charVal];
  70. mData[2] |= hexVal << (i * 4);
  71. }
  72. idx++;
  73. // Fifth group: 12 digits
  74. for(INT32 i = 3; i >= 0; --i)
  75. {
  76. char charVal = (char)tolower(uuid[idx++]);
  77. UINT8 hexVal = LITERAL_TO_HEX[charVal];
  78. mData[2] |= hexVal << (i * 4);
  79. }
  80. for(INT32 i = 7; i >= 0; --i)
  81. {
  82. char charVal = (char)tolower(uuid[idx++]);
  83. UINT8 hexVal = LITERAL_TO_HEX[charVal];
  84. mData[3] |= hexVal << (i * 4);
  85. }
  86. }
  87. String UUID::toString() const
  88. {
  89. UINT8 output[36];
  90. UINT32 idx = 0;
  91. // First group: 8 digits
  92. for(INT32 i = 7; i >= 0; --i)
  93. {
  94. UINT32 hexVal = (mData[0] >> (i * 4)) & 0xF;
  95. output[idx++] = HEX_TO_LITERAL[hexVal];
  96. }
  97. output[idx++] = '-';
  98. // Second group: 4 digits
  99. for(INT32 i = 7; i >= 4; --i)
  100. {
  101. UINT32 hexVal = (mData[1] >> (i * 4)) & 0xF;
  102. output[idx++] = HEX_TO_LITERAL[hexVal];
  103. }
  104. output[idx++] = '-';
  105. // Third group: 4 digits
  106. for(INT32 i = 3; i >= 0; --i)
  107. {
  108. UINT32 hexVal = (mData[1] >> (i * 4)) & 0xF;
  109. output[idx++] = HEX_TO_LITERAL[hexVal];
  110. }
  111. output[idx++] = '-';
  112. // Fourth group: 4 digits
  113. for(INT32 i = 7; i >= 4; --i)
  114. {
  115. UINT32 hexVal = (mData[2] >> (i * 4)) & 0xF;
  116. output[idx++] = HEX_TO_LITERAL[hexVal];
  117. }
  118. output[idx++] = '-';
  119. // Fifth group: 12 digits
  120. for(INT32 i = 3; i >= 0; --i)
  121. {
  122. UINT32 hexVal = (mData[2] >> (i * 4)) & 0xF;
  123. output[idx++] = HEX_TO_LITERAL[hexVal];
  124. }
  125. for(INT32 i = 7; i >= 0; --i)
  126. {
  127. UINT32 hexVal = (mData[3] >> (i * 4)) & 0xF;
  128. output[idx++] = HEX_TO_LITERAL[hexVal];
  129. }
  130. return String((const char*)output, 36);
  131. }
  132. UUID UUIDGenerator::generateRandom()
  133. {
  134. return PlatformUtility::generateUUID();
  135. }
  136. };