From ecd5dd5ff3192253a6b91630ab5bfe4ea7dfd1b9 Mon Sep 17 00:00:00 2001 From: John Maddock Date: Thu, 17 Feb 2005 12:44:16 +0000 Subject: [PATCH] Allow sort keys to contain nothing but a single NUL character (characters may be ignorable). [SVN r27408] --- include/boost/regex/v4/basic_regex_creator.hpp | 6 +++++- include/boost/regex/v4/cpp_regex_traits.hpp | 9 ++++++--- include/boost/regex/v4/perl_matcher.hpp | 18 ++++++++++++++++-- include/boost/regex/v4/w32_regex_traits.hpp | 2 ++ src/c_regex_traits.cpp | 2 ++ src/wc_regex_traits.cpp | 2 ++ test/regress/test_sets.cpp | 8 ++++++++ 7 files changed, 41 insertions(+), 6 deletions(-) diff --git a/include/boost/regex/v4/basic_regex_creator.hpp b/include/boost/regex/v4/basic_regex_creator.hpp index 081de4c4..c37ee5f7 100644 --- a/include/boost/regex/v4/basic_regex_creator.hpp +++ b/include/boost/regex/v4/basic_regex_creator.hpp @@ -407,6 +407,10 @@ re_syntax_base* basic_regex_creator::append_set( charT a2[3] = { c2.first, c2.second, charT(0), }; s1 = this->m_traits.transform(a1, (a1[1] ? a1+2 : a1+1)); s2 = this->m_traits.transform(a2, (a2[1] ? a2+2 : a2+1)); + if(s1.size() == 0) + s1 = string_type(1, charT(0)); + if(s2.size() == 0) + s2 = string_type(1, charT(0)); } else { @@ -546,7 +550,7 @@ re_syntax_base* basic_regex_creator::append_set( } else { - if(!char_less(c1, c2)) + if(char_less(c2, c1)) { // Oops error: return 0; diff --git a/include/boost/regex/v4/cpp_regex_traits.hpp b/include/boost/regex/v4/cpp_regex_traits.hpp index da621856..593271f4 100644 --- a/include/boost/regex/v4/cpp_regex_traits.hpp +++ b/include/boost/regex/v4/cpp_regex_traits.hpp @@ -509,10 +509,8 @@ typename cpp_regex_traits_implementation::string_type case sort_delim: // get a regular sort key, and then truncate everything after the delim: result.assign(this->m_pcollate->transform(p1, p2)); - if(result.size() && (result[0] == m_collate_delim)) - break; std::size_t i; - for(i = 1; i < result.size(); ++i) + for(i = 0; i < result.size(); ++i) { if(result[i] == m_collate_delim) break; @@ -523,6 +521,11 @@ typename cpp_regex_traits_implementation::string_type }catch(...){} while(result.size() && (charT(0) == *result.rbegin())) result.erase(result.size() - 1); + if(result.empty()) + { + // character is ignorable at the primary level: + result = string_type(1, charT(0)); + } return result; } diff --git a/include/boost/regex/v4/perl_matcher.hpp b/include/boost/regex/v4/perl_matcher.hpp index a7e3c600..58f16873 100644 --- a/include/boost/regex/v4/perl_matcher.hpp +++ b/include/boost/regex/v4/perl_matcher.hpp @@ -68,11 +68,25 @@ inline bool can_start(wchar_t c, const unsigned char* map, unsigned char mask) #ifndef _RWSTD_VER template inline int string_compare(const std::basic_string& s, const C* p) -{ return s.compare(p); } +{ + if(0 == *p) + { + if(s.empty() || ((s.size() == 1) && (s[0] == 0))) + return 0; + } + return s.compare(p); +} #else template inline int string_compare(const std::basic_string& s, const C* p) -{ return s.compare(p); } +{ + if(0 == *p) + { + if(s.empty() || ((s.size() == 1) && (s[0] == 0))) + return 0; + } + return s.compare(p); +} inline int string_compare(const std::string& s, const char* p) { return std::strcmp(s.c_str(), p); } # ifndef BOOST_NO_WREGEX diff --git a/include/boost/regex/v4/w32_regex_traits.hpp b/include/boost/regex/v4/w32_regex_traits.hpp index b36d95ca..99de12a0 100644 --- a/include/boost/regex/v4/w32_regex_traits.hpp +++ b/include/boost/regex/v4/w32_regex_traits.hpp @@ -372,6 +372,8 @@ typename w32_regex_traits_implementation::string_type result.erase(i); break; } + if(result.empty()) + result = string_type(1, charT(0)); return result; } diff --git a/src/c_regex_traits.cpp b/src/c_regex_traits.cpp index a34e38f7..da960eb0 100644 --- a/src/c_regex_traits.cpp +++ b/src/c_regex_traits.cpp @@ -102,6 +102,8 @@ c_regex_traits::string_type BOOST_REGEX_CALL c_regex_traits::transfo result.erase(i); break; } + if(result.empty()) + result = std::string(1, char(0)); return result; } diff --git a/src/wc_regex_traits.cpp b/src/wc_regex_traits.cpp index 06cefd2c..406a22be 100644 --- a/src/wc_regex_traits.cpp +++ b/src/wc_regex_traits.cpp @@ -94,6 +94,8 @@ c_regex_traits::string_type BOOST_REGEX_CALL c_regex_traits::t result.erase(i); break; } + if(result.empty()) + result = std::wstring(1, char(0)); return result; } diff --git a/test/regress/test_sets.cpp b/test/regress/test_sets.cpp index 2496c934..b1cd7066 100644 --- a/test/regress/test_sets.cpp +++ b/test/regress/test_sets.cpp @@ -153,6 +153,12 @@ void test_sets2() TEST_REGEX_SEARCH("[[.right-curly-bracket.]]", perl, "}", match_default, make_array(0, 1, -2, -2)); TEST_REGEX_SEARCH("[[.NUL.]]", perl, "\0", match_default, make_array(0, 1, -2, -2)); TEST_REGEX_SEARCH("[[.NUL.][.ae.]]", perl, "\0", match_default, make_array(0, 1, -2, -2)); + TEST_REGEX_SEARCH("[[.NUL.]-a]", extended, "\0", match_default, make_array(0, 1, -2, -2)); + TEST_REGEX_SEARCH("[[.NUL.]-a]", perl, "\0", match_default, make_array(0, 1, -2, -2)); + TEST_REGEX_SEARCH("[[.NUL.]-a]", extended, "a", match_default, make_array(0, 1, -2, -2)); + TEST_REGEX_SEARCH("[[.NUL.]-a]", perl, "a", match_default, make_array(0, 1, -2, -2)); + TEST_REGEX_SEARCH("[[.NUL.]-[.NUL.]a]", extended, "a", match_default, make_array(0, 1, -2, -2)); + TEST_REGEX_SEARCH("[[.NUL.]-[.NUL.]a]", perl, "a", match_default, make_array(0, 1, -2, -2)); TEST_INVALID_REGEX("[[..]]", perl); TEST_INVALID_REGEX("[[.not-a-collating-element.]]", perl); TEST_INVALID_REGEX("[[.", perl); @@ -222,6 +228,8 @@ void test_sets2() TEST_REGEX_SEARCH("[[=a=]]", basic, "A", match_default, make_array(0, 1, -2, -2)); TEST_REGEX_SEARCH("[[=ae=]]", basic, "ae", match_default, make_array(0, 2, -2, -2)); TEST_REGEX_SEARCH("[[=right-curly-bracket=]]", basic, "}", match_default, make_array(0, 1, -2, -2)); + TEST_REGEX_SEARCH("[[=NUL=]]", basic, "\x0", match_default, make_array(0, 1, -2, -2)); + TEST_REGEX_SEARCH("[[=NUL=]]", perl, "\x0", match_default, make_array(0, 1, -2, -2)); TEST_INVALID_REGEX("[[=", perl); TEST_INVALID_REGEX("[[=a", perl); TEST_INVALID_REGEX("[[=ae", perl);