nsID.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "nsID.h"
  6. #include "nsMemory.h"
  7. #include "mozilla/Sprintf.h"
  8. void nsID::Clear()
  9. {
  10. m0 = 0;
  11. m1 = 0;
  12. m2 = 0;
  13. for (int i = 0; i < 8; ++i) {
  14. m3[i] = 0;
  15. }
  16. }
  17. /**
  18. * Multiplies the_int_var with 16 (0x10) and adds the value of the
  19. * hexadecimal digit the_char. If it fails it returns false from
  20. * the function it's used in.
  21. */
  22. #define ADD_HEX_CHAR_TO_INT_OR_RETURN_FALSE(the_char, the_int_var) \
  23. the_int_var = (the_int_var << 4) + the_char; \
  24. if(the_char >= '0' && the_char <= '9') the_int_var -= '0'; \
  25. else if(the_char >= 'a' && the_char <= 'f') the_int_var -= 'a'-10; \
  26. else if(the_char >= 'A' && the_char <= 'F') the_int_var -= 'A'-10; \
  27. else return false
  28. /**
  29. * Parses number_of_chars characters from the char_pointer pointer and
  30. * puts the number in the dest_variable. The pointer is moved to point
  31. * at the first character after the parsed ones. If it fails it returns
  32. * false from the function the macro is used in.
  33. */
  34. #define PARSE_CHARS_TO_NUM(char_pointer, dest_variable, number_of_chars) \
  35. do { int32_t _i=number_of_chars; \
  36. dest_variable = 0; \
  37. while(_i) { \
  38. ADD_HEX_CHAR_TO_INT_OR_RETURN_FALSE(*char_pointer, dest_variable); \
  39. char_pointer++; \
  40. _i--; \
  41. } } while(0)
  42. /**
  43. * Parses a hyphen from the char_pointer string. If there is no hyphen there
  44. * the function returns false from the function it's used in. The
  45. * char_pointer is advanced one step.
  46. */
  47. #define PARSE_HYPHEN(char_pointer) if (*(char_pointer++) != '-') return false
  48. /*
  49. * Turns a {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} string into
  50. * an nsID. It can also handle the old format without the { and }.
  51. */
  52. bool
  53. nsID::Parse(const char* aIDStr)
  54. {
  55. /* Optimized for speed */
  56. if (!aIDStr) {
  57. return false;
  58. }
  59. bool expectFormat1 = (aIDStr[0] == '{');
  60. if (expectFormat1) {
  61. ++aIDStr;
  62. }
  63. PARSE_CHARS_TO_NUM(aIDStr, m0, 8);
  64. PARSE_HYPHEN(aIDStr);
  65. PARSE_CHARS_TO_NUM(aIDStr, m1, 4);
  66. PARSE_HYPHEN(aIDStr);
  67. PARSE_CHARS_TO_NUM(aIDStr, m2, 4);
  68. PARSE_HYPHEN(aIDStr);
  69. int i;
  70. for (i = 0; i < 2; ++i) {
  71. PARSE_CHARS_TO_NUM(aIDStr, m3[i], 2);
  72. }
  73. PARSE_HYPHEN(aIDStr);
  74. while (i < 8) {
  75. PARSE_CHARS_TO_NUM(aIDStr, m3[i], 2);
  76. i++;
  77. }
  78. return expectFormat1 ? *aIDStr == '}' : true;
  79. }
  80. #ifndef XPCOM_GLUE_AVOID_NSPR
  81. static const char gIDFormat[] =
  82. "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}";
  83. /*
  84. * Returns an allocated string in {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
  85. * format. The string is allocated with NS_Alloc and should be freed by
  86. * the caller.
  87. */
  88. char*
  89. nsID::ToString() const
  90. {
  91. char* res = (char*)NS_Alloc(NSID_LENGTH);
  92. if (res) {
  93. snprintf(res, NSID_LENGTH, gIDFormat,
  94. m0, (uint32_t)m1, (uint32_t)m2,
  95. (uint32_t)m3[0], (uint32_t)m3[1], (uint32_t)m3[2],
  96. (uint32_t)m3[3], (uint32_t)m3[4], (uint32_t)m3[5],
  97. (uint32_t)m3[6], (uint32_t)m3[7]);
  98. }
  99. return res;
  100. }
  101. void
  102. nsID::ToProvidedString(char (&aDest)[NSID_LENGTH]) const
  103. {
  104. SprintfLiteral(aDest, gIDFormat,
  105. m0, (uint32_t)m1, (uint32_t)m2,
  106. (uint32_t)m3[0], (uint32_t)m3[1], (uint32_t)m3[2],
  107. (uint32_t)m3[3], (uint32_t)m3[4], (uint32_t)m3[5],
  108. (uint32_t)m3[6], (uint32_t)m3[7]);
  109. }
  110. #endif // XPCOM_GLUE_AVOID_NSPR