]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh) | |
3 | // | |
4 | // Distributed under the Boost Software License, Version 1.0. (See | |
5 | // accompanying file LICENSE_1_0.txt or copy at | |
6 | // http://www.boost.org/LICENSE_1_0.txt) | |
7 | // | |
8 | #ifndef BOOST_LOCALE_BOUNDARY_SEGMENT_HPP_INCLUDED | |
9 | #define BOOST_LOCALE_BOUNDARY_SEGMENT_HPP_INCLUDED | |
10 | #include <boost/locale/config.hpp> | |
11 | #ifdef BOOST_MSVC | |
12 | # pragma warning(push) | |
13 | # pragma warning(disable : 4275 4251 4231 4660) | |
14 | #endif | |
15 | #include <locale> | |
16 | #include <string> | |
17 | #include <iosfwd> | |
18 | #include <iterator> | |
19 | ||
20 | ||
21 | namespace boost { | |
22 | namespace locale { | |
23 | namespace boundary { | |
24 | /// \cond INTERNAL | |
25 | namespace details { | |
26 | template<typename LeftIterator,typename RightIterator> | |
27 | int compare_text(LeftIterator l_begin,LeftIterator l_end,RightIterator r_begin,RightIterator r_end) | |
28 | { | |
29 | typedef LeftIterator left_iterator; | |
30 | typedef typename std::iterator_traits<left_iterator>::value_type char_type; | |
31 | typedef std::char_traits<char_type> traits; | |
32 | while(l_begin!=l_end && r_begin!=r_end) { | |
33 | char_type lchar = *l_begin++; | |
34 | char_type rchar = *r_begin++; | |
35 | if(traits::eq(lchar,rchar)) | |
36 | continue; | |
37 | if(traits::lt(lchar,rchar)) | |
38 | return -1; | |
39 | else | |
40 | return 1; | |
41 | } | |
42 | if(l_begin==l_end && r_begin==r_end) | |
43 | return 0; | |
44 | if(l_begin==l_end) | |
45 | return -1; | |
46 | else | |
47 | return 1; | |
48 | } | |
49 | ||
50 | ||
51 | template<typename Left,typename Right> | |
52 | int compare_text(Left const &l,Right const &r) | |
53 | { | |
54 | return compare_text(l.begin(),l.end(),r.begin(),r.end()); | |
55 | } | |
56 | ||
57 | template<typename Left,typename Char> | |
58 | int compare_string(Left const &l,Char const *begin) | |
59 | { | |
60 | Char const *end = begin; | |
61 | while(*end!=0) | |
62 | end++; | |
63 | return compare_text(l.begin(),l.end(),begin,end); | |
64 | } | |
65 | ||
66 | template<typename Right,typename Char> | |
67 | int compare_string(Char const *begin,Right const &r) | |
68 | { | |
69 | Char const *end = begin; | |
70 | while(*end!=0) | |
71 | end++; | |
72 | return compare_text(begin,end,r.begin(),r.end()); | |
73 | } | |
74 | ||
75 | } | |
76 | /// \endcond | |
77 | ||
78 | /// | |
79 | /// \addtogroup boundary | |
80 | /// @{ | |
81 | ||
82 | /// | |
83 | /// \brief a segment object that represents a pair of two iterators that define the range where | |
84 | /// this segment exits and a rule that defines it. | |
85 | /// | |
86 | /// This type of object is dereferenced by the iterators of segment_index. Using a rule() member function | |
87 | /// you can get a specific rule this segment was selected with. For example, when you use | |
88 | /// word boundary analysis, you can check if the specific word contains Kana letters by checking (rule() & \ref word_kana)!=0 | |
89 | /// For a sentence analysis you can check if the sentence is selected because a sentence terminator is found (\ref sentence_term) or | |
90 | /// there is a line break (\ref sentence_sep). | |
91 | /// | |
92 | /// This object can be automatically converted to std::basic_string with the same type of character. It is also | |
93 | /// valid range that has begin() and end() member functions returning iterators on the location of the segment. | |
94 | /// | |
95 | /// \see | |
96 | /// | |
97 | /// - \ref segment_index | |
98 | /// - \ref boundary_point | |
99 | /// - \ref boundary_point_index | |
100 | /// | |
101 | template<typename IteratorType> | |
102 | class segment : public std::pair<IteratorType,IteratorType> { | |
103 | public: | |
104 | /// | |
105 | /// The type of the underlying character | |
106 | /// | |
107 | typedef typename std::iterator_traits<IteratorType>::value_type char_type; | |
108 | /// | |
109 | /// The type of the string it is converted to | |
110 | /// | |
111 | typedef std::basic_string<char_type> string_type; | |
112 | /// | |
113 | /// The value that iterators return - the character itself | |
114 | /// | |
115 | typedef char_type value_type; | |
116 | /// | |
117 | /// The iterator that allows to iterate the range | |
118 | /// | |
119 | typedef IteratorType iterator; | |
120 | /// | |
121 | /// The iterator that allows to iterate the range | |
122 | /// | |
123 | typedef IteratorType const_iterator; | |
124 | /// | |
125 | /// The type that represent a difference between two iterators | |
126 | /// | |
127 | typedef typename std::iterator_traits<IteratorType>::difference_type difference_type; | |
128 | ||
129 | /// | |
130 | /// Default constructor | |
131 | /// | |
132 | segment() {} | |
133 | /// | |
134 | /// Create a segment using two iterators and a rule that represents this point | |
135 | /// | |
136 | segment(iterator b,iterator e,rule_type r) : | |
137 | std::pair<IteratorType,IteratorType>(b,e), | |
138 | rule_(r) | |
139 | { | |
140 | } | |
141 | /// | |
142 | /// Set the start of the range | |
143 | /// | |
144 | void begin(iterator const &v) | |
145 | { | |
146 | this->first = v; | |
147 | } | |
148 | /// | |
149 | /// Set the end of the range | |
150 | /// | |
151 | void end(iterator const &v) | |
152 | { | |
153 | this->second = v; | |
154 | } | |
155 | ||
156 | /// | |
157 | /// Get the start of the range | |
158 | /// | |
159 | IteratorType begin() const | |
160 | { | |
161 | return this->first; | |
162 | } | |
163 | /// | |
164 | /// Set the end of the range | |
165 | /// | |
166 | IteratorType end() const | |
167 | { | |
168 | return this->second; | |
169 | } | |
170 | ||
171 | /// | |
172 | /// Convert the range to a string automatically | |
173 | /// | |
174 | template <class T, class A> | |
175 | operator std::basic_string<char_type, T, A> ()const | |
176 | { | |
177 | return std::basic_string<char_type, T, A>(this->first, this->second); | |
178 | } | |
179 | ||
180 | /// | |
181 | /// Create a string from the range explicitly | |
182 | /// | |
183 | string_type str() const | |
184 | { | |
185 | return string_type(begin(),end()); | |
186 | } | |
187 | ||
188 | /// | |
189 | /// Get the length of the text chunk | |
190 | /// | |
191 | ||
192 | size_t length() const | |
193 | { | |
194 | return std::distance(begin(),end()); | |
195 | } | |
196 | ||
197 | /// | |
198 | /// Check if the segment is empty | |
199 | /// | |
200 | bool empty() const | |
201 | { | |
202 | return begin() == end(); | |
203 | } | |
204 | ||
205 | /// | |
206 | /// Get the rule that is used for selection of this segment. | |
207 | /// | |
208 | rule_type rule() const | |
209 | { | |
210 | return rule_; | |
211 | } | |
212 | /// | |
213 | /// Set a rule that is used for segment selection | |
214 | /// | |
215 | void rule(rule_type r) | |
216 | { | |
217 | rule_ = r; | |
218 | } | |
219 | ||
220 | // make sure we override std::pair's operator== | |
221 | ||
222 | /// Compare two segments | |
223 | bool operator==(segment const &other) | |
224 | { | |
225 | return details::compare_text(*this,other) == 0; | |
226 | } | |
227 | ||
228 | /// Compare two segments | |
229 | bool operator!=(segment const &other) | |
230 | { | |
231 | return details::compare_text(*this,other) != 0; | |
232 | } | |
233 | ||
234 | private: | |
235 | rule_type rule_; | |
236 | ||
237 | }; | |
238 | ||
239 | ||
240 | /// Compare two segments | |
241 | template<typename IteratorL,typename IteratorR> | |
242 | bool operator==(segment<IteratorL> const &l,segment<IteratorR> const &r) | |
243 | { | |
244 | return details::compare_text(l,r) == 0; | |
245 | } | |
246 | /// Compare two segments | |
247 | template<typename IteratorL,typename IteratorR> | |
248 | bool operator!=(segment<IteratorL> const &l,segment<IteratorR> const &r) | |
249 | { | |
250 | return details::compare_text(l,r) != 0; | |
251 | } | |
252 | ||
253 | /// Compare two segments | |
254 | template<typename IteratorL,typename IteratorR> | |
255 | bool operator<(segment<IteratorL> const &l,segment<IteratorR> const &r) | |
256 | { | |
257 | return details::compare_text(l,r) < 0; | |
258 | } | |
259 | /// Compare two segments | |
260 | template<typename IteratorL,typename IteratorR> | |
261 | bool operator<=(segment<IteratorL> const &l,segment<IteratorR> const &r) | |
262 | { | |
263 | return details::compare_text(l,r) <= 0; | |
264 | } | |
265 | /// Compare two segments | |
266 | template<typename IteratorL,typename IteratorR> | |
267 | bool operator>(segment<IteratorL> const &l,segment<IteratorR> const &r) | |
268 | { | |
269 | return details::compare_text(l,r) > 0; | |
270 | } | |
271 | /// Compare two segments | |
272 | template<typename IteratorL,typename IteratorR> | |
273 | bool operator>=(segment<IteratorL> const &l,segment<IteratorR> const &r) | |
274 | { | |
275 | return details::compare_text(l,r) >= 0; | |
276 | } | |
277 | ||
278 | /// Compare string and segment | |
279 | template<typename CharType,typename Traits,typename Alloc,typename IteratorR> | |
280 | bool operator==(std::basic_string<CharType,Traits,Alloc> const &l,segment<IteratorR> const &r) | |
281 | { | |
282 | return details::compare_text(l,r) == 0; | |
283 | } | |
284 | /// Compare string and segment | |
285 | template<typename CharType,typename Traits,typename Alloc,typename IteratorR> | |
286 | bool operator!=(std::basic_string<CharType,Traits,Alloc> const &l,segment<IteratorR> const &r) | |
287 | { | |
288 | return details::compare_text(l,r) != 0; | |
289 | } | |
290 | ||
291 | /// Compare string and segment | |
292 | template<typename CharType,typename Traits,typename Alloc,typename IteratorR> | |
293 | bool operator<(std::basic_string<CharType,Traits,Alloc> const &l,segment<IteratorR> const &r) | |
294 | { | |
295 | return details::compare_text(l,r) < 0; | |
296 | } | |
297 | /// Compare string and segment | |
298 | template<typename CharType,typename Traits,typename Alloc,typename IteratorR> | |
299 | bool operator<=(std::basic_string<CharType,Traits,Alloc> const &l,segment<IteratorR> const &r) | |
300 | { | |
301 | return details::compare_text(l,r) <= 0; | |
302 | } | |
303 | /// Compare string and segment | |
304 | template<typename CharType,typename Traits,typename Alloc,typename IteratorR> | |
305 | bool operator>(std::basic_string<CharType,Traits,Alloc> const &l,segment<IteratorR> const &r) | |
306 | { | |
307 | return details::compare_text(l,r) > 0; | |
308 | } | |
309 | /// Compare string and segment | |
310 | template<typename CharType,typename Traits,typename Alloc,typename IteratorR> | |
311 | bool operator>=(std::basic_string<CharType,Traits,Alloc> const &l,segment<IteratorR> const &r) | |
312 | { | |
313 | return details::compare_text(l,r) >= 0; | |
314 | } | |
315 | ||
316 | /// Compare string and segment | |
317 | template<typename Iterator,typename CharType,typename Traits,typename Alloc> | |
318 | bool operator==(segment<Iterator> const &l,std::basic_string<CharType,Traits,Alloc> const &r) | |
319 | { | |
320 | return details::compare_text(l,r) == 0; | |
321 | } | |
322 | /// Compare string and segment | |
323 | template<typename Iterator,typename CharType,typename Traits,typename Alloc> | |
324 | bool operator!=(segment<Iterator> const &l,std::basic_string<CharType,Traits,Alloc> const &r) | |
325 | { | |
326 | return details::compare_text(l,r) != 0; | |
327 | } | |
328 | ||
329 | /// Compare string and segment | |
330 | template<typename Iterator,typename CharType,typename Traits,typename Alloc> | |
331 | bool operator<(segment<Iterator> const &l,std::basic_string<CharType,Traits,Alloc> const &r) | |
332 | { | |
333 | return details::compare_text(l,r) < 0; | |
334 | } | |
335 | /// Compare string and segment | |
336 | template<typename Iterator,typename CharType,typename Traits,typename Alloc> | |
337 | bool operator<=(segment<Iterator> const &l,std::basic_string<CharType,Traits,Alloc> const &r) | |
338 | { | |
339 | return details::compare_text(l,r) <= 0; | |
340 | } | |
341 | /// Compare string and segment | |
342 | template<typename Iterator,typename CharType,typename Traits,typename Alloc> | |
343 | bool operator>(segment<Iterator> const &l,std::basic_string<CharType,Traits,Alloc> const &r) | |
344 | { | |
345 | return details::compare_text(l,r) > 0; | |
346 | } | |
347 | /// Compare string and segment | |
348 | template<typename Iterator,typename CharType,typename Traits,typename Alloc> | |
349 | bool operator>=(segment<Iterator> const &l,std::basic_string<CharType,Traits,Alloc> const &r) | |
350 | { | |
351 | return details::compare_text(l,r) >= 0; | |
352 | } | |
353 | ||
354 | ||
355 | /// Compare C string and segment | |
356 | template<typename CharType,typename IteratorR> | |
357 | bool operator==(CharType const *l,segment<IteratorR> const &r) | |
358 | { | |
359 | return details::compare_string(l,r) == 0; | |
360 | } | |
361 | /// Compare C string and segment | |
362 | template<typename CharType,typename IteratorR> | |
363 | bool operator!=(CharType const *l,segment<IteratorR> const &r) | |
364 | { | |
365 | return details::compare_string(l,r) != 0; | |
366 | } | |
367 | ||
368 | /// Compare C string and segment | |
369 | template<typename CharType,typename IteratorR> | |
370 | bool operator<(CharType const *l,segment<IteratorR> const &r) | |
371 | { | |
372 | return details::compare_string(l,r) < 0; | |
373 | } | |
374 | /// Compare C string and segment | |
375 | template<typename CharType,typename IteratorR> | |
376 | bool operator<=(CharType const *l,segment<IteratorR> const &r) | |
377 | { | |
378 | return details::compare_string(l,r) <= 0; | |
379 | } | |
380 | /// Compare C string and segment | |
381 | template<typename CharType,typename IteratorR> | |
382 | bool operator>(CharType const *l,segment<IteratorR> const &r) | |
383 | { | |
384 | return details::compare_string(l,r) > 0; | |
385 | } | |
386 | /// Compare C string and segment | |
387 | template<typename CharType,typename IteratorR> | |
388 | bool operator>=(CharType const *l,segment<IteratorR> const &r) | |
389 | { | |
390 | return details::compare_string(l,r) >= 0; | |
391 | } | |
392 | ||
393 | /// Compare C string and segment | |
394 | template<typename Iterator,typename CharType> | |
395 | bool operator==(segment<Iterator> const &l,CharType const *r) | |
396 | { | |
397 | return details::compare_string(l,r) == 0; | |
398 | } | |
399 | /// Compare C string and segment | |
400 | template<typename Iterator,typename CharType> | |
401 | bool operator!=(segment<Iterator> const &l,CharType const *r) | |
402 | { | |
403 | return details::compare_string(l,r) != 0; | |
404 | } | |
405 | ||
406 | /// Compare C string and segment | |
407 | template<typename Iterator,typename CharType> | |
408 | bool operator<(segment<Iterator> const &l,CharType const *r) | |
409 | { | |
410 | return details::compare_string(l,r) < 0; | |
411 | } | |
412 | /// Compare C string and segment | |
413 | template<typename Iterator,typename CharType> | |
414 | bool operator<=(segment<Iterator> const &l,CharType const *r) | |
415 | { | |
416 | return details::compare_string(l,r) <= 0; | |
417 | } | |
418 | /// Compare C string and segment | |
419 | template<typename Iterator,typename CharType> | |
420 | bool operator>(segment<Iterator> const &l,CharType const *r) | |
421 | { | |
422 | return details::compare_string(l,r) > 0; | |
423 | } | |
424 | /// Compare C string and segment | |
425 | template<typename Iterator,typename CharType> | |
426 | bool operator>=(segment<Iterator> const &l,CharType const *r) | |
427 | { | |
428 | return details::compare_string(l,r) >= 0; | |
429 | } | |
430 | ||
431 | ||
432 | ||
433 | ||
434 | ||
435 | ||
436 | typedef segment<std::string::const_iterator> ssegment; ///< convenience typedef | |
437 | typedef segment<std::wstring::const_iterator> wssegment; ///< convenience typedef | |
438 | #ifdef BOOST_LOCALE_ENABLE_CHAR16_T | |
439 | typedef segment<std::u16string::const_iterator> u16ssegment;///< convenience typedef | |
440 | #endif | |
441 | #ifdef BOOST_LOCALE_ENABLE_CHAR32_T | |
442 | typedef segment<std::u32string::const_iterator> u32ssegment;///< convenience typedef | |
443 | #endif | |
444 | ||
445 | typedef segment<char const *> csegment; ///< convenience typedef | |
446 | typedef segment<wchar_t const *> wcsegment; ///< convenience typedef | |
447 | #ifdef BOOST_LOCALE_ENABLE_CHAR16_T | |
448 | typedef segment<char16_t const *> u16csegment; ///< convenience typedef | |
449 | #endif | |
450 | #ifdef BOOST_LOCALE_ENABLE_CHAR32_T | |
451 | typedef segment<char32_t const *> u32csegment; ///< convenience typedef | |
452 | #endif | |
453 | ||
454 | ||
455 | ||
456 | ||
457 | ||
458 | /// | |
459 | /// Write the segment to the stream character by character | |
460 | /// | |
461 | template<typename CharType,typename TraitsType,typename Iterator> | |
462 | std::basic_ostream<CharType,TraitsType> &operator<<( | |
463 | std::basic_ostream<CharType,TraitsType> &out, | |
464 | segment<Iterator> const &tok) | |
465 | { | |
466 | for(Iterator p=tok.begin(),e=tok.end();p!=e;++p) | |
467 | out << *p; | |
468 | return out; | |
469 | } | |
470 | ||
471 | /// @} | |
472 | ||
473 | } // boundary | |
474 | } // locale | |
475 | } // boost | |
476 | ||
477 | #ifdef BOOST_MSVC | |
478 | #pragma warning(pop) | |
479 | #endif | |
480 | ||
481 | #endif | |
482 | ||
483 | // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 |