CernVM-FS  2.10.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
shortstring.h
Go to the documentation of this file.
1 
9 #ifndef CVMFS_SHORTSTRING_H_
10 #define CVMFS_SHORTSTRING_H_
11 
12 #include <algorithm>
13 #include <cstring>
14 #include <string>
15 
16 #include "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  ShortString() : long_string_(NULL), length_(0) {
30  atomic_inc64(&num_instances_);
31  }
32  ShortString(const ShortString &other) : long_string_(NULL) {
33  atomic_inc64(&num_instances_);
34  Assign(other);
35  }
36  ShortString(const char *chars, const unsigned length) : long_string_(NULL) {
37  atomic_inc64(&num_instances_);
38  Assign(chars, length);
39  }
40  explicit ShortString(const std::string &std_string) : long_string_(NULL) {
41  atomic_inc64(&num_instances_);
42  Assign(std_string.data(), std_string.length());
43  }
44 
45  ShortString & operator= (const ShortString & other) {
46  if (this != &other)
47  Assign(other);
48  return *this;
49  }
50 
51  ~ShortString() { delete long_string_; }
52 
53  void Assign(const char *chars, const unsigned length) {
54  delete long_string_;
55  long_string_ = NULL;
56  this->length_ = length;
57  if (length > StackSize) {
58  atomic_inc64(&num_overflows_);
59  long_string_ = new std::string(chars, length);
60  } else {
61  if (length)
62  memcpy(stack_, chars, length);
63  }
64  }
65 
66  void Assign(const ShortString &other) {
67  Assign(other.GetChars(), other.GetLength());
68  }
69 
70  void Append(const char *chars, const unsigned length) {
71  if (long_string_) {
72  long_string_->append(chars, length);
73  return;
74  }
75 
76  const unsigned new_length = this->length_ + length;
77  if (new_length > StackSize) {
78  atomic_inc64(&num_overflows_);
79  long_string_ = new std::string();
80  long_string_->reserve(new_length);
81  long_string_->assign(stack_, length_);
82  long_string_->append(chars, length);
83  return;
84  }
85  if (length > 0)
86  memcpy(&stack_[this->length_], chars, length);
87  this->length_ = new_length;
88  }
89 
90  void Clear() {
91  delete long_string_;
92  long_string_ = NULL;
93  length_ = 0;
94  }
95 
96  const char *GetChars() const {
97  if (long_string_) {
98  return long_string_->data();
99  } else {
100  return stack_;
101  }
102  }
103 
104  unsigned GetLength() const {
105  if (long_string_)
106  return long_string_->length();
107  return length_;
108  }
109 
110  bool IsEmpty() const {
111  return GetLength() == 0;
112  }
113 
114  std::string ToString() const {
115  return std::string(this->GetChars(), this->GetLength());
116  }
117 
118  const char *c_str() const {
119  if (long_string_)
120  return long_string_->c_str();
121 
122  char *c = const_cast<char *>(stack_) + length_;
123  *c = '\0';
124  return stack_;
125  }
126 
127  bool operator ==(const ShortString &other) const {
128  const unsigned this_length = this->GetLength();
129  const unsigned other_length = other.GetLength();
130  if (this_length != other_length)
131  return false;
132  if (this_length == 0)
133  return true;
134 
135  return memcmp(this->GetChars(), other.GetChars(), this_length) == 0;
136  }
137 
138  bool operator !=(const ShortString &other) const {
139  return !(*this == other);
140  }
141 
142  bool operator <(const ShortString &other) const {
143  const unsigned this_length = this->GetLength();
144  const unsigned other_length = other.GetLength();
145 
146  if (this_length < other_length)
147  return true;
148  if (this_length > other_length)
149  return false;
150 
151  const char *this_chars = this->GetChars();
152  const char *other_chars = other.GetChars();
153  for (unsigned i = 0; i < this_length; ++i) {
154  if (this_chars[i] < other_chars[i])
155  return true;
156  if (this_chars[i] > other_chars[i])
157  return false;
158  }
159  return false;
160  }
161 
162  bool StartsWith(const ShortString &other) const {
163  const unsigned this_length = this->GetLength();
164  const unsigned other_length = other.GetLength();
165  if (this_length < other_length)
166  return false;
167 
168  return memcmp(this->GetChars(), other.GetChars(), other_length) == 0;
169  }
170 
171  ShortString Suffix(const unsigned start_at) const {
172  const unsigned length = this->GetLength();
173  if (start_at >= length)
174  return ShortString("", 0);
175 
176  return ShortString(this->GetChars() + start_at, length-start_at);
177  }
178 
179  static uint64_t num_instances() { return atomic_read64(&num_instances_); }
180  static uint64_t num_overflows() { return atomic_read64(&num_overflows_); }
181 
182  private:
183  std::string *long_string_;
184  char stack_[StackSize+1]; // +1 to add a final '\0' if necessary
185  unsigned char length_;
188 }; // class ShortString
189 
193 
194 template<unsigned char StackSize, char Type>
196 template<unsigned char StackSize, char Type>
198 
199 #ifdef CVMFS_NAMESPACE_GUARD
200 } // namespace CVMFS_NAMESPACE_GUARD
201 #endif
202 
203 #endif // CVMFS_SHORTSTRING_H_
ShortString< kDefaultMaxName, 1 > NameString
Definition: shortstring.h:191
int64_t atomic_int64
Definition: atomic.h:18
unsigned char length_
Definition: shortstring.h:185
ShortString Suffix(const unsigned start_at) const
Definition: shortstring.h:171
void Assign(const char *chars, const unsigned length)
Definition: shortstring.h:53
const unsigned char kDefaultMaxName
Definition: shortstring.h:22
void Clear()
Definition: shortstring.h:90
std::string * long_string_
Definition: shortstring.h:183
static uint64_t num_instances()
Definition: shortstring.h:179
const unsigned char kDefaultMaxPath
Definition: shortstring.h:24
ShortString(const std::string &std_string)
Definition: shortstring.h:40
bool operator<(SharedPtr< T > const &a, SharedPtr< U > const &b)
Definition: shared_ptr.h:140
bool operator!=(const cvmcache_hash &a, const cvmcache_hash &b)
void Append(const char *chars, const unsigned length)
Definition: shortstring.h:70
static atomic_int64 num_overflows_
Definition: shortstring.h:186
void Assign(const ShortString &other)
Definition: shortstring.h:66
const unsigned char kDefaultMaxLink
Definition: shortstring.h:23
std::string ToString() const
Definition: shortstring.h:114
bool operator==(const cvmcache_hash &a, const cvmcache_hash &b)
bool IsEmpty() const
Definition: shortstring.h:110
ShortString< kDefaultMaxLink, 2 > LinkString
Definition: shortstring.h:192
ShortString< kDefaultMaxPath, 0 > PathString
Definition: shortstring.h:190
ShortString(const char *chars, const unsigned length)
Definition: shortstring.h:36
bool StartsWith(const ShortString &other) const
Definition: shortstring.h:162
unsigned GetLength() const
Definition: shortstring.h:104
ShortString(const ShortString &other)
Definition: shortstring.h:32
static uint64_t num_overflows()
Definition: shortstring.h:180
const char * c_str() const
Definition: shortstring.h:118
const char * GetChars() const
Definition: shortstring.h:96
static atomic_int64 num_instances_
Definition: shortstring.h:187