[cfe-commits] [libcxx] r128350 - /libcxx/trunk/include/regex

Howard Hinnant hhinnant at apple.com
Sat Mar 26 13:02:27 PDT 2011


Author: hhinnant
Date: Sat Mar 26 15:02:27 2011
New Revision: 128350

URL: http://llvm.org/viewvc/llvm-project?rev=128350&view=rev
Log:
Jonathan Sauer found a bug in the way ^ was handled

Modified:
    libcxx/trunk/include/regex

Modified: libcxx/trunk/include/regex
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/regex?rev=128350&r1=128349&r2=128350&view=diff
==============================================================================
--- libcxx/trunk/include/regex (original)
+++ libcxx/trunk/include/regex Sat Mar 26 15:02:27 2011
@@ -1264,6 +1264,7 @@
     vector<pair<size_t, const _CharT*> > __loop_data_;
     const __node<_CharT>* __node_;
     regex_constants::match_flag_type __flags_;
+    bool __at_first_;
 
     _LIBCPP_INLINE_VISIBILITY
     __state()
@@ -1890,6 +1891,40 @@
     }
 }
 
+// __l_anchor
+
+template <class _CharT>
+class __l_anchor
+    : public __owns_one_state<_CharT>
+{
+    typedef __owns_one_state<_CharT> base;
+
+public:
+    typedef _STD::__state<_CharT> __state;
+
+    _LIBCPP_INLINE_VISIBILITY
+    __l_anchor(__node<_CharT>* __s)
+        : base(__s) {}
+
+    virtual void __exec(__state&) const;
+};
+
+template <class _CharT>
+void
+__l_anchor<_CharT>::__exec(__state& __s) const
+{
+    if (__s.__at_first_ && __s.__current_ == __s.__first_)
+    {
+        __s.__do_ = __state::__accept_but_not_consume;
+        __s.__node_ = this->first();
+    }
+    else
+    {
+        __s.__do_ = __state::__reject;
+        __s.__node_ = nullptr;
+    }
+}
+
 // __r_anchor
 
 template <class _CharT>
@@ -2394,7 +2429,6 @@
     int __open_count_;
     shared_ptr<__empty_state<_CharT> > __start_;
     __owns_one_state<_CharT>* __end_;
-    bool __left_anchor_;
 
     typedef _STD::__state<_CharT> __state;
     typedef _STD::__node<_CharT> __node;
@@ -2416,17 +2450,17 @@
     _LIBCPP_INLINE_VISIBILITY
     basic_regex()
         : __flags_(), __marked_count_(0), __loop_count_(0), __open_count_(0),
-          __end_(0), __left_anchor_(false)
+          __end_(0)
         {}
     _LIBCPP_INLINE_VISIBILITY
     explicit basic_regex(const value_type* __p, flag_type __f = regex_constants::ECMAScript)
         : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
-          __end_(0), __left_anchor_(false)
+          __end_(0)
         {__parse(__p, __p + __traits_.length(__p));}
     _LIBCPP_INLINE_VISIBILITY
     basic_regex(const value_type* __p, size_t __len, flag_type __f)
         : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
-          __end_(0), __left_anchor_(false)
+          __end_(0)
         {__parse(__p, __p + __len);}
 //     basic_regex(const basic_regex&) = default;
 //     basic_regex(basic_regex&&) = default;
@@ -2435,20 +2469,20 @@
         explicit basic_regex(const basic_string<value_type, _ST, _SA>& __p,
                              flag_type __f = regex_constants::ECMAScript)
         : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
-          __end_(0), __left_anchor_(false)
+          __end_(0)
         {__parse(__p.begin(), __p.end());}
     template <class _ForwardIterator>
         _LIBCPP_INLINE_VISIBILITY
         basic_regex(_ForwardIterator __first, _ForwardIterator __last,
                     flag_type __f = regex_constants::ECMAScript)
         : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
-          __end_(0), __left_anchor_(false)
+          __end_(0)
         {__parse(__first, __last);}
     _LIBCPP_INLINE_VISIBILITY
     basic_regex(initializer_list<value_type> __il,
                 flag_type __f = regex_constants::ECMAScript)
         : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
-          __end_(0), __left_anchor_(false)
+          __end_(0)
         {__parse(__il.begin(), __il.end());}
 
 //    ~basic_regex() = default;
@@ -2506,7 +2540,6 @@
         __loop_count_ = 0;
         __open_count_ = 0;
         __end_ = nullptr;
-        __left_anchor_ = false;
     }
 public:
 
@@ -2694,7 +2727,7 @@
                           basic_string<_CharT>* __str = nullptr);
 
     _LIBCPP_INLINE_VISIBILITY
-    void __push_l_anchor() {__left_anchor_ = true;}
+    void __push_l_anchor();
     void __push_r_anchor();
     void __push_match_any();
     void __push_match_any_but_newline();
@@ -2732,22 +2765,22 @@
         bool
         __match_at_start(const _CharT* __first, const _CharT* __last,
                  match_results<const _CharT*, _Allocator>& __m,
-                 regex_constants::match_flag_type __flags) const;
+                 regex_constants::match_flag_type __flags, bool) const;
     template <class _Allocator>
         bool
         __match_at_start_ecma(const _CharT* __first, const _CharT* __last,
                  match_results<const _CharT*, _Allocator>& __m,
-                 regex_constants::match_flag_type __flags) const;
+                 regex_constants::match_flag_type __flags, bool) const;
     template <class _Allocator>
         bool
         __match_at_start_posix_nosubs(const _CharT* __first, const _CharT* __last,
                  match_results<const _CharT*, _Allocator>& __m,
-                 regex_constants::match_flag_type __flags) const;
+                 regex_constants::match_flag_type __flags, bool) const;
     template <class _Allocator>
         bool
         __match_at_start_posix_subs(const _CharT* __first, const _CharT* __last,
                  match_results<const _CharT*, _Allocator>& __m,
-                 regex_constants::match_flag_type __flags) const;
+                 regex_constants::match_flag_type __flags, bool) const;
 
     template <class _B, class _A, class _C, class _T>
     friend
@@ -2809,7 +2842,6 @@
     swap(__open_count_, __r.__open_count_);
     swap(__start_, __r.__start_);
     swap(__end_, __r.__end_);
-    swap(__left_anchor_, __r.__left_anchor_);
 }
 
 template <class _CharT, class _Traits>
@@ -2850,7 +2882,9 @@
     match_results<const _CharT*> __m;
     __m.__init(1 + __exp_.mark_count(), __s.__current_, __s.__last_);
     bool __matched = __exp_.__match_at_start_ecma(__s.__current_, __s.__last_,
-                                                  __m, __s.__flags_);
+                                                  __m,
+                                                  __s.__flags_ | regex_constants::match_continuous,
+                                                  true);
     if (__matched != __invert_)
     {
         __s.__do_ = __state::__accept_but_not_consume;
@@ -4087,7 +4121,6 @@
                                 basic_regex __exp;
                                 __exp.__flags_ = __flags_;
                                 __temp = __exp.__parse(++__temp, __last);
-                                __exp.__push_l_anchor();
                                 __push_lookahead(_STD::move(__exp), false);
 #ifndef _LIBCPP_NO_EXCEPTIONS
                                 if (__temp == __last || *__temp != ')')
@@ -4101,7 +4134,6 @@
                                 basic_regex __exp;
                                 __exp.__flags_ = __flags_;
                                 __temp = __exp.__parse(++__temp, __last);
-                                __exp.__push_l_anchor();
                                 __push_lookahead(_STD::move(__exp), true);
 #ifndef _LIBCPP_NO_EXCEPTIONS
                                 if (__temp == __last || *__temp != ')')
@@ -4578,6 +4610,14 @@
 
 template <class _CharT, class _Traits>
 void
+basic_regex<_CharT, _Traits>::__push_l_anchor()
+{
+    __end_->first() = new __l_anchor<_CharT>(__end_->first());
+    __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
+}
+
+template <class _CharT, class _Traits>
+void
 basic_regex<_CharT, _Traits>::__push_r_anchor()
 {
     __end_->first() = new __r_anchor<_CharT>(__end_->first());
@@ -5441,7 +5481,7 @@
 basic_regex<_CharT, _Traits>::__match_at_start_ecma(
         const _CharT* __first, const _CharT* __last,
         match_results<const _CharT*, _Allocator>& __m,
-        regex_constants::match_flag_type __flags) const
+        regex_constants::match_flag_type __flags, bool __at_first) const
 {
     vector<__state> __states;
     ptrdiff_t __j = 0;
@@ -5458,6 +5498,7 @@
         __states.back().__loop_data_.resize(__loop_count());
         __states.back().__node_ = __st;
         __states.back().__flags_ = __flags;
+        __states.back().__at_first_ = __at_first;
         bool __matched = false;
         do
         {
@@ -5506,7 +5547,7 @@
 basic_regex<_CharT, _Traits>::__match_at_start_posix_nosubs(
         const _CharT* __first, const _CharT* __last,
         match_results<const _CharT*, _Allocator>& __m,
-        regex_constants::match_flag_type __flags) const
+        regex_constants::match_flag_type __flags, bool __at_first) const
 {
     deque<__state> __states;
     ptrdiff_t __highest_j = 0;
@@ -5522,6 +5563,7 @@
         __states.back().__loop_data_.resize(__loop_count());
         __states.back().__node_ = __st;
         __states.back().__flags_ = __flags;
+        __states.back().__at_first_ = __at_first;
         bool __matched = false;
         do
         {
@@ -5583,7 +5625,7 @@
 basic_regex<_CharT, _Traits>::__match_at_start_posix_subs(
         const _CharT* __first, const _CharT* __last,
         match_results<const _CharT*, _Allocator>& __m,
-        regex_constants::match_flag_type __flags) const
+        regex_constants::match_flag_type __flags, bool __at_first) const
 {
     vector<__state> __states;
     __state __best_state;
@@ -5602,6 +5644,7 @@
         __states.back().__loop_data_.resize(__loop_count());
         __states.back().__node_ = __st;
         __states.back().__flags_ = __flags;
+        __states.back().__at_first_ = __at_first;
         const _CharT* __current = __first;
         bool __matched = false;
         do
@@ -5667,13 +5710,13 @@
 basic_regex<_CharT, _Traits>::__match_at_start(
         const _CharT* __first, const _CharT* __last,
         match_results<const _CharT*, _Allocator>& __m,
-        regex_constants::match_flag_type __flags) const
+        regex_constants::match_flag_type __flags, bool __at_first) const
 {
     if ((__flags_ & 0x1F0) == ECMAScript)
-        return __match_at_start_ecma(__first, __last, __m, __flags);
+        return __match_at_start_ecma(__first, __last, __m, __flags, __at_first);
     if (mark_count() == 0)
-        return __match_at_start_posix_nosubs(__first, __last, __m, __flags);
-    return __match_at_start_posix_subs(__first, __last, __m, __flags);
+        return __match_at_start_posix_nosubs(__first, __last, __m, __flags, __at_first);
+    return __match_at_start_posix_subs(__first, __last, __m, __flags, __at_first);
 }
 
 template <class _CharT, class _Traits>
@@ -5684,11 +5727,9 @@
         match_results<const _CharT*, _Allocator>& __m,
         regex_constants::match_flag_type __flags) const
 {
-    if (__left_anchor_)
-        __flags |= regex_constants::match_continuous;
     __m.__init(1 + mark_count(), __first, __last,
                                     __flags & regex_constants::__no_update_pos);
-    if (__match_at_start(__first, __last, __m, __flags))
+    if (__match_at_start(__first, __last, __m, __flags, true))
     {
         __m.__prefix_.second = __m[0].first;
         __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
@@ -5702,7 +5743,7 @@
         for (++__first; __first != __last; ++__first)
         {
             __m.__matches_.assign(__m.size(), __m.__unmatched_);
-            if (__match_at_start(__first, __last, __m, __flags))
+            if (__match_at_start(__first, __last, __m, __flags, false))
             {
                 __m.__prefix_.second = __m[0].first;
                 __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;





More information about the cfe-commits mailing list