]> git.ipfire.org Git - thirdparty/squid.git/blob - src/String.cci
Enable source-formatting tools to collapse multiple whitelines in the source to one.
[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
46 String::String() : size_(0), len_(0), buf_ (NULL)
47 {
48 #if DEBUGSTRINGS
49 StringRegistry::Instance().add(this);
50 #endif
51 }
52
53 String::size_type
54 String::size() const
55 {
56 return len_;
57 }
58
59 bool String::defined() const
60 {
61 return buf_!=NULL;
62 }
63
64 bool String::undefined() const
65 {
66 return buf_==NULL;
67 }
68
69 char const *
70 String::rawBuf() const
71 {
72 return buf_;
73 }
74
75 char const *
76 String::termedBuf() const
77 {
78 return buf_;
79 }
80
81 char
82 String::operator [](unsigned int aPos) const
83 {
84 assert(aPos < size_);
85
86 return buf_[aPos];
87 }
88
89
90 /// compare NULL and empty strings because str*cmp() may fail on NULL strings
91 /// and because we need to return consistent results for strncmp(count == 0).
92 bool
93 String::nilCmp(const bool thisIsNilOrEmpty, const bool otherIsNilOrEmpty, int &result) const
94 {
95 if (!thisIsNilOrEmpty && !otherIsNilOrEmpty)
96 return false; // result does not matter
97
98 if (thisIsNilOrEmpty && otherIsNilOrEmpty)
99 result = 0;
100 else if (thisIsNilOrEmpty)
101 result = -1;
102 else // otherIsNilOrEmpty
103 result = +1;
104
105 return true;
106 }
107
108
109 int
110 String::cmp (char const *aString) const
111 {
112 int result = 0;
113 if (nilCmp(!size(), (!aString || !*aString), result))
114 return result;
115
116 return strcmp(termedBuf(), aString);
117 }
118
119 int
120 String::cmp (char const *aString, String::size_type count) const
121 {
122 int result = 0;
123 if (nilCmp((!size() || !count), (!aString || !*aString || !count), result))
124 return result;
125
126 return strncmp(termedBuf(), aString, count);
127 }
128
129 int
130 String::cmp (String const &aString) const
131 {
132 int result = 0;
133 if (nilCmp(!size(), !aString.size(), result))
134 return result;
135
136 return strcmp(termedBuf(), aString.termedBuf());
137 }
138
139 int
140 String::caseCmp(char const *aString) const
141 {
142 int result = 0;
143 if (nilCmp(!size(), (!aString || !*aString), result))
144 return result;
145
146 return strcasecmp(termedBuf(), aString);
147 }
148
149 int
150 String::caseCmp(char const *aString, String::size_type count) const
151 {
152 int result = 0;
153 if (nilCmp((!size() || !count), (!aString || !*aString || !count), result))
154 return result;
155
156 return strncasecmp(termedBuf(), aString, count);
157 }
158
159 int
160 String::caseCmp(const String &str) const
161 {
162 return caseCmp(str.rawBuf(),str.size());
163 }
164
165
166 void
167 String::set(char const *loc, char const ch)
168 {
169 if (loc < buf_ || loc > (buf_ + size_) ) return;
170
171 buf_[loc-buf_] = ch;
172 }
173
174 void
175 String::cut(String::size_type newLength)
176 {
177 // size_type is size_t, unsigned. No need to check for newLength <0
178 if (newLength > len_) return;
179
180 len_ = newLength;
181
182 // buf_ may be NULL on zero-length strings.
183 if (len_ == 0 && buf_ == NULL) return;
184
185 buf_[newLength] = '\0';
186 }
187
188 void
189 String::cutPointer(char const *loc)
190 {
191 if (loc < buf_ || loc > (buf_ + size_) ) return;
192
193 len_ = loc-buf_;
194 buf_[len_] = '\0';
195 }
196
197 std::ostream &
198 operator<<(std::ostream& os, String const &aString)
199 {
200 os.write(aString.rawBuf(),aString.size());
201 return os;
202 }
203
204 bool
205 operator<(const String &a, const String &b)
206 {
207 return a.cmp(b) < 0;
208 }