annotate DEPENDENCIES/generic/include/boost/locale/boundary/segment.hpp @ 133:4acb5d8d80b6 tip

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