CppCMS
string_key.h
1 //
3 // Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
4 //
5 // See accompanying file COPYING.TXT file for licensing details.
6 //
8 #ifndef CPPCMS_STRING_KEY_H
9 #define CPPCMS_STRING_KEY_H
10 
11 #include <string>
12 #include <string.h>
13 #include <algorithm>
14 #include <stdexcept>
15 #include <ostream>
16 
17 namespace cppcms {
18 
30  class string_key {
31  public:
32 
36  typedef char const *const_iterator;
37 
41  static const size_t npos = -1;
42 
47  begin_(0),
48  end_(0)
49  {
50  }
51 
55  string_key(char const *key) :
56  begin_(0),
57  end_(0),
58  key_(key)
59  {
60  }
64  string_key(std::string const &key) :
65  begin_(0),
66  end_(0),
67  key_(key)
68  {
69  }
73  size_t size() const
74  {
75  return end() - begin();
76  }
80  size_t length() const
81  {
82  return size();
83  }
87  void clear()
88  {
89  begin_ = end_ = 0;
90  key_.clear();
91  }
95  bool empty() const
96  {
97  return end() == begin();
98  }
103  size_t find(char c,size_t pos = 0) const
104  {
105  size_t s = size();
106  if(pos >= s)
107  return npos;
108  char const *p=begin() + pos;
109  while(pos <= s && *p!=c) {
110  pos++;
111  p++;
112  }
113  if(pos >= s)
114  return npos;
115  return pos;
116  }
117 
121  string_key substr(size_t pos = 0,size_t n=npos) const
122  {
123  string_key tmp = unowned_substr(pos,n);
124  return string_key(std::string(tmp.begin(),tmp.end()));
125  }
130  string_key unowned_substr(size_t pos = 0,size_t n=npos) const
131  {
132  if(pos >= size()) {
133  return string_key();
134  }
135  char const *p=begin() + pos;
136  char const *e=end();
137  if(n > size_t(e-p)) {
138  return string_key(p,e);
139  }
140  else {
141  return string_key(p,p+n);
142  }
143  }
144 
148  char const &operator[](size_t n) const
149  {
150  return *(begin() + n);
151  }
155  char const &at(size_t n) const
156  {
157  if(n > size())
158  throw std::out_of_range("cppcms::string_key::at() range error");
159  return *(begin() + n);
160  }
161 
166  static string_key unowned(std::string const &v)
167  {
168  return string_key(v.c_str(),v.c_str()+v.size());
169  }
174  static string_key unowned(char const *str)
175  {
176  char const *end = str;
177  while(*end)
178  end++;
179  return string_key(str,end);
180  }
185  static string_key unowned(char const *begin,char const *end)
186  {
187  return string_key(begin,end);
188  }
189 
193  char const *begin() const
194  {
195  if(begin_)
196  return begin_;
197  return key_.c_str();
198  }
202  char const *end() const
203  {
204  if(begin_)
205  return end_;
206  return key_.c_str() + key_.size();
207  }
211  bool operator<(string_key const &other) const
212  {
213  return std::lexicographical_compare( begin(),end(),
214  other.begin(),other.end(),
215  std::char_traits<char>::lt);
216  }
220  bool operator>(string_key const &other) const
221  {
222  return other < *this;
223  }
227  bool operator>=(string_key const &other) const
228  {
229  return !(*this < other);
230  }
234  bool operator<=(string_key const &other) const
235  {
236  return !(*this > other);
237  }
241  bool operator==(string_key const &other) const
242  {
243  return (end() - begin() == other.end() - other.begin())
244  && memcmp(begin(),other.begin(),end()-begin()) == 0;
245  }
249  bool operator!=(string_key const &other) const
250  {
251  return !(*this==other);
252  }
253 
257  char const *data() const
258  {
259  return begin();
260  }
261 
265  std::string str() const
266  {
267  if(begin_)
268  return std::string(begin_,end_-begin_);
269  else
270  return key_;
271  }
275  operator std::string() const
276  {
277  return str();
278  }
279  private:
280  string_key(char const *b,char const *e) :
281  begin_(b),
282  end_(e)
283  {
284  }
285 
286  char const *begin_;
287  char const *end_;
288  std::string key_;
289  };
290 
294  inline std::ostream &operator<<(std::ostream &out,string_key const &s)
295  {
296  out.write(s.data(),s.size());
297  return out;
298  }
299 
303  inline bool operator==(string_key const &l,char const *r)
304  {
305  return l==string_key::unowned(r);
306  }
307 
311  inline bool operator==(char const *l,string_key const &r)
312  {
313  return string_key::unowned(l) == r;
314  }
315 
319  inline bool operator==(string_key const &l,std::string const &r)
320  {
321  return l==string_key::unowned(r);
322  }
323 
324 
328  inline bool operator==(std::string const &l,string_key const &r)
329  {
330  return string_key::unowned(l) == r;
331  }
332 
336  inline bool operator!=(string_key const &l,char const *r)
337  {
338  return l!=string_key::unowned(r);
339  }
340 
344  inline bool operator!=(char const *l,string_key const &r)
345  {
346  return string_key::unowned(l) != r;
347  }
348 
352  inline bool operator!=(string_key const &l,std::string const &r)
353  {
354  return l!=string_key::unowned(r);
355  }
356 
360  inline bool operator!=(std::string const &l,string_key const &r)
361  {
362  return string_key::unowned(l) != r;
363  }
367  inline bool operator<=(string_key const &l,char const *r)
368  {
369  return l<=string_key::unowned(r);
370  }
371 
375  inline bool operator<=(char const *l,string_key const &r)
376  {
377  return string_key::unowned(l) <= r;
378  }
379 
383  inline bool operator<=(string_key const &l,std::string const &r)
384  {
385  return l<=string_key::unowned(r);
386  }
387 
391  inline bool operator<=(std::string const &l,string_key const &r)
392  {
393  return string_key::unowned(l) <= r;
394  }
398  inline bool operator>=(string_key const &l,char const *r)
399  {
400  return l>=string_key::unowned(r);
401  }
402 
406  inline bool operator>=(char const *l,string_key const &r)
407  {
408  return string_key::unowned(l) >= r;
409  }
410 
414  inline bool operator>=(string_key const &l,std::string const &r)
415  {
416  return l>=string_key::unowned(r);
417  }
418 
422  inline bool operator>=(std::string const &l,string_key const &r)
423  {
424  return string_key::unowned(l) >= r;
425  }
426 
427 
431  inline bool operator<(string_key const &l,char const *r)
432  {
433  return l<string_key::unowned(r);
434  }
435 
439  inline bool operator<(char const *l,string_key const &r)
440  {
441  return string_key::unowned(l) < r;
442  }
443 
447  inline bool operator<(string_key const &l,std::string const &r)
448  {
449  return l<string_key::unowned(r);
450  }
451 
455  inline bool operator<(std::string const &l,string_key const &r)
456  {
457  return string_key::unowned(l) < r;
458  }
462  inline bool operator>(string_key const &l,char const *r)
463  {
464  return l>string_key::unowned(r);
465  }
466 
470  inline bool operator>(char const *l,string_key const &r)
471  {
472  return string_key::unowned(l) > r;
473  }
474 
478  inline bool operator>(string_key const &l,std::string const &r)
479  {
480  return l>string_key::unowned(r);
481  }
482 
486  inline bool operator>(std::string const &l,string_key const &r)
487  {
488  return string_key::unowned(l) > r;
489  }
490 
491 }
492 
493 #endif
static const size_t npos
Definition: string_key.h:41
string_key()
Definition: string_key.h:46
static string_key unowned(std::string const &v)
Definition: string_key.h:166
string_key(char const *key)
Definition: string_key.h:55
bool operator<(string_key const &other) const
Definition: string_key.h:211
char const & at(size_t n) const
Definition: string_key.h:155
std::string str() const
Definition: string_key.h:265
string_key(std::string const &key)
Definition: string_key.h:64
This is the namespace where all CppCMS functionality is placed.
Definition: application.h:19
static string_key unowned(char const *begin, char const *end)
Definition: string_key.h:185
bool operator!=(string_key const &other) const
Definition: string_key.h:249
size_t size() const
Definition: string_key.h:73
char const * const_iterator
Definition: string_key.h:36
char const * end() const
Definition: string_key.h:202
bool operator>(string_key const &other) const
Definition: string_key.h:220
static string_key unowned(char const *str)
Definition: string_key.h:174
string_key unowned_substr(size_t pos=0, size_t n=npos) const
Definition: string_key.h:130
This is a special object that may hold an std::string or alternatively reference to external (unowned...
Definition: string_key.h:30
char const * begin() const
Definition: string_key.h:193
char const * data() const
Definition: string_key.h:257
char const & operator[](size_t n) const
Definition: string_key.h:148
bool operator==(string_key const &other) const
Definition: string_key.h:241
void clear()
Definition: string_key.h:87
bool operator>=(string_key const &other) const
Definition: string_key.h:227
bool empty() const
Definition: string_key.h:95
size_t find(char c, size_t pos=0) const
Definition: string_key.h:103
archive & operator<<(archive &a, Archivable const &object)
Definition: serialization_classes.h:176
string_key substr(size_t pos=0, size_t n=npos) const
Definition: string_key.h:121
bool operator<=(string_key const &other) const
Definition: string_key.h:234
size_t length() const
Definition: string_key.h:80