]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/String.cc
Source Format Enforcement (#1234)
[thirdparty/squid.git] / src / String.cc
index 6e38af956d2a3d2233866e0f19a07109350c28db..aeb3aacebe142b219762c4f99f56a20f73580cc0 100644 (file)
@@ -1,17 +1,14 @@
 /*
- * 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
@@ -91,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);
@@ -102,16 +97,14 @@ String::limitInit(const char *str, int 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());
@@ -124,8 +117,6 @@ String::String(String const &old) : size_(0), len_(0), buf_(NULL)
 void
 String::clean()
 {
-    PROF_start(StringClean);
-
     /* TODO if mempools has already closed this will FAIL!! */
     if (defined())
         memFreeString(size_, buf_);
@@ -134,8 +125,7 @@ String::clean()
 
     size_ = 0;
 
-    buf_ = NULL;
-    PROF_stop(StringClean);
+    buf_ = nullptr;
 }
 
 String::~String()
@@ -150,11 +140,9 @@ 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
@@ -162,9 +150,8 @@ String::append( char const *str, int len)
 {
     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.
@@ -183,7 +170,6 @@ String::append( char const *str, int len)
 
         absorb(snew);
     }
-    PROF_stop(StringAppend);
 }
 
 void
@@ -215,7 +201,7 @@ String::absorb(String &old)
     setBuffer(old.buf_, old.size_);
     len_ = old.len_;
     old.size_ = 0;
-    old.buf_ = NULL;
+    old.buf_ = nullptr;
     old.len_ = 0;
 }
 
@@ -228,7 +214,7 @@ String::substr(String::size_type from, String::size_type to) const
     Must(to > from);
 
     String rv;
-    rv.limitInit(rawBuf()+from,to-from);
+    rv.assign(rawBuf()+from, to-from);
     return rv;
 }
 
@@ -379,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 */
@@ -406,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;
@@ -498,7 +484,7 @@ const char *
 String::pos(char const *aString) const
 {
     if (undefined())
-        return NULL;
+        return nullptr;
     return strstr(termedBuf(), aString);
 }
 
@@ -506,7 +492,7 @@ const char *
 String::pos(char const ch) const
 {
     if (undefined())
-        return NULL;
+        return nullptr;
     return strchr(termedBuf(), ch);
 }
 
@@ -514,7 +500,7 @@ const char *
 String::rpos(char const ch) const
 {
     if (undefined())
-        return NULL;
+        return nullptr;
     return strrchr(termedBuf(), (ch));
 }
 
@@ -523,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();
 }
@@ -533,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();
 }
@@ -543,7 +529,7 @@ String::rfind(char const ch) const
 {
     const char *c;
     c=rpos(ch);
-    if (c==NULL)
+    if (c==nullptr)
         return npos;
     return c-rawBuf();
 }