]> git.ipfire.org Git - thirdparty/squid.git/blame - src/String.cci
Hacked in delayed initialization of constants, to avoid asserts in mempools at shutdo...
[thirdparty/squid.git] / src / String.cci
CommitLineData
ad80164a 1/*
262a0e14 2 * $Id$
ad80164a 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.
26ac0430 23 *
ad80164a 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.
26ac0430 28 *
ad80164a 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
7b270d4c 35#include "assert.h"
b610642c 36#include <cstring>
9b558d8a
FC
37#include "squid.h"
38
39#ifdef HAVE_STDINT_H
40#include <stdint.h> //for INT_MAX
41#else /* HAVE_STDINT_H */
42#ifndef INT_MAX
43#define INT_MAX 1<<31 //hack but a safe bet
44#endif /* INT_MAX */
45#endif /* HAVE_STDINT_H */
46
7b270d4c 47
30abd221 48String::String() : size_(0), len_(0), buf_ (NULL)
ad80164a 49{
50#if DEBUGSTRINGS
30abd221 51 StringRegistry::Instance().add(this);
ad80164a 52#endif
53}
54
9b558d8a 55size_t
30abd221 56String::size() const
ad80164a 57{
58 return len_;
59}
60
9b558d8a
FC
61int
62String::psize() const
63{
64 Must(size() < INT_MAX);
65 return size();
66}
67
ad80164a 68char const *
99524de7
FC
69String::unsafeBuf() const
70{
71 return buf_;
72}
73
74bool String::defined() const
75{
76 return buf_!=NULL;
77}
78
79bool String::undefined() const
80{
81 return buf_==NULL;
82}
83
84char const *
85String::rawBuf() const
86{
87 return buf_;
88}
89
90char const *
91String::termedBuf() const
ad80164a 92{
93 return buf_;
94}
95
b4f2886c 96char
4ce0e99b 97String::operator [](unsigned int pos) const
3db44fdf 98{
99 assert(pos < size_);
100
101 return buf_[pos];
102}
103
ad80164a 104const char *
30abd221 105String::pos(char const *aString) const
ad80164a 106{
cb3b44fc 107 return strstr(termedBuf(), aString);
ad80164a 108}
109
110const char *
30abd221 111String::pos(char const ch) const
ad80164a 112{
cb3b44fc 113 return strchr(termedBuf(), ch);
ad80164a 114}
115
b4f2886c
FC
116size_t
117String::find(char const ch) const
118{
119 const char *c;
120 c=pos(ch);
121 if (c==NULL)
122 return std::string::npos;
123 return c-rawBuf();
124}
125
ad80164a 126const char *
30abd221 127String::rpos(char const ch) const
ad80164a 128{
cb3b44fc 129 return strrchr(termedBuf(), (ch));
ad80164a 130}
131
9b558d8a
FC
132size_t
133String::rfind(char const ch) const
134{
135 const char *c;
136 c=rpos(ch);
137 if (c==NULL)
138 return std::string::npos;
139 return c-rawBuf();
140}
141
142
ad80164a 143int
30abd221 144String::cmp (char const *aString) const
ad80164a 145{
146 /* strcmp fails on NULLS */
147
148 if (size() == 0 && (aString == NULL || aString[0] == '\0'))
149 return 0;
150
151 if (size() == 0)
152 return -1;
153
154 if (aString == NULL || aString[0] == '\0')
155 return 1;
156
cb3b44fc 157 return strcmp(termedBuf(), aString);
ad80164a 158}
159
160int
30abd221 161String::cmp (char const *aString, size_t count) const
ad80164a 162{
163 /* always the same at length 0 */
164
165 if (count == 0)
166 return 0;
167
168 if (size() == 0 && (aString == NULL || aString[0] == '\0'))
169 return 0;
170
171 if (size() == 0)
172 return -1;
173
174 if (aString == NULL || aString[0] == '\0')
175 return 1;
176
cb3b44fc 177 return strncmp(termedBuf(), aString, count);
ad80164a 178}
179
180int
30abd221 181String::cmp (String const &aString) const
ad80164a 182{
183 /* strcmp fails on NULLS */
184
185 if (size() == 0 && aString.size() == 0)
186 return 0;
187
188 if (size() == 0)
189 return -1;
190
191 if (aString.size() == 0)
192 return 1;
193
cb3b44fc 194 return strcmp(termedBuf(), aString.termedBuf());
30abd221 195}
196
197int
d968c095 198String::caseCmp(char const *aString) const
30abd221 199{
cb3b44fc 200 return strcasecmp(termedBuf(), aString);
30abd221 201}
202
203int
d968c095 204String::caseCmp(char const *aString, size_t count) const
30abd221 205{
cb3b44fc 206 return strncasecmp(termedBuf(), aString, count);
ad80164a 207}
208
9d7a89a5
FC
209int
210String::caseCmp(const String &str) const
211{
212 return caseCmp(str.rawBuf(),str.size());
213}
214
9b558d8a
FC
215
216void
217String::set(char const *loc, char const ch)
218{
219 if (loc < buf_ || loc > (buf_ + size_) ) return;
220
221 buf_[loc-buf_] = ch;
222}
223
ad80164a 224void
d968c095 225String::cut(size_t newLength)
ad80164a 226{
26ac0430 227 if (newLength < 0 || newLength > len_) return;
d968c095 228
ad80164a 229 len_ = newLength;
7b270d4c 230
e1f7507e 231 // buf_ may be NULL on zero-length strings.
26ac0430 232 if (len_ == 0 && buf_ == NULL) return;
e1f7507e 233
30abd221 234 buf_[newLength] = '\0';
ad80164a 235}
236
ad80164a 237void
d968c095 238String::cutPointer(char const *loc)
ad80164a 239{
26ac0430 240 if (loc < buf_ || loc > (buf_ + size_) ) return;
d968c095 241
ad80164a 242 len_ = loc-buf_;
30abd221 243 buf_[len_] = '\0';
ad80164a 244}
30abd221 245
246std::ostream &
247operator<<(std::ostream& os, String const &aString)
248{
cb3b44fc 249 os.write(aString.rawBuf(),aString.size());
30abd221 250 return os;
251}