]> git.ipfire.org Git - thirdparty/squid.git/blob - src/String.cci
3.1 Cleanups pt 1: Add testheaders.sh script
[thirdparty/squid.git] / src / String.cci
1 /*
2 * $Id: String.cci,v 1.12 2008/02/11 23:01:23 amosjeffries Exp $
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 "assert.h"
36
37 String::String() : size_(0), len_(0), buf_ (NULL)
38 {
39 #if DEBUGSTRINGS
40 StringRegistry::Instance().add(this);
41 #endif
42 }
43
44 int
45 String::size() const
46 {
47 return len_;
48 }
49
50 char const *
51 String::buf() const
52 {
53 return buf_;
54 }
55
56 char&
57 String::operator [](unsigned int pos)
58 {
59 assert(pos < size_);
60
61 return buf_[pos];
62 }
63
64 const char *
65 String::pos(char const *aString) const
66 {
67 return strstr(buf(), aString);
68 }
69
70 const char *
71 String::pos(char const ch) const
72 {
73 return strchr(buf(), ch);
74 }
75
76 const char *
77 String::rpos(char const ch) const
78 {
79 return strrchr(buf(), (ch));
80 }
81
82 int
83 String::cmp (char const *aString) const
84 {
85 /* strcmp fails on NULLS */
86
87 if (size() == 0 && (aString == NULL || aString[0] == '\0'))
88 return 0;
89
90 if (size() == 0)
91 return -1;
92
93 if (aString == NULL || aString[0] == '\0')
94 return 1;
95
96 return strcmp(buf(), aString);
97 }
98
99 int
100 String::cmp (char const *aString, size_t count) const
101 {
102 /* always the same at length 0 */
103
104 if (count == 0)
105 return 0;
106
107 if (size() == 0 && (aString == NULL || aString[0] == '\0'))
108 return 0;
109
110 if (size() == 0)
111 return -1;
112
113 if (aString == NULL || aString[0] == '\0')
114 return 1;
115
116 return strncmp(buf(), aString, count);
117 }
118
119 int
120 String::cmp (String const &aString) const
121 {
122 /* strcmp fails on NULLS */
123
124 if (size() == 0 && aString.size() == 0)
125 return 0;
126
127 if (size() == 0)
128 return -1;
129
130 if (aString.size() == 0)
131 return 1;
132
133 return strcmp(buf(), aString.buf());
134 }
135
136 int
137 String::caseCmp(char const *aString) const
138 {
139 return strcasecmp(buf(), aString);
140 }
141
142 int
143 String::caseCmp(char const *aString, size_t count) const
144 {
145 return strncasecmp(buf(), aString, count);
146 }
147
148 void
149 String::set(char const *loc, char const ch)
150 {
151 if(loc < buf_ || loc > (buf_ + size_) ) return;
152
153 buf_[loc-buf_] = ch;
154 }
155
156 void
157 String::cut(size_t newLength)
158 {
159 if(newLength < 0 || newLength > len_) return;
160
161 len_ = newLength;
162
163 // buf_ may be NULL on zero-length strings.
164 if(len_ == 0 && buf_ == NULL) return;
165
166 buf_[newLength] = '\0';
167 }
168
169 void
170 String::cutPointer(char const *loc)
171 {
172 if(loc < buf_ || loc > (buf_ + size_) ) return;
173
174 len_ = loc-buf_;
175 buf_[len_] = '\0';
176 }
177
178 std::ostream &
179 operator<<(std::ostream& os, String const &aString)
180 {
181 os << aString.buf();
182 return os;
183 }