2 * DEBUG: section 67 String
3 * AUTHOR: Duane Wessels
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
37 #else /* HAVE_STDINT_H */
39 #define INT_MAX 1<<31 //hack but a safe bet
41 #endif /* HAVE_STDINT_H */
43 String::String() : size_(0), len_(0), buf_ (NULL)
46 StringRegistry::Instance().add(this);
56 bool String::defined() const
61 bool String::undefined() const
67 String::rawBuf() const
73 String::termedBuf() const
79 String::operator [](unsigned int aPos) const
86 /// compare NULL and empty strings because str*cmp() may fail on NULL strings
87 /// and because we need to return consistent results for strncmp(count == 0).
89 String::nilCmp(const bool thisIsNilOrEmpty, const bool otherIsNilOrEmpty, int &result) const
91 if (!thisIsNilOrEmpty && !otherIsNilOrEmpty)
92 return false; // result does not matter
94 if (thisIsNilOrEmpty && otherIsNilOrEmpty)
96 else if (thisIsNilOrEmpty)
98 else // otherIsNilOrEmpty
105 String::cmp (char const *aString) const
108 if (nilCmp(!size(), (!aString || !*aString), result))
111 return strcmp(termedBuf(), aString);
115 String::cmp (char const *aString, String::size_type count) const
118 if (nilCmp((!size() || !count), (!aString || !*aString || !count), result))
121 return strncmp(termedBuf(), aString, count);
125 String::cmp (String const &aString) const
128 if (nilCmp(!size(), !aString.size(), result))
131 return strcmp(termedBuf(), aString.termedBuf());
135 String::caseCmp(char const *aString) const
138 if (nilCmp(!size(), (!aString || !*aString), result))
141 return strcasecmp(termedBuf(), aString);
145 String::caseCmp(char const *aString, String::size_type count) const
148 if (nilCmp((!size() || !count), (!aString || !*aString || !count), result))
151 return strncasecmp(termedBuf(), aString, count);
155 String::caseCmp(const String &str) const
157 return caseCmp(str.rawBuf(),str.size());
161 String::set(char const *loc, char const ch)
163 if (loc < buf_ || loc > (buf_ + size_) ) return;
169 String::cut(String::size_type newLength)
171 // size_type is size_t, unsigned. No need to check for newLength <0
172 if (newLength > len_) return;
176 // buf_ may be NULL on zero-length strings.
177 if (len_ == 0 && buf_ == NULL) return;
179 buf_[newLength] = '\0';
183 String::cutPointer(char const *loc)
185 if (loc < buf_ || loc > (buf_ + size_) ) return;
192 operator<<(std::ostream& os, String const &aString)
194 os.write(aString.rawBuf(),aString.size());
199 operator<(const String &a, const String &b)