1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
|
/*
* DEBUG: section 67 String
* AUTHOR: Duane Wessels
*
* SQUID Web Proxy Cache http://www.squid-cache.org/
* ----------------------------------------------------------
*
* Squid is the result of efforts by numerous individuals from
* the Internet community; see the CONTRIBUTORS file for full
* details. Many organizations have provided support for Squid's
* development; see the SPONSORS file for full details. Squid is
* Copyrighted (C) 2001 by the Regents of the University of
* California; see the COPYRIGHT file for full details. Squid
* incorporates software developed and/or copyrighted by other
* sources; see the CREDITS file for full details.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
*
*/
#include "squid.h"
#include "base/TextException.h"
#include "Mem.h"
#include "mgr/Registration.h"
#include "profiler/Profiler.h"
#include "Store.h"
#if HAVE_LIMITS_H
#include <limits.h>
#endif
int
String::psize() const
{
Must(size() < INT_MAX);
return size();
}
// low-level buffer allocation,
// does not free old buffer and does not adjust or look at len_
void
String::allocBuffer(String::size_type sz)
{
PROF_start(StringInitBuf);
assert (undefined());
char *newBuffer = (char*)memAllocString(sz, &sz);
setBuffer(newBuffer, sz);
PROF_stop(StringInitBuf);
}
// low-level buffer assignment
// does not free old buffer and does not adjust or look at len_
void
String::setBuffer(char *aBuf, String::size_type aSize)
{
assert(undefined());
assert(aSize < 65536);
buf_ = aBuf;
size_ = aSize;
}
String::String(char const *aString) : size_(0), len_(0), buf_(NULL)
{
if (aString)
allocAndFill(aString, strlen(aString));
#if DEBUGSTRINGS
StringRegistry::Instance().add(this);
#endif
}
String &
String::operator =(char const *aString)
{
reset(aString);
return *this;
}
String &
String::operator =(String const &old)
{
clean(); // TODO: optimize to avoid cleaning the buffer we can use
if (old.size() > 0)
allocAndFill(old.rawBuf(), old.size());
return *this;
}
bool
String::operator ==(String const &that) const
{
if (0 == this->cmp(that))
return true;
return false;
}
bool
String::operator !=(String const &that) const
{
if (0 == this->cmp(that))
return false;
return true;
}
// public interface, makes sure that we clean the old buffer first
void
String::limitInit(const char *str, int len)
{
clean(); // TODO: optimize to avoid cleaning the buffer we can use
allocAndFill(str, len);
}
// Allocates the buffer to fit the supplied string and fills it.
// Does not clean.
void
String::allocAndFill(const char *str, int len)
{
PROF_start(StringAllocAndFill);
assert(this && str);
allocBuffer(len + 1);
len_ = len;
memcpy(buf_, str, len);
buf_[len] = '\0';
PROF_stop(StringAllocAndFill);
}
String::String(String const &old) : size_(0), len_(0), buf_(NULL)
{
if (old.size() > 0)
allocAndFill(old.rawBuf(), old.size());
#if DEBUGSTRINGS
StringRegistry::Instance().add(this);
#endif
}
void
String::clean()
{
PROF_start(StringClean);
assert(this);
/* TODO if mempools has already closed this will FAIL!! */
if (defined())
memFreeString(size_, buf_);
len_ = 0;
size_ = 0;
buf_ = NULL;
PROF_stop(StringClean);
}
String::~String()
{
clean();
#if DEBUGSTRINGS
StringRegistry::Instance().remove(this);
#endif
}
void
String::reset(char const *str)
{
PROF_start(StringReset);
clean(); // TODO: optimize to avoid cleaning the buffer if we can reuse it
if (str)
allocAndFill(str, strlen(str));
PROF_stop(StringReset);
}
void
String::append( char const *str, int len)
{
assert(this);
assert(str && len >= 0);
PROF_start(StringAppend);
if (len_ + len < size_) {
strncat(buf_, str, len);
len_ += len;
} else {
// Create a temporary string and absorb it later.
String snew;
assert(len_ + len < 65536); // otherwise snew.len_ overflows below
snew.len_ = len_ + len;
snew.allocBuffer(snew.len_ + 1);
if (len_)
memcpy(snew.buf_, rawBuf(), len_);
if (len)
memcpy(snew.buf_ + len_, str, len);
snew.buf_[snew.len_] = '\0';
absorb(snew);
}
PROF_stop(StringAppend);
}
void
String::append(char const *str)
{
assert(str);
append(str, strlen(str));
}
void
String::append(char const chr)
{
char myString[2];
myString[0]=chr;
myString[1]='\0';
append(myString, 1);
}
void
String::append(String const &old)
{
append(old.rawBuf(), old.len_);
}
void
String::absorb(String &old)
{
clean();
setBuffer(old.buf_, old.size_);
len_ = old.len_;
old.size_ = 0;
old.buf_ = NULL;
old.len_ = 0;
}
String
String::substr(String::size_type from, String::size_type to) const
{
// Must(from >= 0 && from < size());
Must(from < size());
Must(to > 0 && to <= size());
Must(to > from);
String rv;
rv.limitInit(rawBuf()+from,to-from);
return rv;
}
#if DEBUGSTRINGS
void
String::stat(StoreEntry *entry) const
{
storeAppendPrintf(entry, "%p : %d/%d \"%.*s\"\n",this,len_, size_, size(), rawBuf());
}
StringRegistry &
StringRegistry::Instance()
{
return Instance_;
}
template <class C>
int
ptrcmp(C const &lhs, C const &rhs)
{
return lhs - rhs;
}
StringRegistry::StringRegistry()
{
#if DEBUGSTRINGS
Mgr::RegisterAction("strings",
"Strings in use in squid", Stat, 0, 1);
#endif
}
void
StringRegistry::add(String const *entry)
{
entries.insert(entry, ptrcmp);
}
void
StringRegistry::remove(String const *entry)
{
entries.remove(entry, ptrcmp);
}
StringRegistry StringRegistry::Instance_;
String::size_type memStringCount();
void
StringRegistry::Stat(StoreEntry *entry)
{
storeAppendPrintf(entry, "%lu entries, %lu reported from MemPool\n", (unsigned long) Instance().entries.elements, (unsigned long) memStringCount());
Instance().entries.head->walk(Stater, entry);
}
void
StringRegistry::Stater(String const * const & nodedata, void *state)
{
StoreEntry *entry = (StoreEntry *) state;
nodedata->stat(entry);
}
#endif
/* TODO: move onto String */
int
stringHasWhitespace(const char *s)
{
return strpbrk(s, w_space) != NULL;
}
/* TODO: move onto String */
int
stringHasCntl(const char *s)
{
unsigned char c;
while ((c = (unsigned char) *s++) != '\0') {
if (c <= 0x1f)
return 1;
if (c >= 0x7f && c <= 0x9f)
return 1;
}
return 0;
}
/*
* Similar to strtok, but has some rudimentary knowledge
* of quoting
*/
char *
strwordtok(char *buf, char **t)
{
unsigned char *word = NULL;
unsigned char *p = (unsigned char *) buf;
unsigned char *d;
unsigned char ch;
int quoted = 0;
if (!p)
p = (unsigned char *) *t;
if (!p)
goto error;
while (*p && xisspace(*p))
++p;
if (!*p)
goto error;
word = d = p;
while ((ch = *p)) {
switch (ch) {
case '\\':
if (quoted)
++p;
switch (*p) {
case 'n':
ch = '\n';
break;
case 'r':
ch = '\r';
break;
default:
ch = *p;
break;
}
*d = ch;
++d;
if (ch)
++p;
break;
case '"':
quoted = !quoted;
++p;
break;
default:
if (!quoted && xisspace(*p)) {
++p;
goto done;
}
*d = *p;
++d;
++p;
break;
}
}
done:
*d = '\0';
error:
*t = (char *) p;
return (char *) word;
}
const char *
checkNullString(const char *p)
{
return p ? p : "(NULL)";
}
const char *
String::pos(char const *aString) const
{
if (undefined())
return NULL;
return strstr(termedBuf(), aString);
}
const char *
String::pos(char const ch) const
{
if (undefined())
return NULL;
return strchr(termedBuf(), ch);
}
const char *
String::rpos(char const ch) const
{
if (undefined())
return NULL;
return strrchr(termedBuf(), (ch));
}
String::size_type
String::find(char const ch) const
{
const char *c;
c=pos(ch);
if (c==NULL)
return npos;
return c-rawBuf();
}
String::size_type
String::find(char const *aString) const
{
const char *c;
c=pos(aString);
if (c==NULL)
return npos;
return c-rawBuf();
}
String::size_type
String::rfind(char const ch) const
{
const char *c;
c=rpos(ch);
if (c==NULL)
return npos;
return c-rawBuf();
}
#if !_USE_INLINE_
#include "String.cci"
#endif
|