9 #ifndef CVMFS_SHORTSTRING_H_
10 #define CVMFS_SHORTSTRING_H_
18 #ifdef CVMFS_NAMESPACE_GUARD
19 namespace CVMFS_NAMESPACE_GUARD {
26 template<
unsigned char StackSize,
char Type>
31 atomic_inc64(&num_instances_);
36 atomic_inc64(&num_instances_);
40 ShortString(
const char *chars,
const unsigned length) : long_string_(NULL) {
42 atomic_inc64(&num_instances_);
44 Assign(chars, length);
46 explicit ShortString(
const std::string &std_string) : long_string_(NULL) {
48 atomic_inc64(&num_instances_);
50 Assign(std_string.data(), std_string.length());
61 void Assign(
const char *chars,
const unsigned length) {
64 this->length_ = length;
65 if (length > StackSize) {
67 atomic_inc64(&num_overflows_);
69 long_string_ =
new std::string(chars, length);
72 memcpy(stack_, chars, length);
80 void Append(
const char *chars,
const unsigned length) {
82 long_string_->append(chars, length);
86 const unsigned new_length = this->length_ + length;
87 if (new_length > StackSize) {
89 atomic_inc64(&num_overflows_);
91 long_string_ =
new std::string();
92 long_string_->reserve(new_length);
93 long_string_->assign(stack_, length_);
94 long_string_->append(chars, length);
98 memcpy(&stack_[this->length_], chars, length);
99 this->length_ = new_length;
109 assert(new_length <= this->GetLength());
111 long_string_->erase(new_length);
114 this->length_ = new_length;
125 return long_string_->data();
133 return long_string_->length();
137 bool IsEmpty()
const {
return GetLength() == 0; }
140 return std::string(this->GetChars(), this->GetLength());
145 return long_string_->c_str();
147 char *c =
const_cast<char *
>(stack_) + length_;
153 const unsigned this_length = this->GetLength();
154 const unsigned other_length = other.
GetLength();
155 if (this_length != other_length)
157 if (this_length == 0)
160 return memcmp(this->GetChars(), other.
GetChars(), this_length) == 0;
166 const unsigned this_length = this->GetLength();
167 const unsigned other_length = other.
GetLength();
169 if (this_length < other_length)
171 if (this_length > other_length)
174 const char *this_chars = this->GetChars();
175 const char *other_chars = other.
GetChars();
176 for (
unsigned i = 0; i < this_length; ++i) {
177 if (this_chars[i] < other_chars[i])
179 if (this_chars[i] > other_chars[i])
186 const unsigned this_length = this->GetLength();
187 const unsigned other_length = other.
GetLength();
188 if (this_length < other_length)
191 return memcmp(this->GetChars(), other.
GetChars(), other_length) == 0;
195 const unsigned length = this->GetLength();
196 if (start_at >= length)
199 return ShortString(this->GetChars() + start_at, length - start_at);
207 char stack_[StackSize + 1];
217 template<
unsigned char StackSize,
char Type>
219 template<
unsigned char StackSize,
char Type>
227 #ifdef CVMFS_NAMESPACE_GUARD
231 #endif // CVMFS_SHORTSTRING_H_
ShortString< kDefaultMaxName, 1 > NameString
NameString GetFileName(const PathString &path)
ShortString Suffix(const unsigned start_at) const
bool operator!=(const ShortString &other) const
void Assign(const char *chars, const unsigned length)
void Truncate(unsigned new_length)
const unsigned char kDefaultMaxName
assert((mem||(size==0))&&"Out Of Memory")
bool operator<(const ShortString &other) const
std::string * long_string_
static uint64_t num_instances()
const unsigned char kDefaultMaxPath
ShortString(const std::string &std_string)
void Append(const char *chars, const unsigned length)
static atomic_int64 num_overflows_
void Assign(const ShortString &other)
const unsigned char kDefaultMaxLink
std::string ToString() const
ShortString< kDefaultMaxLink, 2 > LinkString
ShortString< kDefaultMaxPath, 0 > PathString
PathString GetParentPath(const PathString &path)
ShortString(const char *chars, const unsigned length)
bool StartsWith(const ShortString &other) const
unsigned GetLength() const
ShortString & operator=(const ShortString &other)
ShortString(const ShortString &other)
static uint64_t num_overflows()
const char * c_str() const
const char * GetChars() const
bool operator==(const ShortString &other) const
static atomic_int64 num_instances_