-
/*
- * $Id$
- *
- * 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.
+ * 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.
*/
#include "squid.h"
#include "base/TextException.h"
#include "mgr/Registration.h"
-#include "profiler/Profiler.h"
-#include "protos.h"
#include "Store.h"
-#if HAVE_LIMITS_H
-#include <limits.h>
-#endif
-
-int
-String::psize() const
-{
- Must(size() < INT_MAX);
- return size();
-}
+#include <climits>
// 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
String::setBuffer(char *aBuf, String::size_type aSize)
{
assert(undefined());
- assert(aSize < 65536);
+ assert(aSize <= SizeMax_);
buf_ = aBuf;
size_ = aSize;
}
-String::String (char const *aString) : size_(0), len_(0), buf_(NULL)
+String::String()
+{
+#if DEBUGSTRINGS
+ StringRegistry::Instance().add(this);
+#endif
+}
+
+String::String(char const *aString)
{
if (aString)
allocAndFill(aString, strlen(aString));
#if DEBUGSTRINGS
-
StringRegistry::Instance().add(this);
#endif
}
}
String &
-String::operator = (String const &old)
+String::operator =(String const &old)
{
clean(); // TODO: optimize to avoid cleaning the buffer we can use
if (old.size() > 0)
}
bool
-String::operator == (String const &that) const
+String::operator ==(String const &that) const
{
if (0 == this->cmp(that))
return true;
}
bool
-String::operator != (String const &that) const
+String::operator !=(String const &that) const
{
if (0 == this->cmp(that))
return false;
// 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(this && str);
+ 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);
- assert(this);
-
/* 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(const char *str)
+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(const char *str, int len)
+String::append( char const *str, int len)
{
- assert(this);
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.
String snew;
- assert(len_ + len < 65536); // otherwise snew.len_ overflows below
+ assert(canGrowBy(len)); // otherwise snew.len_ may overflow below
snew.len_ = len_ + len;
snew.allocBuffer(snew.len_ + 1);
absorb(snew);
}
- PROF_stop(StringAppend);
}
void
String::append(char const *str)
{
- assert (str);
- append (str, strlen(str));
+ assert(str);
+ append(str, strlen(str));
}
void
-String::append (char chr)
+String::append(char const chr)
{
char myString[2];
myString[0]=chr;
myString[1]='\0';
- append (myString, 1);
+ append(myString, 1);
}
void
String::append(String const &old)
{
- append (old.rawBuf(), old.len_);
+ append(old.rawBuf(), old.len_);
}
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;
}
+void
+String::cut(String::size_type newLength)
+{
+ // size_type is size_t, unsigned. No need to check for newLength <0
+ if (newLength > len_) return;
+
+ len_ = newLength;
+
+ // buf_ may be nullptr on zero-length strings.
+ if (len_ == 0 && !buf_)
+ return;
+
+ buf_[newLength] = '\0';
+}
+
+/// compare NULL and empty strings because str*cmp() may fail on NULL strings
+/// and because we need to return consistent results for strncmp(count == 0).
+static bool
+nilCmp(const bool thisIsNilOrEmpty, const bool otherIsNilOrEmpty, int &result)
+{
+ if (!thisIsNilOrEmpty && !otherIsNilOrEmpty)
+ return false; // result does not matter
+
+ if (thisIsNilOrEmpty && otherIsNilOrEmpty)
+ result = 0;
+ else if (thisIsNilOrEmpty)
+ result = -1;
+ else // otherIsNilOrEmpty
+ result = +1;
+
+ return true;
+}
+
+int
+String::cmp(char const *aString) const
+{
+ int result = 0;
+ if (nilCmp(!size(), (!aString || !*aString), result))
+ return result;
+
+ return strcmp(termedBuf(), aString);
+}
+
+int
+String::cmp(char const *aString, String::size_type count) const
+{
+ int result = 0;
+ if (nilCmp((!size() || !count), (!aString || !*aString || !count), result))
+ return result;
+
+ return strncmp(termedBuf(), aString, count);
+}
+
+int
+String::cmp(String const &aString) const
+{
+ int result = 0;
+ if (nilCmp(!size(), !aString.size(), result))
+ return result;
+
+ return strcmp(termedBuf(), aString.termedBuf());
+}
+
+int
+String::caseCmp(char const *aString) const
+{
+ int result = 0;
+ if (nilCmp(!size(), (!aString || !*aString), result))
+ return result;
+
+ return strcasecmp(termedBuf(), aString);
+}
+
+int
+String::caseCmp(char const *aString, String::size_type count) const
+{
+ int result = 0;
+ if (nilCmp((!size() || !count), (!aString || !*aString || !count), result))
+ return result;
+
+ return strncasecmp(termedBuf(), aString, count);
+}
+
#if DEBUGSTRINGS
void
String::stat(StoreEntry *entry) const
StringRegistry StringRegistry::Instance_;
-extern String::size_type memStringCount();
+String::size_type memStringCount();
void
StringRegistry::Stat(StoreEntry *entry)
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;
switch (ch) {
case '\\':
- ++p;
+ if (quoted)
+ ++p;
switch (*p) {
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();
}
-#if !_USE_INLINE_
-#include "String.cci"
-#endif