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();
138 return GetLength() == 0;
142 return std::string(this->GetChars(), this->GetLength());
147 return long_string_->c_str();
149 char *c =
const_cast<char *
>(stack_) + length_;
155 const unsigned this_length = this->GetLength();
156 const unsigned other_length = other.
GetLength();
157 if (this_length != other_length)
159 if (this_length == 0)
162 return memcmp(this->GetChars(), other.
GetChars(), this_length) == 0;
166 return !(*
this == other);
170 const unsigned this_length = this->GetLength();
171 const unsigned other_length = other.
GetLength();
173 if (this_length < other_length)
175 if (this_length > other_length)
178 const char *this_chars = this->GetChars();
179 const char *other_chars = other.
GetChars();
180 for (
unsigned i = 0; i < this_length; ++i) {
181 if (this_chars[i] < other_chars[i])
183 if (this_chars[i] > other_chars[i])
190 const unsigned this_length = this->GetLength();
191 const unsigned other_length = other.
GetLength();
192 if (this_length < other_length)
195 return memcmp(this->GetChars(), other.
GetChars(), other_length) == 0;
199 const unsigned length = this->GetLength();
200 if (start_at >= length)
203 return ShortString(this->GetChars() + start_at, length-start_at);
211 char stack_[StackSize+1];
221 template<
unsigned char StackSize,
char Type>
223 template<
unsigned char StackSize,
char Type>
231 #ifdef CVMFS_NAMESPACE_GUARD
235 #endif // CVMFS_SHORTSTRING_H_
ShortString< kDefaultMaxName, 1 > NameString
NameString GetFileName(const PathString &path)
ShortString Suffix(const unsigned start_at) 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")
std::string * long_string_
static uint64_t num_instances()
const unsigned char kDefaultMaxPath
ShortString(const std::string &std_string)
bool operator<(SharedPtr< T > const &a, SharedPtr< U > const &b)
bool operator!=(const cvmcache_hash &a, const cvmcache_hash &b)
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
bool operator==(const cvmcache_hash &a, const cvmcache_hash &b)
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(const ShortString &other)
static uint64_t num_overflows()
const char * c_str() const
const char * GetChars() const
static atomic_int64 num_instances_