8 #ifndef BOOST_LOCALE_BOUNDARY_INDEX_HPP_INCLUDED
9 #define BOOST_LOCALE_BOUNDARY_INDEX_HPP_INCLUDED
11 #include <boost/locale/config.hpp>
12 #include <boost/locale/boundary/types.hpp>
13 #include <boost/locale/boundary/facets.hpp>
14 #include <boost/locale/boundary/segment.hpp>
15 #include <boost/locale/boundary/boundary_point.hpp>
16 #include <boost/iterator/iterator_facade.hpp>
17 #include <boost/type_traits/is_same.hpp>
18 #include <boost/shared_ptr.hpp>
19 #include <boost/cstdint.hpp>
20 #include <boost/assert.hpp>
22 # pragma warning(push)
23 # pragma warning(disable : 4275 4251 4231 4660)
51 template<typename IteratorType,typename CategoryType = typename std::iterator_traits<IteratorType>::iterator_category>
52 struct mapping_traits {
53 typedef typename std::iterator_traits<IteratorType>::value_type char_type;
56 std::basic_string<char_type> str(b,e);
57 return std::use_facet<boundary_indexing<char_type> >(l).map(t,str.c_str(),str.c_str()+str.size());
61 template<
typename CharType,
typename SomeIteratorType>
62 struct linear_iterator_traits {
63 static const bool is_linear =
64 is_same<SomeIteratorType,CharType*>::value
65 || is_same<SomeIteratorType,CharType const*>::value
66 || is_same<SomeIteratorType,typename std::basic_string<CharType>::iterator>::value
67 || is_same<SomeIteratorType,typename std::basic_string<CharType>::const_iterator>::value
68 || is_same<SomeIteratorType,typename std::vector<CharType>::iterator>::value
69 || is_same<SomeIteratorType,typename std::vector<CharType>::const_iterator>::value
75 template<
typename IteratorType>
76 struct mapping_traits<IteratorType,std::random_access_iterator_tag> {
78 typedef typename std::iterator_traits<IteratorType>::value_type char_type;
94 if(linear_iterator_traits<char_type,IteratorType>::is_linear && b!=e)
96 char_type
const *begin = &*b;
97 char_type
const *end = begin + (e-b);
98 index_type tmp=std::use_facet<boundary_indexing<char_type> >(l).map(t,begin,end);
102 std::basic_string<char_type> str(b,e);
103 index_type tmp = std::use_facet<boundary_indexing<char_type> >(l).map(t,str.c_str(),str.c_str()+str.size());
110 template<
typename BaseIterator>
113 typedef BaseIterator base_iterator;
114 typedef typename std::iterator_traits<base_iterator>::value_type char_type;
120 std::locale
const &loc)
126 index_type idx=details::mapping_traits<base_iterator>::map(type,begin,end,loc);
139 base_iterator begin()
const
144 base_iterator end()
const
151 base_iterator begin_,end_;
154 template<
typename BaseIterator>
155 class segment_index_iterator :
156 public boost::iterator_facade<
157 segment_index_iterator<BaseIterator>,
158 segment<BaseIterator>,
159 boost::bidirectional_traversal_tag,
160 segment<BaseIterator> const &
164 typedef BaseIterator base_iterator;
165 typedef mapping<base_iterator> mapping_type;
166 typedef segment<base_iterator> segment_type;
168 segment_index_iterator() : current_(0,0),map_(0)
172 segment_index_iterator(base_iterator p,mapping_type
const *map,
rule_type mask,
bool full_select) :
175 full_select_(full_select)
179 segment_index_iterator(
bool is_begin,mapping_type
const *map,
rule_type mask,
bool full_select) :
182 full_select_(full_select)
190 segment_type
const &dereference()
const
195 bool equal(segment_index_iterator
const &other)
const
197 return map_ == other.map_ && current_.second == other.current_.second;
202 std::pair<size_t,size_t> next = current_;
204 next.first = next.second;
205 while(next.second < size()) {
207 if(valid_offset(next.second))
210 if(next.second == size())
211 next.first = next.second - 1;
214 while(next.second < size()) {
215 next.first = next.second;
217 if(valid_offset(next.second))
221 update_current(next);
226 std::pair<size_t,size_t> next = current_;
228 while(next.second >1) {
230 if(valid_offset(next.second))
233 next.first = next.second;
234 while(next.first >0) {
236 if(valid_offset(next.first))
241 while(next.second >1) {
243 if(valid_offset(next.second))
246 next.first = next.second - 1;
248 update_current(next);
255 current_.first = size() - 1;
256 current_.second = size();
257 value_ = segment_type(map_->end(),map_->end(),0);
261 current_.first = current_.second = 0;
262 value_ = segment_type(map_->begin(),map_->begin(),0);
266 void set(base_iterator p)
268 size_t dist=std::distance(map_->begin(),p);
269 index_type::const_iterator b=map_->index().begin(),e=map_->index().end();
270 index_type::const_iterator
271 boundary_point=std::upper_bound(b,e,break_info(dist));
272 while(boundary_point != e && (boundary_point->rule & mask_)==0)
275 current_.first = current_.second = boundary_point - b;
278 while(current_.first > 0) {
280 if(valid_offset(current_.first))
285 if(current_.first > 0)
288 value_.first = map_->begin();
289 std::advance(value_.first,get_offset(current_.first));
290 value_.second = value_.first;
291 std::advance(value_.second,get_offset(current_.second) - get_offset(current_.first));
296 void update_current(std::pair<size_t,size_t> pos)
298 std::ptrdiff_t first_diff = get_offset(pos.first) - get_offset(current_.first);
299 std::ptrdiff_t second_diff = get_offset(pos.second) - get_offset(current_.second);
300 std::advance(value_.first,first_diff);
301 std::advance(value_.second,second_diff);
308 if(current_.second != size()) {
309 value_.rule(index()[current_.second].rule);
312 size_t get_offset(
size_t ind)
const
315 return index().back().offset;
316 return index()[ind].offset;
319 bool valid_offset(
size_t offset)
const
323 || (index()[offset].rule & mask_)!=0;
328 return index().size();
333 return map_->index();
338 std::pair<size_t,size_t> current_;
339 mapping_type
const *map_;
344 template<
typename BaseIterator>
345 class boundary_point_index_iterator :
346 public boost::iterator_facade<
347 boundary_point_index_iterator<BaseIterator>,
348 boundary_point<BaseIterator>,
349 boost::bidirectional_traversal_tag,
350 boundary_point<BaseIterator> const &
354 typedef BaseIterator base_iterator;
355 typedef mapping<base_iterator> mapping_type;
356 typedef boundary_point<base_iterator> boundary_point_type;
358 boundary_point_index_iterator() : current_(0),map_(0)
362 boundary_point_index_iterator(
bool is_begin,mapping_type
const *map,
rule_type mask) :
371 boundary_point_index_iterator(base_iterator p,mapping_type
const *map,
rule_type mask) :
378 boundary_point_type
const &dereference()
const
383 bool equal(boundary_point_index_iterator
const &other)
const
385 return map_ == other.map_ && current_ == other.current_;
390 size_t next = current_;
391 while(next < size()) {
393 if(valid_offset(next))
396 update_current(next);
401 size_t next = current_;
404 if(valid_offset(next))
407 update_current(next);
414 value_ = boundary_point_type(map_->end(),0);
419 value_ = boundary_point_type(map_->begin(),0);
422 void set(base_iterator p)
424 size_t dist = std::distance(map_->begin(),p);
426 index_type::const_iterator b=index().begin();
427 index_type::const_iterator e=index().end();
428 index_type::const_iterator ptr = std::lower_bound(b,e,break_info(dist));
430 if(ptr==index().end())
433 current_=ptr - index().begin();
435 while(!valid_offset(current_))
438 std::ptrdiff_t diff = get_offset(current_) - dist;
439 std::advance(p,diff);
444 void update_current(
size_t pos)
446 std::ptrdiff_t diff = get_offset(pos) - get_offset(current_);
447 base_iterator i=value_.iterator();
448 std::advance(i,diff);
456 if(current_ != size()) {
457 value_.rule(index()[current_].rule);
460 size_t get_offset(
size_t ind)
const
463 return index().back().offset;
464 return index()[ind].offset;
467 bool valid_offset(
size_t offset)
const
470 || offset + 1 >= size()
471 || (index()[offset].rule & mask_)!=0;
476 return index().size();
481 return map_->index();
485 boundary_point_type value_;
487 mapping_type
const *map_;
496 template<
typename BaseIterator>
499 template<
typename BaseIterator>
555 template<
typename BaseIterator>
563 #ifdef BOOST_LOCALE_DOXYGEN
564 typedef unspecified_iterator_type
iterator;
584 typedef details::segment_index_iterator<base_iterator>
iterator;
585 typedef details::segment_index_iterator<base_iterator>
const_iterator;
613 std::locale
const &loc=std::locale())
615 map_(type,begin,end,loc),
627 std::locale
const &loc=std::locale())
629 map_(type,begin,end,loc),
667 map_ = mapping_type(type,begin,end,loc);
681 return iterator(
true,&map_,mask_,full_select_);
693 return iterator(
false,&map_,mask_,full_select_);
715 return iterator(p,&map_,mask_,full_select_);
771 typedef details::mapping<base_iterator> mapping_type;
825 template<
typename BaseIterator>
826 class boundary_point_index {
832 #ifdef BOOST_LOCALE_DOXYGEN
833 typedef unspecified_iterator_type
iterator;
853 typedef details::boundary_point_index_iterator<base_iterator>
iterator;
854 typedef details::boundary_point_index_iterator<base_iterator>
const_iterator;
883 std::locale
const &loc=std::locale())
885 map_(type,begin,end,loc),
896 std::locale
const &loc=std::locale())
898 map_(type,begin,end,loc),
934 map_ = mapping_type(type,begin,end,loc);
1001 typedef details::mapping<base_iterator> mapping_type;
1007 template<
typename BaseIterator>
1015 template<
typename BaseIterator>
1022 template<
typename BaseIterator>
1029 template<
typename BaseIterator>
1039 #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
1042 #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
1048 #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
1051 #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
1057 #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
1060 #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
1066 #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
1069 #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
1088 #pragma warning(pop)
void full_select(bool v)
Definition: index.hpp:764
a segment object that represents a pair of two iterators that define the range where this segment exi...
Definition: segment.hpp:102
boundary_type
Definition: types.hpp:39
boundary_point_index const & operator=(segment_index< base_iterator > const &other)
This class holds an index of boundary points and allows iterating over them.
Definition: index.hpp:500
BaseIterator base_iterator
Definition: index.hpp:562
segment_index< std::u16string::const_iterator > u16ssegment_index
convenience typedef
Definition: index.hpp:1040
segment_index< char const * > csegment_index
convenience typedef
Definition: index.hpp:1046
void map(boundary_type type, base_iterator begin, base_iterator end, std::locale const &loc=std::locale())
Definition: index.hpp:665
boundary_point_index< std::wstring::const_iterator > wsboundary_point_index
convenience typedef
Definition: index.hpp:1056
boundary_point_index< wchar_t const * > wcboundary_point_index
convenience typedef
Definition: index.hpp:1065
boundary_point< base_iterator > value_type
Definition: index.hpp:860
segment< base_iterator > value_type
Definition: index.hpp:591
void rule(rule_type v)
Definition: index.hpp:993
segment_index const & operator=(boundary_point_index< base_iterator > const &)
rule_type rule() const
Definition: index.hpp:986
iterator end() const
Definition: index.hpp:960
uint32_t rule_type
Flags used with word boundary analysis – the type of the word, line or sentence boundary found...
Definition: types.hpp:51
Definition: generator.hpp:23
boundary_point_index< char16_t const * > u16cboundary_point_index
convenience typedef
Definition: index.hpp:1067
void map(boundary_type type, base_iterator begin, base_iterator end, std::locale const &loc=std::locale())
Definition: index.hpp:932
unspecified_iterator_type iterator
Definition: index.hpp:578
segment_index()
Definition: index.hpp:602
segment_index< char16_t const * > u16csegment_index
convenience typedef
Definition: index.hpp:1049
boundary_point_index< std::string::const_iterator > sboundary_point_index
convenience typedef
Definition: index.hpp:1055
segment_index< std::string::const_iterator > ssegment_index
convenience typedef
Definition: index.hpp:1037
segment_index< char32_t const * > u32csegment_index
convenience typedef
Definition: index.hpp:1052
segment_index< std::wstring::const_iterator > wssegment_index
convenience typedef
Definition: index.hpp:1038
unspecified_iterator_type const_iterator
Definition: index.hpp:582
unspecified_iterator_type const_iterator
Definition: index.hpp:851
This class represents a boundary point in the text.
Definition: boundary_point.hpp:48
iterator find(base_iterator p) const
Definition: index.hpp:713
iterator begin() const
Definition: index.hpp:679
void rule(rule_type v)
Definition: index.hpp:728
boundary_point_index(boundary_type type, base_iterator begin, base_iterator end, std::locale const &loc=std::locale())
Definition: index.hpp:893
boundary_point_index< char32_t const * > u32cboundary_point_index
convenience typedef
Definition: index.hpp:1070
segment_index(boundary_type type, base_iterator begin, base_iterator end, rule_type mask, std::locale const &loc=std::locale())
Definition: index.hpp:609
iterator find(base_iterator p) const
Definition: index.hpp:978
boundary_point_index< char const * > cboundary_point_index
convenience typedef
Definition: index.hpp:1064
boundary_point_index< std::u32string::const_iterator > u32sboundary_point_index
convenience typedef
Definition: index.hpp:1061
unspecified_iterator_type iterator
Definition: index.hpp:847
segment_index(boundary_type type, base_iterator begin, base_iterator end, std::locale const &loc=std::locale())
Definition: index.hpp:624
bool full_select() const
Definition: index.hpp:746
boundary_point_index< std::u16string::const_iterator > u16sboundary_point_index
convenience typedef
Definition: index.hpp:1058
BaseIterator base_iterator
Definition: index.hpp:831
iterator end() const
Definition: index.hpp:691
segment_index< std::u32string::const_iterator > u32ssegment_index
convenience typedef
Definition: index.hpp:1043
std::vector< break_info > index_type
Definition: facets.hpp:86
rule_type rule() const
Definition: index.hpp:721
iterator begin() const
Definition: index.hpp:946
boundary_point_index()
Definition: index.hpp:871
This class holds an index of segments in the text range and allows to iterate over them...
Definition: index.hpp:497
boundary_point_index(boundary_type type, base_iterator begin, base_iterator end, rule_type mask, std::locale const &loc=std::locale())
Definition: index.hpp:879
segment_index< wchar_t const * > wcsegment_index
convenience typedef
Definition: index.hpp:1047