1///////////////////////////////////////////////////////////////////////////////
2// test_non_char.cpp
3//
4// Copyright 2008 Eric Niebler. Distributed under the Boost
5// Software License, Version 1.0. (See accompanying file
6// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7
8#include <algorithm>
9#include <boost/xpressive/xpressive.hpp>
10#include <boost/xpressive/traits/null_regex_traits.hpp>
11#include "./test.hpp"
12
13///////////////////////////////////////////////////////////////////////////////
14// test_static
15void test_static()
16{
17 static int const data[] = {0, 1, 2, 3, 4, 5, 6};
18 null_regex_traits<int> nul;
19 basic_regex<int const *> rex = imbue(loc: nul)(1 >> +((set= 2,3) | 4) >> 5);
20 match_results<int const *> what;
21
22 if(!regex_search(begin: data, end: data + (sizeof(data)/sizeof(*data)), what, re: rex))
23 {
24 BOOST_ERROR("regex_search on integral data failed");
25 }
26 else
27 {
28 BOOST_CHECK(*what[0].first == 1);
29 BOOST_CHECK(*what[0].second == 6);
30 }
31}
32
33///////////////////////////////////////////////////////////////////////////////
34// UChar
35struct UChar
36{
37 UChar(unsigned int code = 0)
38 : code_(code)
39 {}
40
41 operator unsigned int () const
42 {
43 return this->code_;
44 }
45
46private:
47 unsigned int code_;
48};
49
50///////////////////////////////////////////////////////////////////////////////
51// UChar_traits
52struct UChar_traits
53 : null_regex_traits<UChar>
54{};
55
56///////////////////////////////////////////////////////////////////////////////
57// test_dynamic
58void test_dynamic()
59{
60 typedef std::vector<UChar>::const_iterator uchar_iterator;
61 typedef basic_regex<uchar_iterator> uregex;
62 typedef match_results<uchar_iterator> umatch;
63 typedef regex_compiler<uchar_iterator, UChar_traits> uregex_compiler;
64
65 std::string pattern_("b.*r"), str_("foobarboo");
66 std::vector<UChar> pattern(pattern_.begin(), pattern_.end());
67 std::vector<UChar> str(str_.begin(), str_.end());
68
69 UChar_traits tr;
70 uregex_compiler compiler(tr);
71 uregex urx = compiler.compile(pat: pattern);
72 umatch what;
73
74 if(!regex_search(rng&: str, what, re: urx))
75 {
76 BOOST_ERROR("regex_search on UChar failed");
77 }
78 else
79 {
80 BOOST_CHECK_EQUAL(3, what.position());
81 BOOST_CHECK_EQUAL(3, what.length());
82 }
83
84 // test for range-based regex_replace
85 std::vector<UChar> output = regex_replace(str, re: urx, format: pattern_);
86 std::string output_(output.begin(), output.end());
87 std::string expected("foob.*rboo");
88 BOOST_CHECK_EQUAL(output_, expected);
89}
90
91///////////////////////////////////////////////////////////////////////////////
92// init_unit_test_suite
93//
94test_suite* init_unit_test_suite( int argc, char* argv[] )
95{
96 test_suite *test = BOOST_TEST_SUITE("test_non_char");
97 test->add(BOOST_TEST_CASE(&test_static));
98 test->add(BOOST_TEST_CASE(&test_dynamic));
99 return test;
100}
101
102

source code of boost/libs/xpressive/test/test_non_char.cpp