1 | // |
2 | // String.h |
3 | // |
4 | // Library: Foundation |
5 | // Package: Core |
6 | // Module: String |
7 | // |
8 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/String.h" |
16 | |
17 | |
18 | namespace Poco { |
19 | |
20 | |
21 | #if defined(POCO_NO_TEMPLATE_ICOMPARE) |
22 | |
23 | |
24 | int icompare(const std::string& str, std::string::size_type pos, std::string::size_type n, std::string::const_iterator it2, std::string::const_iterator end2) |
25 | { |
26 | std::string::size_type sz = str.size(); |
27 | if (pos > sz) pos = sz; |
28 | if (pos + n > sz) n = sz - pos; |
29 | std::string::const_iterator it1 = str.begin() + pos; |
30 | std::string::const_iterator end1 = str.begin() + pos + n; |
31 | while (it1 != end1 && it2 != end2) |
32 | { |
33 | std::string::value_type c1 = Ascii::toLower(*it1); |
34 | std::string::value_type c2 = Ascii::toLower(*it2); |
35 | if (c1 < c2) |
36 | return -1; |
37 | else if (c1 > c2) |
38 | return 1; |
39 | ++it1; ++it2; |
40 | } |
41 | |
42 | if (it1 == end1) |
43 | return it2 == end2 ? 0 : -1; |
44 | else |
45 | return 1; |
46 | } |
47 | |
48 | |
49 | int icompare(const std::string& str1, const std::string& str2) |
50 | { |
51 | return icompare(str1, 0, str1.size(), str2.begin(), str2.end()); |
52 | } |
53 | |
54 | |
55 | int icompare(const std::string& str1, std::string::size_type n1, const std::string& str2, std::string::size_type n2) |
56 | { |
57 | if (n2 > str2.size()) n2 = str2.size(); |
58 | return icompare(str1, 0, n1, str2.begin(), str2.begin() + n2); |
59 | } |
60 | |
61 | |
62 | int icompare(const std::string& str1, std::string::size_type n, const std::string& str2) |
63 | { |
64 | if (n > str2.size()) n = str2.size(); |
65 | return icompare(str1, 0, n, str2.begin(), str2.begin() + n); |
66 | } |
67 | |
68 | |
69 | int icompare(const std::string& str1, std::string::size_type pos, std::string::size_type n, const std::string& str2) |
70 | { |
71 | return icompare(str1, pos, n, str2.begin(), str2.end()); |
72 | } |
73 | |
74 | |
75 | int icompare(const std::string& str1, std::string::size_type pos1, std::string::size_type n1, const std::string& str2, std::string::size_type pos2, std::string::size_type n2) |
76 | { |
77 | std::string::size_type sz2 = str2.size(); |
78 | if (pos2 > sz2) pos2 = sz2; |
79 | if (pos2 + n2 > sz2) n2 = sz2 - pos2; |
80 | return icompare(str1, pos1, n1, str2.begin() + pos2, str2.begin() + pos2 + n2); |
81 | } |
82 | |
83 | |
84 | int icompare(const std::string& str1, std::string::size_type pos1, std::string::size_type n, const std::string& str2, std::string::size_type pos2) |
85 | { |
86 | std::string::size_type sz2 = str2.size(); |
87 | if (pos2 > sz2) pos2 = sz2; |
88 | if (pos2 + n > sz2) n = sz2 - pos2; |
89 | return icompare(str1, pos1, n, str2.begin() + pos2, str2.begin() + pos2 + n); |
90 | } |
91 | |
92 | |
93 | int icompare(const std::string& str, std::string::size_type pos, std::string::size_type n, const std::string::value_type* ptr) |
94 | { |
95 | poco_check_ptr (ptr); |
96 | std::string::size_type sz = str.size(); |
97 | if (pos > sz) pos = sz; |
98 | if (pos + n > sz) n = sz - pos; |
99 | std::string::const_iterator it = str.begin() + pos; |
100 | std::string::const_iterator end = str.begin() + pos + n; |
101 | while (it != end && *ptr) |
102 | { |
103 | std::string::value_type c1 = Ascii::toLower(*it); |
104 | std::string::value_type c2 = Ascii::toLower(*ptr); |
105 | if (c1 < c2) |
106 | return -1; |
107 | else if (c1 > c2) |
108 | return 1; |
109 | ++it; ++ptr; |
110 | } |
111 | |
112 | if (it == end) |
113 | return *ptr == 0 ? 0 : -1; |
114 | else |
115 | return 1; |
116 | } |
117 | |
118 | |
119 | int icompare(const std::string& str, std::string::size_type pos, const std::string::value_type* ptr) |
120 | { |
121 | return icompare(str, pos, str.size() - pos, ptr); |
122 | } |
123 | |
124 | |
125 | int icompare(const std::string& str, const std::string::value_type* ptr) |
126 | { |
127 | return icompare(str, 0, str.size(), ptr); |
128 | } |
129 | |
130 | |
131 | std::string replace(const std::string& str, const std::string& from, const std::string& to, std::string::size_type start) |
132 | { |
133 | std::string result(str); |
134 | replaceInPlace(result, from, to, start); |
135 | return result; |
136 | } |
137 | |
138 | |
139 | std::string replace(const std::string& str, const std::string::value_type* from, const std::string::value_type* to, std::string::size_type start) |
140 | { |
141 | std::string result(str); |
142 | replaceInPlace(result, from, to, start); |
143 | return result; |
144 | } |
145 | |
146 | |
147 | std::string replace(const std::string& str, const std::string::value_type from, const std::string::value_type to, std::string::size_type start) |
148 | { |
149 | std::string result(str); |
150 | replaceInPlace(result, from, to, start); |
151 | return result; |
152 | } |
153 | |
154 | |
155 | std::string remove(const std::string& str, const std::string::value_type ch, std::string::size_type start) |
156 | { |
157 | std::string result(str); |
158 | replaceInPlace(result, ch, 0, start); |
159 | return result; |
160 | } |
161 | |
162 | |
163 | std::string& replaceInPlace(std::string& str, const std::string& from, const std::string& to, std::string::size_type start) |
164 | { |
165 | poco_assert (from.size() > 0); |
166 | |
167 | std::string result; |
168 | std::string::size_type pos = 0; |
169 | result.append(str, 0, start); |
170 | do |
171 | { |
172 | pos = str.find(from, start); |
173 | if (pos != std::string::npos) |
174 | { |
175 | result.append(str, start, pos - start); |
176 | result.append(to); |
177 | start = pos + from.length(); |
178 | } |
179 | else result.append(str, start, str.size() - start); |
180 | } |
181 | while (pos != std::string::npos); |
182 | str.swap(result); |
183 | return str; |
184 | } |
185 | |
186 | |
187 | std::string& replaceInPlace(std::string& str, const std::string::value_type* from, const std::string::value_type* to, std::string::size_type start) |
188 | { |
189 | poco_assert (*from); |
190 | |
191 | std::string result; |
192 | std::string::size_type pos = 0; |
193 | std::string::size_type fromLen = std::strlen(from); |
194 | result.append(str, 0, start); |
195 | do |
196 | { |
197 | pos = str.find(from, start); |
198 | if (pos != std::string::npos) |
199 | { |
200 | result.append(str, start, pos - start); |
201 | result.append(to); |
202 | start = pos + fromLen; |
203 | } |
204 | else result.append(str, start, str.size() - start); |
205 | } |
206 | while (pos != std::string::npos); |
207 | str.swap(result); |
208 | return str; |
209 | } |
210 | |
211 | |
212 | std::string& replaceInPlace(std::string& str, const std::string::value_type from, const std::string::value_type to, std::string::size_type start) |
213 | { |
214 | if (from == to) return str; |
215 | |
216 | std::string::size_type pos = 0; |
217 | do |
218 | { |
219 | pos = str.find(from, start); |
220 | if (pos != std::string::npos) |
221 | { |
222 | if (to) str[pos] = to; |
223 | else str.erase(pos, 1); |
224 | } |
225 | } while (pos != std::string::npos); |
226 | |
227 | return str; |
228 | } |
229 | |
230 | |
231 | std::string& removeInPlace(std::string& str, const std::string::value_type ch, std::string::size_type start) |
232 | { |
233 | return replaceInPlace(str, ch, 0, start); |
234 | } |
235 | |
236 | |
237 | #endif |
238 | |
239 | |
240 | } // namespace Poco |
241 | |