GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/shortstring.h
Date: 2026-08-09 02:40:25
Exec Total Coverage
Lines: 96 109 88.1%
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 <cstring>
14 #include <string>
15
16 #include "util/atomic.h"
17
18 #ifdef CVMFS_NAMESPACE_GUARD
19 namespace CVMFS_NAMESPACE_GUARD {
20 #endif
21
22 const unsigned char kDefaultMaxName = 25;
23 const unsigned char kDefaultMaxLink = 25;
24 const unsigned char kDefaultMaxPath = 200;
25
26 template<unsigned char StackSize, char Type>
27 class ShortString {
28 public:
29 179361890 ShortString() : long_string_(NULL), length_(0) {
30 #ifdef DEBUGMSG
31 179361890 atomic_inc64(&num_instances_);
32 #endif
33 179361890 }
34 31396391 ShortString(const ShortString &other) : long_string_(NULL) {
35 #ifdef DEBUGMSG
36 62792782 atomic_inc64(&num_instances_);
37 #endif
38 62928014 Assign(other);
39 62781262 }
40 200954 ShortString(const char *chars, const unsigned length) : long_string_(NULL) {
41 #ifdef DEBUGMSG
42 200954 atomic_inc64(&num_instances_);
43 #endif
44 201706 Assign(chars, length);
45 201258 }
46 34964590 explicit ShortString(const std::string &std_string) : long_string_(NULL) {
47 #ifdef DEBUGMSG
48 34964590 atomic_inc64(&num_instances_);
49 #endif
50 35425390 Assign(std_string.data(), std_string.length());
51 35202286 }
52
53 23471572 ShortString &operator=(const ShortString &other) {
54
1/2
✓ Branch 0 taken 11741002 times.
✗ Branch 1 not taken.
23471572 if (this != &other)
55 23482004 Assign(other);
56 23212116 return *this;
57 }
58
59
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 132942343 times.
265884714 ~ShortString() { delete long_string_; }
60
61 121526846 void Assign(const char *chars, const unsigned length) {
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60763423 times.
121526846 delete long_string_;
63 121310302 long_string_ = NULL;
64 121310302 this->length_ = length;
65
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 60655137 times.
121310302 if (length > StackSize) {
66 #ifdef DEBUGMSG
67 28 atomic_inc64(&num_overflows_);
68 #endif
69
2/4
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 14 times.
✗ Branch 6 not taken.
28 long_string_ = new std::string(chars, length);
70 } else {
71
2/2
✓ Branch 0 taken 60345422 times.
✓ Branch 1 taken 309715 times.
121310274 if (length)
72 120690844 memcpy(stack_, chars, length);
73 }
74 121310302 }
75
76 86314894 void Assign(const ShortString &other) {
77 86314894 Assign(other.GetChars(), other.GetLength());
78 86012046 }
79
80 191759 void Append(const char *chars, const unsigned length) {
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 191759 times.
191759 if (long_string_) {
82 long_string_->append(chars, length);
83 return;
84 }
85
86 191759 const unsigned new_length = this->length_ + length;
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 191759 times.
191759 if (new_length > StackSize) {
88 #ifdef DEBUGMSG
89 atomic_inc64(&num_overflows_);
90 #endif
91 long_string_ = new std::string();
92 long_string_->reserve(new_length);
93 long_string_->assign(stack_, length_);
94 long_string_->append(chars, length);
95 return;
96 }
97
2/2
✓ Branch 0 taken 191123 times.
✓ Branch 1 taken 636 times.
191759 if (length > 0)
98 191123 memcpy(&stack_[this->length_], chars, length);
99 191759 this->length_ = new_length;
100 }
101
102 /**
103 * Truncates the current string to be of size smaller or equal to current size
104 *
105 * Note: Can lead to a heap allocated string that is shorter than
106 * the reserved stack space.
107 */
108 918 void Truncate(unsigned new_length) {
109
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 918 times.
918 assert(new_length <= this->GetLength());
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 918 times.
918 if (long_string_) {
111 long_string_->erase(new_length);
112 return;
113 }
114 918 this->length_ = new_length;
115 }
116
117 void Clear() {
118 delete long_string_;
119 long_string_ = NULL;
120 length_ = 0;
121 }
122
123 122839372 const char *GetChars() const {
124
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 61419665 times.
122839372 if (long_string_) {
125 42 return long_string_->data();
126 } else {
127 122839330 return stack_;
128 }
129 }
130
131 123790388 unsigned GetLength() const {
132
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 61895173 times.
123790388 if (long_string_)
133 42 return long_string_->length();
134 123790346 return length_;
135 }
136
137 56664 bool IsEmpty() const { return GetLength() == 0; }
138
139 17894313 std::string ToString() const {
140
1/2
✓ Branch 4 taken 17889971 times.
✗ Branch 5 not taken.
17894313 return std::string(this->GetChars(), this->GetLength());
141 }
142
143 161146 const char *c_str() const {
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 97769 times.
161146 if (long_string_)
145 return long_string_->c_str();
146
147 161146 char *c = const_cast<char *>(stack_) + length_;
148 161146 *c = '\0';
149 161146 return stack_;
150 }
151
152 28856 bool operator==(const ShortString &other) const {
153 28856 const unsigned this_length = this->GetLength();
154 28856 const unsigned other_length = other.GetLength();
155
2/2
✓ Branch 0 taken 19927 times.
✓ Branch 1 taken 8044 times.
28856 if (this_length != other_length)
156 19968 return false;
157
2/2
✓ Branch 0 taken 3704 times.
✓ Branch 1 taken 4340 times.
8888 if (this_length == 0)
158 4360 return true;
159
160 4528 return memcmp(this->GetChars(), other.GetChars(), this_length) == 0;
161 }
162
163 20994 bool operator!=(const ShortString &other) const { return !(*this == other); }
164
165 43605 bool operator<(const ShortString &other) const {
166 43605 const unsigned this_length = this->GetLength();
167 43605 const unsigned other_length = other.GetLength();
168
169
2/2
✓ Branch 0 taken 10838 times.
✓ Branch 1 taken 32767 times.
43605 if (this_length < other_length)
170 10838 return true;
171
2/2
✓ Branch 0 taken 9004 times.
✓ Branch 1 taken 23763 times.
32767 if (this_length > other_length)
172 9004 return false;
173
174 23763 const char *this_chars = this->GetChars();
175 23763 const char *other_chars = other.GetChars();
176
2/2
✓ Branch 0 taken 286207 times.
✓ Branch 1 taken 19990 times.
306197 for (unsigned i = 0; i < this_length; ++i) {
177
2/2
✓ Branch 0 taken 2932 times.
✓ Branch 1 taken 283275 times.
286207 if (this_chars[i] < other_chars[i])
178 2932 return true;
179
2/2
✓ Branch 0 taken 841 times.
✓ Branch 1 taken 282434 times.
283275 if (this_chars[i] > other_chars[i])
180 841 return false;
181 }
182 19990 return false;
183 }
184
185 35187 bool StartsWith(const ShortString &other) const {
186 35187 const unsigned this_length = this->GetLength();
187 35187 const unsigned other_length = other.GetLength();
188
2/2
✓ Branch 0 taken 2481 times.
✓ Branch 1 taken 32706 times.
35187 if (this_length < other_length)
189 2481 return false;
190
191 32706 return memcmp(this->GetChars(), other.GetChars(), other_length) == 0;
192 }
193
194 16331 ShortString Suffix(const unsigned start_at) const {
195 16331 const unsigned length = this->GetLength();
196
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 16307 times.
16331 if (start_at >= length)
197 24 return ShortString("", 0);
198
199 16307 return ShortString(this->GetChars() + start_at, length - start_at);
200 }
201
202 static uint64_t num_instances() { return atomic_read64(&num_instances_); }
203 static uint64_t num_overflows() { return atomic_read64(&num_overflows_); }
204
205 56 operator bool() const { return not IsEmpty(); }
206
207 private:
208 std::string *long_string_;
209 char stack_[StackSize + 1]; // +1 to add a final '\0' if necessary
210 unsigned char length_;
211 static atomic_int64 num_overflows_;
212 static atomic_int64 num_instances_;
213 }; // class ShortString
214
215 typedef ShortString<kDefaultMaxPath, 0> PathString;
216 typedef ShortString<kDefaultMaxName, 1> NameString;
217 typedef ShortString<kDefaultMaxLink, 2> LinkString;
218
219 template<unsigned char StackSize, char Type>
220 atomic_int64 ShortString<StackSize, Type>::num_overflows_ = 0;
221 template<unsigned char StackSize, char Type>
222 atomic_int64 ShortString<StackSize, Type>::num_instances_ = 0;
223
224 // See posix.cc for the std::string counterparts
225 PathString GetParentPath(const PathString &path);
226 NameString GetFileName(const PathString &path);
227
228 bool IsSubPath(const PathString &parent, const PathString &path);
229
230
231 #ifdef CVMFS_NAMESPACE_GUARD
232 } // namespace CVMFS_NAMESPACE_GUARD
233 #endif
234
235 #endif // CVMFS_SHORTSTRING_H_
236
237