]>
Commit | Line | Data |
---|---|---|
412da427 | 1 | /* |
bde978a6 | 2 | * Copyright (C) 1996-2015 The Squid Software Foundation and contributors |
412da427 | 3 | * |
bbc27441 AJ |
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. | |
412da427 FC |
7 | */ |
8 | ||
9 | #ifndef SQUID_SBUF_H | |
10 | #define SQUID_SBUF_H | |
11 | ||
12 | #include "base/InstanceId.h" | |
412da427 | 13 | #include "MemBlob.h" |
62842d40 | 14 | #include "SBufExceptions.h" |
412da427 FC |
15 | #include "SquidString.h" |
16 | ||
62842d40 | 17 | #include <climits> |
074d6a40 AJ |
18 | #include <cstdarg> |
19 | #include <iosfwd> | |
412da427 FC |
20 | #if HAVE_UNISTD_H |
21 | #include <unistd.h> | |
22 | #endif | |
412da427 | 23 | |
496a9e97 | 24 | /* SBuf placeholder for printf */ |
412da427 FC |
25 | #ifndef SQUIDSBUFPH |
26 | #define SQUIDSBUFPH "%.*s" | |
27 | #define SQUIDSBUFPRINT(s) (s).plength(),(s).rawContent() | |
28 | #endif /* SQUIDSBUFPH */ | |
29 | ||
6a17a36d | 30 | // TODO: move within SBuf and rename |
412da427 FC |
31 | typedef enum { |
32 | caseSensitive, | |
33 | caseInsensitive | |
34 | } SBufCaseSensitive; | |
35 | ||
36 | /** | |
37 | * Container for various SBuf class-wide statistics. | |
38 | * | |
39 | * The stats are not completely accurate; they're mostly meant to | |
40 | * understand whether Squid is leaking resources | |
41 | * and whether SBuf is paying off the expected gains. | |
42 | */ | |
43 | class SBufStats | |
44 | { | |
45 | public: | |
7cbd3256 FC |
46 | uint64_t alloc; ///<number of calls to SBuf constructors |
47 | uint64_t allocCopy; ///<number of calls to SBuf copy-constructor | |
48 | uint64_t allocFromString; ///<number of copy-allocations from Strings | |
49 | uint64_t allocFromCString; ///<number of copy-allocations from c-strings | |
50 | uint64_t assignFast; ///<number of no-copy assignment operations | |
51 | uint64_t clear; ///<number of clear operations | |
52 | uint64_t append; ///<number of append operations | |
53 | uint64_t toStream; ///<number of write operations to ostreams | |
54 | uint64_t setChar; ///<number of calls to setAt | |
55 | uint64_t getChar; ///<number of calls to at() and operator[] | |
56 | uint64_t compareSlow; ///<number of comparison operations requiring data scan | |
57 | uint64_t compareFast; ///<number of comparison operations not requiring data scan | |
58 | uint64_t copyOut; ///<number of data-copies to other forms of buffers | |
59 | uint64_t rawAccess; ///<number of accesses to raw contents | |
7f175a76 | 60 | uint64_t nulTerminate; ///<number of c_str() terminations |
7cbd3256 FC |
61 | uint64_t chop; ///<number of chop operations |
62 | uint64_t trim; ///<number of trim operations | |
63 | uint64_t find; ///<number of find operations | |
64 | uint64_t scanf; ///<number of scanf operations | |
65 | uint64_t caseChange; ///<number of toUpper and toLower operations | |
66 | uint64_t cowFast; ///<number of cow operations not actually requiring a copy | |
67 | uint64_t cowSlow; ///<number of cow operations requiring a copy | |
68 | uint64_t live; ///<number of currently-allocated SBuf | |
412da427 | 69 | |
496a9e97 | 70 | ///Dump statistics to an ostream. |
412da427 FC |
71 | std::ostream& dump(std::ostream &os) const; |
72 | SBufStats(); | |
73 | ||
74 | SBufStats& operator +=(const SBufStats&); | |
75 | }; | |
76 | ||
4685b0c4 FC |
77 | class CharacterSet; |
78 | ||
412da427 FC |
79 | /** |
80 | * A String or Buffer. | |
81 | * Features: refcounted backing store, cheap copy and sub-stringing | |
82 | * operations, copy-on-write to isolate change operations to each instance. | |
83 | * Where possible, we're trying to mimic std::string's interface. | |
84 | */ | |
85 | class SBuf | |
86 | { | |
87 | public: | |
7cbd3256 | 88 | typedef MemBlob::size_type size_type; |
50827187 | 89 | static const size_type npos = 0xffffffff; // max(uint32_t) |
412da427 FC |
90 | |
91 | /// Maximum size of a SBuf. By design it MUST be < MAX(size_type)/2. Currently 256Mb. | |
92 | static const size_type maxSize = 0xfffffff; | |
93 | ||
94 | /// create an empty (zero-size) SBuf | |
95 | SBuf(); | |
96 | SBuf(const SBuf &S); | |
97 | ||
98 | /** Constructor: import c-style string | |
99 | * | |
100 | * Create a new SBuf containing a COPY of the contents of the | |
101 | * c-string | |
102 | * \param S the c string to be copied | |
7cbd3256 | 103 | * \param n how many bytes to import into the SBuf. If it is npos |
412da427 FC |
104 | * or unspecified, imports to end-of-cstring |
105 | * \note it is the caller's responsibility not to go out of bounds | |
50827187 | 106 | * \note bounds is 0 <= pos < length(); caller must pay attention to signedness |
412da427 | 107 | */ |
7cbd3256 | 108 | explicit SBuf(const char *S, size_type n = npos); |
412da427 FC |
109 | |
110 | /** Constructor: import SquidString, copying contents. | |
111 | * | |
112 | * This method will be removed once SquidString has gone. | |
113 | */ | |
496a9e97 FC |
114 | explicit SBuf(const String &S); |
115 | ||
116 | /// Constructor: import std::string. Contents are copied. | |
117 | explicit SBuf(const std::string &s); | |
412da427 FC |
118 | |
119 | ~SBuf(); | |
62842d40 | 120 | |
412da427 FC |
121 | /** Explicit assignment. |
122 | * | |
123 | * Current SBuf will share backing store with the assigned one. | |
124 | */ | |
125 | SBuf& assign(const SBuf &S); | |
62842d40 | 126 | |
412da427 FC |
127 | /** Assignment operator. |
128 | * | |
129 | * Current SBuf will share backing store with the assigned one. | |
130 | */ | |
62842d40 | 131 | SBuf& operator =(const SBuf & S) {return assign(S);} |
412da427 FC |
132 | |
133 | /** Import a c-string into a SBuf, copying the data. | |
134 | * | |
135 | * It is the caller's duty to free the imported string, if needed. | |
136 | * \param S the c string to be copied | |
7cbd3256 | 137 | * \param n how many bytes to import into the SBuf. If it is npos |
412da427 FC |
138 | * or unspecified, imports to end-of-cstring |
139 | * \note it is the caller's responsibility not to go out of bounds | |
7cbd3256 FC |
140 | * \note to assign a std::string use the pattern: |
141 | * assign(stdstr.data(), stdstd.length()) | |
412da427 | 142 | */ |
7cbd3256 | 143 | SBuf& assign(const char *S, size_type n = npos); |
412da427 FC |
144 | |
145 | /** Assignment operator. Copy a NULL-terminated c-style string into a SBuf. | |
146 | * | |
147 | * Copy a c-style string into a SBuf. Shortcut for SBuf.assign(S) | |
148 | * It is the caller's duty to free the imported string, if needed. | |
7cbd3256 | 149 | * \note not \0-clean |
412da427 | 150 | */ |
62842d40 | 151 | SBuf& operator =(const char *S) {return assign(S);} |
412da427 | 152 | |
412da427 FC |
153 | /** reset the SBuf as if it was just created. |
154 | * | |
155 | * Resets the SBuf to empty, memory is freed lazily. | |
156 | */ | |
157 | void clear(); | |
158 | ||
159 | /** Append operation | |
160 | * | |
161 | * Append the supplied SBuf to the current one; extend storage as needed. | |
162 | */ | |
163 | SBuf& append(const SBuf & S); | |
164 | ||
e7ce227f AR |
165 | /// Append a single character. The character may be NUL (\0). |
166 | SBuf& append(const char c); | |
167 | ||
412da427 FC |
168 | /** Append operation for C-style strings. |
169 | * | |
170 | * Append the supplied c-string to the SBuf; extend storage | |
171 | * as needed. | |
172 | * | |
173 | * \param S the c string to be copied. Can be NULL. | |
a452bc8b FC |
174 | * \param Ssize how many bytes to import into the SBuf. If it is npos |
175 | * or unspecified, imports to end-of-cstring. If S is NULL, | |
176 | * Ssize is ignored. | |
7cbd3256 FC |
177 | * \note to append a std::string use the pattern |
178 | * cstr_append(stdstr.data(), stdstd.length()) | |
412da427 | 179 | */ |
7cbd3256 | 180 | SBuf& append(const char * S, size_type Ssize = npos); |
412da427 FC |
181 | |
182 | /** Assignment operation with printf(3)-style definition | |
183 | * \note arguments may be evaluated more than once, be careful | |
184 | * of side-effects | |
185 | */ | |
186 | SBuf& Printf(const char *fmt, ...); | |
187 | ||
188 | /** Append operation with printf-style arguments | |
189 | * \note arguments may be evaluated more than once, be careful | |
190 | * of side-effects | |
191 | */ | |
192 | SBuf& appendf(const char *fmt, ...); | |
496a9e97 | 193 | |
412da427 FC |
194 | /** Append operation, with vsprintf(3)-style arguments. |
195 | * \note arguments may be evaluated more than once, be careful | |
196 | * of side-effects | |
197 | */ | |
198 | SBuf& vappendf(const char *fmt, va_list vargs); | |
199 | ||
496a9e97 | 200 | /// print the SBuf contents to the supplied ostream |
412da427 FC |
201 | std::ostream& print(std::ostream &os) const; |
202 | ||
496a9e97 | 203 | /** print SBuf contents and debug information about the SBuf to an ostream |
412da427 FC |
204 | * |
205 | * Debug function, dumps to a stream informations on the current SBuf, | |
206 | * including low-level details and statistics. | |
207 | */ | |
208 | std::ostream& dump(std::ostream &os) const; | |
209 | ||
210 | /** random-access read to any char within the SBuf | |
211 | * | |
212 | * does not check access bounds. If you need that, use at() | |
213 | */ | |
fdd1982c | 214 | char operator [](size_type pos) const {++stats.getChar; return store_->mem[off_+pos];} |
412da427 FC |
215 | |
216 | /** random-access read to any char within the SBuf. | |
217 | * | |
218 | * \throw OutOfBoundsException when access is out of bounds | |
50827187 | 219 | * \note bounds is 0 <= pos < length(); caller must pay attention to signedness |
412da427 | 220 | */ |
fdd1982c | 221 | char at(size_type pos) const {checkAccessBounds(pos); return operator[](pos);} |
412da427 FC |
222 | |
223 | /** direct-access set a byte at a specified operation. | |
224 | * | |
225 | * \param pos the position to be overwritten | |
226 | * \param toset the value to be written | |
227 | * \throw OutOfBoundsException when pos is of bounds | |
50827187 | 228 | * \note bounds is 0 <= pos < length(); caller must pay attention to signedness |
412da427 FC |
229 | * \note performs a copy-on-write if needed. |
230 | */ | |
231 | void setAt(size_type pos, char toset); | |
232 | ||
233 | /** compare to other SBuf, str(case)cmp-style | |
234 | * | |
235 | * \param isCaseSensitive one of caseSensitive or caseInsensitive | |
7cbd3256 | 236 | * \param n compare up to this many bytes. if npos (default), compare whole SBufs |
412da427 FC |
237 | * \retval >0 argument of the call is greater than called SBuf |
238 | * \retval <0 argument of the call is smaller than called SBuf | |
239 | * \retval 0 argument of the call has the same contents of called SBuf | |
240 | */ | |
1284f77c | 241 | int compare(const SBuf &S, const SBufCaseSensitive isCaseSensitive, const size_type n = npos) const; |
7cbd3256 | 242 | |
1284f77c AJ |
243 | /// shorthand version for compare() |
244 | inline int cmp(const SBuf &S, const size_type n = npos) const { | |
7cbd3256 FC |
245 | return compare(S,caseSensitive,n); |
246 | } | |
247 | ||
1284f77c AJ |
248 | /// shorthand version for case-insensitive compare() |
249 | inline int caseCmp(const SBuf &S, const size_type n = npos) const { | |
250 | return compare(S,caseInsensitive,n); | |
251 | } | |
252 | ||
253 | /// Comparison with a C-string. | |
254 | int compare(const char *s, const SBufCaseSensitive isCaseSensitive, const size_type n = npos) const; | |
255 | ||
256 | /// Shorthand version for C-string compare(). | |
257 | inline int cmp(const char *S, const size_type n = npos) const { | |
258 | return compare(S,caseSensitive,n); | |
259 | } | |
260 | ||
261 | /// Shorthand version for case-insensitive C-string compare(). | |
262 | inline int caseCmp(const char *S, const size_type n = npos) const { | |
7cbd3256 FC |
263 | return compare(S,caseInsensitive,n); |
264 | } | |
412da427 FC |
265 | |
266 | /** check whether the entire supplied argument is a prefix of the SBuf. | |
267 | * \param S the prefix to match against | |
268 | * \param isCaseSensitive one of caseSensitive or caseInsensitive | |
269 | * \retval true argument is a prefix of the SBuf | |
270 | */ | |
1284f77c | 271 | bool startsWith(const SBuf &S, const SBufCaseSensitive isCaseSensitive = caseSensitive) const; |
412da427 | 272 | |
412da427 FC |
273 | bool operator ==(const SBuf & S) const; |
274 | bool operator !=(const SBuf & S) const; | |
7cbd3256 FC |
275 | bool operator <(const SBuf &S) const {return (cmp(S) < 0);} |
276 | bool operator >(const SBuf &S) const {return (cmp(S) > 0);} | |
277 | bool operator <=(const SBuf &S) const {return (cmp(S) <= 0);} | |
278 | bool operator >=(const SBuf &S) const {return (cmp(S) >= 0);} | |
412da427 FC |
279 | |
280 | /** Consume bytes at the head of the SBuf | |
281 | * | |
282 | * Consume N chars at SBuf head, or to SBuf's end, | |
283 | * whichever is shorter. If more bytes are consumed than available, | |
284 | * the SBuf is emptied | |
285 | * \param n how many bytes to remove; could be zero. | |
7cbd3256 | 286 | * npos (or no argument) means 'to the end of SBuf' |
412da427 FC |
287 | * \return a new SBuf containing the consumed bytes. |
288 | */ | |
289 | SBuf consume(size_type n = npos); | |
290 | ||
496a9e97 | 291 | /// gets global statistic informations |
412da427 FC |
292 | static const SBufStats& GetStats(); |
293 | ||
294 | /** Copy SBuf contents into user-supplied C buffer. | |
295 | * | |
296 | * Export a copy of the SBuf's contents into the user-supplied | |
297 | * buffer, up to the user-supplied-length. No zero-termination is performed | |
298 | * \return num the number of actually-copied chars. | |
299 | */ | |
300 | size_type copy(char *dest, size_type n) const; | |
301 | ||
302 | /** exports a pointer to the SBuf internal storage. | |
303 | * \warning ACCESSING RAW STORAGE IS DANGEROUS! | |
304 | * | |
496a9e97 FC |
305 | * Returns a ead-only pointer to SBuf's content. No terminating null |
306 | * character is appended (use c_str() for that). | |
412da427 FC |
307 | * The returned value points to an internal location whose contents |
308 | * are guaranteed to remain unchanged only until the next call | |
309 | * to a non-constant member function of the SBuf object. Such a | |
310 | * call may be implicit (e.g., when SBuf is destroyed | |
311 | * upon leaving the current context). | |
312 | * This is a very UNSAFE way of accessing the data. | |
313 | * This call never returns NULL. | |
314 | * \see c_str | |
315 | * \note the memory management system guarantees that the exported region | |
316 | * of memory will remain valid if the caller keeps holding | |
317 | * a valid reference to the SBuf object and does not write or append to | |
318 | * it. For example: | |
319 | * \code | |
320 | * SBuf foo("some string"); | |
321 | * const char *bar = foo.rawContent(); | |
322 | * doSomething(bar); //safe | |
323 | * foo.append(" other string"); | |
324 | * doSomething(bar); //unsafe | |
325 | * \endcode | |
326 | */ | |
327 | const char* rawContent() const; | |
328 | ||
329 | /** Exports a writable pointer to the SBuf internal storage. | |
330 | * \warning Use with EXTREME caution, this is a dangerous operation. | |
331 | * | |
332 | * Returns a pointer to the first unused byte in the SBuf's storage, | |
ac7947fd FC |
333 | * which can be be used for appending. At least minSize bytes will |
334 | * be available for writing. | |
335 | * The returned pointer must not be stored by the caller, as it will | |
496a9e97 | 336 | * be invalidated by the first call to a non-const method call |
412da427 | 337 | * on the SBuf. |
ac7947fd | 338 | * This call guarantees to never return NULL. |
8aa34dad FC |
339 | * \see reserveSpace |
340 | * \note Unlike reserveSpace(), this method does not guarantee exclusive | |
341 | * buffer ownership. It is instead optimized for a one writer | |
342 | * (appender), many readers scenario by avoiding unnecessary | |
343 | * copying and allocations. | |
412da427 FC |
344 | * \throw SBufTooBigException if the user tries to allocate too big a SBuf |
345 | */ | |
ac7947fd | 346 | char *rawSpace(size_type minSize); |
412da427 | 347 | |
50827187 | 348 | /** Obtain how much free space is available in the backing store. |
8aa34dad | 349 | * |
50827187 FC |
350 | * \note: unless the client just cow()ed, it is not guaranteed that |
351 | * the free space can be used. | |
8aa34dad FC |
352 | */ |
353 | size_type spaceSize() const { return store_->spaceSize(); } | |
354 | ||
412da427 FC |
355 | /** Force a SBuf's size |
356 | * \warning use with EXTREME caution, this is a dangerous operation | |
357 | * | |
358 | * Adapt the SBuf internal state after external interference | |
359 | * such as writing into it via rawSpace. | |
7cbd3256 FC |
360 | * \throw TextException if SBuf doesn't have exclusive ownership of store |
361 | * \throw SBufTooBigException if new size is bigger than available store space | |
412da427 FC |
362 | */ |
363 | void forceSize(size_type newSize); | |
364 | ||
365 | /** exports a null-terminated reference to the SBuf internal storage. | |
366 | * \warning ACCESSING RAW STORAGE IS DANGEROUS! DO NOT EVER USE | |
367 | * THE RETURNED POINTER FOR WRITING | |
368 | * | |
369 | * The returned value points to an internal location whose contents | |
370 | * are guaranteed to remain unchanged only until the next call | |
371 | * to a non-constant member function of the SBuf object. Such a | |
372 | * call may be implicit (e.g., when SBuf is destroyed | |
373 | * upon leaving the current context). | |
374 | * This is a very UNSAFE way of accessing the data. | |
375 | * This call never returns NULL. | |
376 | * \see rawContent | |
377 | * \note the memory management system guarantees that the exported region | |
496a9e97 FC |
378 | * of memory will remain valid will remain valid only if the |
379 | * caller keeps holding a valid reference to the SBuf object and | |
380 | * does not write or append to it | |
412da427 FC |
381 | */ |
382 | const char* c_str(); | |
383 | ||
496a9e97 | 384 | /// Returns the number of bytes stored in SBuf. |
62842d40 | 385 | size_type length() const {return len_;} |
412da427 FC |
386 | |
387 | /** Get the length of the SBuf, as a signed integer | |
388 | * | |
389 | * Compatibility function for printf(3) which requires a signed int | |
390 | * \throw SBufTooBigException if the SBuf is too big for a signed integer | |
391 | */ | |
62842d40 FC |
392 | int plength() const { |
393 | if (length()>INT_MAX) | |
394 | throw SBufTooBigException(__FILE__, __LINE__); | |
395 | return static_cast<int>(length()); | |
396 | } | |
412da427 FC |
397 | |
398 | /** Check whether the SBuf is empty | |
399 | * | |
400 | * \return true if length() == 0 | |
401 | */ | |
62842d40 | 402 | bool isEmpty() const {return (len_==0);} |
412da427 | 403 | |
8aa34dad | 404 | /** Request to guarantee the SBuf's free store space. |
412da427 FC |
405 | * |
406 | * After the reserveSpace request, the SBuf is guaranteed to have at | |
7cbd3256 | 407 | * least minSpace bytes of unused backing store following the currently |
ac7947fd | 408 | * used portion and single ownership of the backing store. |
412da427 FC |
409 | * \throw SBufTooBigException if the user tries to allocate too big a SBuf |
410 | */ | |
e842ecce | 411 | void reserveSpace(size_type minSpace) { |
6fd0701a | 412 | Must(minSpace <= maxSize); |
a44b8b80 | 413 | Must(length() <= maxSize - minSpace); |
e842ecce FC |
414 | reserveCapacity(length()+minSpace); |
415 | } | |
412da427 | 416 | |
8aa34dad | 417 | /** Request to guarantee the SBuf's store capacity |
412da427 FC |
418 | * |
419 | * After this method is called, the SBuf is guaranteed to have at least | |
7cbd3256 | 420 | * minCapacity bytes of total buffer size, including the currently-used |
577bcebc FC |
421 | * portion; it is also guaranteed that after this call this SBuf |
422 | * has unique ownership of the underlying memory store. | |
412da427 FC |
423 | * \throw SBufTooBigException if the user tries to allocate too big a SBuf |
424 | */ | |
425 | void reserveCapacity(size_type minCapacity); | |
426 | ||
427 | /** slicing method | |
428 | * | |
496a9e97 FC |
429 | * Removes SBuf prefix and suffix, leaving a sequence of 'n' |
430 | * bytes starting from position 'pos', first byte is at pos 0. | |
431 | * It is an in-place-modifying version of substr. | |
412da427 | 432 | * \param pos start sub-stringing from this byte. If it is |
496a9e97 | 433 | * npos or it is greater than the SBuf length, the SBuf is cleared and |
50827187 | 434 | * an empty SBuf is returned. |
412da427 | 435 | * \param n maximum number of bytes of the resulting SBuf. |
7cbd3256 | 436 | * npos means "to end of SBuf". |
496a9e97 | 437 | * if it is 0, the SBuf is cleared and an empty SBuf is returned. |
496a9e97 FC |
438 | * if it overflows the end of the SBuf, it is capped to the end of SBuf |
439 | * \see substr, trim | |
412da427 FC |
440 | */ |
441 | SBuf& chop(size_type pos, size_type n = npos); | |
442 | ||
443 | /** Remove characters in the toremove set at the beginning, end or both | |
444 | * | |
445 | * \param toremove characters to be removed. Stops chomping at the first | |
446 | * found char not in the set | |
447 | * \param atBeginning if true (default), strips at the beginning of the SBuf | |
448 | * \param atEnd if true (default), strips at the end of the SBuf | |
449 | */ | |
450 | SBuf& trim(const SBuf &toRemove, bool atBeginning = true, bool atEnd = true); | |
451 | ||
452 | /** Extract a part of the current SBuf. | |
453 | * | |
496a9e97 FC |
454 | * Return a fresh a fresh copy of a portion the current SBuf, which is |
455 | * left untouched. The same parameter convetions apply as for chop. | |
456 | * \see trim, chop | |
412da427 FC |
457 | */ |
458 | SBuf substr(size_type pos, size_type n = npos) const; | |
459 | ||
460 | /** Find first occurrence of character in SBuf | |
461 | * | |
462 | * Returns the index in the SBuf of the first occurrence of char c. | |
7cbd3256 | 463 | * \return npos if the char was not found |
412da427 | 464 | * \param startPos if specified, ignore any occurrences before that position |
496a9e97 | 465 | * if startPos is npos or greater than length() npos is always returned |
7cbd3256 | 466 | * if startPos is less than zero, it is ignored |
412da427 FC |
467 | */ |
468 | size_type find(char c, size_type startPos = 0) const; | |
469 | ||
470 | /** Find first occurrence of SBuf in SBuf. | |
471 | * | |
472 | * Returns the index in the SBuf of the first occurrence of the | |
473 | * sequence contained in the str argument. | |
474 | * \param startPos if specified, ignore any occurrences before that position | |
496a9e97 | 475 | * if startPos is npos or greater than length() npos is always returned |
7cbd3256 | 476 | * \return npos if the SBuf was not found |
412da427 FC |
477 | */ |
478 | size_type find(const SBuf & str, size_type startPos = 0) const; | |
479 | ||
480 | /** Find last occurrence of character in SBuf | |
481 | * | |
482 | * Returns the index in the SBuf of the last occurrence of char c. | |
7cbd3256 | 483 | * \return npos if the char was not found |
412da427 | 484 | * \param endPos if specified, ignore any occurrences after that position. |
496a9e97 | 485 | * if npos or greater than length(), the whole SBuf is considered |
412da427 FC |
486 | */ |
487 | size_type rfind(char c, size_type endPos = npos) const; | |
488 | ||
489 | /** Find last occurrence of SBuf in SBuf | |
490 | * | |
491 | * Returns the index in the SBuf of the last occurrence of the | |
492 | * sequence contained in the str argument. | |
7cbd3256 | 493 | * \return npos if the sequence was not found |
412da427 | 494 | * \param endPos if specified, ignore any occurrences after that position |
496a9e97 | 495 | * if npos or greater than length(), the whole SBuf is considered |
412da427 FC |
496 | */ |
497 | size_type rfind(const SBuf &str, size_type endPos = npos) const; | |
498 | ||
499 | /** Find first occurrence of character of set in SBuf | |
500 | * | |
501 | * Finds the first occurrence of ANY of the characters in the supplied set in | |
502 | * the SBuf. | |
7cbd3256 | 503 | * \return npos if no character in the set could be found |
412da427 | 504 | * \param startPos if specified, ignore any occurrences before that position |
7cbd3256 | 505 | * if npos, then npos is always returned |
c8046ec7 FC |
506 | * |
507 | * TODO: rename to camelCase | |
412da427 | 508 | */ |
810a5427 | 509 | size_type findFirstOf(const CharacterSet &set, size_type startPos = 0) const; |
3c1106a0 FC |
510 | |
511 | /** Find first occurrence character NOT in character set | |
512 | * | |
c8046ec7 | 513 | * \return npos if all characters in the SBuf are from set |
3c1106a0 FC |
514 | * \param startPos if specified, ignore any occurrences before that position |
515 | * if npos, then npos is always returned | |
c8046ec7 FC |
516 | * |
517 | * TODO: rename to camelCase | |
3c1106a0 | 518 | */ |
810a5427 | 519 | size_type findFirstNotOf(const CharacterSet &set, size_type startPos = 0) const; |
412da427 FC |
520 | |
521 | /** sscanf-alike | |
522 | * | |
523 | * sscanf re-implementation. Non-const, and not \0-clean. | |
524 | * \return same as sscanf | |
525 | * \see man sscanf(3) | |
526 | */ | |
527 | int scanf(const char *format, ...); | |
528 | ||
37cf7314 AR |
529 | /// converts all characters to lower case; \see man tolower(3) |
530 | void toLower(); | |
412da427 | 531 | |
37cf7314 AR |
532 | /// converts all characters to upper case; \see man toupper(3) |
533 | void toUpper(); | |
412da427 FC |
534 | |
535 | /** String export function | |
496a9e97 FC |
536 | * converts the SBuf to a legacy String, by copy. |
537 | * \deprecated | |
412da427 FC |
538 | */ |
539 | String toString() const; | |
540 | ||
7cbd3256 FC |
541 | /// std::string export function |
542 | std::string toStdString() const { return std::string(buf(),length()); } | |
543 | ||
496a9e97 FC |
544 | // TODO: possibly implement erase() similar to std::string's erase |
545 | // TODO: possibly implement a replace() call | |
412da427 FC |
546 | private: |
547 | ||
548 | MemBlob::Pointer store_; ///< memory block, possibly shared with other SBufs | |
549 | size_type off_; ///< our content start offset from the beginning of shared store_ | |
550 | size_type len_; ///< number of our content bytes in shared store_ | |
551 | static SBufStats stats; ///< class-wide statistics | |
552 | ||
7cbd3256 FC |
553 | /// SBuf object identifier; does not change when contents do, |
554 | /// including during assignment | |
555 | const InstanceId<SBuf> id; | |
412da427 | 556 | |
62842d40 FC |
557 | /** obtain prototype store |
558 | * | |
559 | * Just-created SBufs all share to the same MemBlob. | |
560 | * This call instantiates and returns it. | |
561 | */ | |
562 | static MemBlob::Pointer GetStorePrototype(); | |
563 | ||
564 | /** | |
565 | * obtains a char* to the beginning of this SBuf in memory. | |
566 | * \note the obtained string is NOT null-terminated. | |
567 | */ | |
568 | char * buf() const {return (store_->mem+off_);} | |
569 | ||
570 | /** returns the pointer to the first char after this SBuf end | |
571 | * | |
572 | * No checks are made that the space returned is safe, checking that is | |
573 | * up to the caller. | |
574 | */ | |
575 | char * bufEnd() const {return (store_->mem+off_+len_);} | |
576 | ||
577 | /** | |
578 | * Try to guesstimate how big a MemBlob to allocate. | |
579 | * The result is guarranteed to be to be at least the desired size. | |
580 | */ | |
fdd1982c | 581 | size_type estimateCapacity(size_type desired) const {return (2*desired);} |
412da427 | 582 | |
412da427 FC |
583 | void reAlloc(size_type newsize); |
584 | ||
a452bc8b | 585 | void cow(size_type minsize = npos); |
412da427 FC |
586 | |
587 | void checkAccessBounds(size_type pos) const; | |
62842d40 | 588 | |
7cbd3256 FC |
589 | /** Low-level append operation |
590 | * | |
591 | * Takes as input a contiguous area of memory and appends its contents | |
592 | * to the SBuf, taking care of memory management. Does no bounds checking | |
593 | * on the supplied memory buffer, it is the duty of the caller to ensure | |
594 | * that the supplied area is valid. | |
595 | */ | |
596 | SBuf& lowAppend(const char * memArea, size_type areaSize); | |
412da427 FC |
597 | }; |
598 | ||
496a9e97 | 599 | /// ostream output operator |
62842d40 FC |
600 | inline std::ostream & |
601 | operator <<(std::ostream& os, const SBuf& S) | |
602 | { | |
603 | return S.print(os); | |
604 | } | |
412da427 | 605 | |
37cf7314 AR |
606 | /// Returns a lower-cased copy of its parameter. |
607 | inline SBuf | |
608 | ToUpper(SBuf buf) | |
609 | { | |
610 | buf.toUpper(); | |
611 | return buf; | |
612 | } | |
613 | ||
614 | /// Returns an upper-cased copy of its parameter. | |
615 | inline SBuf | |
616 | ToLower(SBuf buf) | |
617 | { | |
618 | buf.toLower(); | |
619 | return buf; | |
620 | } | |
621 | ||
412da427 | 622 | #endif /* SQUID_SBUF_H */ |
f53969cc | 623 |