]> git.ipfire.org Git - thirdparty/squid.git/blob - src/String.cci
SourceFormat Enforcement
[thirdparty/squid.git] / src / String.cci
1 /*
2 * $Id$
3 *
4 * DEBUG: section 67 String
5 * AUTHOR: Duane Wessels
6 *
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
9 *
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
32 *
33 */
34
35 #include <cstring>
36
37 #if HAVE_STDINT_H
38 #include <stdint.h>
39 #else /* HAVE_STDINT_H */
40 #ifndef INT_MAX
41 #define INT_MAX 1<<31 //hack but a safe bet
42 #endif /* INT_MAX */
43 #endif /* HAVE_STDINT_H */
44
45 String::String() : size_(0), len_(0), buf_ (NULL)
46 {
47 #if DEBUGSTRINGS
48 StringRegistry::Instance().add(this);
49 #endif
50 }
51
52 String::size_type
53 String::size() const
54 {
55 return len_;
56 }
57
58 bool String::defined() const
59 {
60 return buf_!=NULL;
61 }
62
63 bool String::undefined() const
64 {
65 return buf_==NULL;
66 }
67
68 char const *
69 String::rawBuf() const
70 {
71 return buf_;
72 }
73
74 char const *
75 String::termedBuf() const
76 {
77 return buf_;
78 }
79
80 char
81 String::operator [](unsigned int aPos) const
82 {
83 assert(aPos < size_);
84
85 return buf_[aPos];
86 }
87
88 /// compare NULL and empty strings because str*cmp() may fail on NULL strings
89 /// and because we need to return consistent results for strncmp(count == 0).
90 bool
91 String::nilCmp(const bool thisIsNilOrEmpty, const bool otherIsNilOrEmpty, int &result) const
92 {
93 if (!thisIsNilOrEmpty && !otherIsNilOrEmpty)
94 return false; // result does not matter
95
96 if (thisIsNilOrEmpty && otherIsNilOrEmpty)
97 result = 0;
98 else if (thisIsNilOrEmpty)
99 result = -1;
100 else // otherIsNilOrEmpty
101 result = +1;
102
103 return true;
104 }
105
106 int
107 String::cmp (char const *aString) const
108 {
109 int result = 0;
110 if (nilCmp(!size(), (!aString || !*aString), result))
111 return result;
112
113 return strcmp(termedBuf(), aString);
114 }
115
116 int
117 String::cmp (char const *aString, String::size_type count) const
118 {
119 int result = 0;
120 if (nilCmp((!size() || !count), (!aString || !*aString || !count), result))
121 return result;
122
123 return strncmp(termedBuf(), aString, count);
124 }
125
126 int
127 String::cmp (String const &aString) const
128 {
129 int result = 0;
130 if (nilCmp(!size(), !aString.size(), result))
131 return result;
132
133 return strcmp(termedBuf(), aString.termedBuf());
134 }
135
136 int
137 String::caseCmp(char const *aString) const
138 {
139 int result = 0;
140 if (nilCmp(!size(), (!aString || !*aString), result))
141 return result;
142
143 return strcasecmp(termedBuf(), aString);
144 }
145
146 int
147 String::caseCmp(char const *aString, String::size_type count) const
148 {
149 int result = 0;
150 if (nilCmp((!size() || !count), (!aString || !*aString || !count), result))
151 return result;
152
153 return strncasecmp(termedBuf(), aString, count);
154 }
155
156 int
157 String::caseCmp(const String &str) const
158 {
159 return caseCmp(str.rawBuf(),str.size());
160 }
161
162 void
163 String::set(char const *loc, char const ch)
164 {
165 if (loc < buf_ || loc > (buf_ + size_) ) return;
166
167 buf_[loc-buf_] = ch;
168 }
169
170 void
171 String::cut(String::size_type newLength)
172 {
173 // size_type is size_t, unsigned. No need to check for newLength <0
174 if (newLength > len_) return;
175
176 len_ = newLength;
177
178 // buf_ may be NULL on zero-length strings.
179 if (len_ == 0 && buf_ == NULL) return;
180
181 buf_[newLength] = '\0';
182 }
183
184 void
185 String::cutPointer(char const *loc)
186 {
187 if (loc < buf_ || loc > (buf_ + size_) ) return;
188
189 len_ = loc-buf_;
190 buf_[len_] = '\0';
191 }
192
193 std::ostream &
194 operator<<(std::ostream& os, String const &aString)
195 {
196 os.write(aString.rawBuf(),aString.size());
197 return os;
198 }
199
200 bool
201 operator<(const String &a, const String &b)
202 {
203 return a.cmp(b) < 0;
204 }