proxygen
LogName.cpp
Go to the documentation of this file.
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 
18 namespace {
19 constexpr bool isSeparator(char c) {
20  return c == '.' || c == '/' || c == '\\';
21 }
22 } // namespace
23 
24 namespace folly {
25 
27  std::string cname;
28  cname.reserve(input.size());
29 
30  // Ignore trailing category separator characters
31  size_t end = input.size();
32  while (end > 0 && isSeparator(input[end - 1])) {
33  --end;
34  }
35 
36  bool ignoreSeparator = true;
37  for (size_t idx = 0; idx < end; ++idx) {
38  if (isSeparator(input[idx])) {
39  if (ignoreSeparator) {
40  continue;
41  }
42  cname.push_back('.');
43  ignoreSeparator = true;
44  } else {
45  cname.push_back(input[idx]);
46  ignoreSeparator = false;
47  }
48  }
49  return cname;
50 }
51 
53  // Code based on StringPiece::hash(), but which ignores leading and trailing
54  // category separator characters, as well as multiple consecutive separator
55  // characters, so equivalent names result in the same hash.
56  uint32_t hash = 5381;
57 
58  size_t end = name.size();
59  while (end > 0 && isSeparator(name[end - 1])) {
60  --end;
61  }
62 
63  bool ignoreSeparator = true;
64  for (size_t idx = 0; idx < end; ++idx) {
65  uint8_t value;
66  if (isSeparator(name[idx])) {
67  if (ignoreSeparator) {
68  continue;
69  }
70  value = '.';
71  ignoreSeparator = true;
72  } else {
73  value = static_cast<uint8_t>(name[idx]);
74  ignoreSeparator = false;
75  }
76  hash = ((hash << 5) + hash) + value;
77  }
78  return hash;
79 }
80 
82  // Ignore trailing separators
83  auto stripTrailingSeparators = [](StringPiece& s) {
84  while (!s.empty() && isSeparator(s.back())) {
85  s.uncheckedSubtract(1);
86  }
87  };
88  stripTrailingSeparators(a);
89  stripTrailingSeparators(b);
90 
91  // Advance ptr until it no longer points to a category separator.
92  // This is used to skip over consecutive sequences of separator characters.
93  auto skipOverSeparators = [](StringPiece& s) {
94  while (!s.empty() && isSeparator(s.front())) {
95  s.uncheckedAdvance(1);
96  }
97  };
98 
99  bool ignoreSeparator = true;
100  while (true) {
101  if (ignoreSeparator) {
102  skipOverSeparators(a);
103  skipOverSeparators(b);
104  }
105  if (a.empty()) {
106  return b.empty() ? 0 : -1;
107  } else if (b.empty()) {
108  return 1;
109  }
110  if (isSeparator(a.front())) {
111  if (!isSeparator(b.front())) {
112  return '.' - b.front();
113  }
114  ignoreSeparator = true;
115  } else {
116  if (a.front() != b.front()) {
117  return a.front() - b.front();
118  }
119  ignoreSeparator = false;
120  }
121  a.uncheckedAdvance(1);
122  b.uncheckedAdvance(1);
123  }
124 }
125 
127  if (name.empty()) {
128  return name;
129  }
130 
131  ssize_t idx = name.size();
132 
133  // Skip over any trailing separator characters
134  while (idx > 0 && isSeparator(name[idx - 1])) {
135  --idx;
136  }
137 
138  // Now walk backwards to the next separator character
139  while (idx > 0 && !isSeparator(name[idx - 1])) {
140  --idx;
141  }
142 
143  // And again skip over any separator characters, in case there are multiple
144  // repeated characters.
145  while (idx > 0 && isSeparator(name[idx - 1])) {
146  --idx;
147  }
148 
149  return StringPiece(name.begin(), idx);
150 }
151 } // namespace folly
static int cmp(folly::StringPiece nameA, folly::StringPiece nameB)
Definition: LogName.cpp:81
static folly::StringPiece getParent(folly::StringPiece name)
Definition: LogName.cpp:126
char b
constexpr size_type size() const
Definition: Range.h:431
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
static size_t hash(folly::StringPiece name)
Definition: LogName.cpp:52
const char * name
Definition: http_parser.c:437
constexpr bool empty() const
Definition: Range.h:443
static std::string canonicalize(folly::StringPiece name)
Definition: LogName.cpp:26
auto end(TestAdlIterable &instance)
Definition: ForeachTest.cpp:62
char a
constexpr Iter begin() const
Definition: Range.h:452
const char * string
Definition: Conv.cpp:212
void uncheckedAdvance(size_type n)
Definition: Range.h:695
value_type & front()
Definition: Range.h:464
static set< string > s
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
Range< const char * > StringPiece
char c