]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/String.cc
Source Format Enforcement (#1234)
[thirdparty/squid.git] / src / String.cc
index a68af96883678c127e4e1f1c6ec002c5c932b6ec..aeb3aacebe142b219762c4f99f56a20f73580cc0 100644 (file)
@@ -1,60 +1,26 @@
-
 /*
- * $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 "Store.h"
-#include "TextException.h"
-
-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
@@ -63,17 +29,23 @@ void
 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
 }
@@ -86,7 +58,7 @@ String::operator =(char const *aString)
 }
 
 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)
@@ -95,7 +67,7 @@ String::operator = (String const &old)
 }
 
 bool
-String::operator == (String const &that) const
+String::operator ==(String const &that) const
 {
     if (0 == this->cmp(that))
         return true;
@@ -104,7 +76,7 @@ String::operator == (String const &that) const
 }
 
 bool
-String::operator != (String const &that) const
+String::operator !=(String const &that) const
 {
     if (0 == this->cmp(that))
         return false;
@@ -114,7 +86,7 @@ String::operator != (String const &that) const
 
 // 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);
@@ -125,16 +97,14 @@ String::limitInit(const char *str, int len)
 void
 String::allocAndFill(const char *str, int len)
 {
-    PROF_start(StringAllocAndFill);
-    assert(this && str);
+    assert(str);
     allocBuffer(len + 1);
     len_ = len;
-    xmemcpy(buf_, str, 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());
@@ -147,9 +117,7 @@ String::String (String const &old) : size_(0), len_(0), buf_(NULL)
 void
 String::clean()
 {
-    PROF_start(StringClean);
-    assert(this);
-
+    /* TODO if mempools has already closed this will FAIL!! */
     if (defined())
         memFreeString(size_, buf_);
 
@@ -157,8 +125,7 @@ String::clean()
 
     size_ = 0;
 
-    buf_ = NULL;
-    PROF_stop(StringClean);
+    buf_ = nullptr;
 }
 
 String::~String()
@@ -171,64 +138,60 @@ 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(canGrowBy(len)); // otherwise snew.len_ may overflow below
         snew.len_ = len_ + len;
         snew.allocBuffer(snew.len_ + 1);
 
         if (len_)
-            xmemcpy(snew.buf_, rawBuf(), len_);
+            memcpy(snew.buf_, rawBuf(), len_);
 
         if (len)
-            xmemcpy(snew.buf_ + len_, str, 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));
+    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
@@ -238,22 +201,105 @@ String::absorb(String &old)
     setBuffer(old.buf_, old.size_);
     len_ = old.len_;
     old.size_ = 0;
-    old.buf_ = NULL;
+    old.buf_ = nullptr;
     old.len_ = 0;
 }
 
 String
 String::substr(String::size_type from, String::size_type to) const
 {
-    Must(from >= 0 && from < size());
+//    Must(from >= 0 && from < size());
+    Must(from < size());
     Must(to > 0 && to <= size());
     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
@@ -278,30 +324,26 @@ ptrcmp(C const &lhs, C const &rhs)
 StringRegistry::StringRegistry()
 {
 #if DEBUGSTRINGS
-    CacheManager::GetInstance()->registerAction("strings",
-            "Strings in use in squid", Stat, 0, 1);
+    Mgr::RegisterAction("strings",
+                        "Strings in use in squid", Stat, 0, 1);
 #endif
 }
 
 void
-
-StringRegistry::add
-(String const *entry)
+StringRegistry::add(String const *entry)
 {
     entries.insert(entry, ptrcmp);
 }
 
 void
-
-StringRegistry::remove
-(String const *entry)
+StringRegistry::remove(String const *entry)
 {
     entries.remove(entry, ptrcmp);
 }
 
 StringRegistry StringRegistry::Instance_;
 
-extern String::size_type memStringCount();
+String::size_type memStringCount();
 
 void
 StringRegistry::Stat(StoreEntry *entry)
@@ -323,7 +365,7 @@ StringRegistry::Stater(String const * const & nodedata, void *state)
 int
 stringHasWhitespace(const char *s)
 {
-    return strpbrk(s, w_space) != NULL;
+    return strpbrk(s, w_space) != nullptr;
 }
 
 /* TODO: move onto String */
@@ -350,7 +392,7 @@ stringHasCntl(const char *s)
 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;
@@ -363,7 +405,7 @@ strwordtok(char *buf, char **t)
         goto error;
 
     while (*p && xisspace(*p))
-        p++;
+        ++p;
 
     if (!*p)
         goto error;
@@ -374,7 +416,8 @@ strwordtok(char *buf, char **t)
         switch (ch) {
 
         case '\\':
-            p++;
+            if (quoted)
+                ++p;
 
             switch (*p) {
 
@@ -395,33 +438,36 @@ strwordtok(char *buf, char **t)
 
             }
 
-            *d++ = ch;
+            *d = ch;
+            ++d;
 
             if (ch)
-                p++;
+                ++p;
 
             break;
 
         case '"':
             quoted = !quoted;
 
-            p++;
+            ++p;
 
             break;
 
         default:
             if (!quoted && xisspace(*p)) {
-                p++;
+                ++p;
                 goto done;
             }
 
-            *d++ = *p++;
+            *d = *p;
+            ++d;
+            ++p;
             break;
         }
     }
 
 done:
-    *d++ = '\0';
+    *d = '\0';
 
 error:
     *t = (char *) p;
@@ -438,7 +484,7 @@ const char *
 String::pos(char const *aString) const
 {
     if (undefined())
-        return NULL;
+        return nullptr;
     return strstr(termedBuf(), aString);
 }
 
@@ -446,7 +492,7 @@ const char *
 String::pos(char const ch) const
 {
     if (undefined())
-        return NULL;
+        return nullptr;
     return strchr(termedBuf(), ch);
 }
 
@@ -454,7 +500,7 @@ const char *
 String::rpos(char const ch) const
 {
     if (undefined())
-        return NULL;
+        return nullptr;
     return strrchr(termedBuf(), (ch));
 }
 
@@ -463,7 +509,7 @@ String::find(char const ch) const
 {
     const char *c;
     c=pos(ch);
-    if (c==NULL)
+    if (c==nullptr)
         return npos;
     return c-rawBuf();
 }
@@ -473,7 +519,7 @@ String::find(char const *aString) const
 {
     const char *c;
     c=pos(aString);
-    if (c==NULL)
+    if (c==nullptr)
         return npos;
     return c-rawBuf();
 }
@@ -483,13 +529,8 @@ String::rfind(char const ch) const
 {
     const char *c;
     c=rpos(ch);
-    if (c==NULL)
+    if (c==nullptr)
         return npos;
     return c-rawBuf();
 }
 
-
-
-#ifndef _USE_INLINE_
-#include "String.cci"
-#endif