• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright (c) 2004
4  * John Maddock
5  *
6  * Use, modification and distribution are subject to the
7  * Boost Software License, Version 1.0. (See accompanying file
8  * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9  *
10  */
11 
12  /*
13   *   LOCATION:    see http://www.boost.org for most recent version.
14   *   FILE         test_deprecated.cpp
15   *   VERSION      see <boost/version.hpp>
16   *   DESCRIPTION: Tests for deprecated interfaces.
17   */
18 
19 #include "test.hpp"
20 #include <boost/cregex.hpp>
21 
22 #ifdef BOOST_MSVC
23 #pragma warning(disable:4267)
24 #endif
25 
26 #ifdef BOOST_NO_STDC_NAMESPACE
27 namespace std{
28    using ::atoi;
29    using ::wcstol;
30 }
31 #endif
32 
get_posix_compile_options(boost::regex_constants::syntax_option_type opts)33 int get_posix_compile_options(boost::regex_constants::syntax_option_type opts)
34 {
35    using namespace boost;
36    int result = 0;
37    switch(opts & regbase::main_option_type)
38    {
39    case regbase::perl:
40       result = (opts & regbase::no_perl_ex) ? REG_EXTENDED : REG_PERL;
41       if(opts & (regbase::no_bk_refs|regbase::no_mod_m|regbase::mod_x|regbase::mod_s|regbase::no_mod_s|regbase::no_escape_in_lists|regbase::no_empty_expressions))
42          return -1;
43       break;
44    case regbase::basic:
45       result = REG_BASIC;
46       if(opts & (regbase::no_char_classes|regbase::no_intervals|regbase::bk_plus_qm|regbase::bk_vbar))
47          return -1;
48       if((opts & regbase::no_escape_in_lists) == 0)
49          return -1;
50       break;
51    default:
52       return -1;
53    }
54 
55    if(opts & regbase::icase)
56       result |= REG_ICASE;
57    if(opts & regbase::nosubs)
58       result |= REG_NOSUB;
59    if(opts & regbase::newline_alt)
60       result |= REG_NEWLINE;
61    if((opts & regbase::collate) == 0)
62       result |= REG_NOCOLLATE;
63 
64    return result;
65 }
66 
get_posix_match_flags(boost::regex_constants::match_flag_type f)67 int get_posix_match_flags(boost::regex_constants::match_flag_type f)
68 {
69    int result = 0;
70    if(f & boost::regex_constants::match_not_bol)
71       result |= boost::REG_NOTBOL;
72    if(f & boost::regex_constants::match_not_eol)
73       result |= boost::REG_NOTEOL;
74    if(f & ~(boost::regex_constants::match_not_bol|boost::regex_constants::match_not_eol))
75       return -1;
76    return result;
77 }
78 
test_deprecated(const char &,const test_regex_search_tag &)79 void test_deprecated(const char&, const test_regex_search_tag&)
80 {
81    const std::string& expression = test_info<char>::expression();
82    if(expression.find('\0') != std::string::npos)
83       return;
84    const std::string& search_text = test_info<char>::search_text();
85    if(search_text.find('\0') != std::string::npos)
86       return;
87    int posix_options = get_posix_compile_options(test_info<char>::syntax_options());
88    if(posix_options < 0)
89       return;
90    int posix_match_options = get_posix_match_flags(test_info<char>::match_options());
91    if(posix_match_options < 0)
92       return;
93    const int* results = test_info<char>::answer_table();
94 
95    // OK try and compile the expression:
96    boost::regex_tA re;
97    if(boost::regcompA(&re, expression.c_str(), posix_options) != 0)
98    {
99       BOOST_REGEX_TEST_ERROR("Expression : \"" << expression.c_str() << "\" did not compile with the POSIX C API.", char);
100       return;
101    }
102    // try and find the first occurrence:
103    static const unsigned max_subs = 100;
104    boost::regmatch_t matches[max_subs];
105    if(boost::regexecA(&re, search_text.c_str(), max_subs, matches, posix_match_options) == 0)
106    {
107       int i = 0;
108       while(results[2*i] != -2)
109       {
110          if((int)max_subs > i)
111          {
112             if(results[2*i] != matches[i].rm_so)
113             {
114                BOOST_REGEX_TEST_ERROR("Mismatch in start of subexpression " << i << " found with the POSIX C API.", char);
115             }
116             if(results[2*i+1] != matches[i].rm_eo)
117             {
118                BOOST_REGEX_TEST_ERROR("Mismatch in end of subexpression " << i << " found with the POSIX C API.", char);
119             }
120          }
121          ++i;
122       }
123    }
124    else
125    {
126       if(results[0] >= 0)
127       {
128          BOOST_REGEX_TEST_ERROR("Expression : \"" << expression.c_str() << "\" was not found with the POSIX C API.", char);
129       }
130    }
131    // clean up whatever:
132    boost::regfreeA(&re);
133 
134 }
135 
to_narrow_string(std::wstring const & w)136 std::string to_narrow_string(std::wstring const& w)
137 {
138    return std::string(w.begin(), w.end());
139 }
140 
test_deprecated(const wchar_t &,const test_regex_search_tag &)141 void test_deprecated(const wchar_t&, const test_regex_search_tag&)
142 {
143 #ifndef BOOST_NO_WREGEX
144    const std::wstring& expression = test_info<wchar_t>::expression();
145    if(expression.find(L'\0') != std::wstring::npos)
146       return;
147    const std::wstring& search_text = test_info<wchar_t>::search_text();
148    if(search_text.find(L'\0') != std::wstring::npos)
149       return;
150    int posix_options = get_posix_compile_options(test_info<wchar_t>::syntax_options());
151    if(posix_options < 0)
152       return;
153    int posix_match_options = get_posix_match_flags(test_info<wchar_t>::match_options());
154    if(posix_match_options < 0)
155       return;
156    const int* results = test_info<wchar_t>::answer_table();
157 
158    // OK try and compile the expression:
159    boost::regex_tW re;
160    if(boost::regcompW(&re, expression.c_str(), posix_options) != 0)
161    {
162       BOOST_REGEX_TEST_ERROR("Expression : \"" << to_narrow_string(expression.c_str()) << "\" did not compile with the POSIX C API.", wchar_t);
163       return;
164    }
165    // try and find the first occurrence:
166    static const unsigned max_subs = 100;
167    boost::regmatch_t matches[max_subs];
168    if(boost::regexecW(&re, search_text.c_str(), max_subs, matches, posix_match_options) == 0)
169    {
170       int i = 0;
171       while(results[2*i] != -2)
172       {
173          if((int)max_subs > i)
174          {
175             if(results[2*i] != matches[i].rm_so)
176             {
177                BOOST_REGEX_TEST_ERROR("Mismatch in start of subexpression " << i << " found with the POSIX C API.", wchar_t);
178             }
179             if(results[2*i+1] != matches[i].rm_eo)
180             {
181                BOOST_REGEX_TEST_ERROR("Mismatch in end of subexpression " << i << " found with the POSIX C API.", wchar_t);
182             }
183          }
184          ++i;
185       }
186    }
187    else
188    {
189       if(results[0] >= 0)
190       {
191          BOOST_REGEX_TEST_ERROR("Expression : \"" << to_narrow_string(expression.c_str()) << "\" was not found with the POSIX C API.", wchar_t);
192       }
193    }
194    // clean up whatever:
195    boost::regfreeW(&re);
196 #endif
197 }
198 
test_deprecated(const char &,const test_invalid_regex_tag &)199 void test_deprecated(const char&, const test_invalid_regex_tag&)
200 {
201    const std::string& expression = test_info<char>::expression();
202    if(expression.find('\0') != std::string::npos)
203       return;
204    int posix_options = get_posix_compile_options(test_info<char>::syntax_options());
205    if(posix_options < 0)
206       return;
207 
208    // OK try and compile the expression:
209    boost::regex_tA re;
210    int code = boost::regcompA(&re, expression.c_str(), posix_options);
211    if(code == 0)
212    {
213       boost::regfreeA(&re);
214       BOOST_REGEX_TEST_ERROR("Expression : \"" << expression.c_str() << "\" unexpectedly compiled with the POSIX C API.", char);
215    }
216    else
217    {
218       char buf[100];
219       int s = boost::regerrorA(code, &re, 0, 0);
220       if(s < 100)
221          s = boost::regerrorA(code, &re, buf, 100);
222       s = boost::regerrorA(code | boost::REG_ITOA, &re, 0, 0);
223       if(s < 100)
224       {
225          s = boost::regerrorA(code | boost::REG_ITOA, &re, buf, 100);
226          re.re_endp = buf;
227          s = boost::regerrorA(code | boost::REG_ATOI, &re, buf, 100);
228          if(s)
229          {
230             int code2 = std::atoi(buf);
231             if(code2 != code)
232             {
233                BOOST_REGEX_TEST_ERROR("Got a bad error code from regerrA with REG_ATOI set: ", char);
234             }
235          }
236       }
237    }
238 }
239 
test_deprecated(const wchar_t &,const test_invalid_regex_tag &)240 void test_deprecated(const wchar_t&, const test_invalid_regex_tag&)
241 {
242 #ifndef BOOST_NO_WREGEX
243    const std::wstring& expression = test_info<wchar_t>::expression();
244    if(expression.find(L'\0') != std::string::npos)
245       return;
246    int posix_options = get_posix_compile_options(test_info<wchar_t>::syntax_options());
247    if(posix_options < 0)
248       return;
249 
250    // OK try and compile the expression:
251    boost::regex_tW re;
252    int code = boost::regcompW(&re, expression.c_str(), posix_options);
253    if(code == 0)
254    {
255       boost::regfreeW(&re);
256       BOOST_REGEX_TEST_ERROR("Expression : \"" << to_narrow_string(expression.c_str()) << "\" unexpectedly compiled with the POSIX C API.", wchar_t);
257    }
258    else
259    {
260       wchar_t buf[100];
261       int s = boost::regerrorW(code, &re, 0, 0);
262       if(s < 100)
263          s = boost::regerrorW(code, &re, buf, 100);
264       s = boost::regerrorW(code | boost::REG_ITOA, &re, 0, 0);
265       if(s < 100)
266       {
267          s = boost::regerrorW(code | boost::REG_ITOA, &re, buf, 100);
268          re.re_endp = buf;
269          s = boost::regerrorW(code | boost::REG_ATOI, &re, buf, 100);
270          if(s)
271          {
272             long code2 = std::wcstol(buf, 0, 10);
273             if(code2 != code)
274             {
275                BOOST_REGEX_TEST_ERROR("Got a bad error code from regerrW with REG_ATOI set: ", char);
276             }
277          }
278       }
279    }
280 #endif
281 }
282