1 | // |
---|
2 | // String.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/src/String.cpp#2 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Core |
---|
8 | // Module: String |
---|
9 | // |
---|
10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
11 | // and Contributors. |
---|
12 | // |
---|
13 | // Permission is hereby granted, free of charge, to any person or organization |
---|
14 | // obtaining a copy of the software and accompanying documentation covered by |
---|
15 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
16 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
17 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
18 | // do so, all subject to the following: |
---|
19 | // |
---|
20 | // The copyright notices in the Software and this entire statement, including |
---|
21 | // the above license grant, this restriction and the following disclaimer, |
---|
22 | // must be included in all copies of the Software, in whole or in part, and |
---|
23 | // all derivative works of the Software, unless such copies or derivative |
---|
24 | // works are solely in the form of machine-executable object code generated by |
---|
25 | // a source language processor. |
---|
26 | // |
---|
27 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
28 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
29 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
30 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
31 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
32 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
33 | // DEALINGS IN THE SOFTWARE. |
---|
34 | // |
---|
35 | |
---|
36 | |
---|
37 | #include "Poco/String.h" |
---|
38 | |
---|
39 | |
---|
40 | namespace Poco { |
---|
41 | |
---|
42 | |
---|
43 | #if defined(POCO_NO_TEMPLATE_ICOMPARE) |
---|
44 | |
---|
45 | |
---|
46 | 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) |
---|
47 | { |
---|
48 | std::string::size_type sz = str.size(); |
---|
49 | if (pos > sz) pos = sz; |
---|
50 | if (pos + n > sz) n = sz - pos; |
---|
51 | std::string::const_iterator it1 = str.begin() + pos; |
---|
52 | std::string::const_iterator end1 = str.begin() + pos + n; |
---|
53 | while (it1 != end1 && it2 != end2) |
---|
54 | { |
---|
55 | std::string::value_type c1 = std::tolower(*it1); |
---|
56 | std::string::value_type c2 = std::tolower(*it2); |
---|
57 | if (c1 < c2) |
---|
58 | return -1; |
---|
59 | else if (c1 > c2) |
---|
60 | return 1; |
---|
61 | ++it1; ++it2; |
---|
62 | } |
---|
63 | |
---|
64 | if (it1 == end1) |
---|
65 | return it2 == end2 ? 0 : -1; |
---|
66 | else |
---|
67 | return 1; |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | int icompare(const std::string& str1, const std::string& str2) |
---|
72 | { |
---|
73 | return icompare(str1, 0, str1.size(), str2.begin(), str2.end()); |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | int icompare(const std::string& str1, std::string::size_type n1, const std::string& str2, std::string::size_type n2) |
---|
78 | { |
---|
79 | if (n2 > str2.size()) n2 = str2.size(); |
---|
80 | return icompare(str1, 0, n1, str2.begin(), str2.begin() + n2); |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | int icompare(const std::string& str1, std::string::size_type n, const std::string& str2) |
---|
85 | { |
---|
86 | if (n > str2.size()) n = str2.size(); |
---|
87 | return icompare(str1, 0, n, str2.begin(), str2.begin() + n); |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | int icompare(const std::string& str1, std::string::size_type pos, std::string::size_type n, const std::string& str2) |
---|
92 | { |
---|
93 | return icompare(str1, pos, n, str2.begin(), str2.end()); |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | 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) |
---|
98 | { |
---|
99 | std::string::size_type sz2 = str2.size(); |
---|
100 | if (pos2 > sz2) pos2 = sz2; |
---|
101 | if (pos2 + n2 > sz2) n2 = sz2 - pos2; |
---|
102 | return icompare(str1, pos1, n1, str2.begin() + pos2, str2.begin() + pos2 + n2); |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | 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) |
---|
107 | { |
---|
108 | std::string::size_type sz2 = str2.size(); |
---|
109 | if (pos2 > sz2) pos2 = sz2; |
---|
110 | if (pos2 + n > sz2) n = sz2 - pos2; |
---|
111 | return icompare(str1, pos1, n, str2.begin() + pos2, str2.begin() + pos2 + n); |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | int icompare(const std::string& str, std::string::size_type pos, std::string::size_type n, const std::string::value_type* ptr) |
---|
116 | { |
---|
117 | poco_check_ptr (ptr); |
---|
118 | std::string::size_type sz = str.size(); |
---|
119 | if (pos > sz) pos = sz; |
---|
120 | if (pos + n > sz) n = sz - pos; |
---|
121 | std::string::const_iterator it = str.begin() + pos; |
---|
122 | std::string::const_iterator end = str.begin() + pos + n; |
---|
123 | while (it != end && *ptr) |
---|
124 | { |
---|
125 | std::string::value_type c1 = std::tolower(*it); |
---|
126 | std::string::value_type c2 = std::tolower(*ptr); |
---|
127 | if (c1 < c2) |
---|
128 | return -1; |
---|
129 | else if (c1 > c2) |
---|
130 | return 1; |
---|
131 | ++it; ++ptr; |
---|
132 | } |
---|
133 | |
---|
134 | if (it == end) |
---|
135 | return *ptr == 0 ? 0 : -1; |
---|
136 | else |
---|
137 | return 1; |
---|
138 | } |
---|
139 | |
---|
140 | |
---|
141 | int icompare(const std::string& str, std::string::size_type pos, const std::string::value_type* ptr) |
---|
142 | { |
---|
143 | return icompare(str, pos, str.size() - pos, ptr); |
---|
144 | } |
---|
145 | |
---|
146 | |
---|
147 | int icompare(const std::string& str, const std::string::value_type* ptr) |
---|
148 | { |
---|
149 | return icompare(str, 0, str.size(), ptr); |
---|
150 | } |
---|
151 | |
---|
152 | |
---|
153 | std::string replace(const std::string& str, const std::string& from, const std::string& to, std::string::size_type start) |
---|
154 | { |
---|
155 | std::string result(str); |
---|
156 | replaceInPlace(result, from, to, start); |
---|
157 | return result; |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | std::string replace(const std::string& str, const std::string::value_type* from, const std::string::value_type* to, std::string::size_type start) |
---|
162 | { |
---|
163 | std::string result(str); |
---|
164 | replaceInPlace(result, from, to, start); |
---|
165 | return result; |
---|
166 | } |
---|
167 | |
---|
168 | |
---|
169 | std::string& replaceInPlace(std::string& str, const std::string& from, const std::string& to, std::string::size_type start) |
---|
170 | { |
---|
171 | poco_assert (from.size() > 0); |
---|
172 | |
---|
173 | std::string result; |
---|
174 | std::string::size_type pos = 0; |
---|
175 | result.append(str, 0, start); |
---|
176 | do |
---|
177 | { |
---|
178 | pos = str.find(from, start); |
---|
179 | if (pos != std::string::npos) |
---|
180 | { |
---|
181 | result.append(str, start, pos - start); |
---|
182 | result.append(to); |
---|
183 | start = pos + from.length(); |
---|
184 | } |
---|
185 | else result.append(str, start, str.size() - start); |
---|
186 | } |
---|
187 | while (pos != std::string::npos); |
---|
188 | str.swap(result); |
---|
189 | return str; |
---|
190 | } |
---|
191 | |
---|
192 | |
---|
193 | std::string& replaceInPlace(std::string& str, const std::string::value_type* from, const std::string::value_type* to, std::string::size_type start) |
---|
194 | { |
---|
195 | poco_assert (*from); |
---|
196 | |
---|
197 | std::string result; |
---|
198 | std::string::size_type pos = 0; |
---|
199 | std::string::size_type fromLen = std::strlen(from); |
---|
200 | result.append(str, 0, start); |
---|
201 | do |
---|
202 | { |
---|
203 | pos = str.find(from, start); |
---|
204 | if (pos != std::string::npos) |
---|
205 | { |
---|
206 | result.append(str, start, pos - start); |
---|
207 | result.append(to); |
---|
208 | start = pos + fromLen; |
---|
209 | } |
---|
210 | else result.append(str, start, str.size() - start); |
---|
211 | } |
---|
212 | while (pos != std::string::npos); |
---|
213 | str.swap(result); |
---|
214 | return str; |
---|
215 | } |
---|
216 | |
---|
217 | |
---|
218 | #endif |
---|
219 | |
---|
220 | |
---|
221 | } // namespace Poco |
---|