]>
Commit | Line | Data |
---|---|---|
ad80164a | 1 | /* |
2cd0bda2 | 2 | * Copyright (C) 1996-2017 The Squid Software Foundation and contributors |
ad80164a | 3 | * |
bbc27441 AJ |
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. | |
ad80164a | 7 | */ |
8 | ||
bbc27441 AJ |
9 | /* DEBUG: section 67 String */ |
10 | ||
b610642c | 11 | #include <cstring> |
7b270d4c | 12 | |
9e9ef416 | 13 | String::String() : size_(0), len_(0), buf_(NULL) |
ad80164a | 14 | { |
15 | #if DEBUGSTRINGS | |
30abd221 | 16 | StringRegistry::Instance().add(this); |
ad80164a | 17 | #endif |
18 | } | |
19 | ||
a7a42b14 | 20 | String::size_type |
30abd221 | 21 | String::size() const |
ad80164a | 22 | { |
23 | return len_; | |
24 | } | |
25 | ||
99524de7 FC |
26 | char const * |
27 | String::rawBuf() const | |
28 | { | |
29 | return buf_; | |
30 | } | |
31 | ||
32 | char const * | |
33 | String::termedBuf() const | |
ad80164a | 34 | { |
35 | return buf_; | |
36 | } | |
37 | ||
b4f2886c | 38 | char |
18ec8500 | 39 | String::operator [](unsigned int aPos) const |
3db44fdf | 40 | { |
18ec8500 | 41 | assert(aPos < size_); |
3db44fdf | 42 | |
18ec8500 | 43 | return buf_[aPos]; |
3db44fdf | 44 | } |
45 | ||
15267b22 AR |
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 | |
ad80164a | 50 | { |
15267b22 AR |
51 | if (!thisIsNilOrEmpty && !otherIsNilOrEmpty) |
52 | return false; // result does not matter | |
ad80164a | 53 | |
15267b22 AR |
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 | } | |
ad80164a | 63 | |
15267b22 | 64 | int |
9e9ef416 | 65 | String::cmp(char const *aString) const |
15267b22 AR |
66 | { |
67 | int result = 0; | |
68 | if (nilCmp(!size(), (!aString || !*aString), result)) | |
69 | return result; | |
ad80164a | 70 | |
cb3b44fc | 71 | return strcmp(termedBuf(), aString); |
ad80164a | 72 | } |
73 | ||
74 | int | |
9e9ef416 | 75 | String::cmp(char const *aString, String::size_type count) const |
ad80164a | 76 | { |
15267b22 AR |
77 | int result = 0; |
78 | if (nilCmp((!size() || !count), (!aString || !*aString || !count), result)) | |
79 | return result; | |
ad80164a | 80 | |
cb3b44fc | 81 | return strncmp(termedBuf(), aString, count); |
ad80164a | 82 | } |
83 | ||
84 | int | |
9e9ef416 | 85 | String::cmp(String const &aString) const |
ad80164a | 86 | { |
15267b22 AR |
87 | int result = 0; |
88 | if (nilCmp(!size(), !aString.size(), result)) | |
89 | return result; | |
ad80164a | 90 | |
cb3b44fc | 91 | return strcmp(termedBuf(), aString.termedBuf()); |
30abd221 | 92 | } |
93 | ||
94 | int | |
d968c095 | 95 | String::caseCmp(char const *aString) const |
30abd221 | 96 | { |
15267b22 AR |
97 | int result = 0; |
98 | if (nilCmp(!size(), (!aString || !*aString), result)) | |
99 | return result; | |
100 | ||
cb3b44fc | 101 | return strcasecmp(termedBuf(), aString); |
30abd221 | 102 | } |
103 | ||
104 | int | |
a7a42b14 | 105 | String::caseCmp(char const *aString, String::size_type count) const |
30abd221 | 106 | { |
15267b22 AR |
107 | int result = 0; |
108 | if (nilCmp((!size() || !count), (!aString || !*aString || !count), result)) | |
109 | return result; | |
110 | ||
cb3b44fc | 111 | return strncasecmp(termedBuf(), aString, count); |
ad80164a | 112 | } |
113 | ||
9d7a89a5 FC |
114 | int |
115 | String::caseCmp(const String &str) const | |
116 | { | |
117 | return caseCmp(str.rawBuf(),str.size()); | |
118 | } | |
119 | ||
ad80164a | 120 | void |
d968c095 | 121 | String::set(char const *loc, char const ch) |
ad80164a | 122 | { |
26ac0430 | 123 | if (loc < buf_ || loc > (buf_ + size_) ) return; |
d968c095 | 124 | |
30abd221 | 125 | buf_[loc-buf_] = ch; |
ad80164a | 126 | } |
127 | ||
ad80164a | 128 | void |
a7a42b14 | 129 | String::cut(String::size_type newLength) |
ad80164a | 130 | { |
f2a972b9 FC |
131 | // size_type is size_t, unsigned. No need to check for newLength <0 |
132 | if (newLength > len_) return; | |
d968c095 | 133 | |
ad80164a | 134 | len_ = newLength; |
7b270d4c | 135 | |
e1f7507e | 136 | // buf_ may be NULL on zero-length strings. |
26ac0430 | 137 | if (len_ == 0 && buf_ == NULL) return; |
e1f7507e | 138 | |
30abd221 | 139 | buf_[newLength] = '\0'; |
ad80164a | 140 | } |
141 | ||
ad80164a | 142 | void |
d968c095 | 143 | String::cutPointer(char const *loc) |
ad80164a | 144 | { |
26ac0430 | 145 | if (loc < buf_ || loc > (buf_ + size_) ) return; |
d968c095 | 146 | |
ad80164a | 147 | len_ = loc-buf_; |
30abd221 | 148 | buf_[len_] = '\0'; |
ad80164a | 149 | } |
30abd221 | 150 | |
151 | std::ostream & | |
152 | operator<<(std::ostream& os, String const &aString) | |
153 | { | |
cb3b44fc | 154 | os.write(aString.rawBuf(),aString.size()); |
30abd221 | 155 | return os; |
156 | } | |
634592a6 FC |
157 | |
158 | bool | |
159 | operator<(const String &a, const String &b) | |
160 | { | |
161 | return a.cmp(b) < 0; | |
162 | } | |
f53969cc | 163 |