]> git.ipfire.org Git - thirdparty/squid.git/blob - src/SBuf.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / SBuf.cc
1 /*
2 * Copyright (C) 1996-2014 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 #include "squid.h"
10 #include "base/CharacterSet.h"
11 #include "base/RefCount.h"
12 #include "Debug.h"
13 #include "OutOfBoundsException.h"
14 #include "SBuf.h"
15 #include "SBufDetailedStats.h"
16 #include "SBufExceptions.h"
17 #include "util.h"
18
19 #include <cstring>
20 #include <iostream>
21 #include <sstream>
22
23 #ifdef VA_COPY
24 #undef VA_COPY
25 #endif
26 #if defined HAVE_VA_COPY
27 #define VA_COPY va_copy
28 #elif defined HAVE___VA_COPY
29 #define VA_COPY __va_copy
30 #endif
31
32 InstanceIdDefinitions(SBuf, "SBuf");
33
34 SBufStats SBuf::stats;
35 const SBuf::size_type SBuf::npos;
36 const SBuf::size_type SBuf::maxSize;
37
38 SBufStats::SBufStats()
39 : alloc(0), allocCopy(0), allocFromString(0), allocFromCString(0),
40 assignFast(0), clear(0), append(0), toStream(0), setChar(0),
41 getChar(0), compareSlow(0), compareFast(0), copyOut(0),
42 rawAccess(0), nulTerminate(0), chop(0), trim(0), find(0), scanf(0),
43 caseChange(0), cowFast(0), cowSlow(0), live(0)
44 {}
45
46 SBufStats&
47 SBufStats::operator +=(const SBufStats& ss)
48 {
49 alloc += ss.alloc;
50 allocCopy += ss.allocCopy;
51 allocFromString += ss.allocFromString;
52 allocFromCString += ss.allocFromCString;
53 assignFast += ss.assignFast;
54 clear += ss.clear;
55 append += ss.append;
56 toStream += ss.toStream;
57 setChar += ss.setChar;
58 getChar += ss.getChar;
59 compareSlow += ss.compareSlow;
60 compareFast += ss.compareFast;
61 copyOut += ss.copyOut;
62 rawAccess += ss.rawAccess;
63 nulTerminate += ss.nulTerminate;
64 chop += ss.chop;
65 trim += ss.trim;
66 find += ss.find;
67 scanf += ss.scanf;
68 caseChange += ss.caseChange;
69 cowFast += ss.cowFast;
70 cowSlow += ss.cowSlow;
71 live += ss.live;
72
73 return *this;
74 }
75
76 SBuf::SBuf()
77 : store_(GetStorePrototype()), off_(0), len_(0)
78 {
79 debugs(24, 8, id << " created");
80 ++stats.alloc;
81 ++stats.live;
82 }
83
84 SBuf::SBuf(const SBuf &S)
85 : store_(S.store_), off_(S.off_), len_(S.len_)
86 {
87 debugs(24, 8, id << " created from id " << S.id);
88 ++stats.alloc;
89 ++stats.allocCopy;
90 ++stats.live;
91 }
92
93 SBuf::SBuf(const String &S)
94 : store_(GetStorePrototype()), off_(0), len_(0)
95 {
96 debugs(24, 8, id << " created from string");
97 assign(S.rawBuf(), S.size());
98 ++stats.alloc;
99 ++stats.allocFromString;
100 ++stats.live;
101 }
102
103 SBuf::SBuf(const std::string &s)
104 : store_(GetStorePrototype()), off_(0), len_(0)
105 {
106 debugs(24, 8, id << " created from std::string");
107 lowAppend(s.data(),s.length());
108 ++stats.alloc;
109 ++stats.allocFromString;
110 ++stats.live;
111 }
112
113 SBuf::SBuf(const char *S, size_type n)
114 : store_(GetStorePrototype()), off_(0), len_(0)
115 {
116 append(S,n);
117 ++stats.alloc;
118 ++stats.allocFromCString;
119 ++stats.live;
120 }
121
122 SBuf::~SBuf()
123 {
124 debugs(24, 8, id << " destructed");
125 --stats.live;
126 recordSBufSizeAtDestruct(len_);
127 }
128
129 MemBlob::Pointer
130 SBuf::GetStorePrototype()
131 {
132 static MemBlob::Pointer InitialStore = new MemBlob(0);
133 return InitialStore;
134 }
135
136 SBuf&
137 SBuf::assign(const SBuf &S)
138 {
139 debugs(24, 7, "assigning " << id << " from " << S.id);
140 if (&S == this) //assignment to self. Noop.
141 return *this;
142 ++stats.assignFast;
143 store_ = S.store_;
144 off_ = S.off_;
145 len_ = S.len_;
146 return *this;
147 }
148
149 SBuf&
150 SBuf::assign(const char *S, size_type n)
151 {
152 debugs(24, 6, id << " from c-string, n=" << n << ")");
153 clear();
154 return append(S, n); //bounds checked in append()
155 }
156
157 void
158 SBuf::reserveCapacity(size_type minCapacity)
159 {
160 Must(minCapacity <= maxSize);
161 cow(minCapacity);
162 }
163
164 char *
165 SBuf::rawSpace(size_type minSpace)
166 {
167 Must(length() <= maxSize - minSpace);
168 debugs(24, 7, "reserving " << minSpace << " for " << id);
169 ++stats.rawAccess;
170 // we're not concerned about RefCounts here,
171 // the store knows the last-used portion. If
172 // it's available, we're effectively claiming ownership
173 // of it. If it's not, we need to go away (realloc)
174 if (store_->canAppend(off_+len_, minSpace)) {
175 debugs(24, 7, "not growing");
176 return bufEnd();
177 }
178 // TODO: we may try to memmove before realloc'ing in order to avoid
179 // one allocation operation, if we're the sole owners of a MemBlob.
180 // Maybe some heuristic on off_ and length()?
181 cow(minSpace+length());
182 return bufEnd();
183 }
184
185 void
186 SBuf::clear()
187 {
188 #if 0
189 //enabling this code path, the store will be freed and reinitialized
190 store_ = GetStorePrototype(); //uncomment to actually free storage upon clear()
191 #else
192 //enabling this code path, we try to release the store without deallocating it.
193 // will be lazily reallocated if needed.
194 if (store_->LockCount() == 1)
195 store_->clear();
196 #endif
197 len_ = 0;
198 off_ = 0;
199 ++stats.clear;
200 }
201
202 SBuf&
203 SBuf::append(const SBuf &S)
204 {
205 return lowAppend(S.buf(), S.length());
206 }
207
208 SBuf &
209 SBuf::append(const char * S, size_type Ssize)
210 {
211 if (S == NULL)
212 return *this;
213 if (Ssize == SBuf::npos)
214 Ssize = strlen(S);
215 debugs(24, 7, "from c-string to id " << id);
216 // coverity[access_dbuff_in_call]
217 return lowAppend(S, Ssize);
218 }
219
220 SBuf &
221 SBuf::append(const char c)
222 {
223 return lowAppend(&c, 1);
224 }
225
226 SBuf&
227 SBuf::Printf(const char *fmt, ...)
228 {
229 va_list args;
230 va_start(args, fmt);
231 clear();
232 vappendf(fmt, args);
233 va_end(args);
234 return *this;
235 }
236
237 SBuf&
238 SBuf::appendf(const char *fmt, ...)
239 {
240 va_list args;
241 va_start(args, fmt);
242 vappendf(fmt, args);
243 va_end(args);
244 return *this;
245 }
246
247 SBuf&
248 SBuf::vappendf(const char *fmt, va_list vargs)
249 {
250 Must(fmt != NULL);
251 int sz = 0;
252 //reserve twice the format-string size, it's a likely heuristic
253 size_type requiredSpaceEstimate = strlen(fmt)*2;
254
255 char *space = rawSpace(requiredSpaceEstimate);
256 #ifdef VA_COPY
257 va_list ap;
258 VA_COPY(ap, vargs);
259 sz = vsnprintf(space, spaceSize(), fmt, ap);
260 va_end(ap);
261 #else
262 sz = vsnprintf(space, spaceSize(), fmt, vargs);
263 #endif
264
265 /* check for possible overflow */
266 /* snprintf on Linux returns -1 on output errors, or the size
267 * that would have been written if enough space had been available */
268 /* vsnprintf is standard in C99 */
269
270 if (sz >= static_cast<int>(spaceSize())) {
271 // not enough space on the first go, we now know how much we need
272 requiredSpaceEstimate = sz*2; // TODO: tune heuristics
273 space = rawSpace(requiredSpaceEstimate);
274 sz = vsnprintf(space, spaceSize(), fmt, vargs);
275 if (sz < 0) // output error in vsnprintf
276 throw TextException("output error in second-go vsnprintf",__FILE__,
277 __LINE__);
278 }
279
280 if (sz < 0) // output error in either vsnprintf
281 throw TextException("output error in vsnprintf",__FILE__, __LINE__);
282
283 // data was appended, update internal state
284 len_ += sz;
285
286 /* C99 specifies that the final '\0' is not counted in vsnprintf's
287 * return value. Older compilers/libraries might instead count it */
288 /* check whether '\0' was appended and counted */
289 static bool snPrintfTerminatorChecked = false;
290 static bool snPrintfTerminatorCounted = false;
291 if (!snPrintfTerminatorChecked) {
292 char testbuf[16];
293 snPrintfTerminatorCounted = snprintf(testbuf, sizeof(testbuf),
294 "%s", "1") == 2;
295 snPrintfTerminatorChecked = true;
296 }
297 if (snPrintfTerminatorCounted) {
298 --sz;
299 --len_;
300 }
301
302 store_->size += sz;
303 ++stats.append;
304
305 return *this;
306 }
307
308 std::ostream&
309 SBuf::print(std::ostream &os) const
310 {
311 os.write(buf(), length());
312 ++stats.toStream;
313 return os;
314 }
315
316 std::ostream&
317 SBuf::dump(std::ostream &os) const
318 {
319 os << id
320 << ": ";
321 store_->dump(os);
322 os << ", offset:" << off_
323 << ", len:" << len_
324 << ") : '";
325 print(os);
326 os << '\'' << std::endl;
327 return os;
328 # if 0
329 // alternate implementation, based on Raw() API.
330 os << Raw("SBuf", buf(), length()) <<
331 ". id: " << id <<
332 ", offset:" << off_ <<
333 ", len:" << len_ <<
334 ", store: ";
335 store_->dump(os);
336 os << std::endl;
337 return os;
338 #endif
339 }
340
341 void
342 SBuf::setAt(size_type pos, char toset)
343 {
344 checkAccessBounds(pos);
345 cow();
346 store_->mem[off_+pos] = toset;
347 ++stats.setChar;
348 }
349
350 static int
351 memcasecmp(const char *b1, const char *b2, SBuf::size_type len)
352 {
353 int rv=0;
354 while (len > 0) {
355 rv = tolower(*b1)-tolower(*b2);
356 if (rv != 0)
357 return rv;
358 ++b1;
359 ++b2;
360 --len;
361 }
362 return rv;
363 }
364
365 int
366 SBuf::compare(const SBuf &S, const SBufCaseSensitive isCaseSensitive, const size_type n) const
367 {
368 if (n != npos)
369 return substr(0,n).compare(S.substr(0,n),isCaseSensitive);
370
371 const size_type byteCompareLen = min(S.length(), length());
372 ++stats.compareSlow;
373 int rv = 0;
374 if (isCaseSensitive == caseSensitive) {
375 rv = memcmp(buf(), S.buf(), byteCompareLen);
376 } else {
377 rv = memcasecmp(buf(), S.buf(), byteCompareLen);
378 }
379 if (rv != 0)
380 return rv;
381 if (length() == S.length())
382 return 0;
383 if (length() > S.length())
384 return 1;
385 return -1;
386 }
387
388 int
389 SBuf::compare(const char *s, const SBufCaseSensitive isCaseSensitive, const size_type n) const
390 {
391 // 0-length comparison is always true regardless of buffer states
392 if (!n) {
393 ++stats.compareFast;
394 return 0;
395 }
396
397 // N-length compare MUST provide a non-NULL C-string pointer
398 assert(s);
399
400 // when this is a 0-length string, no need for any complexity.
401 if (!length()) {
402 ++stats.compareFast;
403 return '\0' - *s;
404 }
405
406 // brute-force scan in order to avoid ever needing strlen() on a c-string.
407 ++stats.compareSlow;
408 const char *left = buf();
409 const char *right = s;
410 int rv = 0;
411 // what area to scan.
412 // n may be npos, but we treat that as a huge positive value
413 size_type byteCount = min(length(), n);
414
415 // loop until we find a difference, a '\0', or reach the end of area to scan
416 if (isCaseSensitive == caseSensitive) {
417 while ((rv = *left - *right++) == 0) {
418 if (*left++ == '\0' || --byteCount == 0)
419 break;
420 }
421 } else {
422 while ((rv = tolower(*left) - tolower(*right++)) == 0) {
423 if (*left++ == '\0' || --byteCount == 0)
424 break;
425 }
426 }
427
428 // If we stopped scanning because we reached the end
429 // of buf() before we reached the end of s,
430 // pretend we have a 0-terminator there to compare.
431 // NP: the loop already incremented "right" ready for this comparison
432 if (!byteCount && length() < n)
433 return '\0' - *right;
434
435 // If we found a difference within the scan area,
436 // or we found a '\0',
437 // or all n characters were identical (and none was \0).
438 return rv;
439 }
440
441 bool
442 SBuf::startsWith(const SBuf &S, const SBufCaseSensitive isCaseSensitive) const
443 {
444 debugs(24, 8, id << " startsWith " << S.id << ", caseSensitive: " <<
445 isCaseSensitive);
446 if (length() < S.length()) {
447 debugs(24, 8, "no, too short");
448 ++stats.compareFast;
449 return false;
450 }
451 return (compare(S, isCaseSensitive, S.length()) == 0);
452 }
453
454 bool
455 SBuf::operator ==(const SBuf & S) const
456 {
457 debugs(24, 8, id << " == " << S.id);
458 if (length() != S.length()) {
459 debugs(24, 8, "no, different lengths");
460 ++stats.compareFast;
461 return false; //shortcut: must be equal length
462 }
463 if (store_ == S.store_ && off_ == S.off_) {
464 debugs(24, 8, "yes, same length and backing store");
465 ++stats.compareFast;
466 return true; //shortcut: same store, offset and length
467 }
468 ++stats.compareSlow;
469 const bool rv = (0 == memcmp(buf(), S.buf(), length()));
470 debugs(24, 8, "returning " << rv);
471 return rv;
472 }
473
474 bool
475 SBuf::operator !=(const SBuf & S) const
476 {
477 return !(*this == S);
478 }
479
480 SBuf
481 SBuf::consume(size_type n)
482 {
483 if (n == npos)
484 n = length();
485 else
486 n = min(n, length());
487 debugs(24, 8, "consume " << n);
488 SBuf rv(substr(0, n));
489 chop(n);
490 return rv;
491 }
492
493 const
494 SBufStats& SBuf::GetStats()
495 {
496 return stats;
497 }
498
499 SBuf::size_type
500 SBuf::copy(char *dest, size_type n) const
501 {
502 size_type toexport = min(n,length());
503 memcpy(dest, buf(), toexport);
504 ++stats.copyOut;
505 return toexport;
506 }
507
508 const char*
509 SBuf::rawContent() const
510 {
511 ++stats.rawAccess;
512 return buf();
513 }
514
515 void
516 SBuf::forceSize(size_type newSize)
517 {
518 Must(store_->LockCount() == 1);
519 if (newSize > min(maxSize,store_->capacity-off_))
520 throw SBufTooBigException(__FILE__,__LINE__);
521 len_ = newSize;
522 store_->size = newSize;
523 }
524
525 const char*
526 SBuf::c_str()
527 {
528 ++stats.rawAccess;
529 /* null-terminate the current buffer, by hand-appending a \0 at its tail but
530 * without increasing its length. May COW, the side-effect is to guarantee that
531 * the MemBlob's tail is availabe for us to use */
532 *rawSpace(1) = '\0';
533 ++store_->size;
534 ++stats.setChar;
535 ++stats.nulTerminate;
536 return buf();
537 }
538
539 SBuf&
540 SBuf::chop(size_type pos, size_type n)
541 {
542 if (pos == npos || pos > length() || n == 0) {
543 clear();
544 return *this;
545 }
546 if (n == npos || (pos+n) > length())
547 n = length()-pos;
548 ++stats.chop;
549 off_ += pos;
550 len_ = n;
551 return *this;
552 }
553
554 SBuf&
555 SBuf::trim(const SBuf &toRemove, bool atBeginning, bool atEnd)
556 {
557 ++stats.trim;
558 if (atEnd) {
559 const char *p = bufEnd()-1;
560 while (!isEmpty() && memchr(toRemove.buf(), *p, toRemove.length()) != NULL) {
561 //current end-of-buf is in the searched set
562 --len_;
563 --p;
564 }
565 }
566 if (atBeginning) {
567 const char *p = buf();
568 while (!isEmpty() && memchr(toRemove.buf(), *p, toRemove.length()) != NULL) {
569 --len_;
570 ++off_;
571 ++p;
572 }
573 }
574 if (isEmpty())
575 clear();
576 return *this;
577 }
578
579 SBuf
580 SBuf::substr(size_type pos, size_type n) const
581 {
582 SBuf rv(*this);
583 rv.chop(pos, n); //stats handled by callee
584 return rv;
585 }
586
587 SBuf::size_type
588 SBuf::find(char c, size_type startPos) const
589 {
590 ++stats.find;
591
592 if (startPos == npos) // can't find anything if we look past end of SBuf
593 return npos;
594
595 // std::string returns npos if needle is outside hay
596 if (startPos > length())
597 return npos;
598
599 const void *i = memchr(buf()+startPos, (int)c, (size_type)length()-startPos);
600
601 if (i == NULL)
602 return npos;
603
604 return (static_cast<const char *>(i)-buf());
605 }
606
607 SBuf::size_type
608 SBuf::find(const SBuf &needle, size_type startPos) const
609 {
610 if (startPos == npos) { // can't find anything if we look past end of SBuf
611 ++stats.find;
612 return npos;
613 }
614
615 // std::string allows needle to overhang hay but not start outside
616 if (startPos > length()) {
617 ++stats.find;
618 return npos;
619 }
620
621 // for empty needle std::string returns startPos
622 if (needle.length() == 0) {
623 ++stats.find;
624 return startPos;
625 }
626
627 // if needle length is 1 use the char search
628 if (needle.length() == 1)
629 return find(needle[0], startPos);
630
631 ++stats.find;
632
633 char *begin = buf()+startPos;
634 char *lastPossible = buf()+length()-needle.length()+1;
635 char needleBegin = needle[0];
636
637 debugs(24, 7, "looking for " << needle << "starting at " << startPos <<
638 " in id " << id);
639 while (begin < lastPossible) {
640 char *tmp;
641 debugs(24, 8, " begin=" << (void *) begin <<
642 ", lastPossible=" << (void*) lastPossible );
643 tmp = static_cast<char *>(memchr(begin, needleBegin, lastPossible-begin));
644 if (tmp == NULL) {
645 debugs(24, 8 , "First byte not found");
646 return npos;
647 }
648 // lastPossible guarrantees no out-of-bounds with memcmp()
649 if (0 == memcmp(needle.buf(), tmp, needle.length())) {
650 debugs(24, 8, "Found at " << (tmp-buf()));
651 return (tmp-buf());
652 }
653 begin = tmp+1;
654 }
655 debugs(24, 8, "not found");
656 return npos;
657 }
658
659 SBuf::size_type
660 SBuf::rfind(const SBuf &needle, SBuf::size_type endPos) const
661 {
662 // when the needle is 1 char, use the 1-char rfind()
663 if (needle.length() == 1)
664 return rfind(needle[0], endPos);
665
666 ++stats.find;
667
668 // needle is bigger than haystack, impossible find
669 if (length() < needle.length())
670 return npos;
671
672 // if startPos is npos, std::string scans from the end of hay
673 if (endPos == npos || endPos > length()-needle.length())
674 endPos = length()-needle.length();
675
676 // an empty needle found at the end of the haystack
677 if (needle.length() == 0)
678 return endPos;
679
680 char *bufBegin = buf();
681 char *cur = bufBegin+endPos;
682 const char needleBegin = needle[0];
683 while (cur >= bufBegin) {
684 if (*cur == needleBegin) {
685 if (0 == memcmp(needle.buf(), cur, needle.length())) {
686 // found
687 return (cur-buf());
688 }
689 }
690 --cur;
691 }
692 return npos;
693 }
694
695 SBuf::size_type
696 SBuf::rfind(char c, SBuf::size_type endPos) const
697 {
698 ++stats.find;
699
700 // shortcut: haystack is empty, can't find anything by definition
701 if (length() == 0)
702 return npos;
703
704 // on npos input std::string compares last octet of hay
705 if (endPos == npos || endPos >= length()) {
706 endPos = length();
707 } else {
708 // NP: off-by-one weirdness:
709 // endPos is an offset ... 0-based
710 // length() is a count ... 1-based
711 // memrhr() requires a 1-based count of space to scan.
712 ++endPos;
713 }
714
715 if (length() == 0)
716 return endPos;
717
718 const void *i = memrchr(buf(), (int)c, (size_type)endPos);
719
720 if (i == NULL)
721 return npos;
722
723 return (static_cast<const char *>(i)-buf());
724 }
725
726 SBuf::size_type
727 SBuf::findFirstOf(const CharacterSet &set, size_type startPos) const
728 {
729 ++stats.find;
730
731 if (startPos == npos)
732 return npos;
733
734 if (startPos >= length())
735 return npos;
736
737 debugs(24, 7, "first of characterset " << set.name << " in id " << id);
738 char *cur = buf()+startPos;
739 const char *end = bufEnd();
740 while (cur < end) {
741 if (set[*cur])
742 return cur-buf();
743 ++cur;
744 }
745 debugs(24, 7, "not found");
746 return npos;
747 }
748
749 SBuf::size_type
750 SBuf::findFirstNotOf(const CharacterSet &set, size_type startPos) const
751 {
752 ++stats.find;
753
754 if (startPos == npos)
755 return npos;
756
757 if (startPos >= length())
758 return npos;
759
760 debugs(24, 7, "first not of characterset " << set.name << " in id " << id);
761 char *cur = buf()+startPos;
762 const char *end = bufEnd();
763 while (cur < end) {
764 if (!set[*cur])
765 return cur-buf();
766 ++cur;
767 }
768 debugs(24, 7, "not found");
769 return npos;
770 }
771
772 /*
773 * TODO: borrow a sscanf implementation from Linux or similar?
774 * we'd really need a vsnscanf(3)... ? As an alternative, a
775 * light-regexp-like domain-specific syntax might be an idea.
776 */
777 int
778 SBuf::scanf(const char *format, ...)
779 {
780 va_list arg;
781 int rv;
782 ++stats.scanf;
783 va_start(arg, format);
784 rv = vsscanf(c_str(), format, arg);
785 va_end(arg);
786 return rv;
787 }
788
789 std::ostream &
790 SBufStats::dump(std::ostream& os) const
791 {
792 MemBlobStats ststats = MemBlob::GetStats();
793 os <<
794 "SBuf stats:\nnumber of allocations: " << alloc <<
795 "\ncopy-allocations: " << allocCopy <<
796 "\ncopy-allocations from SquidString: " << allocFromString <<
797 "\ncopy-allocations from C String: " << allocFromCString <<
798 "\nlive references: " << live <<
799 "\nno-copy assignments: " << assignFast <<
800 "\nclearing operations: " << clear <<
801 "\nappend operations: " << append <<
802 "\ndump-to-ostream: " << toStream <<
803 "\nset-char: " << setChar <<
804 "\nget-char: " << getChar <<
805 "\ncomparisons with data-scan: " << compareSlow <<
806 "\ncomparisons not requiring data-scan: " << compareFast <<
807 "\ncopy-out ops: " << copyOut <<
808 "\nraw access to memory: " << rawAccess <<
809 "\nNULL terminate C string: " << nulTerminate <<
810 "\nchop operations: " << chop <<
811 "\ntrim operations: " << trim <<
812 "\nfind: " << find <<
813 "\nscanf: " << scanf <<
814 "\ncase-change ops: " << caseChange <<
815 "\nCOW not actually requiring a copy: " << cowFast <<
816 "\nCOW: " << cowSlow <<
817 "\naverage store share factor: " <<
818 (ststats.live != 0 ? static_cast<float>(live)/ststats.live : 0) <<
819 std::endl;
820 return os;
821 }
822
823 void
824 SBuf::toLower()
825 {
826 debugs(24, 8, "\"" << *this << "\"");
827 for (size_type j = 0; j < length(); ++j) {
828 const int c = (*this)[j];
829 if (isupper(c))
830 setAt(j, tolower(c));
831 }
832 debugs(24, 8, "result: \"" << *this << "\"");
833 ++stats.caseChange;
834 }
835
836 void
837 SBuf::toUpper()
838 {
839 debugs(24, 8, "\"" << *this << "\"");
840 for (size_type j = 0; j < length(); ++j) {
841 const int c = (*this)[j];
842 if (islower(c))
843 setAt(j, toupper(c));
844 }
845 debugs(24, 8, "result: \"" << *this << "\"");
846 ++stats.caseChange;
847 }
848
849 /**
850 * checks whether the requested 'pos' is within the bounds of the SBuf
851 * \throw OutOfBoundsException if access is out of bounds
852 */
853 void
854 SBuf::checkAccessBounds(size_type pos) const
855 {
856 if (pos >= length())
857 throw OutOfBoundsException(*this, pos, __FILE__, __LINE__);
858 }
859
860 String
861 SBuf::toString() const
862 {
863 String rv;
864 rv.limitInit(buf(), length());
865 ++stats.copyOut;
866 return rv;
867 }
868
869 /** re-allocate the backing store of the SBuf.
870 *
871 * If there are contents in the SBuf, they will be copied over.
872 * NO verifications are made on the size parameters, it's up to the caller to
873 * make sure that the new size is big enough to hold the copied contents.
874 * The re-allocated storage MAY be bigger than the requested size due to size-chunking
875 * algorithms in MemBlock, it is guarranteed NOT to be smaller.
876 */
877 void
878 SBuf::reAlloc(size_type newsize)
879 {
880 debugs(24, 8, "new size: " << newsize);
881 if (newsize > maxSize)
882 throw SBufTooBigException(__FILE__, __LINE__);
883 MemBlob::Pointer newbuf = new MemBlob(newsize);
884 if (length() > 0)
885 newbuf->append(buf(), length());
886 store_ = newbuf;
887 off_ = 0;
888 ++stats.cowSlow;
889 debugs(24, 7, "new store capacity: " << store_->capacity);
890 }
891
892 SBuf&
893 SBuf::lowAppend(const char * memArea, size_type areaSize)
894 {
895 rawSpace(areaSize); //called method also checks n <= maxSize()
896 store_->append(memArea, areaSize);
897 len_ += areaSize;
898 ++stats.append;
899 return *this;
900 }
901
902 /**
903 * copy-on-write: make sure that we are the only holder of the backing store.
904 * If not, reallocate. If a new size is specified, and it is greater than the
905 * current length, the backing store will be extended as needed
906 */
907 void
908 SBuf::cow(SBuf::size_type newsize)
909 {
910 debugs(24, 8, "new size:" << newsize);
911 if (newsize == npos || newsize < length())
912 newsize = length();
913
914 if (store_->LockCount() == 1 && newsize == length()) {
915 debugs(24, 8, "no cow needed");
916 ++stats.cowFast;
917 return;
918 }
919 reAlloc(newsize);
920 }
921