2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 67 String */
13 String::String() : size_(0), len_(0), buf_(NULL)
16 StringRegistry::Instance().add(this);
27 String::rawBuf() const
33 String::termedBuf() const
39 String::operator [](unsigned int aPos) const
46 /// compare NULL and empty strings because str*cmp() may fail on NULL strings
47 /// and because we need to return consistent results for strncmp(count == 0).
49 String::nilCmp(const bool thisIsNilOrEmpty, const bool otherIsNilOrEmpty, int &result) const
51 if (!thisIsNilOrEmpty && !otherIsNilOrEmpty)
52 return false; // result does not matter
54 if (thisIsNilOrEmpty && otherIsNilOrEmpty)
56 else if (thisIsNilOrEmpty)
58 else // otherIsNilOrEmpty
65 String::cmp(char const *aString) const
68 if (nilCmp(!size(), (!aString || !*aString), result))
71 return strcmp(termedBuf(), aString);
75 String::cmp(char const *aString, String::size_type count) const
78 if (nilCmp((!size() || !count), (!aString || !*aString || !count), result))
81 return strncmp(termedBuf(), aString, count);
85 String::cmp(String const &aString) const
88 if (nilCmp(!size(), !aString.size(), result))
91 return strcmp(termedBuf(), aString.termedBuf());
95 String::caseCmp(char const *aString) const
98 if (nilCmp(!size(), (!aString || !*aString), result))
101 return strcasecmp(termedBuf(), aString);
105 String::caseCmp(char const *aString, String::size_type count) const
108 if (nilCmp((!size() || !count), (!aString || !*aString || !count), result))
111 return strncasecmp(termedBuf(), aString, count);
115 String::caseCmp(const String &str) const
117 return caseCmp(str.rawBuf(),str.size());
121 String::set(char const *loc, char const ch)
123 if (loc < buf_ || loc > (buf_ + size_) ) return;
129 String::cut(String::size_type newLength)
131 // size_type is size_t, unsigned. No need to check for newLength <0
132 if (newLength > len_) return;
136 // buf_ may be NULL on zero-length strings.
137 if (len_ == 0 && buf_ == NULL) return;
139 buf_[newLength] = '\0';
143 String::cutPointer(char const *loc)
145 if (loc < buf_ || loc > (buf_ + size_) ) return;
152 operator<<(std::ostream& os, String const &aString)
154 os.write(aString.rawBuf(),aString.size());
159 operator<(const String &a, const String &b)