7 #ifndef BOOST_LOCALE_BOUNDARY_INDEX_HPP_INCLUDED 8 #define BOOST_LOCALE_BOUNDARY_INDEX_HPP_INCLUDED 10 #include <boost/locale/boundary/types.hpp> 11 #include <boost/locale/boundary/facets.hpp> 12 #include <boost/locale/boundary/segment.hpp> 13 #include <boost/locale/boundary/boundary_point.hpp> 14 #include <boost/assert.hpp> 15 #include <boost/cstdint.hpp> 16 #include <boost/iterator/iterator_facade.hpp> 17 #include <boost/shared_ptr.hpp> 18 #include <boost/type_traits/is_same.hpp> 28 # pragma warning(push) 29 # pragma warning(disable : 4275 4251 4231 4660) 49 template<typename IteratorType,typename CategoryType = typename std::iterator_traits<IteratorType>::iterator_category>
50 struct mapping_traits {
51 typedef typename std::iterator_traits<IteratorType>::value_type char_type;
54 std::basic_string<char_type> str(b,e);
55 return std::use_facet<boundary_indexing<char_type> >(l).map(t,str.c_str(),str.c_str()+str.size());
59 template<
typename CharType,
typename SomeIteratorType>
60 struct linear_iterator_traits {
61 static const bool is_linear =
62 is_same<SomeIteratorType,CharType*>::value
63 || is_same<SomeIteratorType,CharType const*>::value
64 || is_same<SomeIteratorType,typename std::basic_string<CharType>::iterator>::value
65 || is_same<SomeIteratorType,typename std::basic_string<CharType>::const_iterator>::value
66 || is_same<SomeIteratorType,typename std::vector<CharType>::iterator>::value
67 || is_same<SomeIteratorType,typename std::vector<CharType>::const_iterator>::value
73 template<
typename IteratorType>
74 struct mapping_traits<IteratorType,std::random_access_iterator_tag> {
76 typedef typename std::iterator_traits<IteratorType>::value_type char_type;
92 if(linear_iterator_traits<char_type,IteratorType>::is_linear && b!=e)
94 char_type
const *begin = &*b;
95 char_type
const *end = begin + (e-b);
96 index_type tmp=std::use_facet<boundary_indexing<char_type> >(l).map(t,begin,end);
100 std::basic_string<char_type> str(b,e);
101 index_type tmp = std::use_facet<boundary_indexing<char_type> >(l).map(t,str.c_str(),str.c_str()+str.size());
108 template<
typename BaseIterator>
111 typedef BaseIterator base_iterator;
112 typedef typename std::iterator_traits<base_iterator>::value_type char_type;
118 std::locale
const &loc)
124 index_type idx=details::mapping_traits<base_iterator>::map(type,begin,end,loc);
137 base_iterator begin()
const 142 base_iterator end()
const 148 boost::shared_ptr<index_type> index_;
149 base_iterator begin_,end_;
152 template<
typename BaseIterator>
153 class segment_index_iterator :
154 public boost::iterator_facade<
155 segment_index_iterator<BaseIterator>,
156 segment<BaseIterator>,
157 boost::bidirectional_traversal_tag,
158 segment<BaseIterator> const &
162 typedef BaseIterator base_iterator;
163 typedef mapping<base_iterator> mapping_type;
164 typedef segment<base_iterator> segment_type;
166 segment_index_iterator() : current_(0,0),map_(0)
170 segment_index_iterator(base_iterator p,mapping_type
const *map,
rule_type mask,
bool full_select) :
173 full_select_(full_select)
177 segment_index_iterator(
bool is_begin,mapping_type
const *map,
rule_type mask,
bool full_select) :
180 full_select_(full_select)
188 segment_type
const &dereference()
const 193 bool equal(segment_index_iterator
const &other)
const 195 return map_ == other.map_ && current_.second == other.current_.second;
200 std::pair<size_t,size_t> next = current_;
202 next.first = next.second;
203 while(next.second < size()) {
205 if(valid_offset(next.second))
208 if(next.second == size())
209 next.first = next.second - 1;
212 while(next.second < size()) {
213 next.first = next.second;
215 if(valid_offset(next.second))
219 update_current(next);
224 std::pair<size_t,size_t> next = current_;
226 while(next.second >1) {
228 if(valid_offset(next.second))
231 next.first = next.second;
232 while(next.first >0) {
234 if(valid_offset(next.first))
239 while(next.second >1) {
241 if(valid_offset(next.second))
244 next.first = next.second - 1;
246 update_current(next);
253 current_.first = size() - 1;
254 current_.second = size();
255 value_ = segment_type(map_->end(),map_->end(),0);
259 current_.first = current_.second = 0;
260 value_ = segment_type(map_->begin(),map_->begin(),0);
264 void set(base_iterator p)
266 size_t dist=std::distance(map_->begin(),p);
267 index_type::const_iterator b=map_->index().begin(),e=map_->index().end();
268 index_type::const_iterator
269 boundary_point=std::upper_bound(b,e,break_info(dist));
270 while(boundary_point != e && (boundary_point->rule & mask_)==0)
273 current_.first = current_.second = boundary_point - b;
276 while(current_.first > 0) {
278 if(valid_offset(current_.first))
283 if(current_.first > 0)
286 value_.first = map_->begin();
287 std::advance(value_.first,get_offset(current_.first));
288 value_.second = value_.first;
289 std::advance(value_.second,get_offset(current_.second) - get_offset(current_.first));
294 void update_current(std::pair<size_t,size_t> pos)
296 std::ptrdiff_t first_diff = get_offset(pos.first) - get_offset(current_.first);
297 std::ptrdiff_t second_diff = get_offset(pos.second) - get_offset(current_.second);
298 std::advance(value_.first,first_diff);
299 std::advance(value_.second,second_diff);
306 if(current_.second != size()) {
307 value_.rule(index()[current_.second].rule);
310 size_t get_offset(
size_t ind)
const 313 return index().back().offset;
314 return index()[ind].offset;
317 bool valid_offset(
size_t offset)
const 321 || (index()[offset].rule & mask_)!=0;
326 return index().size();
331 return map_->index();
336 std::pair<size_t,size_t> current_;
337 mapping_type
const *map_;
342 template<
typename BaseIterator>
343 class boundary_point_index_iterator :
344 public boost::iterator_facade<
345 boundary_point_index_iterator<BaseIterator>,
346 boundary_point<BaseIterator>,
347 boost::bidirectional_traversal_tag,
348 boundary_point<BaseIterator> const &
352 typedef BaseIterator base_iterator;
353 typedef mapping<base_iterator> mapping_type;
354 typedef boundary_point<base_iterator> boundary_point_type;
356 boundary_point_index_iterator() : current_(0),map_(0)
360 boundary_point_index_iterator(
bool is_begin,mapping_type
const *map,
rule_type mask) :
369 boundary_point_index_iterator(base_iterator p,mapping_type
const *map,
rule_type mask) :
376 boundary_point_type
const &dereference()
const 381 bool equal(boundary_point_index_iterator
const &other)
const 383 return map_ == other.map_ && current_ == other.current_;
388 size_t next = current_;
389 while(next < size()) {
391 if(valid_offset(next))
394 update_current(next);
399 size_t next = current_;
402 if(valid_offset(next))
405 update_current(next);
412 value_ = boundary_point_type(map_->end(),0);
417 value_ = boundary_point_type(map_->begin(),0);
420 void set(base_iterator p)
422 size_t dist = std::distance(map_->begin(),p);
424 index_type::const_iterator b=index().begin();
425 index_type::const_iterator e=index().end();
426 index_type::const_iterator ptr = std::lower_bound(b,e,break_info(dist));
428 if(ptr==index().end())
431 current_=ptr - index().begin();
433 while(!valid_offset(current_))
436 std::ptrdiff_t diff = get_offset(current_) - dist;
437 std::advance(p,diff);
442 void update_current(
size_t pos)
444 std::ptrdiff_t diff = get_offset(pos) - get_offset(current_);
445 base_iterator i=value_.iterator();
446 std::advance(i,diff);
454 if(current_ != size()) {
455 value_.rule(index()[current_].rule);
458 size_t get_offset(
size_t ind)
const 461 return index().back().offset;
462 return index()[ind].offset;
465 bool valid_offset(
size_t offset)
const 468 || offset + 1 >= size()
469 || (index()[offset].rule & mask_)!=0;
474 return index().size();
479 return map_->index();
483 boundary_point_type value_;
485 mapping_type
const *map_;
494 template<
typename BaseIterator>
497 template<
typename BaseIterator>
553 template<
typename BaseIterator>
561 #ifdef BOOST_LOCALE_DOXYGEN 562 typedef unspecified_iterator_type
iterator;
582 typedef details::segment_index_iterator<base_iterator>
iterator;
583 typedef details::segment_index_iterator<base_iterator>
const_iterator;
611 std::locale
const &loc=std::locale())
625 std::locale
const &loc=std::locale())
665 map_ = mapping_type(type,
begin,
end,loc);
679 return iterator(
true,&map_,mask_,full_select_);
691 return iterator(
false,&map_,mask_,full_select_);
713 return iterator(p,&map_,mask_,full_select_);
769 typedef details::mapping<base_iterator> mapping_type;
823 template<
typename BaseIterator>
824 class boundary_point_index {
830 #ifdef BOOST_LOCALE_DOXYGEN 831 typedef unspecified_iterator_type
iterator;
851 typedef details::boundary_point_index_iterator<base_iterator>
iterator;
852 typedef details::boundary_point_index_iterator<base_iterator>
const_iterator;
881 std::locale
const &loc=std::locale())
894 std::locale
const &loc=std::locale())
932 map_ = mapping_type(type,
begin,
end,loc);
999 typedef details::mapping<base_iterator> mapping_type;
1005 template<
typename BaseIterator>
1013 template<
typename BaseIterator>
1020 template<
typename BaseIterator>
1027 template<
typename BaseIterator>
1037 #ifdef BOOST_LOCALE_ENABLE_CHAR16_T 1040 #ifdef BOOST_LOCALE_ENABLE_CHAR32_T 1046 #ifdef BOOST_LOCALE_ENABLE_CHAR16_T 1049 #ifdef BOOST_LOCALE_ENABLE_CHAR32_T 1055 #ifdef BOOST_LOCALE_ENABLE_CHAR16_T 1058 #ifdef BOOST_LOCALE_ENABLE_CHAR32_T 1064 #ifdef BOOST_LOCALE_ENABLE_CHAR16_T 1067 #ifdef BOOST_LOCALE_ENABLE_CHAR32_T 1086 #pragma warning(pop) void full_select(bool v)
Definition: index.hpp:762
iterator find(base_iterator p) const
Definition: index.hpp:976
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:38
bool full_select() const
Definition: index.hpp:744
rule_type rule() const
Definition: index.hpp:719
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:498
BaseIterator base_iterator
Definition: index.hpp:560
segment_index< std::u16string::const_iterator > u16ssegment_index
convenience typedef
Definition: index.hpp:1038
segment_index< char const * > csegment_index
convenience typedef
Definition: index.hpp:1044
void map(boundary_type type, base_iterator begin, base_iterator end, std::locale const &loc=std::locale())
Definition: index.hpp:663
iterator begin() const
Definition: index.hpp:677
boundary_point_index< std::wstring::const_iterator > wsboundary_point_index
convenience typedef
Definition: index.hpp:1054
iterator end() const
Definition: index.hpp:958
boundary_point_index< wchar_t const * > wcboundary_point_index
convenience typedef
Definition: index.hpp:1063
boundary_point< base_iterator > value_type
Definition: index.hpp:858
segment< base_iterator > value_type
Definition: index.hpp:589
void rule(rule_type v)
Definition: index.hpp:991
segment_index const & operator=(boundary_point_index< base_iterator > const &)
uint32_t rule_type
Flags used with word boundary analysis – the type of the word, line or sentence boundary found.
Definition: types.hpp:50
boundary_point_index< char16_t const * > u16cboundary_point_index
convenience typedef
Definition: index.hpp:1065
void map(boundary_type type, base_iterator begin, base_iterator end, std::locale const &loc=std::locale())
Definition: index.hpp:930
unspecified_iterator_type iterator
Definition: index.hpp:576
segment_index()
Definition: index.hpp:600
segment_index< char16_t const * > u16csegment_index
convenience typedef
Definition: index.hpp:1047
iterator end() const
Definition: index.hpp:689
iterator begin() const
Definition: index.hpp:944
boundary_point_index< std::string::const_iterator > sboundary_point_index
convenience typedef
Definition: index.hpp:1053
segment_index< std::string::const_iterator > ssegment_index
convenience typedef
Definition: index.hpp:1035
segment_index< char32_t const * > u32csegment_index
convenience typedef
Definition: index.hpp:1050
segment_index< std::wstring::const_iterator > wssegment_index
convenience typedef
Definition: index.hpp:1036
unspecified_iterator_type const_iterator
Definition: index.hpp:580
unspecified_iterator_type const_iterator
Definition: index.hpp:849
This class represents a boundary point in the text.
Definition: boundary_point.hpp:47
rule_type rule() const
Definition: index.hpp:984
void rule(rule_type v)
Definition: index.hpp:726
boundary_point_index(boundary_type type, base_iterator begin, base_iterator end, std::locale const &loc=std::locale())
Definition: index.hpp:891
boundary_point_index< char32_t const * > u32cboundary_point_index
convenience typedef
Definition: index.hpp:1068
segment_index(boundary_type type, base_iterator begin, base_iterator end, rule_type mask, std::locale const &loc=std::locale())
Definition: index.hpp:607
boundary_point_index< char const * > cboundary_point_index
convenience typedef
Definition: index.hpp:1062
boundary_point_index< std::u32string::const_iterator > u32sboundary_point_index
convenience typedef
Definition: index.hpp:1059
iterator find(base_iterator p) const
Definition: index.hpp:711
unspecified_iterator_type iterator
Definition: index.hpp:845
segment_index(boundary_type type, base_iterator begin, base_iterator end, std::locale const &loc=std::locale())
Definition: index.hpp:622
boundary_point_index< std::u16string::const_iterator > u16sboundary_point_index
convenience typedef
Definition: index.hpp:1056
BaseIterator base_iterator
Definition: index.hpp:829
segment_index< std::u32string::const_iterator > u32ssegment_index
convenience typedef
Definition: index.hpp:1041
std::vector< break_info > index_type
Definition: facets.hpp:82
boundary_point_index()
Definition: index.hpp:869
This class holds an index of segments in the text range and allows to iterate over them.
Definition: index.hpp:495
boundary_point_index(boundary_type type, base_iterator begin, base_iterator end, rule_type mask, std::locale const &loc=std::locale())
Definition: index.hpp:877
segment_index< wchar_t const * > wcsegment_index
convenience typedef
Definition: index.hpp:1045