]> git.ipfire.org Git - thirdparty/squid.git/blob - src/String.cci
ddc65952188b4dbc51b3673f30ed057e7dea4bce
[thirdparty/squid.git] / src / String.cci
1 /*
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 /* DEBUG: section 67 String */
10
11 #include <cstring>
12
13 String::String() : size_(0), len_(0), buf_(NULL)
14 {
15 #if DEBUGSTRINGS
16 StringRegistry::Instance().add(this);
17 #endif
18 }
19
20 String::size_type
21 String::size() const
22 {
23 return len_;
24 }
25
26 char const *
27 String::rawBuf() const
28 {
29 return buf_;
30 }
31
32 char const *
33 String::termedBuf() const
34 {
35 return buf_;
36 }
37
38 char
39 String::operator [](unsigned int aPos) const
40 {
41 assert(aPos < size_);
42
43 return buf_[aPos];
44 }
45
46 /// compare NULL and empty strings because str*cmp() may fail on NULL strings
47 /// and because we need to return consistent results for strncmp(count == 0).
48 bool
49 String::nilCmp(const bool thisIsNilOrEmpty, const bool otherIsNilOrEmpty, int &result) const
50 {
51 if (!thisIsNilOrEmpty && !otherIsNilOrEmpty)
52 return false; // result does not matter
53
54 if (thisIsNilOrEmpty && otherIsNilOrEmpty)
55 result = 0;
56 else if (thisIsNilOrEmpty)
57 result = -1;
58 else // otherIsNilOrEmpty
59 result = +1;
60
61 return true;
62 }
63
64 int
65 String::cmp(char const *aString) const
66 {
67 int result = 0;
68 if (nilCmp(!size(), (!aString || !*aString), result))
69 return result;
70
71 return strcmp(termedBuf(), aString);
72 }
73
74 int
75 String::cmp(char const *aString, String::size_type count) const
76 {
77 int result = 0;
78 if (nilCmp((!size() || !count), (!aString || !*aString || !count), result))
79 return result;
80
81 return strncmp(termedBuf(), aString, count);
82 }
83
84 int
85 String::cmp(String const &aString) const
86 {
87 int result = 0;
88 if (nilCmp(!size(), !aString.size(), result))
89 return result;
90
91 return strcmp(termedBuf(), aString.termedBuf());
92 }
93
94 int
95 String::caseCmp(char const *aString) const
96 {
97 int result = 0;
98 if (nilCmp(!size(), (!aString || !*aString), result))
99 return result;
100
101 return strcasecmp(termedBuf(), aString);
102 }
103
104 int
105 String::caseCmp(char const *aString, String::size_type count) const
106 {
107 int result = 0;
108 if (nilCmp((!size() || !count), (!aString || !*aString || !count), result))
109 return result;
110
111 return strncasecmp(termedBuf(), aString, count);
112 }
113
114 int
115 String::caseCmp(const String &str) const
116 {
117 return caseCmp(str.rawBuf(),str.size());
118 }
119
120 void
121 String::set(char const *loc, char const ch)
122 {
123 if (loc < buf_ || loc > (buf_ + size_) ) return;
124
125 buf_[loc-buf_] = ch;
126 }
127
128 void
129 String::cut(String::size_type newLength)
130 {
131 // size_type is size_t, unsigned. No need to check for newLength <0
132 if (newLength > len_) return;
133
134 len_ = newLength;
135
136 // buf_ may be NULL on zero-length strings.
137 if (len_ == 0 && buf_ == NULL) return;
138
139 buf_[newLength] = '\0';
140 }
141
142 void
143 String::cutPointer(char const *loc)
144 {
145 if (loc < buf_ || loc > (buf_ + size_) ) return;
146
147 len_ = loc-buf_;
148 buf_[len_] = '\0';
149 }
150
151 std::ostream &
152 operator<<(std::ostream& os, String const &aString)
153 {
154 os.write(aString.rawBuf(),aString.size());
155 return os;
156 }
157
158 bool
159 operator<(const String &a, const String &b)
160 {
161 return a.cmp(b) < 0;
162 }
163