LogName.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright 2017-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <folly/logging/LogName.h>
  17. namespace {
  18. constexpr bool isSeparator(char c) {
  19. return c == '.' || c == '/' || c == '\\';
  20. }
  21. } // namespace
  22. namespace folly {
  23. std::string LogName::canonicalize(StringPiece input) {
  24. std::string cname;
  25. cname.reserve(input.size());
  26. // Ignore trailing category separator characters
  27. size_t end = input.size();
  28. while (end > 0 && isSeparator(input[end - 1])) {
  29. --end;
  30. }
  31. bool ignoreSeparator = true;
  32. for (size_t idx = 0; idx < end; ++idx) {
  33. if (isSeparator(input[idx])) {
  34. if (ignoreSeparator) {
  35. continue;
  36. }
  37. cname.push_back('.');
  38. ignoreSeparator = true;
  39. } else {
  40. cname.push_back(input[idx]);
  41. ignoreSeparator = false;
  42. }
  43. }
  44. return cname;
  45. }
  46. size_t LogName::hash(StringPiece name) {
  47. // Code based on StringPiece::hash(), but which ignores leading and trailing
  48. // category separator characters, as well as multiple consecutive separator
  49. // characters, so equivalent names result in the same hash.
  50. uint32_t hash = 5381;
  51. size_t end = name.size();
  52. while (end > 0 && isSeparator(name[end - 1])) {
  53. --end;
  54. }
  55. bool ignoreSeparator = true;
  56. for (size_t idx = 0; idx < end; ++idx) {
  57. uint8_t value;
  58. if (isSeparator(name[idx])) {
  59. if (ignoreSeparator) {
  60. continue;
  61. }
  62. value = '.';
  63. ignoreSeparator = true;
  64. } else {
  65. value = static_cast<uint8_t>(name[idx]);
  66. ignoreSeparator = false;
  67. }
  68. hash = ((hash << 5) + hash) + value;
  69. }
  70. return hash;
  71. }
  72. int LogName::cmp(StringPiece a, StringPiece b) {
  73. // Ignore trailing separators
  74. auto stripTrailingSeparators = [](StringPiece& s) {
  75. while (!s.empty() && isSeparator(s.back())) {
  76. s.uncheckedSubtract(1);
  77. }
  78. };
  79. stripTrailingSeparators(a);
  80. stripTrailingSeparators(b);
  81. // Advance ptr until it no longer points to a category separator.
  82. // This is used to skip over consecutive sequences of separator characters.
  83. auto skipOverSeparators = [](StringPiece& s) {
  84. while (!s.empty() && isSeparator(s.front())) {
  85. s.uncheckedAdvance(1);
  86. }
  87. };
  88. bool ignoreSeparator = true;
  89. while (true) {
  90. if (ignoreSeparator) {
  91. skipOverSeparators(a);
  92. skipOverSeparators(b);
  93. }
  94. if (a.empty()) {
  95. return b.empty() ? 0 : -1;
  96. } else if (b.empty()) {
  97. return 1;
  98. }
  99. if (isSeparator(a.front())) {
  100. if (!isSeparator(b.front())) {
  101. return '.' - b.front();
  102. }
  103. ignoreSeparator = true;
  104. } else {
  105. if (a.front() != b.front()) {
  106. return a.front() - b.front();
  107. }
  108. ignoreSeparator = false;
  109. }
  110. a.uncheckedAdvance(1);
  111. b.uncheckedAdvance(1);
  112. }
  113. }
  114. StringPiece LogName::getParent(StringPiece name) {
  115. if (name.empty()) {
  116. return name;
  117. }
  118. ssize_t idx = name.size();
  119. // Skip over any trailing separator characters
  120. while (idx > 0 && isSeparator(name[idx - 1])) {
  121. --idx;
  122. }
  123. // Now walk backwards to the next separator character
  124. while (idx > 0 && !isSeparator(name[idx - 1])) {
  125. --idx;
  126. }
  127. // And again skip over any separator characters, in case there are multiple
  128. // repeated characters.
  129. while (idx > 0 && isSeparator(name[idx - 1])) {
  130. --idx;
  131. }
  132. return StringPiece(name.begin(), idx);
  133. }
  134. } // namespace folly