7 #ifndef BOOST_LOCALE_BOUNDARY_INDEX_HPP_INCLUDED 8 #define BOOST_LOCALE_BOUNDARY_INDEX_HPP_INCLUDED 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/cstdint.hpp> 15 #include <boost/iterator/iterator_facade.hpp> 22 #include <type_traits> 26 # pragma warning(push) 27 # pragma warning(disable : 4275 4251 4231 4660) 30 namespace boost {
namespace locale {
namespace boundary {
43 template<
typename Char>
44 const boundary_indexing<Char>& get_boundary_indexing(
const std::locale& l)
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);
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;
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());
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;
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;
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);
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());
100 template<
typename BaseIterator>
103 typedef BaseIterator base_iterator;
104 typedef typename std::iterator_traits<base_iterator>::value_type char_type;
106 mapping(
boundary_type type, base_iterator begin, base_iterator end,
const std::locale& loc) :
107 index_(new
index_type()), begin_(begin), end_(end)
109 index_type idx = detail::mapping_traits<base_iterator>::map(type, begin, end, loc);
115 const index_type& index()
const {
return *index_; }
117 base_iterator begin()
const {
return begin_; }
119 base_iterator end()
const {
return end_; }
122 std::shared_ptr<index_type> index_;
123 base_iterator begin_, end_;
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>&> {
132 typedef BaseIterator base_iterator;
133 typedef mapping<base_iterator> mapping_type;
134 typedef segment<base_iterator> segment_type;
136 segment_index_iterator() : current_(0, 0), map_(0), mask_(0), full_select_(false) {}
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)
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)
152 const segment_type& dereference()
const {
return value_; }
154 bool equal(
const segment_index_iterator& other)
const 156 return map_ == other.map_ && current_.second == other.current_.second;
161 std::pair<size_t, size_t> next = current_;
163 next.first = next.second;
164 while(next.second < size()) {
166 if(valid_offset(next.second))
169 if(next.second == size())
170 next.first = next.second - 1;
172 while(next.second < size()) {
173 next.first = next.second;
175 if(valid_offset(next.second))
179 update_current(next);
184 std::pair<size_t, size_t> next = current_;
186 while(next.second > 1) {
188 if(valid_offset(next.second))
191 next.first = next.second;
192 while(next.first > 0) {
194 if(valid_offset(next.first))
198 while(next.second > 1) {
200 if(valid_offset(next.second))
203 next.first = next.second - 1;
205 update_current(next);
211 current_.first = size() - 1;
212 current_.second = size();
213 value_ = segment_type(map_->end(), map_->end(), 0);
217 current_.first = current_.second = 0;
218 value_ = segment_type(map_->begin(), map_->begin(), 0);
222 void set(base_iterator p)
224 size_t dist = std::distance(map_->begin(), p);
225 index_type::const_iterator b = map_->index().begin(), e = map_->index().end();
226 index_type::const_iterator boundary_point = std::upper_bound(b, e, break_info(dist));
227 while(boundary_point != e && (boundary_point->rule & mask_) == 0)
230 current_.first = current_.second = boundary_point - b;
233 while(current_.first > 0) {
235 if(valid_offset(current_.first))
239 if(current_.first > 0)
242 value_.first = map_->begin();
243 std::advance(value_.first, get_offset(current_.first));
244 value_.second = value_.first;
245 std::advance(value_.second, get_offset(current_.second) - get_offset(current_.first));
250 void update_current(std::pair<size_t, size_t> pos)
252 std::ptrdiff_t first_diff = get_offset(pos.first) - get_offset(current_.first);
253 std::ptrdiff_t second_diff = get_offset(pos.second) - get_offset(current_.second);
254 std::advance(value_.first, first_diff);
255 std::advance(value_.second, second_diff);
262 if(current_.second != size()) {
263 value_.rule(index()[current_.second].rule);
266 size_t get_offset(
size_t ind)
const 269 return index().back().offset;
270 return index()[ind].offset;
273 bool valid_offset(
size_t offset)
const 275 return offset == 0 || offset == size()
276 || (index()[offset].rule & mask_) != 0;
279 size_t size()
const {
return index().size(); }
281 const index_type& index()
const {
return map_->index(); }
284 std::pair<size_t, size_t> current_;
285 const mapping_type* map_;
290 template<
typename BaseIterator>
291 class boundary_point_index_iterator :
public boost::iterator_facade<boundary_point_index_iterator<BaseIterator>,
292 boundary_point<BaseIterator>,
293 boost::bidirectional_traversal_tag,
294 const boundary_point<BaseIterator>&> {
296 typedef BaseIterator base_iterator;
297 typedef mapping<base_iterator> mapping_type;
298 typedef boundary_point<base_iterator> boundary_point_type;
300 boundary_point_index_iterator() : current_(0), map_(0), mask_(0) {}
302 boundary_point_index_iterator(
bool is_begin,
const mapping_type* map,
rule_type mask) :
303 map_(map), mask_(mask)
310 boundary_point_index_iterator(base_iterator p,
const mapping_type* map,
rule_type mask) :
311 map_(map), mask_(mask)
316 const boundary_point_type& dereference()
const {
return value_; }
318 bool equal(
const boundary_point_index_iterator& other)
const 320 return map_ == other.map_ && current_ == other.current_;
325 size_t next = current_;
326 while(next < size()) {
328 if(valid_offset(next))
331 update_current(next);
336 size_t next = current_;
339 if(valid_offset(next))
342 update_current(next);
349 value_ = boundary_point_type(map_->end(), 0);
354 value_ = boundary_point_type(map_->begin(), 0);
357 void set(base_iterator p)
359 size_t dist = std::distance(map_->begin(), p);
361 index_type::const_iterator b = index().begin();
362 index_type::const_iterator e = index().end();
363 index_type::const_iterator ptr = std::lower_bound(b, e, break_info(dist));
365 if(ptr == index().end())
366 current_ = size() - 1;
368 current_ = ptr - index().begin();
370 while(!valid_offset(current_))
373 std::ptrdiff_t diff = get_offset(current_) - dist;
374 std::advance(p, diff);
379 void update_current(
size_t pos)
381 std::ptrdiff_t diff = get_offset(pos) - get_offset(current_);
382 base_iterator i = value_.iterator();
383 std::advance(i, diff);
391 if(current_ != size()) {
392 value_.rule(index()[current_].rule);
395 size_t get_offset(
size_t ind)
const 398 return index().back().offset;
399 return index()[ind].offset;
402 bool valid_offset(
size_t offset)
const 404 return offset == 0 || offset + 1 >= size()
405 || (index()[offset].rule & mask_) != 0;
408 size_t size()
const {
return index().size(); }
410 const index_type& index()
const {
return map_->index(); }
412 boundary_point_type value_;
414 const mapping_type* map_;
422 template<
typename BaseIterator>
425 template<
typename BaseIterator>
478 template<
typename BaseIterator>
484 #ifdef BOOST_LOCALE_DOXYGEN 485 typedef unspecified_iterator_type
iterator;
501 typedef detail::segment_index_iterator<base_iterator>
iterator;
502 typedef detail::segment_index_iterator<base_iterator>
const_iterator;
522 const std::locale& loc = std::locale()) :
524 mask_(mask), full_select_(false)
531 const std::locale& loc = std::locale()) :
533 mask_(0xFFFFFFFFu), full_select_(false)
562 map_ = mapping_type(type,
begin,
end, loc);
574 return iterator(
true, &map_, mask_, full_select_);
584 return iterator(
false, &map_, mask_, full_select_);
604 return iterator(p, &map_, mask_, full_select_);
650 typedef detail::mapping<base_iterator> mapping_type;
700 template<
typename BaseIterator>
701 class boundary_point_index {
706 #ifdef BOOST_LOCALE_DOXYGEN 707 typedef unspecified_iterator_type
iterator;
724 typedef detail::boundary_point_index_iterator<base_iterator>
iterator;
725 typedef detail::boundary_point_index_iterator<base_iterator>
const_iterator;
746 const std::locale& loc = std::locale()) :
755 const std::locale& loc = std::locale()) :
785 map_ = mapping_type(type,
begin,
end, loc);
797 return iterator(
true, &map_, mask_);
809 return iterator(
false, &map_, mask_);
841 typedef detail::mapping<base_iterator> mapping_type;
847 template<
typename BaseIterator>
849 map_(other.map_), mask_(0xFFFFFFFFu), full_select_(false)
852 template<
typename BaseIterator>
854 map_(other.map_), mask_(0xFFFFFFFFu)
857 template<
typename BaseIterator>
864 template<
typename BaseIterator>
865 boundary_point_index<BaseIterator>&
875 #ifdef BOOST_LOCALE_ENABLE_CHAR16_T 878 #ifdef BOOST_LOCALE_ENABLE_CHAR32_T 884 #ifdef BOOST_LOCALE_ENABLE_CHAR16_T 887 #ifdef BOOST_LOCALE_ENABLE_CHAR32_T 893 #ifdef BOOST_LOCALE_ENABLE_CHAR16_T 896 #ifdef BOOST_LOCALE_ENABLE_CHAR32_T 902 #ifdef BOOST_LOCALE_ENABLE_CHAR16_T 905 #ifdef BOOST_LOCALE_ENABLE_CHAR32_T 919 # pragma warning(pop) void full_select(bool v)
Definition: index.hpp:643
iterator find(base_iterator p) const
Definition: index.hpp:823
boundary_point_index< const char32_t * > u32cboundary_point_index
convenience typedef
Definition: index.hpp:906
a segment object that represents a pair of two iterators that define the range where this segment exi...
Definition: segment.hpp:90
boundary_type
This type describes a possible boundary analysis alternatives.
Definition: types.hpp:30
bool full_select() const
Definition: index.hpp:628
rule_type rule() const
Get the mask of rules that are used.
Definition: index.hpp:608
This class holds an index of boundary points and allows iterating over them.
Definition: index.hpp:426
BaseIterator base_iterator
The type of the iterator used to iterate over the original text.
Definition: index.hpp:482
boundary_point_index(boundary_type type, base_iterator begin, base_iterator end, rule_type mask, const std::locale &loc=std::locale())
Definition: index.hpp:742
segment_index< std::u16string::const_iterator > u16ssegment_index
convenience typedef
Definition: index.hpp:876
iterator begin() const
Definition: index.hpp:572
segment_index(boundary_type type, base_iterator begin, base_iterator end, const std::locale &loc=std::locale())
Definition: index.hpp:528
boundary_point_index< std::wstring::const_iterator > wsboundary_point_index
convenience typedef
Definition: index.hpp:892
iterator end() const
Definition: index.hpp:807
segment_index & operator=(const boundary_point_index< base_iterator > &)
segment_index(boundary_type type, base_iterator begin, base_iterator end, rule_type mask, const std::locale &loc=std::locale())
Definition: index.hpp:518
boundary_point< base_iterator > value_type
Definition: index.hpp:729
segment< base_iterator > value_type
Definition: index.hpp:506
void rule(rule_type v)
Set the mask of rules that are used.
Definition: index.hpp:834
boundary_point_index< const wchar_t * > wcboundary_point_index
convenience typedef
Definition: index.hpp:901
boundary_point_index< const char16_t * > u16cboundary_point_index
convenience typedef
Definition: index.hpp:903
boundary_point_index & operator=(const segment_index< base_iterator > &other)
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< const wchar_t * > wcsegment_index
convenience typedef
Definition: index.hpp:883
unspecified_iterator_type iterator
Definition: index.hpp:497
segment_index()
Definition: index.hpp:515
iterator end() const
Definition: index.hpp:582
iterator begin() const
Definition: index.hpp:795
boundary_point_index< std::string::const_iterator > sboundary_point_index
convenience typedef
Definition: index.hpp:891
segment_index< std::string::const_iterator > ssegment_index
convenience typedef
Definition: index.hpp:873
segment_index< std::wstring::const_iterator > wssegment_index
convenience typedef
Definition: index.hpp:874
unspecified_iterator_type const_iterator
Definition: index.hpp:499
void map(boundary_type type, base_iterator begin, base_iterator end, const std::locale &loc=std::locale())
Definition: index.hpp:783
unspecified_iterator_type const_iterator
Definition: index.hpp:722
This class represents a boundary point in the text.
Definition: boundary_point.hpp:44
rule_type rule() const
Get the mask of rules that are used.
Definition: index.hpp:829
void rule(rule_type v)
Set the mask of rules that are used.
Definition: index.hpp:613
boundary_point_index(boundary_type type, base_iterator begin, base_iterator end, const std::locale &loc=std::locale())
Definition: index.hpp:752
boundary_point_index< std::u32string::const_iterator > u32sboundary_point_index
convenience typedef
Definition: index.hpp:897
iterator find(base_iterator p) const
Definition: index.hpp:602
unspecified_iterator_type iterator
Definition: index.hpp:720
boundary_point_index< std::u16string::const_iterator > u16sboundary_point_index
convenience typedef
Definition: index.hpp:894
Generate boundary analysis facet.
segment_index< const char16_t * > u16csegment_index
convenience typedef
Definition: index.hpp:885
BaseIterator base_iterator
The type of the iterator used to iterate over the original text.
Definition: index.hpp:704
segment_index< const char32_t * > u32csegment_index
convenience typedef
Definition: index.hpp:888
segment_index< const char * > csegment_index
convenience typedef
Definition: index.hpp:882
segment_index< std::u32string::const_iterator > u32ssegment_index
convenience typedef
Definition: index.hpp:879
std::vector< break_info > index_type
Definition: facets.hpp:50
boundary_point_index< const char * > cboundary_point_index
convenience typedef
Definition: index.hpp:900
boundary_point_index()
Definition: index.hpp:738
This class holds an index of segments in the text range and allows to iterate over them.
Definition: index.hpp:423
void map(boundary_type type, base_iterator begin, base_iterator end, const std::locale &loc=std::locale())
Definition: index.hpp:560