GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/shortstring.h
Date: 2026-11-08 02:41:18
Exec Total Coverage
Lines: 91 103 88.3%
Branches: 40 50 80.0%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 *
4 * Implements a string class that stores short strings on the stack and
5 * malloc's a std::string on the heap on overflow. Used for file names and
6 * path names that are usually small.
7 */
8
9 #ifndef CVMFS_SHORTSTRING_H_
10 #define CVMFS_SHORTSTRING_H_
11
12 #include <algorithm>
13 #include <atomic>
14 #include <cstring>
15 #include <string>
16
17 #ifdef CVMFS_NAMESPACE_GUARD
18 namespace CVMFS_NAMESPACE_GUARD {
19 #endif
20
21 const unsigned char kDefaultMaxName = 25;
22 const unsigned char kDefaultMaxLink = 25;
23 const unsigned char kDefaultMaxPath = 200;
24
25 template<unsigned char StackSize, char Type>
26 class ShortString {
27 public:
28 97779372 ShortString() : long_string_(NULL), length_(0) {
29 #ifdef DEBUGMSG
30 num_instances_.fetch_add(1);
31 #endif
32 97779372 }
33 28866022 ShortString(const ShortString &other) : long_string_(NULL) {
34 #ifdef DEBUGMSG
35 num_instances_.fetch_add(1);
36 #endif
37 57732044 Assign(other);
38 57751154 }
39 90955 ShortString(const char *chars, const unsigned length) : long_string_(NULL) {
40 #ifdef DEBUGMSG
41 num_instances_.fetch_add(1);
42 #endif
43 90955 Assign(chars, length);
44 91531 }
45 33332652 explicit ShortString(const std::string &std_string) : long_string_(NULL) {
46 #ifdef DEBUGMSG
47 num_instances_.fetch_add(1);
48 #endif
49 33332652 Assign(std_string.data(), std_string.length());
50 33464812 }
51
52 22560514 ShortString &operator=(const ShortString &other) {
53
1/2
✓ Branch 0 taken 11286033 times.
✗ Branch 1 not taken.
22560514 if (this != &other)
54 22572066 Assign(other);
55 22392962 return *this;
56 }
57
58
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 89154786 times.
178309640 ~ShortString() { delete long_string_; }
59
60 113622320 void Assign(const char *chars, const unsigned length) {
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56811160 times.
113622320 delete long_string_;
62 113405100 long_string_ = NULL;
63 113405100 this->length_ = length;
64
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 56702516 times.
113405100 if (length > StackSize) {
65 #ifdef DEBUGMSG
66 num_overflows_.fetch_add(1);
67 #endif
68
2/4
✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 34 times.
✗ Branch 6 not taken.
68 long_string_ = new std::string(chars, length);
69 } else {
70
2/2
✓ Branch 0 taken 56571617 times.
✓ Branch 1 taken 130899 times.
113405032 if (length)
71 113143234 memcpy(stack_, chars, length);
72 }
73 113405100 }
74
75 80355224 void Assign(const ShortString &other) {
76 80355224 Assign(other.GetChars(), other.GetLength());
77 80159112 }
78
79 80648 void Append(const char *chars, const unsigned length) {
80
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80648 times.
80648 if (long_string_) {
81 ✗ long_string_->append(chars, length);
82 ✗ return;
83 }
84
85 80648 const unsigned new_length = this->length_ + length;
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80648 times.
80648 if (new_length > StackSize) {
87 #ifdef DEBUGMSG
88 num_overflows_.fetch_add(1);
89 #endif
90 ✗ long_string_ = new std::string();
91 ✗ long_string_->reserve(new_length);
92 ✗ long_string_->assign(stack_, length_);
93 ✗ long_string_->append(chars, length);
94 ✗ return;
95 }
96
2/2
✓ Branch 0 taken 80080 times.
✓ Branch 1 taken 568 times.
80648 if (length > 0)
97 80080 memcpy(&stack_[this->length_], chars, length);
98 80648 this->length_ = new_length;
99 }
100
101 /**
102 * Truncates the current string to be of size smaller or equal to current size
103 *
104 * Note: Can lead to a heap allocated string that is shorter than
105 * the reserved stack space.
106 */
107 624 void Truncate(unsigned new_length) {
108
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 624 times.
624 assert(new_length <= this->GetLength());
109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 624 times.
624 if (long_string_) {
110 ✗ long_string_->erase(new_length);
111 ✗ return;
112 }
113 624 this->length_ = new_length;
114 }
115
116 void Clear() {
117 delete long_string_;
118 long_string_ = NULL;
119 length_ = 0;
120 }
121
122 114710396 const char *GetChars() const {
123
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 57355147 times.
114710396 if (long_string_) {
124 102 return long_string_->data();
125 } else {
126 114710294 return stack_;
127 }
128 }
129
130 115122402 unsigned GetLength() const {
131
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 57561150 times.
115122402 if (long_string_)
132 102 return long_string_->length();
133 115122300 return length_;
134 }
135
136 21982 bool IsEmpty() const { return GetLength() == 0; }
137
138 17037927 std::string ToString() const {
139
1/2
✓ Branch 4 taken 17034700 times.
✗ Branch 5 not taken.
17037927 return std::string(this->GetChars(), this->GetLength());
140 }
141
142 67462 const char *c_str() const {
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45135 times.
67462 if (long_string_)
144 ✗ return long_string_->c_str();
145
146 67462 char *c = const_cast<char *>(stack_) + length_;
147 67462 *c = '\0';
148 67462 return stack_;
149 }
150
151 19284 bool operator==(const ShortString &other) const {
152 19284 const unsigned this_length = this->GetLength();
153 19284 const unsigned other_length = other.GetLength();
154
2/2
✓ Branch 0 taken 11889 times.
✓ Branch 1 taken 6319 times.
19284 if (this_length != other_length)
155 12087 return false;
156
2/2
✓ Branch 0 taken 3186 times.
✓ Branch 1 taken 3133 times.
7197 if (this_length == 0)
157 3664 return true;
158
159 3533 return memcmp(this->GetChars(), other.GetChars(), this_length) == 0;
160 }
161
162 11540 bool operator!=(const ShortString &other) const { return !(*this == other); }
163
164 21636 bool operator<(const ShortString &other) const {
165 21636 const unsigned this_length = this->GetLength();
166 21636 const unsigned other_length = other.GetLength();
167
168
2/2
✓ Branch 0 taken 6118 times.
✓ Branch 1 taken 15518 times.
21636 if (this_length < other_length)
169 6118 return true;
170
2/2
✓ Branch 0 taken 3546 times.
✓ Branch 1 taken 11972 times.
15518 if (this_length > other_length)
171 3546 return false;
172
173 11972 const char *this_chars = this->GetChars();
174 11972 const char *other_chars = other.GetChars();
175
2/2
✓ Branch 0 taken 118148 times.
✓ Branch 1 taken 10598 times.
128746 for (unsigned i = 0; i < this_length; ++i) {
176
2/2
✓ Branch 0 taken 1080 times.
✓ Branch 1 taken 117068 times.
118148 if (this_chars[i] < other_chars[i])
177 1080 return true;
178
2/2
✓ Branch 0 taken 294 times.
✓ Branch 1 taken 116774 times.
117068 if (this_chars[i] > other_chars[i])
179 294 return false;
180 }
181 10598 return false;
182 }
183
184 18763 bool StartsWith(const ShortString &other) const {
185 18763 const unsigned this_length = this->GetLength();
186 18763 const unsigned other_length = other.GetLength();
187
2/2
✓ Branch 0 taken 1206 times.
✓ Branch 1 taken 17557 times.
18763 if (this_length < other_length)
188 1206 return false;
189
190 17557 return memcmp(this->GetChars(), other.GetChars(), other_length) == 0;
191 }
192
193 7367 ShortString Suffix(const unsigned start_at) const {
194 7367 const unsigned length = this->GetLength();
195
2/2
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 7243 times.
7367 if (start_at >= length)
196 124 return ShortString("", 0);
197
198 7243 return ShortString(this->GetChars() + start_at, length - start_at);
199 }
200
201 ✗ static uint64_t num_instances() { return num_instances_.load(); }
202 ✗ static uint64_t num_overflows() { return num_overflows_.load(); }
203
204 136 operator bool() const { return not IsEmpty(); }
205
206 private:
207 std::string *long_string_;
208 char stack_[StackSize + 1]; // +1 to add a final '\0' if necessary
209 unsigned char length_;
210 static std::atomic<int64_t> num_overflows_;
211 static std::atomic<int64_t> num_instances_;
212 }; // class ShortString
213
214 typedef ShortString<kDefaultMaxPath, 0> PathString;
215 typedef ShortString<kDefaultMaxName, 1> NameString;
216 typedef ShortString<kDefaultMaxLink, 2> LinkString;
217
218 template<unsigned char StackSize, char Type>
219 std::atomic<int64_t> ShortString<StackSize, Type>::num_overflows_(0);
220 template<unsigned char StackSize, char Type>
221 std::atomic<int64_t> ShortString<StackSize, Type>::num_instances_(0);
222
223 // See posix.cc for the std::string counterparts
224 PathString GetParentPath(const PathString &path);
225 NameString GetFileName(const PathString &path);
226
227 bool IsSubPath(const PathString &parent, const PathString &path);
228
229
230 #ifdef CVMFS_NAMESPACE_GUARD
231 } // namespace CVMFS_NAMESPACE_GUARD
232 #endif
233
234 #endif // CVMFS_SHORTSTRING_H_
235
236