Boost.Locale
index.hpp
1//
2// Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
3//
4// Distributed under the Boost Software License, Version 1.0.
5// https://www.boost.org/LICENSE_1_0.txt
6
7#ifndef BOOST_LOCALE_BOUNDARY_INDEX_HPP_INCLUDED
8#define BOOST_LOCALE_BOUNDARY_INDEX_HPP_INCLUDED
9
10#include <boost/locale/boundary/boundary_point.hpp>
11#include <boost/locale/boundary/facets.hpp>
12#include <boost/locale/boundary/segment.hpp>
13#include <boost/locale/boundary/types.hpp>
14#include <boost/iterator/iterator_facade.hpp>
15#include <algorithm>
16#include <cstdint>
17#include <iterator>
18#include <locale>
19#include <memory>
20#include <stdexcept>
21#include <string>
22#include <type_traits>
23#include <vector>
24
25#ifdef BOOST_MSVC
26# pragma warning(push)
27# pragma warning(disable : 4275 4251 4231 4660)
28#endif
29
30namespace boost { namespace locale { namespace boundary {
39
41
42 namespace detail {
43 template<typename Char>
44 const boundary_indexing<Char>& get_boundary_indexing(const std::locale& l)
45 {
46 using facet_type = boundary_indexing<Char>;
47 if(!std::has_facet<facet_type>(l))
48 throw std::runtime_error("Locale was generated without segmentation support!");
49 return std::use_facet<facet_type>(l);
50 }
51
52 template<typename IteratorType,
53 typename CategoryType = typename std::iterator_traits<IteratorType>::iterator_category>
54 struct mapping_traits {
55 typedef typename std::iterator_traits<IteratorType>::value_type char_type;
56 static index_type map(boundary_type t, IteratorType b, IteratorType e, const std::locale& l)
57 {
58 std::basic_string<char_type> str(b, e);
59 return get_boundary_indexing<char_type>(l).map(t, str.c_str(), str.c_str() + str.size());
60 }
61 };
62
63 template<typename CharType, typename SomeIteratorType>
64 struct linear_iterator_traits {
65 static constexpr bool is_linear =
66 std::is_same<SomeIteratorType, CharType*>::value || std::is_same<SomeIteratorType, const CharType*>::value
67 || std::is_same<SomeIteratorType, typename std::basic_string<CharType>::iterator>::value
68 || std::is_same<SomeIteratorType, typename std::basic_string<CharType>::const_iterator>::value
69 || std::is_same<SomeIteratorType, typename std::vector<CharType>::iterator>::value
70 || std::is_same<SomeIteratorType, typename std::vector<CharType>::const_iterator>::value;
71 };
72
73 template<typename IteratorType>
74 struct mapping_traits<IteratorType, std::random_access_iterator_tag> {
75 typedef typename std::iterator_traits<IteratorType>::value_type char_type;
76
77 static index_type map(boundary_type t, IteratorType b, IteratorType e, const std::locale& l)
78 {
79 index_type result;
80
81 // Optimize for most common cases
82 //
83 // C++11 requires that string is continuous in memory and all known
84 // string implementations do this because of c_str() support.
85
86 if(linear_iterator_traits<char_type, IteratorType>::is_linear && b != e) {
87 const char_type* begin = &*b;
88 const char_type* end = begin + (e - b);
89 index_type tmp = get_boundary_indexing<char_type>(l).map(t, begin, end);
90 result.swap(tmp);
91 } else {
92 std::basic_string<char_type> str(b, e);
93 index_type tmp = get_boundary_indexing<char_type>(l).map(t, str.c_str(), str.c_str() + str.size());
94 result.swap(tmp);
95 }
96 return result;
97 }
98 };
99
100 template<typename BaseIterator>
101 class mapping {
102 public:
103 typedef BaseIterator base_iterator;
104 typedef typename std::iterator_traits<base_iterator>::value_type char_type;
105
106 mapping(boundary_type type, base_iterator begin, base_iterator end, const std::locale& loc) :
107 index_(new index_type()), begin_(begin), end_(end)
108 {
109 index_type idx = detail::mapping_traits<base_iterator>::map(type, begin, end, loc);
110 index_->swap(idx);
111 }
112
113 mapping() {}
114
115 const index_type& index() const { return *index_; }
116
117 base_iterator begin() const { return begin_; }
118
119 base_iterator end() const { return end_; }
120
121 private:
122 std::shared_ptr<index_type> index_;
123 base_iterator begin_, end_;
124 };
125
126 template<typename BaseIterator>
127 class segment_index_iterator : public boost::iterator_facade<segment_index_iterator<BaseIterator>,
128 segment<BaseIterator>,
129 boost::bidirectional_traversal_tag,
130 const segment<BaseIterator>&> {
131 public:
132 typedef BaseIterator base_iterator;
133 typedef mapping<base_iterator> mapping_type;
134 typedef segment<base_iterator> segment_type;
135
136 segment_index_iterator() : current_(0, 0), map_(nullptr), mask_(0), full_select_(false) {}
137
138 segment_index_iterator(base_iterator p, const mapping_type* map, rule_type mask, bool full_select) :
139 map_(map), mask_(mask), full_select_(full_select)
140 {
141 set(p);
142 }
143 segment_index_iterator(bool is_begin, const mapping_type* map, rule_type mask, bool full_select) :
144 map_(map), mask_(mask), full_select_(full_select)
145 {
146 if(is_begin)
147 set_begin();
148 else
149 set_end();
150 }
151
152 const segment_type& dereference() const { return value_; }
153
154 bool equal(const segment_index_iterator& other) const
155 {
156 return map_ == other.map_ && current_.second == other.current_.second;
157 }
158
159 void increment()
160 {
161 std::pair<size_t, size_t> next = current_;
162 if(full_select_) {
163 next.first = next.second;
164 while(next.second < size()) {
165 next.second++;
166 if(valid_offset(next.second))
167 break;
168 }
169 if(next.second == size())
170 next.first = next.second - 1;
171 } else {
172 while(next.second < size()) {
173 next.first = next.second;
174 next.second++;
175 if(valid_offset(next.second))
176 break;
177 }
178 }
179 update_current(next);
180 }
181
182 void decrement()
183 {
184 std::pair<size_t, size_t> next = current_;
185 if(full_select_) {
186 while(next.second > 1) {
187 next.second--;
188 if(valid_offset(next.second))
189 break;
190 }
191 next.first = next.second;
192 while(next.first > 0) {
193 next.first--;
194 if(valid_offset(next.first))
195 break;
196 }
197 } else {
198 while(next.second > 1) {
199 next.second--;
200 if(valid_offset(next.second))
201 break;
202 }
203 next.first = next.second - 1;
204 }
205 update_current(next);
206 }
207
208 private:
209 void set_end()
210 {
211 current_.first = size() - 1;
212 current_.second = size();
213 value_ = segment_type(map_->end(), map_->end(), 0);
214 }
215 void set_begin()
216 {
217 current_.first = current_.second = 0;
218 value_ = segment_type(map_->begin(), map_->begin(), 0);
219 increment();
220 }
221
222 void set(base_iterator p)
223 {
224 const auto b = map_->index().begin(), e = map_->index().end();
225 auto boundary_point = std::upper_bound(b, e, break_info(std::distance(map_->begin(), p)));
226 while(boundary_point != e && (boundary_point->rule & mask_) == 0)
227 ++boundary_point;
228
229 current_.first = current_.second = boundary_point - b;
230
231 if(full_select_) {
232 while(current_.first > 0) {
233 current_.first--;
234 if(valid_offset(current_.first))
235 break;
236 }
237 } else {
238 if(current_.first > 0)
239 current_.first--;
240 }
241 value_.first = map_->begin();
242 std::advance(value_.first, get_offset(current_.first));
243 value_.second = value_.first;
244 std::advance(value_.second, get_offset(current_.second) - get_offset(current_.first));
245
246 update_rule();
247 }
248
249 void update_current(std::pair<size_t, size_t> pos)
250 {
251 std::ptrdiff_t first_diff = get_offset(pos.first) - get_offset(current_.first);
252 std::ptrdiff_t second_diff = get_offset(pos.second) - get_offset(current_.second);
253 std::advance(value_.first, first_diff);
254 std::advance(value_.second, second_diff);
255 current_ = pos;
256 update_rule();
257 }
258
259 void update_rule()
260 {
261 if(current_.second != size())
262 value_.rule(index()[current_.second].rule);
263 }
264 size_t get_offset(size_t ind) const
265 {
266 if(ind == size())
267 return index().back().offset;
268 return index()[ind].offset;
269 }
270
271 bool valid_offset(size_t offset) const
272 {
273 return offset == 0 || offset == size() // make sure we not acess index[size]
274 || (index()[offset].rule & mask_) != 0;
275 }
276
277 size_t size() const { return index().size(); }
278
279 const index_type& index() const { return map_->index(); }
280
281 segment_type value_;
282 std::pair<size_t, size_t> current_;
283 const mapping_type* map_;
284 rule_type mask_;
285 bool full_select_;
286 };
287
288 template<typename BaseIterator>
289 class boundary_point_index_iterator : public boost::iterator_facade<boundary_point_index_iterator<BaseIterator>,
290 boundary_point<BaseIterator>,
291 boost::bidirectional_traversal_tag,
292 const boundary_point<BaseIterator>&> {
293 public:
294 typedef BaseIterator base_iterator;
295 typedef mapping<base_iterator> mapping_type;
296 typedef boundary_point<base_iterator> boundary_point_type;
297
298 boundary_point_index_iterator() : current_(0), map_(nullptr), mask_(0) {}
299
300 boundary_point_index_iterator(bool is_begin, const mapping_type* map, rule_type mask) :
301 map_(map), mask_(mask)
302 {
303 if(is_begin)
304 set_begin();
305 else
306 set_end();
307 }
308 boundary_point_index_iterator(base_iterator p, const mapping_type* map, rule_type mask) :
309 map_(map), mask_(mask)
310 {
311 set(p);
312 }
313
314 const boundary_point_type& dereference() const { return value_; }
315
316 bool equal(const boundary_point_index_iterator& other) const
317 {
318 return map_ == other.map_ && current_ == other.current_;
319 }
320
321 void increment()
322 {
323 size_t next = current_;
324 while(next < size()) {
325 next++;
326 if(valid_offset(next))
327 break;
328 }
329 update_current(next);
330 }
331
332 void decrement()
333 {
334 size_t next = current_;
335 while(next > 0) {
336 next--;
337 if(valid_offset(next))
338 break;
339 }
340 update_current(next);
341 }
342
343 private:
344 void set_end()
345 {
346 current_ = size();
347 value_ = boundary_point_type(map_->end(), 0);
348 }
349 void set_begin()
350 {
351 current_ = 0;
352 value_ = boundary_point_type(map_->begin(), 0);
353 }
354
355 void set(base_iterator p)
356 {
357 size_t dist = std::distance(map_->begin(), p);
358
359 const auto b = index().begin(), e = index().end();
360 const auto ptr = std::lower_bound(b, e, break_info(dist));
361
362 if(ptr == e)
363 current_ = size() - 1;
364 else
365 current_ = ptr - b;
366
367 while(!valid_offset(current_))
368 current_++;
369
370 std::ptrdiff_t diff = get_offset(current_) - dist;
371 std::advance(p, diff);
372 value_.iterator(p);
373 update_rule();
374 }
375
376 void update_current(size_t pos)
377 {
378 std::ptrdiff_t diff = get_offset(pos) - get_offset(current_);
379 base_iterator i = value_.iterator();
380 std::advance(i, diff);
381 current_ = pos;
382 value_.iterator(i);
383 update_rule();
384 }
385
386 void update_rule()
387 {
388 if(current_ != size())
389 value_.rule(index()[current_].rule);
390 }
391 size_t get_offset(size_t ind) const
392 {
393 if(ind == size())
394 return index().back().offset;
395 return index()[ind].offset;
396 }
397
398 bool valid_offset(size_t offset) const
399 {
400 return offset == 0 || offset + 1 >= size() // last and first are always valid regardless of mark
401 || (index()[offset].rule & mask_) != 0;
402 }
403
404 size_t size() const { return index().size(); }
405
406 const index_type& index() const { return map_->index(); }
407
408 boundary_point_type value_;
409 size_t current_;
410 const mapping_type* map_;
411 rule_type mask_;
412 };
413
414 } // namespace detail
415
417
418 template<typename BaseIterator>
419 class segment_index;
420
421 template<typename BaseIterator>
422 class boundary_point_index;
423
473
474 template<typename BaseIterator>
476 public:
478 typedef BaseIterator base_iterator;
479
480#ifdef BOOST_LOCALE_DOXYGEN
493 typedef unspecified_iterator_type iterator;
495 typedef unspecified_iterator_type const_iterator;
496#else
497 typedef detail::segment_index_iterator<base_iterator> iterator;
498 typedef detail::segment_index_iterator<base_iterator> const_iterator;
499#endif
503
511 segment_index() : mask_(0xFFFFFFFFu), full_select_(false) {}
517 rule_type mask,
518 const std::locale& loc = std::locale()) :
519 map_(type, begin, end, loc),
520 mask_(mask), full_select_(false)
521 {}
527 const std::locale& loc = std::locale()) :
528 map_(type, begin, end, loc),
529 mask_(0xFFFFFFFFu), full_select_(false)
530 {}
531
541
551
556 void map(boundary_type type, base_iterator begin, base_iterator end, const std::locale& loc = std::locale())
557 {
558 map_ = mapping_type(type, begin, end, loc);
559 }
560
569 {
570 return iterator(true, &map_, mask_, full_select_);
571 }
572
578 iterator end() const
579 {
580 return iterator(false, &map_, mask_, full_select_);
581 }
582
599 {
600 return iterator(p, &map_, mask_, full_select_);
601 }
602
605 {
606 return mask_;
607 }
610 {
611 mask_ = v;
612 }
613
624 bool full_select() const
625 {
626 return full_select_;
627 }
628
639 void full_select(bool v)
640 {
641 full_select_ = v;
642 }
643
644 private:
646 typedef detail::mapping<base_iterator> mapping_type;
647 mapping_type map_;
648 rule_type mask_;
649 bool full_select_;
650 };
651
696 template<typename BaseIterator>
698 public:
700 typedef BaseIterator base_iterator;
701
702#ifdef BOOST_LOCALE_DOXYGEN
716 typedef unspecified_iterator_type iterator;
718 typedef unspecified_iterator_type const_iterator;
719#else
720 typedef detail::boundary_point_index_iterator<base_iterator> iterator;
721 typedef detail::boundary_point_index_iterator<base_iterator> const_iterator;
722#endif
726
734 boundary_point_index() : mask_(0xFFFFFFFFu) {}
735
741 rule_type mask,
742 const std::locale& loc = std::locale()) :
743 map_(type, begin, end, loc),
744 mask_(mask)
745 {}
751 const std::locale& loc = std::locale()) :
752 map_(type, begin, end, loc),
753 mask_(0xFFFFFFFFu)
754 {}
755
774
779 void map(boundary_type type, base_iterator begin, base_iterator end, const std::locale& loc = std::locale())
780 {
781 map_ = mapping_type(type, begin, end, loc);
782 }
783
792 {
793 return iterator(true, &map_, mask_);
794 }
795
803 iterator end() const
804 {
805 return iterator(false, &map_, mask_);
806 }
807
820 {
821 return iterator(p, &map_, mask_);
822 }
823
826 {
827 return mask_;
828 }
831 {
832 mask_ = v;
833 }
834
835 private:
836 friend class segment_index<base_iterator>;
837 typedef detail::mapping<base_iterator> mapping_type;
838 mapping_type map_;
839 rule_type mask_;
840 };
841
843 template<typename BaseIterator>
844 segment_index<BaseIterator>::segment_index(const boundary_point_index<BaseIterator>& other) :
845 map_(other.map_), mask_(0xFFFFFFFFu), full_select_(false)
846 {}
847
848 template<typename BaseIterator>
849 boundary_point_index<BaseIterator>::boundary_point_index(const segment_index<BaseIterator>& other) :
850 map_(other.map_), mask_(0xFFFFFFFFu)
851 {}
852
853 template<typename BaseIterator>
854 segment_index<BaseIterator>& segment_index<BaseIterator>::operator=(const boundary_point_index<BaseIterator>& other)
855 {
856 map_ = other.map_;
857 return *this;
858 }
859
860 template<typename BaseIterator>
861 boundary_point_index<BaseIterator>&
862 boundary_point_index<BaseIterator>::operator=(const segment_index<BaseIterator>& other)
863 {
864 map_ = other.map_;
865 return *this;
866 }
868
871#ifndef BOOST_LOCALE_NO_CXX20_STRING8
873#endif
874#ifdef BOOST_LOCALE_ENABLE_CHAR16_T
876#endif
877#ifdef BOOST_LOCALE_ENABLE_CHAR32_T
879#endif
880
883#ifdef __cpp_char8_t
884 typedef segment_index<const char8_t*> u8csegment_index;
885#endif
886#ifdef BOOST_LOCALE_ENABLE_CHAR16_T
888#endif
889#ifdef BOOST_LOCALE_ENABLE_CHAR32_T
891#endif
892
895#ifndef BOOST_LOCALE_NO_CXX20_STRING8
897#endif
898#ifdef BOOST_LOCALE_ENABLE_CHAR16_T
900#endif
901#ifdef BOOST_LOCALE_ENABLE_CHAR32_T
903#endif
904
907#ifdef __cpp_char8_t
908 typedef boundary_point_index<const char8_t*> u8cboundary_point_index;
909#endif
910#ifdef BOOST_LOCALE_ENABLE_CHAR16_T
912#endif
913#ifdef BOOST_LOCALE_ENABLE_CHAR32_T
915#endif
916
917}}} // namespace boost::locale::boundary
918
925
926#ifdef BOOST_MSVC
927# pragma warning(pop)
928#endif
929
930#endif
This class holds an index of boundary points and allows iterating over them.
Definition: index.hpp:697
This class represents a boundary point in the text.
Definition: boundary_point.hpp:44
This class holds an index of segments in the text range and allows to iterate over them.
Definition: index.hpp:475
a segment object that represents a pair of two iterators that define the range where this segment exi...
Definition: segment.hpp:90
boundary_point_index< const char16_t * > u16cboundary_point_index
convenience typedef
Definition: index.hpp:911
segment< base_iterator > value_type
Definition: index.hpp:502
segment_index< const wchar_t * > wcsegment_index
convenience typedef
Definition: index.hpp:882
BaseIterator base_iterator
The type of the iterator used to iterate over the original text.
Definition: index.hpp:478
void map(boundary_type type, base_iterator begin, base_iterator end, const std::locale &loc=std::locale())
Definition: index.hpp:779
boundary_point_index< const char * > cboundary_point_index
convenience typedef
Definition: index.hpp:905
boundary_point_index< std::u32string::const_iterator > u32sboundary_point_index
convenience typedef
Definition: index.hpp:902
boundary_type
This type describes a possible boundary analysis alternatives.
Definition: types.hpp:30
unspecified_iterator_type iterator
Definition: index.hpp:716
void full_select(bool v)
Definition: index.hpp:639
uint32_t rule_type
Flags used with word boundary analysis – the type of the word, line or sentence boundary found.
Definition: types.hpp:40
segment_index()
Definition: index.hpp:511
segment_index< std::u8string::const_iterator > u8ssegment_index
convenience typedef
Definition: index.hpp:872
segment_index< std::u32string::const_iterator > u32ssegment_index
convenience typedef
Definition: index.hpp:878
void map(boundary_type type, base_iterator begin, base_iterator end, const std::locale &loc=std::locale())
Definition: index.hpp:556
boundary_point_index< const wchar_t * > wcboundary_point_index
convenience typedef
Definition: index.hpp:906
segment_index(const boundary_point_index< base_iterator > &)
boundary_point_index< std::string::const_iterator > sboundary_point_index
convenience typedef
Definition: index.hpp:893
boundary_point_index< std::u8string::const_iterator > u8sboundary_point_index
convenience typedef
Definition: index.hpp:896
iterator end() const
Definition: index.hpp:803
void rule(rule_type v)
Set the mask of rules that are used.
Definition: index.hpp:830
segment_index< std::wstring::const_iterator > wssegment_index
convenience typedef
Definition: index.hpp:870
segment_index & operator=(const boundary_point_index< base_iterator > &)
unspecified_iterator_type const_iterator
Definition: index.hpp:495
iterator find(base_iterator p) const
Definition: index.hpp:819
boundary_point_index(boundary_type type, base_iterator begin, base_iterator end, rule_type mask, const std::locale &loc=std::locale())
Definition: index.hpp:738
iterator begin() const
Definition: index.hpp:791
segment_index< std::u16string::const_iterator > u16ssegment_index
convenience typedef
Definition: index.hpp:875
boundary_point_index(const segment_index< base_iterator > &other)
segment_index< const char * > csegment_index
convenience typedef
Definition: index.hpp:881
segment_index(boundary_type type, base_iterator begin, base_iterator end, rule_type mask, const std::locale &loc=std::locale())
Definition: index.hpp:514
std::vector< break_info > index_type
Definition: facets.hpp:52
boundary_point< base_iterator > value_type
Definition: index.hpp:725
bool full_select() const
Definition: index.hpp:624
segment_index< const char16_t * > u16csegment_index
convenience typedef
Definition: index.hpp:887
iterator find(base_iterator p) const
Definition: index.hpp:598
segment_index(boundary_type type, base_iterator begin, base_iterator end, const std::locale &loc=std::locale())
Definition: index.hpp:524
iterator end() const
Definition: index.hpp:578
BaseIterator base_iterator
The type of the iterator used to iterate over the original text.
Definition: index.hpp:700
boundary_point_index< const char32_t * > u32cboundary_point_index
convenience typedef
Definition: index.hpp:914
boundary_point_index< std::u16string::const_iterator > u16sboundary_point_index
convenience typedef
Definition: index.hpp:899
boundary_point_index()
Definition: index.hpp:734
segment_index< const char32_t * > u32csegment_index
convenience typedef
Definition: index.hpp:890
unspecified_iterator_type const_iterator
Definition: index.hpp:718
void rule(rule_type v)
Set the mask of rules that are used.
Definition: index.hpp:609
segment_index< std::string::const_iterator > ssegment_index
convenience typedef
Definition: index.hpp:869
boundary_point_index< std::wstring::const_iterator > wsboundary_point_index
convenience typedef
Definition: index.hpp:894
rule_type rule() const
Get the mask of rules that are used.
Definition: index.hpp:604
iterator begin() const
Definition: index.hpp:568
rule_type rule() const
Get the mask of rules that are used.
Definition: index.hpp:825
boundary_point_index(boundary_type type, base_iterator begin, base_iterator end, const std::locale &loc=std::locale())
Definition: index.hpp:748
unspecified_iterator_type iterator
Definition: index.hpp:493
boundary_point_index & operator=(const segment_index< base_iterator > &other)
@ boundary
Generate boundary analysis facet.