]> git.ipfire.org Git - thirdparty/squid.git/blob - src/String.cci
Removed CVS $ markers
[thirdparty/squid.git] / src / String.cci
1 /*
2 * DEBUG: section 67 String
3 * AUTHOR: Duane Wessels
4 *
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
7 *
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.
16 *
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.
21 *
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.
26 *
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.
30 *
31 */
32
33 #include <cstring>
34
35 #if HAVE_STDINT_H
36 #include <stdint.h>
37 #else /* HAVE_STDINT_H */
38 #ifndef INT_MAX
39 #define INT_MAX 1<<31 //hack but a safe bet
40 #endif /* INT_MAX */
41 #endif /* HAVE_STDINT_H */
42
43 String::String() : size_(0), len_(0), buf_ (NULL)
44 {
45 #if DEBUGSTRINGS
46 StringRegistry::Instance().add(this);
47 #endif
48 }
49
50 String::size_type
51 String::size() const
52 {
53 return len_;
54 }
55
56 bool String::defined() const
57 {
58 return buf_!=NULL;
59 }
60
61 bool String::undefined() const
62 {
63 return buf_==NULL;
64 }
65
66 char const *
67 String::rawBuf() const
68 {
69 return buf_;
70 }
71
72 char const *
73 String::termedBuf() const
74 {
75 return buf_;
76 }
77
78 char
79 String::operator [](unsigned int aPos) const
80 {
81 assert(aPos < size_);
82
83 return buf_[aPos];
84 }
85
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).
88 bool
89 String::nilCmp(const bool thisIsNilOrEmpty, const bool otherIsNilOrEmpty, int &result) const
90 {
91 if (!thisIsNilOrEmpty && !otherIsNilOrEmpty)
92 return false; // result does not matter
93
94 if (thisIsNilOrEmpty && otherIsNilOrEmpty)
95 result = 0;
96 else if (thisIsNilOrEmpty)
97 result = -1;
98 else // otherIsNilOrEmpty
99 result = +1;
100
101 return true;
102 }
103
104 int
105 String::cmp (char const *aString) const
106 {
107 int result = 0;
108 if (nilCmp(!size(), (!aString || !*aString), result))
109 return result;
110
111 return strcmp(termedBuf(), aString);
112 }
113
114 int
115 String::cmp (char const *aString, String::size_type count) const
116 {
117 int result = 0;
118 if (nilCmp((!size() || !count), (!aString || !*aString || !count), result))
119 return result;
120
121 return strncmp(termedBuf(), aString, count);
122 }
123
124 int
125 String::cmp (String const &aString) const
126 {
127 int result = 0;
128 if (nilCmp(!size(), !aString.size(), result))
129 return result;
130
131 return strcmp(termedBuf(), aString.termedBuf());
132 }
133
134 int
135 String::caseCmp(char const *aString) const
136 {
137 int result = 0;
138 if (nilCmp(!size(), (!aString || !*aString), result))
139 return result;
140
141 return strcasecmp(termedBuf(), aString);
142 }
143
144 int
145 String::caseCmp(char const *aString, String::size_type count) const
146 {
147 int result = 0;
148 if (nilCmp((!size() || !count), (!aString || !*aString || !count), result))
149 return result;
150
151 return strncasecmp(termedBuf(), aString, count);
152 }
153
154 int
155 String::caseCmp(const String &str) const
156 {
157 return caseCmp(str.rawBuf(),str.size());
158 }
159
160 void
161 String::set(char const *loc, char const ch)
162 {
163 if (loc < buf_ || loc > (buf_ + size_) ) return;
164
165 buf_[loc-buf_] = ch;
166 }
167
168 void
169 String::cut(String::size_type newLength)
170 {
171 // size_type is size_t, unsigned. No need to check for newLength <0
172 if (newLength > len_) return;
173
174 len_ = newLength;
175
176 // buf_ may be NULL on zero-length strings.
177 if (len_ == 0 && buf_ == NULL) return;
178
179 buf_[newLength] = '\0';
180 }
181
182 void
183 String::cutPointer(char const *loc)
184 {
185 if (loc < buf_ || loc > (buf_ + size_) ) return;
186
187 len_ = loc-buf_;
188 buf_[len_] = '\0';
189 }
190
191 std::ostream &
192 operator<<(std::ostream& os, String const &aString)
193 {
194 os.write(aString.rawBuf(),aString.size());
195 return os;
196 }
197
198 bool
199 operator<(const String &a, const String &b)
200 {
201 return a.cmp(b) < 0;
202 }