GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/shortstring.h
Date: 2026-10-04 02:40:33
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 82149424 ShortString() : long_string_(NULL), length_(0) {
30 #ifdef DEBUGMSG
31 82149424 atomic_inc64(&num_instances_);
32 #endif
33 82149424 }
34 23236655 ShortString(const ShortString &other) : long_string_(NULL) {
35 #ifdef DEBUGMSG
36 46473310 atomic_inc64(&num_instances_);
37 #endif
38 46609990 Assign(other);
39 46488718 }
40 68211 ShortString(const char *chars, const unsigned length) : long_string_(NULL) {
41 #ifdef DEBUGMSG
42 68211 atomic_inc64(&num_instances_);
43 #endif
44 68433 Assign(chars, length);
45 68349 }
46 21933026 explicit ShortString(const std::string &std_string) : long_string_(NULL) {
47 #ifdef DEBUGMSG
48 21933026 atomic_inc64(&num_instances_);
49 #endif
50 22135658 Assign(std_string.data(), std_string.length());
51 21997334 }
52
53 13276534 ShortString &operator=(const ShortString &other) {
54
1/2
✓ Branch 0 taken 6639743 times.
✗ Branch 1 not taken.
13276534 if (this != &other)
55 13279486 Assign(other);
56 13184518 return *this;
57 }
58
59
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 71015487 times.
142030998 ~ShortString() { delete long_string_; }
60
61 81847372 void Assign(const char *chars, const unsigned length) {
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40923686 times.
81847372 delete long_string_;
63 81780112 long_string_ = NULL;
64 81780112 this->length_ = length;
65
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 40890044 times.
81780112 if (length > StackSize) {
66 #ifdef DEBUGMSG
67 24 atomic_inc64(&num_overflows_);
68 #endif
69
2/4
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 12 times.
✗ Branch 6 not taken.
24 long_string_ = new std::string(chars, length);
70 } else {
71
2/2
✓ Branch 0 taken 40709923 times.
✓ Branch 1 taken 180121 times.
81780088 if (length)
72 81419846 memcpy(stack_, chars, length);
73 }
74 81780112 }
75
76 59821426 void Assign(const ShortString &other) {
77 59821426 Assign(other.GetChars(), other.GetLength());
78 59683738 }
79
80 126690 void Append(const char *chars, const unsigned length) {
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126690 times.
126690 if (long_string_) {
82 ✗ long_string_->append(chars, length);
83 ✗ return;
84 }
85
86 126690 const unsigned new_length = this->length_ + length;
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126690 times.
126690 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 126222 times.
✓ Branch 1 taken 468 times.
126690 if (length > 0)
98 126222 memcpy(&stack_[this->length_], chars, length);
99 126690 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 624 void Truncate(unsigned new_length) {
109
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 624 times.
624 assert(new_length <= this->GetLength());
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 624 times.
624 if (long_string_) {
111 ✗ long_string_->erase(new_length);
112 ✗ return;
113 }
114 624 this->length_ = new_length;
115 }
116
117 void Clear() {
118 delete long_string_;
119 long_string_ = NULL;
120 length_ = 0;
121 }
122
123 82435420 const char *GetChars() const {
124
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 41217692 times.
82435420 if (long_string_) {
125 36 return long_string_->data();
126 } else {
127 82435384 return stack_;
128 }
129 }
130
131 82935728 unsigned GetLength() const {
132
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 41467846 times.
82935728 if (long_string_)
133 36 return long_string_->length();
134 82935692 return length_;
135 }
136
137 18315 bool IsEmpty() const { return GetLength() == 0; }
138
139 11125444 std::string ToString() const {
140
1/2
✓ Branch 4 taken 11122688 times.
✗ Branch 5 not taken.
11125444 return std::string(this->GetChars(), this->GetLength());
141 }
142
143 62968 const char *c_str() const {
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44893 times.
62968 if (long_string_)
145 ✗ return long_string_->c_str();
146
147 62968 char *c = const_cast<char *>(stack_) + length_;
148 62968 *c = '\0';
149 62968 return stack_;
150 }
151
152 20845 bool operator==(const ShortString &other) const {
153 20845 const unsigned this_length = this->GetLength();
154 20845 const unsigned other_length = other.GetLength();
155
2/2
✓ Branch 0 taken 14778 times.
✓ Branch 1 taken 5441 times.
20845 if (this_length != other_length)
156 14808 return false;
157
2/2
✓ Branch 0 taken 3123 times.
✓ Branch 1 taken 2318 times.
6037 if (this_length == 0)
158 3581 return true;
159
160 2456 return memcmp(this->GetChars(), other.GetChars(), this_length) == 0;
161 }
162
163 15322 bool operator!=(const ShortString &other) const { return !(*this == other); }
164
165 22862 bool operator<(const ShortString &other) const {
166 22862 const unsigned this_length = this->GetLength();
167 22862 const unsigned other_length = other.GetLength();
168
169
2/2
✓ Branch 0 taken 5636 times.
✓ Branch 1 taken 17226 times.
22862 if (this_length < other_length)
170 5636 return true;
171
2/2
✓ Branch 0 taken 3684 times.
✓ Branch 1 taken 13542 times.
17226 if (this_length > other_length)
172 3684 return false;
173
174 13542 const char *this_chars = this->GetChars();
175 13542 const char *other_chars = other.GetChars();
176
2/2
✓ Branch 0 taken 130746 times.
✓ Branch 1 taken 12068 times.
142814 for (unsigned i = 0; i < this_length; ++i) {
177
2/2
✓ Branch 0 taken 1163 times.
✓ Branch 1 taken 129583 times.
130746 if (this_chars[i] < other_chars[i])
178 1163 return true;
179
2/2
✓ Branch 0 taken 311 times.
✓ Branch 1 taken 129272 times.
129583 if (this_chars[i] > other_chars[i])
180 311 return false;
181 }
182 12068 return false;
183 }
184
185 26005 bool StartsWith(const ShortString &other) const {
186 26005 const unsigned this_length = this->GetLength();
187 26005 const unsigned other_length = other.GetLength();
188
2/2
✓ Branch 0 taken 1009 times.
✓ Branch 1 taken 24996 times.
26005 if (this_length < other_length)
189 1009 return false;
190
191 24996 return memcmp(this->GetChars(), other.GetChars(), other_length) == 0;
192 }
193
194 11717 ShortString Suffix(const unsigned start_at) const {
195 11717 const unsigned length = this->GetLength();
196
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 11693 times.
11717 if (start_at >= length)
197 24 return ShortString("", 0);
198
199 11693 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 48 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