SBuf.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 /* DEBUG: section 24 SBuf */
10 
11 #ifndef SQUID_SBUF_H
12 #define SQUID_SBUF_H
13 
14 #include "base/InstanceId.h"
15 #include "base/TextException.h"
16 #include "Debug.h"
17 #include "globals.h"
18 #include "sbuf/forward.h"
19 #include "sbuf/MemBlob.h"
20 #include "sbuf/Stats.h"
21 
22 #include <climits>
23 #include <iosfwd>
24 #include <iterator>
25 #if HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28 
29 /* SBuf placeholder for printf */
30 #ifndef SQUIDSBUFPH
31 #define SQUIDSBUFPH "%.*s"
32 #define SQUIDSBUFPRINT(s) (s).plength(),(s).rawContent()
33 #endif /* SQUIDSBUFPH */
34 
35 // TODO: move within SBuf and rename
36 typedef enum {
40 
41 class CharacterSet;
42 
48 class SBufIterator : public std::iterator<std::input_iterator_tag, char>
49 {
50 public:
51  friend class SBuf;
53  bool operator==(const SBufIterator &s) const;
54  bool operator!=(const SBufIterator &s) const;
55 
56  const char &operator*() const { return *iter; }
57  SBufIterator& operator++() { ++iter; return *this; }
58 
59 protected:
60  SBufIterator(const SBuf &, size_type);
61 
62  const char *iter = nullptr;
63 };
64 
71 {
72  friend class SBuf;
73 public:
74  SBufReverseIterator& operator++() { --iter; return *this;}
75  const char &operator*() const { return *(iter-1); }
76 protected:
78 };
79 
86 class SBuf
87 {
88 public:
92  static const size_type npos = 0xffffffff; // max(uint32_t)
93 
95  static const size_type maxSize = 0xfffffff;
96 
98  SBuf();
99  SBuf(const SBuf &S);
100  SBuf(SBuf&& S) : store_(std::move(S.store_)), off_(S.off_), len_(S.len_) {
101  ++stats.moves;
102  S.store_ = nullptr; //RefCount supports nullptr, and S is about to be destructed
103  S.off_ = S.len_ = 0;
104  }
105 
116  explicit SBuf(const char *S, size_type n);
117  explicit SBuf(const char *S);
118 
120  explicit SBuf(const std::string &s);
121 
122  ~SBuf();
123 
128  SBuf& assign(const SBuf &S);
129 
134  SBuf& operator =(const SBuf & S) {return assign(S);}
136  ++stats.moves;
137  if (this != &S) {
138  store_ = std::move(S.store_);
139  off_ = S.off_;
140  len_ = S.len_;
141  S.store_ = NULL; //RefCount supports NULL, and S is about to be destructed
142  S.off_ = 0;
143  S.len_ = 0;
144  }
145  return *this;
146  }
147 
148  // XXX: assign(s,n)/append(s,n) calls do not assign or append a c-string as
149  // documented -- they do not stop at the first NUL character! They assign or
150  // append the entire raw memory area, including any embedded NUL characters.
151 
162  SBuf& assign(const char *S, size_type n);
163  SBuf& assign(const char *S) {return assign(S,npos);}
164 
171  SBuf& operator =(const char *S) {return assign(S);}
172 
177  void clear();
178 
183  SBuf& append(const SBuf & S);
184 
186  SBuf& append(const char c);
187 
200  SBuf& append(const char * S, size_type Ssize);
201  SBuf& append(const char * S) { return append(S,npos); }
202 
207  SBuf& Printf(const char *fmt, ...);
208 
213  SBuf& appendf(const char *fmt, ...);
214 
219  SBuf& vappendf(const char *fmt, va_list vargs);
220 
222  std::ostream& print(std::ostream &os) const;
223 
229  std::ostream& dump(std::ostream &os) const;
230 
235  char operator [](size_type pos) const {++stats.getChar; return store_->mem[off_+pos];}
236 
242  char at(size_type pos) const {checkAccessBounds(pos); return operator[](pos);}
243 
252  void setAt(size_type pos, char toset);
253 
262  int compare(const SBuf &S, const SBufCaseSensitive isCaseSensitive, const size_type n) const;
263  int compare(const SBuf &S, const SBufCaseSensitive isCaseSensitive) const {
264  return compare(S, isCaseSensitive, npos);
265  }
266 
268  inline int cmp(const SBuf &S, const size_type n) const {
269  return compare(S,caseSensitive,n);
270  }
271  inline int cmp(const SBuf &S) const {
272  return compare(S,caseSensitive,npos);
273  }
274 
276  inline int caseCmp(const SBuf &S, const size_type n) const {
277  return compare(S,caseInsensitive,n);
278  }
279  inline int caseCmp(const SBuf &S) const {
280  return compare(S,caseInsensitive,npos);
281  }
282 
284  int compare(const char *s, const SBufCaseSensitive isCaseSensitive, const size_type n) const;
285  int compare(const char *s, const SBufCaseSensitive isCaseSensitive) const {
286  return compare(s,isCaseSensitive,npos);
287  }
288 
290  inline int cmp(const char *S, const size_type n) const {
291  return compare(S,caseSensitive,n);
292  }
293  inline int cmp(const char *S) const {
294  return compare(S,caseSensitive,npos);
295  }
296 
298  inline int caseCmp(const char *S, const size_type n) const {
299  return compare(S,caseInsensitive,n);
300  }
301  inline int caseCmp(const char *S) const {
302  return compare(S,caseInsensitive,npos);
303  }
304 
310  bool startsWith(const SBuf &S, const SBufCaseSensitive isCaseSensitive = caseSensitive) const;
311 
312  bool operator ==(const SBuf & S) const;
313  bool operator !=(const SBuf & S) const;
314  bool operator <(const SBuf &S) const {return (cmp(S) < 0);}
315  bool operator >(const SBuf &S) const {return (cmp(S) > 0);}
316  bool operator <=(const SBuf &S) const {return (cmp(S) <= 0);}
317  bool operator >=(const SBuf &S) const {return (cmp(S) >= 0);}
318 
328  SBuf consume(size_type n = npos);
329 
331  static const SBufStats& GetStats();
332 
339  size_type copy(char *dest, size_type n) const;
340 
366  const char* rawContent() const;
367 
372  char *rawAppendStart(size_type anticipatedSize);
373 
379  void rawAppendFinish(const char *start, size_type actualSize);
380 
386  size_type spaceSize() const { return store_->spaceSize(); }
387 
405  const char* c_str();
406 
408  size_type length() const {return len_;}
409 
415  int plength() const {
416  Must(length() <= INT_MAX);
417  return static_cast<int>(length());
418  }
419 
424  bool isEmpty() const {return (len_==0);}
425 
433  void reserveSpace(size_type minSpace) {
434  Must(minSpace <= maxSize);
435  Must(length() <= maxSize - minSpace);
436  reserveCapacity(length()+minSpace);
437  }
438 
447  void reserveCapacity(size_type minCapacity);
448 
453  size_type reserve(const SBufReservationRequirements &requirements);
454 
469  SBuf& chop(size_type pos, size_type n = npos);
470 
478  SBuf& trim(const SBuf &toRemove, bool atBeginning = true, bool atEnd = true);
479 
486  SBuf substr(size_type pos, size_type n = npos) const;
487 
496  size_type find(char c, size_type startPos = 0) const;
497 
506  size_type find(const SBuf & str, size_type startPos = 0) const;
507 
515  size_type rfind(char c, size_type endPos = npos) const;
516 
525  size_type rfind(const SBuf &str, size_type endPos = npos) const;
526 
537  size_type findFirstOf(const CharacterSet &set, size_type startPos = 0) const;
538 
547  size_type findLastOf(const CharacterSet &set, size_type endPos = npos) const;
548 
557  size_type findFirstNotOf(const CharacterSet &set, size_type startPos = 0) const;
558 
565  size_type findLastNotOf(const CharacterSet &set, size_type endPos = npos) const;
566 
568  void toLower();
569 
571  void toUpper();
572 
574  std::string toStdString() const { return std::string(buf(),length()); }
575 
577  return const_iterator(*this, 0);
578  }
579 
580  const_iterator end() const {
581  return const_iterator(*this, length());
582  }
583 
585  return const_reverse_iterator(*this, length());
586  }
587 
589  return const_reverse_iterator(*this, 0);
590  }
591 
592  // TODO: possibly implement erase() similar to std::string's erase
593  // TODO: possibly implement a replace() call
594 
598 
599 private:
600 
608  class Locker
609  {
610  public:
611  Locker(SBuf *parent, const char *otherBuffer) {
612  // lock if otherBuffer intersects the parents buffer area
613  const MemBlob *blob = parent->store_.getRaw();
614  if (blob->mem <= otherBuffer && otherBuffer < (blob->mem + blob->capacity))
615  locket = blob;
616  }
617  private:
619  };
620  friend class Locker;
621 
625  static SBufStats stats;
626 
633 
638  char * buf() const {return (store_->mem+off_);}
639 
645  char * bufEnd() const {return (store_->mem+off_+len_);}
646 
651  size_type estimateCapacity(size_type desired) const {return (2*desired);}
652 
653  void reAlloc(size_type newsize);
654 
655  void cow(size_type minsize = npos);
656 
657  void checkAccessBounds(const size_type pos) const { Must(pos < length()); }
658 
676  char *rawSpace(size_type minSize);
677 
685  SBuf& lowAppend(const char * memArea, size_type areaSize);
686 };
687 
690 {
691 public:
693 
694  /*
695  * Parameters are listed in the reverse order of importance: Satisfaction of
696  * the lower-listed requirements may violate the higher-listed requirements.
697  * For example, idealSpace has no effect unless it exceeds minSpace.
698  */
702  bool allowShared = true;
703 };
704 
706 inline std::ostream &
707 operator <<(std::ostream& os, const SBuf& S)
708 {
709  return S.print(os);
710 }
711 
713 inline SBuf
715 {
716  buf.toUpper();
717  return buf;
718 }
719 
721 inline SBuf
723 {
724  buf.toLower();
725  return buf;
726 }
727 
744 inline void
745 SBufToCstring(char *d, const SBuf &s)
746 {
747  s.copy(d, s.length());
748  d[s.length()] = '\0'; // 0-terminate the destination
749  debugs(1, DBG_DATA, "built c-string '" << d << "' from " << s);
750 }
751 
760 inline char *
762 {
763  char *d = static_cast<char*>(xmalloc(s.length()+1));
764  SBufToCstring(d, s);
765  return d;
766 }
767 
768 inline
770  : iter(s.rawContent()+pos)
771 {}
772 
773 inline bool
775 {
776  // note: maybe the sbuf comparison is unnecessary?
777  return iter == s.iter;
778 }
779 
780 inline bool
782 {
783  // note: maybe the sbuf comparison is unnecessary?
784  return iter != s.iter;
785 }
786 
787 #endif /* SQUID_SBUF_H */
788 
const char * iter
Definition: SBuf.h:62
size_type find(char c, size_type startPos=0) const
Definition: SBuf.cc:584
SBuf(SBuf &&S)
Definition: SBuf.h:100
size_type idealSpace
if allocating anyway, provide this much space
Definition: SBuf.h:699
SBuf & assign(const SBuf &S)
Definition: SBuf.cc:83
bool operator!=(const SBufIterator &s) const
Definition: SBuf.h:781
#define xmalloc
const_reverse_iterator rbegin() const
Definition: SBuf.h:584
bool operator>=(const SBuf &S) const
Definition: SBuf.h:317
@ caseSensitive
Definition: SBuf.h:37
SBuf & append(const char *S)
Definition: SBuf.h:201
bool isEmpty() const
Definition: SBuf.h:424
std::ostream & operator<<(std::ostream &os, const SBuf &S)
ostream output operator
Definition: SBuf.h:707
size_type len_
number of our content bytes in shared store_
Definition: SBuf.h:624
SBufReverseIterator const_reverse_iterator
Definition: SBuf.h:91
void checkAccessBounds(const size_type pos) const
Definition: SBuf.h:657
bool operator==(const SBufIterator &s) const
Definition: SBuf.h:774
void reserveSpace(size_type minSpace)
Definition: SBuf.h:433
size_type estimateCapacity(size_type desired) const
Definition: SBuf.h:651
static MemBlob::Pointer GetStorePrototype()
Definition: SBuf.cc:76
SBuf & lowAppend(const char *memArea, size_type areaSize)
Definition: SBuf.cc:863
size_type spaceSize() const
the number unused bytes at the end of the allocated blob
Definition: MemBlob.h:65
SBuf & vappendf(const char *fmt, va_list vargs)
Definition: SBuf.cc:239
size_type maxCapacity
do not allocate more than this
Definition: SBuf.h:701
SBuf::size_type size_type
Definition: SBuf.h:692
int compare(const SBuf &S, const SBufCaseSensitive isCaseSensitive, const size_type n) const
Definition: SBuf.cc:352
int cmp(const char *S) const
Definition: SBuf.h:293
Definition: SBuf.h:87
int cmp(const char *S, const size_type n) const
Shorthand version for C-string compare().
Definition: SBuf.h:290
void SBufToCstring(char *d, const SBuf &s)
Definition: SBuf.h:745
Named SBuf::reserve() parameters. Defaults ask for and restrict nothing.
Definition: SBuf.h:690
void rawAppendFinish(const char *start, size_type actualSize)
Definition: SBuf.cc:144
bool operator==(const SBuf &S) const
Definition: SBuf.cc:455
int caseCmp(const char *S) const
Definition: SBuf.h:301
char * bufEnd() const
Definition: SBuf.h:645
char * rawSpace(size_type minSize)
Definition: SBuf.cc:157
C * getRaw() const
Definition: RefCount.h:80
size_type spaceSize() const
Definition: SBuf.h:386
void toLower()
converts all characters to lower case;
Definition: SBuf.cc:811
bool startsWith(const SBuf &S, const SBufCaseSensitive isCaseSensitive=caseSensitive) const
Definition: SBuf.cc:442
SBuf & Printf(const char *fmt,...)
Definition: SBuf.cc:214
SBufCaseSensitive
Definition: SBuf.h:36
bool allowShared
whether sharing our storage with others is OK
Definition: SBuf.h:702
static const size_type maxSize
Maximum size of a SBuf. By design it MUST be < MAX(size_type)/2. Currently 256Mb.
Definition: SBuf.h:95
int caseCmp(const SBuf &S) const
Definition: SBuf.h:279
SBuf substr(size_type pos, size_type n=npos) const
Definition: SBuf.cc:576
Locker(SBuf *parent, const char *otherBuffer)
Definition: SBuf.h:611
void clear()
Definition: SBuf.cc:175
size_type reserve(const SBufReservationRequirements &requirements)
Definition: SBuf.cc:112
int caseCmp(const char *S, const size_type n) const
Shorthand version for case-insensitive C-string compare().
Definition: SBuf.h:298
SBuf & chop(size_type pos, size_type n=npos)
Definition: SBuf.cc:530
char * rawAppendStart(size_type anticipatedSize)
Definition: SBuf.cc:136
size_type rfind(char c, size_type endPos=npos) const
Definition: SBuf.cc:692
void toUpper()
converts all characters to upper case;
Definition: SBuf.cc:824
const_reverse_iterator rend() const
Definition: SBuf.h:588
bool operator>(const SBuf &S) const
Definition: SBuf.h:315
#define NULL
Definition: types.h:166
const char * rawContent() const
Definition: SBuf.cc:509
SBuf consume(size_type n=npos)
Definition: SBuf.cc:481
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:128
@ caseInsensitive
Definition: SBuf.h:38
MemBlob::Pointer locket
Definition: SBuf.h:618
const_iterator end() const
Definition: SBuf.h:580
MemBlob::size_type size_type
Definition: SBuf.h:89
std::ostream & dump(std::ostream &os) const
Definition: SBuf.cc:303
#define INT_MAX
Definition: types.h:76
char at(size_type pos) const
Definition: SBuf.h:242
SBuf()
create an empty (zero-size) SBuf
Definition: SBuf.cc:28
SBuf ToUpper(SBuf buf)
Returns a lower-cased copy of its parameter.
Definition: SBuf.h:714
SBufReverseIterator(const SBuf &s, size_type sz)
Definition: SBuf.h:77
const char & operator*() const
Definition: SBuf.h:56
const_iterator begin() const
Definition: SBuf.h:576
const char & operator*() const
Definition: SBuf.h:75
std::string toStdString() const
std::string export function
Definition: SBuf.h:574
bool operator<(const SBuf &S) const
Definition: SBuf.h:314
std::ostream & print(std::ostream &os) const
print the SBuf contents to the supplied ostream
Definition: SBuf.cc:295
SBufIterator & operator++()
Definition: SBuf.h:57
void cow(size_type minsize=npos)
Definition: SBuf.cc:878
~SBuf()
Definition: SBuf.cc:68
SBuf & trim(const SBuf &toRemove, bool atBeginning=true, bool atEnd=true)
Definition: SBuf.cc:551
bool operator<=(const SBuf &S) const
Definition: SBuf.h:316
const char * c_str()
Definition: SBuf.cc:516
void const char int sz
Definition: stub_cbdata.cc:16
size_type length() const
Returns the number of bytes stored in SBuf.
Definition: SBuf.h:408
size_type off_
our content start offset from the beginning of shared store_
Definition: SBuf.h:623
int plength() const
Definition: SBuf.h:415
static SBufStats stats
class-wide statistics
Definition: SBuf.h:625
SBuf & append(const SBuf &S)
Definition: SBuf.cc:185
size_type findLastOf(const CharacterSet &set, size_type endPos=npos) const
Definition: SBuf.cc:769
static const size_type npos
Definition: SBuf.h:92
size_type findFirstNotOf(const CharacterSet &set, size_type startPos=0) const
Definition: SBuf.cc:746
size_type minSpace
allocate [at least this much] if spaceSize() is smaller
Definition: SBuf.h:700
void setAt(size_type pos, char toset)
Definition: SBuf.cc:328
SBufReverseIterator & operator++()
Definition: SBuf.h:74
SBuf & operator=(const SBuf &S)
Definition: SBuf.h:134
uint32_t size_type
Definition: MemBlob.h:51
MemBlob::Pointer store_
memory block, possibly shared with other SBufs
Definition: SBuf.h:622
size_type capacity
size of the raw allocated memory block
Definition: MemBlob.h:113
char * buf() const
Definition: SBuf.h:638
int cmp(const SBuf &S) const
Definition: SBuf.h:271
void reserveCapacity(size_type minCapacity)
Definition: SBuf.cc:105
int cmp(const SBuf &S, const size_type n) const
shorthand version for compare()
Definition: SBuf.h:268
static const SBufStats & GetStats()
gets global statistic information
Definition: SBuf.cc:494
void reAlloc(size_type newsize)
Definition: SBuf.cc:845
MemBlob::size_type size_type
Definition: SBuf.h:52
#define Must(condition)
Like assert() but throws an exception instead of aborting the process.
Definition: TextException.h:73
bool operator!=(const SBuf &S) const
Definition: SBuf.cc:475
SBuf ToLower(SBuf buf)
Returns an upper-cased copy of its parameter.
Definition: SBuf.h:722
SBuf & appendf(const char *fmt,...)
Definition: SBuf.cc:229
size_type findLastNotOf(const CharacterSet &set, size_type endPos=npos) const
Definition: SBuf.cc:790
optimized set of C chars, with quick membership test and merge support
Definition: CharacterSet.h:18
int caseCmp(const SBuf &S, const size_type n) const
shorthand version for case-insensitive compare()
Definition: SBuf.h:276
SBufIterator(const SBuf &, size_type)
Definition: SBuf.h:769
char operator[](size_type pos) const
Definition: SBuf.h:235
const InstanceId< SBuf > id
Definition: SBuf.h:597
int compare(const char *s, const SBufCaseSensitive isCaseSensitive) const
Definition: SBuf.h:285
size_type findFirstOf(const CharacterSet &set, size_type startPos=0) const
Definition: SBuf.cc:723
char * mem
raw allocated memory block
Definition: MemBlob.h:112
int compare(const SBuf &S, const SBufCaseSensitive isCaseSensitive) const
Definition: SBuf.h:263
SBufIterator const_iterator
Definition: SBuf.h:90
uint64_t getChar
number of calls to at() and operator[]
Definition: Stats.h:39
size_type copy(char *dest, size_type n) const
Definition: SBuf.cc:500
void const char * buf
Definition: stub_helper.cc:16
SBuf & assign(const char *S)
Definition: SBuf.h:163
uint64_t moves
number of move constructions/assignments
Definition: Stats.h:36
#define DBG_DATA
Definition: Debug.h:48

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors