From: Amos Jeffries Date: Fri, 6 Jan 2017 04:41:57 +0000 (+1300) Subject: Cleanup: remove String.cci file X-Git-Tag: M-staged-PR71~320 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f63c4b05af40ea82396fca8993164362548efb06;p=thirdparty%2Fsquid.git Cleanup: remove String.cci file Moving the simple methods inline to the SquidString.h and more complicated others to String.cc --- diff --git a/src/Makefile.am b/src/Makefile.am index 4d47dadbf2..ace10596c5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -504,7 +504,6 @@ EXTRA_squid_SOURCES = \ noinst_HEADERS = \ client_side_request.cci \ MemBuf.h \ - String.cci \ SquidString.h \ SquidTime.h diff --git a/src/SquidString.h b/src/SquidString.h index bbe0c74bf4..f74b0d5980 100644 --- a/src/SquidString.h +++ b/src/SquidString.h @@ -11,6 +11,8 @@ #ifndef SQUID_STRING_H #define SQUID_STRING_H +#include "base/TextException.h" + #include /* squid string placeholder (for printf) */ @@ -23,7 +25,7 @@ class String { public: - _SQUID_INLINE_ String(); + String(); String(char const *); String(String const &); ~String(); @@ -38,25 +40,34 @@ public: /** * Retrieve a single character in the string. - \param pos Position of character to retrieve. + \param aPos Position of character to retrieve. */ - _SQUID_INLINE_ char operator [](unsigned int pos) const; + char operator [](unsigned int aPos) const { + assert(aPos < size_); + return buf_[aPos]; + } + + size_type size() const { return len_; } - _SQUID_INLINE_ size_type size() const; /// variant of size() suited to be used for printf-alikes. - /// throws when size() > MAXINT - int psize() const; + /// throws when size() >= INT_MAX + int psize() const { + Must(size() < INT_MAX); + return size(); + } /** * Returns a raw pointer to the underlying backing store. The caller has been * verified not to make any assumptions about null-termination */ - _SQUID_INLINE_ char const * rawBuf() const; + char const * rawBuf() const { return buf_; } + /** * Returns a raw pointer to the underlying backing store. * The caller requires it to be null-terminated. */ - _SQUID_INLINE_ char const * termedBuf() const; + char const * termedBuf() const { return buf_; } + void limitInit(const char *str, int len); // TODO: rename to assign() void clean(); void reset(char const *str); @@ -73,12 +84,14 @@ public: size_type find(char const *aString) const; const char * rpos(char const ch) const; size_type rfind(char const ch) const; - _SQUID_INLINE_ int cmp(char const *) const; - _SQUID_INLINE_ int cmp(char const *, size_type count) const; - _SQUID_INLINE_ int cmp(String const &) const; - _SQUID_INLINE_ int caseCmp(char const *) const; - _SQUID_INLINE_ int caseCmp(char const *, size_type count) const; - _SQUID_INLINE_ int caseCmp(String const &) const; + int cmp(char const *) const; + int cmp(char const *, size_type count) const; + int cmp(String const &) const; + int caseCmp(char const *) const; + int caseCmp(char const *, size_type count) const; + int caseCmp(String const &str) const { + return caseCmp(str.rawBuf(),str.size()); + } /// Whether creating a totalLen-character string is safe (i.e., unlikely to assert). /// Optional extras can be used for overflow-safe length addition. @@ -89,7 +102,7 @@ public: String substr(size_type from, size_type to) const; - _SQUID_INLINE_ void cut(size_type newLength); + void cut(size_type newLength); private: void allocAndFill(const char *str, int len); @@ -99,31 +112,41 @@ private: bool defined() const {return buf_!=NULL;} bool undefined() const {return !defined();} - _SQUID_INLINE_ bool nilCmp(bool, bool, int &) const; - /* never reference these directly! */ - size_type size_; /* buffer size; limited by SizeMax_ */ + size_type size_ = 0; /* buffer size; limited by SizeMax_ */ - size_type len_; /* current length */ + size_type len_ = 0; /* current length */ static const size_type SizeMax_ = 65535; ///< 64K limit protects some fixed-size buffers /// returns true after increasing the first argument by extra if the sum does not exceed SizeMax_ static bool SafeAdd(size_type &base, size_type extra) { if (extra <= SizeMax_ && base <= SizeMax_ - extra) { base += extra; return true; } return false; } - char *buf_; + char *buf_ = nullptr; - _SQUID_INLINE_ void set(char const *loc, char const ch); - _SQUID_INLINE_ void cutPointer(char const *loc); + void set(char const *loc, char const ch) { + if (loc < buf_ || loc > (buf_ + size_)) + return; + buf_[loc-buf_] = ch; + } + void cutPointer(char const *loc) { + if (loc < buf_ || loc > (buf_ + size_)) + return; + len_ = loc-buf_; + buf_[len_] = '\0'; + } }; -_SQUID_INLINE_ std::ostream & operator<<(std::ostream& os, String const &aString); - -_SQUID_INLINE_ bool operator<(const String &a, const String &b); +inline std::ostream & operator<<(std::ostream &os, String const &aString) +{ + os.write(aString.rawBuf(),aString.size()); + return os; +} -#if _USE_INLINE_ -#include "String.cci" -#endif +inline bool operator<(const String &a, const String &b) +{ + return a.cmp(b) < 0; +} const char *checkNullString(const char *p); int stringHasWhitespace(const char *); diff --git a/src/String.cc b/src/String.cc index dda0915602..6e38af956d 100644 --- a/src/String.cc +++ b/src/String.cc @@ -16,13 +16,6 @@ #include -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 @@ -46,12 +39,18 @@ String::setBuffer(char *aBuf, String::size_type aSize) 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 } @@ -233,6 +232,89 @@ String::substr(String::size_type from, String::size_type to) const 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 @@ -466,7 +548,3 @@ String::rfind(char const ch) const return c-rawBuf(); } -#if !_USE_INLINE_ -#include "String.cci" -#endif - diff --git a/src/String.cci b/src/String.cci deleted file mode 100644 index b21b3c32b1..0000000000 --- a/src/String.cci +++ /dev/null @@ -1,163 +0,0 @@ -/* - * Copyright (C) 1996-2017 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 - -String::String() : size_(0), len_(0), buf_(NULL) -{ -#if DEBUGSTRINGS - StringRegistry::Instance().add(this); -#endif -} - -String::size_type -String::size() const -{ - return len_; -} - -char const * -String::rawBuf() const -{ - return buf_; -} - -char const * -String::termedBuf() const -{ - return buf_; -} - -char -String::operator [](unsigned int aPos) const -{ - assert(aPos < size_); - - return buf_[aPos]; -} - -/// 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). -bool -String::nilCmp(const bool thisIsNilOrEmpty, const bool otherIsNilOrEmpty, int &result) const -{ - 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); -} - -int -String::caseCmp(const String &str) const -{ - return caseCmp(str.rawBuf(),str.size()); -} - -void -String::set(char const *loc, char const ch) -{ - if (loc < buf_ || loc > (buf_ + size_) ) return; - - buf_[loc-buf_] = ch; -} - -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 NULL on zero-length strings. - if (len_ == 0 && buf_ == NULL) return; - - buf_[newLength] = '\0'; -} - -void -String::cutPointer(char const *loc) -{ - if (loc < buf_ || loc > (buf_ + size_) ) return; - - len_ = loc-buf_; - buf_[len_] = '\0'; -} - -std::ostream & -operator<<(std::ostream& os, String const &aString) -{ - os.write(aString.rawBuf(),aString.size()); - return os; -} - -bool -operator<(const String &a, const String &b) -{ - return a.cmp(b) < 0; -} -