00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00026
00036
00037
00038 #ifndef _CTTL_CONST_EDGE_H_INCLUDED_
00039 #define _CTTL_CONST_EDGE_H_INCLUDED_
00040
00041 #include "node.h"
00042 #include "xtl_edge_trait.h"
00043
00044 namespace cttl {
00045
00061 template< typename PolicyT = policy_default, typename StringT = CTTL_STD_STRING >
00062 class const_edge
00063 {
00064 public:
00066 typedef xtl_const_offset_guard offset_guard_T;
00067
00069 typedef StringT string_T;
00070
00072 typedef typename StringT::value_type char_T;
00073
00075 typedef StringT value_type;
00076
00078 typedef typename StringT::size_type size_type;
00079
00081 typedef PolicyT policy_T;
00082
00086 typedef typename PolicyT::strict_policy_T strict_policy_T;
00087
00092 typedef const_edge< strict_policy_T, StringT > strict_edge_T;
00093
00098 typedef node< StringT > node_T;
00099
00100
00102 node< StringT > first;
00103
00105 node< StringT > second;
00106
00107 protected:
00112 PolicyT& m_space_policy;
00113
00126 static inline PolicyT& default_policy()
00127 {
00128 #ifdef CTTL_STATEFUL_RUNTIME_MATCH
00129
00130 static PolicyT* const ptr_bad_policy = NULL;
00131 return *ptr_bad_policy;
00132 #else
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143 static PolicyT stateless_static_policy;
00144 return stateless_static_policy;
00145 #endif //CTTL_STATEFUL_RUNTIME_MATCH
00146 }
00147
00148 public:
00153 PolicyT& space_policy()
00154 {
00155 return m_space_policy;
00156 }
00157
00162 PolicyT const& space_policy() const
00163 {
00164 return m_space_policy;
00165 }
00166
00168
00170
00182 explicit const_edge( StringT& inp_ )
00183 :
00184 first( inp_, 0 ),
00185 second( inp_, inp_.length() ),
00186 m_space_policy( default_policy() )
00187 {
00188 first.link( second );
00189 }
00190
00204 const_edge( StringT& inp_, size_type start_, size_type finish_ )
00205 :
00206 first( inp_, start_ ),
00207 second( inp_, finish_ ),
00208 m_space_policy( default_policy() )
00209 {
00210 first.link( second );
00211 }
00212
00227 const_edge( node< StringT > const& node_first_, node< StringT > const& node_second_ )
00228 :
00229 first( node_first_ ),
00230 second( node_second_ ),
00231 m_space_policy( default_policy() )
00232 {
00233 assert( &node_first_.parent() == &node_second_.parent() );
00234 }
00235
00256 const_edge(
00257 node< StringT > const& node_first_,
00258 node< StringT > const& node_second_,
00259 size_type start_,
00260 size_type finish_
00261 )
00262 :
00263 first( node_first_, start_ ),
00264 second( node_second_, finish_ ),
00265 m_space_policy( default_policy() )
00266 {
00267 assert( &node_first_.parent() == &node_second_.parent() );
00268 }
00269
00288 template< typename Other_policyT >
00289 const_edge( const_edge< Other_policyT, StringT > const& other_ )
00290 :
00291 first( other_.first ),
00292 second( other_.second ),
00293 m_space_policy( default_policy() )
00294 {
00295 }
00296
00298 const_edge( const_edge< PolicyT, StringT > const& other_ )
00299 :
00300 first( other_.first ),
00301 second( other_.second ),
00302 m_space_policy( other_.m_space_policy )
00303 {
00304 }
00305
00319 const_edge(
00320 const_edge< PolicyT, StringT > const& other_,
00321 size_type start_,
00322 size_type finish_
00323 )
00324 :
00325 first( other_.first, start_ ),
00326 second( other_.second, finish_ ),
00327 m_space_policy( other_.m_space_policy )
00328 {
00329 }
00330
00353 template< typename Other_policyT >
00354 const_edge( const_edge< Other_policyT, StringT > const& other_, PolicyT& policy_ )
00355 :
00356 first( other_.first ),
00357 second( other_.second ),
00358 m_space_policy( policy_ )
00359 {
00360 }
00361
00379 const_edge( StringT& inp_, PolicyT& policy_ )
00380 :
00381 first( inp_, 0 ),
00382 second( inp_, inp_.length() ),
00383 m_space_policy( policy_ )
00384 {
00385 first.link( second );
00386 }
00387
00396 const_edge< PolicyT, StringT >& operator= ( const_edge< PolicyT, StringT > const& other_ )
00397 {
00398 if ( this != &other_ ) {
00399 first = other_.first;
00400 second = other_.second;
00401 }
00402 return *this;
00403 }
00404
00405
00417 operator StringT() const
00418 {
00419 return text();
00420 }
00421
00423 StringT& parent()
00424 {
00425 return first.parent();
00426 }
00427
00429 StringT const& parent() const
00430 {
00431 return first.parent();
00432 }
00433
00434 public:
00436 StringT text() const
00437 {
00438 assert( &first.parent() == &second.parent() );
00439 assert( first.offset() <= second.offset() );
00440 assert( second.offset() <= second.parent().length() );
00441
00442 if ( first.offset() == second.offset() ) {
00443 return StringT();
00444 }
00445
00446 return second.parent().substr(
00447 first.offset(),
00448 length()
00449 );
00450 }
00451
00463 size_type length() const
00464 {
00465 if ( first.offset() > second.offset() )
00466 return StringT::npos;
00467
00468 return second.offset() - first.offset();
00469 }
00470
00476 void region_insert()
00477 {
00478 space_policy().region_insert(
00479 first.offset(),
00480 second.offset()
00481 );
00482 }
00483
00489 void region_erase()
00490 {
00491 space_policy().region_erase(
00492 first.offset(),
00493 second.offset()
00494 );
00495 }
00496
00503 StringT region_difference()
00504 {
00505 return space_policy().region_difference(
00506 parent(),
00507 first.offset(),
00508 second.offset()
00509 );
00510 }
00511
00513 void text_tolower()
00514 {
00515 typename StringT::iterator it_first = parent().begin() + first.offset();
00516 typename StringT::iterator it_last = parent().begin() + second.offset();
00517
00518 std::transform(
00519 it_first,
00520 it_last,
00521 it_first,
00522 tolower
00523 );
00524 }
00525
00527 void text_toupper()
00528 {
00529 typename StringT::iterator it_first = parent().begin() + first.offset();
00530 typename StringT::iterator it_last = parent().begin() + second.offset();
00531
00532 std::transform(
00533 it_first,
00534 it_last,
00535 it_first,
00536 toupper
00537 );
00538 }
00539
00552 template< typename ExprT >
00553 xtl_wrap<
00554 xtl_edge<
00555 const_edge< PolicyT, StringT >,
00556 xtl_wrap< ExprT >
00557 >
00558 >
00559 operator() ( xtl_wrap< ExprT > const& expr_ )
00560 {
00561 typedef
00562 xtl_edge<
00563 const_edge< PolicyT, StringT >,
00564 xtl_wrap< ExprT >
00565 > xtl_edge_T;
00566
00567 return xtl_wrap< xtl_edge_T >( xtl_edge_T( *this, expr_ ) );
00568 }
00569
00570 };
00571
00572
00574
00576
00577
00579 template< typename PolicyT, typename StringT >
00580 inline
00581 std::ostream& operator<<( std::ostream& os_, const_edge< PolicyT, StringT > const& edge_ )
00582 {
00583 os_ << edge_.text();
00584 return os_;
00585 }
00586
00596 template< typename PolicyT, typename StringT >
00597 bool operator< ( const_edge< PolicyT, StringT > const& one_, const_edge< PolicyT, StringT > const& another_ )
00598 {
00599 return one_.parent().compare(
00600 one_.first.offset(),
00601 one_.length(),
00602 another_.parent(),
00603 another_.first.offset(),
00604 another_.length()
00605 ) < 0;
00606 }
00607
00617 template< typename PolicyT, typename StringT >
00618 bool operator> ( const_edge< PolicyT, StringT > const& one_, const_edge< PolicyT, StringT > const& another_ )
00619 {
00620 return one_.parent().compare(
00621 one_.first.offset(),
00622 one_.length(),
00623 another_.parent(),
00624 another_.first.offset(),
00625 another_.length()
00626 ) > 0;
00627 }
00628
00638 template< typename PolicyT, typename StringT >
00639 bool operator>= ( const_edge< PolicyT, StringT > const& one_, const_edge< PolicyT, StringT > const& another_ )
00640 {
00641 return one_.parent().compare(
00642 one_.first.offset(),
00643 one_.length(),
00644 another_.parent(),
00645 another_.first.offset(),
00646 another_.length()
00647 ) >= 0;
00648 }
00649
00659 template< typename PolicyT, typename StringT >
00660 bool operator<= ( const_edge< PolicyT, StringT > const& one_, const_edge< PolicyT, StringT > const& another_ )
00661 {
00662 return one_.parent().compare(
00663 one_.first.offset(),
00664 one_.length(),
00665 another_.parent(),
00666 another_.first.offset(),
00667 another_.length()
00668 ) <= 0;
00669 }
00670
00680 template< typename PolicyT, typename StringT >
00681 bool operator== ( const_edge< PolicyT, StringT > const& one_, const_edge< PolicyT, StringT > const& another_ )
00682 {
00683 return one_.parent().compare(
00684 one_.first.offset(),
00685 one_.length(),
00686 another_.parent(),
00687 another_.first.offset(),
00688 another_.length()
00689 ) == 0;
00690 }
00691
00701 template< typename PolicyT, typename StringT >
00702 bool operator!= ( const_edge< PolicyT, StringT > const& one_, const_edge< PolicyT, StringT > const& another_ )
00703 {
00704 return one_.parent().compare(
00705 one_.first.offset(),
00706 one_.length(),
00707 another_.parent(),
00708 another_.first.offset(),
00709 another_.length()
00710 ) != 0;
00711 }
00712
00713
00715
00717
00727 template< typename PolicyT, typename StringT >
00728 bool operator< ( const_edge< PolicyT, StringT > const& substr_, StringT const& str_ )
00729 {
00730 return substr_.parent().compare(
00731 substr_.first.offset(),
00732 substr_.length(),
00733 str_
00734 ) < 0;
00735 }
00736
00746 template< typename PolicyT, typename StringT >
00747 bool operator> ( const_edge< PolicyT, StringT > const& substr_, StringT const& str_ )
00748 {
00749 return substr_.parent().compare(
00750 substr_.first.offset(),
00751 substr_.length(),
00752 str_
00753 ) > 0;
00754 }
00755
00765 template< typename PolicyT, typename StringT >
00766 bool operator>= ( const_edge< PolicyT, StringT > const& substr_, StringT const& str_ )
00767 {
00768 return substr_.parent().compare(
00769 substr_.first.offset(),
00770 substr_.length(),
00771 str_
00772 ) >= 0;
00773 }
00774
00784 template< typename PolicyT, typename StringT >
00785 bool operator<= ( const_edge< PolicyT, StringT > const& substr_, StringT const& str_ )
00786 {
00787 return substr_.parent().compare(
00788 substr_.first.offset(),
00789 substr_.length(),
00790 str_
00791 ) <= 0;
00792 }
00793
00803 template< typename PolicyT, typename StringT >
00804 bool operator== ( const_edge< PolicyT, StringT > const& substr_, StringT const& str_ )
00805 {
00806 return substr_.parent().compare(
00807 substr_.first.offset(),
00808 substr_.length(),
00809 str_
00810 ) == 0;
00811 }
00812
00822 template< typename PolicyT, typename StringT >
00823 bool operator!= ( const_edge< PolicyT, StringT > const& substr_, StringT const& str_ )
00824 {
00825 return substr_.parent().compare(
00826 substr_.first.offset(),
00827 substr_.length(),
00828 str_
00829 ) != 0;
00830 }
00831
00832
00834
00836
00846 template< typename PolicyT, typename StringT >
00847 bool operator< ( StringT const& str_, const_edge< PolicyT, StringT > const& substr_ )
00848 {
00849 return str_.compare(
00850 0,
00851 str_.length(),
00852 substr_.parent(),
00853 substr_.first.offset(),
00854 substr_.length()
00855 ) < 0;
00856 }
00857
00867 template< typename PolicyT, typename StringT >
00868 bool operator> ( StringT const& str_, const_edge< PolicyT, StringT > const& substr_ )
00869 {
00870 return str_.compare(
00871 0,
00872 str_.length(),
00873 substr_.parent(),
00874 substr_.first.offset(),
00875 substr_.length()
00876 ) > 0;
00877 }
00878
00888 template< typename PolicyT, typename StringT >
00889 bool operator>= ( StringT const& str_, const_edge< PolicyT, StringT > const& substr_ )
00890 {
00891 return str_.compare(
00892 0,
00893 str_.length(),
00894 substr_.parent(),
00895 substr_.first.offset(),
00896 substr_.length()
00897 ) >= 0;
00898 }
00899
00909 template< typename PolicyT, typename StringT >
00910 bool operator<= ( StringT const& str_, const_edge< PolicyT, StringT > const& substr_ )
00911 {
00912 return str_.compare(
00913 0,
00914 str_.length(),
00915 substr_.parent(),
00916 substr_.first.offset(),
00917 substr_.length()
00918 ) <= 0;
00919 }
00920
00930 template< typename PolicyT, typename StringT >
00931 bool operator== ( StringT const& str_, const_edge< PolicyT, StringT > const& substr_ )
00932 {
00933 return str_.compare(
00934 0,
00935 str_.length(),
00936 substr_.parent(),
00937 substr_.first.offset(),
00938 substr_.length()
00939 ) == 0;
00940 }
00941
00951 template< typename PolicyT, typename StringT >
00952 bool operator!= ( StringT const& str_, const_edge< PolicyT, StringT > const& substr_ )
00953 {
00954 return str_.compare(
00955 0,
00956 str_.length(),
00957 substr_.parent(),
00958 substr_.first.offset(),
00959 substr_.length()
00960 ) != 0;
00961 }
00962
00963
00965
00967
00977 template< typename PolicyT, typename StringT >
00978 bool operator< ( typename StringT::value_type const* cstr_, const_edge< PolicyT, StringT > const& substr_ )
00979 {
00980 return -substr_.parent().compare(
00981 substr_.first.offset(),
00982 substr_.length(),
00983 cstr_
00984 ) < 0;
00985 }
00986
00996 template< typename PolicyT, typename StringT >
00997 bool operator> ( typename StringT::value_type const* cstr_, const_edge< PolicyT, StringT > const& substr_ )
00998 {
00999 return -substr_.parent().compare(
01000 substr_.first.offset(),
01001 substr_.length(),
01002 cstr_
01003 ) > 0;
01004 }
01005
01015 template< typename PolicyT, typename StringT >
01016 bool operator>= ( typename StringT::value_type const* cstr_, const_edge< PolicyT, StringT > const& substr_ )
01017 {
01018 return -substr_.parent().compare(
01019 substr_.first.offset(),
01020 substr_.length(),
01021 cstr_
01022 ) >= 0;
01023 }
01024
01034 template< typename PolicyT, typename StringT >
01035 bool operator<= ( typename StringT::value_type const* cstr_, const_edge< PolicyT, StringT > const& substr_ )
01036 {
01037 return -substr_.parent().compare(
01038 substr_.first.offset(),
01039 substr_.length(),
01040 cstr_
01041 ) <= 0;
01042 }
01043
01053 template< typename PolicyT, typename StringT >
01054 bool operator== ( typename StringT::value_type const* cstr_, const_edge< PolicyT, StringT > const& substr_ )
01055 {
01056 return substr_.parent().compare(
01057 substr_.first.offset(),
01058 substr_.length(),
01059 cstr_
01060 ) == 0;
01061 }
01062
01072 template< typename PolicyT, typename StringT >
01073 bool operator!= ( typename StringT::value_type const* cstr_, const_edge< PolicyT, StringT > const& substr_ )
01074 {
01075 return substr_.parent().compare(
01076 substr_.first.offset(),
01077 substr_.length(),
01078 cstr_
01079 ) != 0;
01080 }
01081
01082
01084
01086
01096 template< typename PolicyT, typename StringT >
01097 bool operator< ( const_edge< PolicyT, StringT > const& substr_, typename StringT::value_type const* cstr_ )
01098 {
01099 return substr_.parent().compare(
01100 substr_.first.offset(),
01101 substr_.length(),
01102 cstr_
01103 ) < 0;
01104 }
01105
01115 template< typename PolicyT, typename StringT >
01116 bool operator> ( const_edge< PolicyT, StringT > const& substr_, typename StringT::value_type const* cstr_ )
01117 {
01118 return substr_.parent().compare(
01119 substr_.first.offset(),
01120 substr_.length(),
01121 cstr_
01122 ) > 0;
01123 }
01124
01134 template< typename PolicyT, typename StringT >
01135 bool operator>= ( const_edge< PolicyT, StringT > const& substr_, typename StringT::value_type const* cstr_ )
01136 {
01137 return substr_.parent().compare(
01138 substr_.first.offset(),
01139 substr_.length(),
01140 cstr_
01141 ) >= 0;
01142 }
01143
01153 template< typename PolicyT, typename StringT >
01154 bool operator<= ( const_edge< PolicyT, StringT > const& substr_, typename StringT::value_type const* cstr_ )
01155 {
01156 return substr_.parent().compare(
01157 substr_.first.offset(),
01158 substr_.length(),
01159 cstr_
01160 ) <= 0;
01161 }
01162
01172 template< typename PolicyT, typename StringT >
01173 bool operator== ( const_edge< PolicyT, StringT > const& substr_, typename StringT::value_type const* cstr_ )
01174 {
01175 return substr_.parent().compare(
01176 substr_.first.offset(),
01177 substr_.length(),
01178 cstr_
01179 ) == 0;
01180 }
01181
01191 template< typename PolicyT, typename StringT >
01192 bool operator!= ( const_edge< PolicyT, StringT > const& substr_, typename StringT::value_type const* cstr_ )
01193 {
01194 return substr_.parent().compare(
01195 substr_.first.offset(),
01196 substr_.length(),
01197 cstr_
01198 ) != 0;
01199 }
01200
01201 }
01202
01203 #endif // _CTTL_CONST_EDGE_H_INCLUDED_