/*
- * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
+ * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
*
* Squid software is distributed under GPLv2+ license and includes
* contributions from numerous individuals and organizations.
* Please see the COPYING and CONTRIBUTORS files for details.
*/
-/* DEBUG: section 67 String */
-
#include "squid.h"
#include "base/TextException.h"
#include "mgr/Registration.h"
-#include "profiler/Profiler.h"
#include "Store.h"
#include <climits>
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
// public interface, makes sure that we clean the old buffer first
void
-String::limitInit(const char *str, int len)
+String::assign(const char *str, int len)
{
clean(); // TODO: optimize to avoid cleaning the buffer we can use
allocAndFill(str, len);
void
String::allocAndFill(const char *str, int len)
{
- PROF_start(StringAllocAndFill);
assert(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)
+String::String(String const &old) : size_(0), len_(0), buf_(nullptr)
{
if (old.size() > 0)
allocAndFill(old.rawBuf(), old.size());
void
String::clean()
{
- PROF_start(StringClean);
-
/* TODO if mempools has already closed this will FAIL!! */
if (defined())
memFreeString(size_, buf_);
size_ = 0;
- buf_ = NULL;
- PROF_stop(StringClean);
+ buf_ = nullptr;
}
String::~String()
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
{
assert(str && len >= 0);
- PROF_start(StringAppend);
- if (len_ + len < size_) {
- strncat(buf_, str, len);
+ if (len_ + len + 1 /*'\0'*/ < size_) {
+ xstrncpy(buf_+len_, str, len+1);
len_ += len;
} else {
// Create a temporary string and absorb it later.
absorb(snew);
}
- PROF_stop(StringAppend);
}
void
setBuffer(old.buf_, old.size_);
len_ = old.len_;
old.size_ = 0;
- old.buf_ = NULL;
+ old.buf_ = nullptr;
old.len_ = 0;
}
Must(to > from);
String rv;
- rv.limitInit(rawBuf()+from,to-from);
+ rv.assign(rawBuf()+from, to-from);
return rv;
}
int
stringHasWhitespace(const char *s)
{
- return strpbrk(s, w_space) != NULL;
+ return strpbrk(s, w_space) != nullptr;
}
/* TODO: move onto String */
char *
strwordtok(char *buf, char **t)
{
- unsigned char *word = NULL;
+ unsigned char *word = nullptr;
unsigned char *p = (unsigned char *) buf;
unsigned char *d;
unsigned char ch;
String::pos(char const *aString) const
{
if (undefined())
- return NULL;
+ return nullptr;
return strstr(termedBuf(), aString);
}
String::pos(char const ch) const
{
if (undefined())
- return NULL;
+ return nullptr;
return strchr(termedBuf(), ch);
}
String::rpos(char const ch) const
{
if (undefined())
- return NULL;
+ return nullptr;
return strrchr(termedBuf(), (ch));
}
{
const char *c;
c=pos(ch);
- if (c==NULL)
+ if (c==nullptr)
return npos;
return c-rawBuf();
}
{
const char *c;
c=pos(aString);
- if (c==NULL)
+ if (c==nullptr)
return npos;
return c-rawBuf();
}
{
const char *c;
c=rpos(ch);
- if (c==NULL)
+ if (c==nullptr)
return npos;
return c-rawBuf();
}