SBuf.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2022 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/Stream.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
36typedef enum {
40
41class CharacterSet;
42
49{
50public:
51 // iterator traits
52 using iterator_category = std::input_iterator_tag;
53 using value_type = char;
54 using difference_type = std::ptrdiff_t;
55 using pointer = char*;
56 using reference = char&;
57
58 friend class SBuf;
60 bool operator==(const SBufIterator &s) const;
61 bool operator!=(const SBufIterator &s) const;
62
63 const char &operator*() const { return *iter; }
64 SBufIterator& operator++() { ++iter; return *this; }
65
66protected:
67 SBufIterator(const SBuf &, size_type);
68
69 const char *iter = nullptr;
70};
71
78{
79 friend class SBuf;
80public:
81 SBufReverseIterator& operator++() { --iter; return *this;}
82 const char &operator*() const { return *(iter-1); }
83protected:
85};
86
93class SBuf
94{
95public:
99 static const size_type npos = 0xffffffff; // max(uint32_t)
100
102 static const size_type maxSize = 0xfffffff;
103
105 SBuf();
106 SBuf(const SBuf &S);
107 SBuf(SBuf&& S) : store_(std::move(S.store_)), off_(S.off_), len_(S.len_) {
108 ++stats.moves;
109 S.store_ = nullptr; //RefCount supports nullptr, and S is about to be destructed
110 S.off_ = S.len_ = 0;
111 }
112
123 explicit SBuf(const char *S, size_type n);
124 explicit SBuf(const char *S);
125
127 explicit SBuf(const std::string &s);
128
129 ~SBuf();
130
135 SBuf& assign(const SBuf &S);
136
141 SBuf& operator =(const SBuf & S) {return assign(S);}
143 ++stats.moves;
144 if (this != &S) {
145 store_ = std::move(S.store_);
146 off_ = S.off_;
147 len_ = S.len_;
148 S.store_ = nullptr; //RefCount supports NULL, and S is about to be destructed
149 S.off_ = 0;
150 S.len_ = 0;
151 }
152 return *this;
153 }
154
155 // XXX: assign(s,n)/append(s,n) calls do not assign or append a c-string as
156 // documented -- they do not stop at the first NUL character! They assign or
157 // append the entire raw memory area, including any embedded NUL characters.
158
169 SBuf& assign(const char *S, size_type n);
170 SBuf& assign(const char *S) {return assign(S,npos);}
171
178 SBuf& operator =(const char *S) {return assign(S);}
179
184 void clear();
185
190 SBuf& append(const SBuf & S);
191
193 SBuf& append(const char c);
194
207 SBuf& append(const char * S, size_type Ssize);
208 SBuf& append(const char * S) { return append(S,npos); }
209
214 SBuf& Printf(const char *fmt, ...);
215
220 SBuf& appendf(const char *fmt, ...);
221
226 SBuf& vappendf(const char *fmt, va_list vargs);
227
229 std::ostream& print(std::ostream &os) const;
230
236 std::ostream& dump(std::ostream &os) const;
237
242 char operator [](size_type pos) const {++stats.getChar; return store_->mem[off_+pos];}
243
249 char at(size_type pos) const {checkAccessBounds(pos); return operator[](pos);}
250
259 void setAt(size_type pos, char toset);
260
269 int compare(const SBuf &S, const SBufCaseSensitive isCaseSensitive, const size_type n) const;
270 int compare(const SBuf &S, const SBufCaseSensitive isCaseSensitive) const {
271 return compare(S, isCaseSensitive, npos);
272 }
273
275 inline int cmp(const SBuf &S, const size_type n) const {
276 return compare(S,caseSensitive,n);
277 }
278 inline int cmp(const SBuf &S) const {
279 return compare(S,caseSensitive,npos);
280 }
281
283 inline int caseCmp(const SBuf &S, const size_type n) const {
284 return compare(S,caseInsensitive,n);
285 }
286 inline int caseCmp(const SBuf &S) const {
287 return compare(S,caseInsensitive,npos);
288 }
289
291 int compare(const char *s, const SBufCaseSensitive isCaseSensitive, const size_type n) const;
292 int compare(const char *s, const SBufCaseSensitive isCaseSensitive) const {
293 return compare(s,isCaseSensitive,npos);
294 }
295
297 inline int cmp(const char *S, const size_type n) const {
298 return compare(S,caseSensitive,n);
299 }
300 inline int cmp(const char *S) const {
301 return compare(S,caseSensitive,npos);
302 }
303
305 inline int caseCmp(const char *S, const size_type n) const {
306 return compare(S,caseInsensitive,n);
307 }
308 inline int caseCmp(const char *S) const {
309 return compare(S,caseInsensitive,npos);
310 }
311
317 bool startsWith(const SBuf &S, const SBufCaseSensitive isCaseSensitive = caseSensitive) const;
318
319 bool operator ==(const SBuf & S) const;
320 bool operator !=(const SBuf & S) const;
321 bool operator <(const SBuf &S) const {return (cmp(S) < 0);}
322 bool operator >(const SBuf &S) const {return (cmp(S) > 0);}
323 bool operator <=(const SBuf &S) const {return (cmp(S) <= 0);}
324 bool operator >=(const SBuf &S) const {return (cmp(S) >= 0);}
325
336
338 static const SBufStats& GetStats();
339
346 size_type copy(char *dest, size_type n) const;
347
373 const char* rawContent() const;
374
379 char *rawAppendStart(size_type anticipatedSize);
380
386 void rawAppendFinish(const char *start, size_type actualSize);
387
393 size_type spaceSize() const { return store_->spaceSize(); }
394
412 const char* c_str();
413
415 size_type length() const {return len_;}
416
422 int plength() const {
423 Must(length() <= INT_MAX);
424 return static_cast<int>(length());
425 }
426
431 bool isEmpty() const {return (len_==0);}
432
440 void reserveSpace(size_type minSpace) {
441 Must(minSpace <= maxSize);
442 Must(length() <= maxSize - minSpace);
443 reserveCapacity(length()+minSpace);
444 }
445
454 void reserveCapacity(size_type minCapacity);
455
460 size_type reserve(const SBufReservationRequirements &requirements);
461
476 SBuf& chop(size_type pos, size_type n = npos);
477
485 SBuf& trim(const SBuf &toRemove, bool atBeginning = true, bool atEnd = true);
486
493 SBuf substr(size_type pos, size_type n = npos) const;
494
503 size_type find(char c, size_type startPos = 0) const;
504
513 size_type find(const SBuf & str, size_type startPos = 0) const;
514
522 size_type rfind(char c, size_type endPos = npos) const;
523
532 size_type rfind(const SBuf &str, size_type endPos = npos) const;
533
544 size_type findFirstOf(const CharacterSet &set, size_type startPos = 0) const;
545
554 size_type findLastOf(const CharacterSet &set, size_type endPos = npos) const;
555
564 size_type findFirstNotOf(const CharacterSet &set, size_type startPos = 0) const;
565
572 size_type findLastNotOf(const CharacterSet &set, size_type endPos = npos) const;
573
575 void toLower();
576
578 void toUpper();
579
581 std::string toStdString() const { return std::string(buf(),length()); }
582
584 return const_iterator(*this, 0);
585 }
586
588 return const_iterator(*this, length());
589 }
590
592 return const_reverse_iterator(*this, length());
593 }
594
596 return const_reverse_iterator(*this, 0);
597 }
598
599 // TODO: possibly implement erase() similar to std::string's erase
600 // TODO: possibly implement a replace() call
601
605
606private:
607
615 class Locker
616 {
617 public:
618 Locker(SBuf *parent, const char *otherBuffer) {
619 // lock if otherBuffer intersects the parents buffer area
620 const MemBlob *blob = parent->store_.getRaw();
621 if (blob->mem <= otherBuffer && otherBuffer < (blob->mem + blob->capacity))
622 locket = blob;
623 }
624 private:
626 };
627 friend class Locker;
628
633
640
645 char * buf() const {return (store_->mem+off_);}
646
652 char * bufEnd() const {return (store_->mem+off_+len_);}
653
658 size_type estimateCapacity(size_type desired) const {return (2*desired);}
659
660 void reAlloc(size_type newsize);
661
662 void cow(size_type minsize = npos);
663
664 void checkAccessBounds(const size_type pos) const { Must(pos < length()); }
665
683 char *rawSpace(size_type minSize);
684
692 SBuf& lowAppend(const char * memArea, size_type areaSize);
693};
694
697{
698public:
700
701 /*
702 * Parameters are listed in the reverse order of importance: Satisfaction of
703 * the lower-listed requirements may violate the higher-listed requirements.
704 * For example, idealSpace has no effect unless it exceeds minSpace.
705 */
709 bool allowShared = true;
710};
711
713inline std::ostream &
714operator <<(std::ostream& os, const SBuf& S)
715{
716 return S.print(os);
717}
718
720inline SBuf
722{
723 buf.toUpper();
724 return buf;
725}
726
728inline SBuf
730{
731 buf.toLower();
732 return buf;
733}
734
751inline void
752SBufToCstring(char *d, const SBuf &s)
753{
754 s.copy(d, s.length());
755 d[s.length()] = '\0'; // 0-terminate the destination
756 debugs(1, DBG_DATA, "built c-string '" << d << "' from " << s);
757}
758
767inline char *
769{
770 char *d = static_cast<char*>(xmalloc(s.length()+1));
771 SBufToCstring(d, s);
772 return d;
773}
774
775inline
777 : iter(s.rawContent()+pos)
778{}
779
780inline bool
782{
783 // note: maybe the sbuf comparison is unnecessary?
784 return iter == s.iter;
785}
786
787inline bool
789{
790 // note: maybe the sbuf comparison is unnecessary?
791 return iter != s.iter;
792}
793
794#endif /* SQUID_SBUF_H */
795
SBuf ToUpper(SBuf buf)
Returns a lower-cased copy of its parameter.
Definition: SBuf.h:721
std::ostream & operator<<(std::ostream &os, const SBuf &S)
ostream output operator
Definition: SBuf.h:714
SBufCaseSensitive
Definition: SBuf.h:36
@ caseInsensitive
Definition: SBuf.h:38
@ caseSensitive
Definition: SBuf.h:37
void SBufToCstring(char *d, const SBuf &s)
Definition: SBuf.h:752
SBuf ToLower(SBuf buf)
Returns an upper-cased copy of its parameter.
Definition: SBuf.h:729
#define Must(condition)
Definition: TextException.h:71
optimized set of C chars, with quick membership test and merge support
Definition: CharacterSet.h:18
size_type spaceSize() const
the number unused bytes at the end of the allocated blob
Definition: MemBlob.h:65
char * mem
raw allocated memory block
Definition: MemBlob.h:112
uint32_t size_type
Definition: MemBlob.h:51
size_type capacity
size of the raw allocated memory block
Definition: MemBlob.h:113
C * getRaw() const
Definition: RefCount.h:80
MemBlob::size_type size_type
Definition: SBuf.h:59
bool operator!=(const SBufIterator &s) const
Definition: SBuf.h:788
char & reference
Definition: SBuf.h:56
bool operator==(const SBufIterator &s) const
Definition: SBuf.h:781
char value_type
Definition: SBuf.h:53
const char * iter
Definition: SBuf.h:69
std::ptrdiff_t difference_type
Definition: SBuf.h:54
std::input_iterator_tag iterator_category
Definition: SBuf.h:52
char * pointer
Definition: SBuf.h:55
const char & operator*() const
Definition: SBuf.h:63
SBufIterator & operator++()
Definition: SBuf.h:64
SBufIterator(const SBuf &, size_type)
Definition: SBuf.h:776
Named SBuf::reserve() parameters. Defaults ask for and restrict nothing.
Definition: SBuf.h:697
bool allowShared
whether sharing our storage with others is OK
Definition: SBuf.h:709
size_type minSpace
allocate [at least this much] if spaceSize() is smaller
Definition: SBuf.h:707
SBuf::size_type size_type
Definition: SBuf.h:699
size_type maxCapacity
do not allocate more than this
Definition: SBuf.h:708
size_type idealSpace
if allocating anyway, provide this much space
Definition: SBuf.h:706
SBufReverseIterator & operator++()
Definition: SBuf.h:81
SBufReverseIterator(const SBuf &s, size_type sz)
Definition: SBuf.h:84
const char & operator*() const
Definition: SBuf.h:82
uint64_t moves
number of move constructions/assignments
Definition: Stats.h:42
uint64_t getChar
number of calls to at() and operator[]
Definition: Stats.h:45
Locker(SBuf *parent, const char *otherBuffer)
Definition: SBuf.h:618
MemBlob::Pointer locket
Definition: SBuf.h:625
Definition: SBuf.h:94
char * rawAppendStart(size_type anticipatedSize)
Definition: SBuf.cc:136
void toUpper()
converts all characters to upper case;
Definition: SBuf.cc:824
int cmp(const char *S) const
Definition: SBuf.h:300
int caseCmp(const SBuf &S, const size_type n) const
shorthand version for case-insensitive compare()
Definition: SBuf.h:283
SBuf & assign(const char *S)
Definition: SBuf.h:170
int cmp(const char *S, const size_type n) const
Shorthand version for C-string compare().
Definition: SBuf.h:297
SBuf(SBuf &&S)
Definition: SBuf.h:107
const char * rawContent() const
Definition: SBuf.cc:509
static const size_type npos
Definition: SBuf.h:99
bool operator<(const SBuf &S) const
Definition: SBuf.h:321
char at(size_type pos) const
Definition: SBuf.h:249
SBuf & append(const char *S)
Definition: SBuf.h:208
SBuf consume(size_type n=npos)
Definition: SBuf.cc:481
SBuf & vappendf(const char *fmt, va_list vargs)
Definition: SBuf.cc:239
const char * c_str()
Definition: SBuf.cc:516
~SBuf()
Definition: SBuf.cc:68
SBuf & chop(size_type pos, size_type n=npos)
Definition: SBuf.cc:530
void reserveCapacity(size_type minCapacity)
Definition: SBuf.cc:105
size_type len_
number of our content bytes in shared store_
Definition: SBuf.h:631
static const SBufStats & GetStats()
gets global statistic information
Definition: SBuf.cc:494
static MemBlob::Pointer GetStorePrototype()
Definition: SBuf.cc:76
SBuf & lowAppend(const char *memArea, size_type areaSize)
Definition: SBuf.cc:863
const InstanceId< SBuf > id
Definition: SBuf.h:604
void reAlloc(size_type newsize)
Definition: SBuf.cc:845
char * buf() const
Definition: SBuf.h:645
size_type length() const
Returns the number of bytes stored in SBuf.
Definition: SBuf.h:415
static SBufStats stats
class-wide statistics
Definition: SBuf.h:632
SBuf()
create an empty (zero-size) SBuf
Definition: SBuf.cc:28
size_type rfind(char c, size_type endPos=npos) const
Definition: SBuf.cc:692
SBuf & operator=(const SBuf &S)
Definition: SBuf.h:141
size_type reserve(const SBufReservationRequirements &requirements)
Definition: SBuf.cc:112
int cmp(const SBuf &S, const size_type n) const
shorthand version for compare()
Definition: SBuf.h:275
size_type findFirstNotOf(const CharacterSet &set, size_type startPos=0) const
Definition: SBuf.cc:746
int plength() const
Definition: SBuf.h:422
SBuf & trim(const SBuf &toRemove, bool atBeginning=true, bool atEnd=true)
Definition: SBuf.cc:551
bool operator>=(const SBuf &S) const
Definition: SBuf.h:324
int compare(const SBuf &S, const SBufCaseSensitive isCaseSensitive) const
Definition: SBuf.h:270
size_type spaceSize() const
Definition: SBuf.h:393
size_type find(char c, size_type startPos=0) const
Definition: SBuf.cc:584
int cmp(const SBuf &S) const
Definition: SBuf.h:278
size_type findFirstOf(const CharacterSet &set, size_type startPos=0) const
Definition: SBuf.cc:723
bool operator!=(const SBuf &S) const
Definition: SBuf.cc:475
size_type copy(char *dest, size_type n) const
Definition: SBuf.cc:500
std::string toStdString() const
std::string export function
Definition: SBuf.h:581
bool isEmpty() const
Definition: SBuf.h:431
bool operator==(const SBuf &S) const
Definition: SBuf.cc:455
size_type estimateCapacity(size_type desired) const
Definition: SBuf.h:658
size_type findLastNotOf(const CharacterSet &set, size_type endPos=npos) const
Definition: SBuf.cc:790
char * rawSpace(size_type minSize)
Definition: SBuf.cc:157
bool operator>(const SBuf &S) const
Definition: SBuf.h:322
const_iterator begin() const
Definition: SBuf.h:583
char operator[](size_type pos) const
Definition: SBuf.h:242
std::ostream & dump(std::ostream &os) const
Definition: SBuf.cc:303
void cow(size_type minsize=npos)
Definition: SBuf.cc:878
void checkAccessBounds(const size_type pos) const
Definition: SBuf.h:664
SBuf & append(const SBuf &S)
Definition: SBuf.cc:185
MemBlob::Pointer store_
memory block, possibly shared with other SBufs
Definition: SBuf.h:629
const_reverse_iterator rbegin() const
Definition: SBuf.h:591
SBufIterator const_iterator
Definition: SBuf.h:97
SBufReverseIterator const_reverse_iterator
Definition: SBuf.h:98
int caseCmp(const SBuf &S) const
Definition: SBuf.h:286
int caseCmp(const char *S, const size_type n) const
Shorthand version for case-insensitive C-string compare().
Definition: SBuf.h:305
void reserveSpace(size_type minSpace)
Definition: SBuf.h:440
int caseCmp(const char *S) const
Definition: SBuf.h:308
int compare(const char *s, const SBufCaseSensitive isCaseSensitive) const
Definition: SBuf.h:292
size_type findLastOf(const CharacterSet &set, size_type endPos=npos) const
Definition: SBuf.cc:769
size_type off_
our content start offset from the beginning of shared store_
Definition: SBuf.h:630
const_iterator end() const
Definition: SBuf.h:587
SBuf & Printf(const char *fmt,...)
Definition: SBuf.cc:214
bool startsWith(const SBuf &S, const SBufCaseSensitive isCaseSensitive=caseSensitive) const
Definition: SBuf.cc:442
void clear()
Definition: SBuf.cc:175
SBuf substr(size_type pos, size_type n=npos) const
Definition: SBuf.cc:576
std::ostream & print(std::ostream &os) const
print the SBuf contents to the supplied ostream
Definition: SBuf.cc:295
MemBlob::size_type size_type
Definition: SBuf.h:96
void rawAppendFinish(const char *start, size_type actualSize)
Definition: SBuf.cc:144
SBuf & assign(const SBuf &S)
Definition: SBuf.cc:83
bool operator<=(const SBuf &S) const
Definition: SBuf.h:323
const_reverse_iterator rend() const
Definition: SBuf.h:595
void setAt(size_type pos, char toset)
Definition: SBuf.cc:328
void toLower()
converts all characters to lower case;
Definition: SBuf.cc:811
int compare(const SBuf &S, const SBufCaseSensitive isCaseSensitive, const size_type n) const
Definition: SBuf.cc:352
char * bufEnd() const
Definition: SBuf.h:652
static const size_type maxSize
Maximum size of a SBuf. By design it MUST be < MAX(size_type)/2. Currently 256Mb.
Definition: SBuf.h:102
SBuf & appendf(const char *fmt,...)
Definition: SBuf.cc:229
#define DBG_DATA
Definition: Stream.h:43
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:196
STL namespace.
#define xmalloc
void const char int sz
Definition: stub_cbdata.cc:16
#define INT_MAX
Definition: types.h:76

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors