1 | // |
---|
2 | // UTF8String.cpp |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/src/UTF8String.cpp#2 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Text |
---|
8 | // Module: UTF8String |
---|
9 | // |
---|
10 | // Copyright (c) 2007, 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/UTF8String.h> |
---|
38 | #include <Poco/Unicode.h> |
---|
39 | #include <Poco/TextIterator.h> |
---|
40 | #include <Poco/TextConverter.h> |
---|
41 | #include <Poco/UTF8Encoding.h> |
---|
42 | #include <algorithm> |
---|
43 | |
---|
44 | |
---|
45 | namespace Poco { |
---|
46 | |
---|
47 | |
---|
48 | int UTF8::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) |
---|
49 | { |
---|
50 | static UTF8Encoding utf8; |
---|
51 | |
---|
52 | std::string::size_type sz = str.size(); |
---|
53 | if (pos > sz) pos = sz; |
---|
54 | if (pos + n > sz) n = sz - pos; |
---|
55 | TextIterator uit1(str.begin() + pos, str.begin() + pos + n, utf8); |
---|
56 | TextIterator uend1(str.begin() + pos + n); |
---|
57 | TextIterator uit2(it2, end2, utf8); |
---|
58 | TextIterator uend2(end2); |
---|
59 | while (uit1 != uend1 && uit2 != uend2) |
---|
60 | { |
---|
61 | int c1 = Unicode::toLower(*uit1); |
---|
62 | int c2 = Unicode::toLower(*uit2); |
---|
63 | if (c1 < c2) |
---|
64 | return -1; |
---|
65 | else if (c1 > c2) |
---|
66 | return 1; |
---|
67 | ++uit1; ++uit2; |
---|
68 | } |
---|
69 | |
---|
70 | if (uit1 == uend1) |
---|
71 | return uit2 == uend2 ? 0 : -1; |
---|
72 | else |
---|
73 | return 1; |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | int UTF8::icompare(const std::string& str1, const std::string& str2) |
---|
78 | { |
---|
79 | return icompare(str1, 0, str1.size(), str2.begin(), str2.end()); |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | int UTF8::icompare(const std::string& str1, std::string::size_type n1, const std::string& str2, std::string::size_type n2) |
---|
84 | { |
---|
85 | if (n2 > str2.size()) n2 = str2.size(); |
---|
86 | return icompare(str1, 0, n1, str2.begin(), str2.begin() + n2); |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | int UTF8::icompare(const std::string& str1, std::string::size_type n, const std::string& str2) |
---|
91 | { |
---|
92 | if (n > str2.size()) n = str2.size(); |
---|
93 | return icompare(str1, 0, n, str2.begin(), str2.begin() + n); |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | int UTF8::icompare(const std::string& str1, std::string::size_type pos, std::string::size_type n, const std::string& str2) |
---|
98 | { |
---|
99 | return icompare(str1, pos, n, str2.begin(), str2.end()); |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | int UTF8::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) |
---|
104 | { |
---|
105 | std::string::size_type sz2 = str2.size(); |
---|
106 | if (pos2 > sz2) pos2 = sz2; |
---|
107 | if (pos2 + n2 > sz2) n2 = sz2 - pos2; |
---|
108 | return icompare(str1, pos1, n1, str2.begin() + pos2, str2.begin() + pos2 + n2); |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | int UTF8::icompare(const std::string& str1, std::string::size_type pos1, std::string::size_type n, const std::string& str2, std::string::size_type pos2) |
---|
113 | { |
---|
114 | std::string::size_type sz2 = str2.size(); |
---|
115 | if (pos2 > sz2) pos2 = sz2; |
---|
116 | if (pos2 + n > sz2) n = sz2 - pos2; |
---|
117 | return icompare(str1, pos1, n, str2.begin() + pos2, str2.begin() + pos2 + n); |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | int UTF8::icompare(const std::string& str, std::string::size_type pos, std::string::size_type n, const std::string::value_type* ptr) |
---|
122 | { |
---|
123 | static UTF8Encoding utf8; |
---|
124 | |
---|
125 | poco_check_ptr (ptr); |
---|
126 | std::string::size_type sz = str.size(); |
---|
127 | if (pos > sz) pos = sz; |
---|
128 | if (pos + n > sz) n = sz - pos; |
---|
129 | TextIterator uit(str.begin() + pos, str.begin() + pos + n, utf8); |
---|
130 | TextIterator uend(str.begin() + pos + n); |
---|
131 | while (uit != uend && *ptr) |
---|
132 | { |
---|
133 | int c1 = Unicode::toLower(*uit); |
---|
134 | int c2 = Unicode::toLower(*ptr); |
---|
135 | if (c1 < c2) |
---|
136 | return -1; |
---|
137 | else if (c1 > c2) |
---|
138 | return 1; |
---|
139 | ++uit; ++ptr; |
---|
140 | } |
---|
141 | |
---|
142 | if (uit == uend) |
---|
143 | return *ptr == 0 ? 0 : -1; |
---|
144 | else |
---|
145 | return 1; |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | int UTF8::icompare(const std::string& str, std::string::size_type pos, const std::string::value_type* ptr) |
---|
150 | { |
---|
151 | return icompare(str, pos, str.size() - pos, ptr); |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | int UTF8::icompare(const std::string& str, const std::string::value_type* ptr) |
---|
156 | { |
---|
157 | return icompare(str, 0, str.size(), ptr); |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | std::string UTF8::toUpper(const std::string& str) |
---|
162 | { |
---|
163 | static UTF8Encoding utf8; |
---|
164 | std::string result; |
---|
165 | TextConverter converter(utf8, utf8); |
---|
166 | converter.convert(str, result, Unicode::toUpper); |
---|
167 | return result; |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | std::string& UTF8::toUpperInPlace(std::string& str) |
---|
172 | { |
---|
173 | static UTF8Encoding utf8; |
---|
174 | std::string result; |
---|
175 | TextConverter converter(utf8, utf8); |
---|
176 | converter.convert(str, result, Unicode::toUpper); |
---|
177 | std::swap(str, result); |
---|
178 | return str; |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | std::string UTF8::toLower(const std::string& str) |
---|
183 | { |
---|
184 | static UTF8Encoding utf8; |
---|
185 | std::string result; |
---|
186 | TextConverter converter(utf8, utf8); |
---|
187 | converter.convert(str, result, Unicode::toLower); |
---|
188 | return result; |
---|
189 | } |
---|
190 | |
---|
191 | |
---|
192 | std::string& UTF8::toLowerInPlace(std::string& str) |
---|
193 | { |
---|
194 | static UTF8Encoding utf8; |
---|
195 | std::string result; |
---|
196 | TextConverter converter(utf8, utf8); |
---|
197 | converter.convert(str, result, Unicode::toLower); |
---|
198 | std::swap(str, result); |
---|
199 | return str; |
---|
200 | } |
---|
201 | |
---|
202 | |
---|
203 | } // namespace Poco |
---|