1212
1313// / Smoke test that std::regex works with generated locales
1414template <typename CharType>
15- void test_by_char (const std::locale& l)
15+ void test_by_char_impl (const std::locale& l)
1616{
1717 using string_type = std::basic_string<CharType>;
18- std::basic_regex<CharType> pattern;
19- pattern. imbue (l);
20- pattern = ascii_to<CharType>(" [[:alnum:]]+" );
21- const string_type text = ascii_to<CharType>(" ab12cd " );
18+
19+ // Needs at least a '\s' and '=' to reproduce issue #249
20+ const string_type s_pattern = ascii_to<CharType>(R"( [[:alnum:]]+\s*= \d+ ) " );
21+ const string_type text = ascii_to<CharType>(" a2b2 = 42 " );
2222 std::match_results<typename string_type::const_iterator> pieces;
23+
24+ // Sanity check using default locale
25+ std::basic_regex<CharType> pattern{s_pattern};
2326 if TEST (std::regex_match (text, pieces, pattern)) {
27+ std::basic_regex<CharType> pattern;
2428 TEST_EQ (pieces.size (), 1u );
2529 TEST_EQ (pieces[0 ].str (), text);
30+
31+ pattern.imbue (l);
32+ pattern = s_pattern;
33+ if TEST (std::regex_match (text, pieces, pattern)) {
34+ TEST_EQ (pieces.size (), 1u );
35+ TEST_EQ (pieces[0 ].str (), text);
36+ }
37+
38+ // Set via global locale
39+ const std::locale oldLoc = std::locale::global (l);
40+ std::basic_regex<CharType> globalPattern{s_pattern};
41+ if TEST (std::regex_match (text, pieces, globalPattern)) {
42+ TEST_EQ (pieces.size (), 1u );
43+ TEST_EQ (pieces[0 ].str (), text);
44+ }
45+ std::locale::global (oldLoc);
46+ }
47+ }
48+
49+ template <typename CharType>
50+ void test_by_char (const std::locale& loc, const std::locale& loc_collation, const std::locale& loc_no_collation)
51+ {
52+ test_by_char_impl<CharType>(loc);
53+ {
54+ TEST_CONTEXT (" without collation" );
55+ test_by_char_impl<CharType>(loc_no_collation);
56+ }
57+ {
58+ TEST_CONTEXT (" just collation" );
59+ test_by_char_impl<CharType>(loc_collation);
2660 }
2761}
2862
@@ -42,19 +76,11 @@ void test_main(int /*argc*/, char** /*argv*/)
4276 const std::locale loc_collation = gen (" en_US.UTF-8" );
4377 {
4478 TEST_CONTEXT (" char" );
45- test_by_char<char >(loc);
46- {
47- TEST_CONTEXT (" without collation" );
48- test_by_char<char >(loc_no_collation);
49- }
50- {
51- TEST_CONTEXT (" just collation" );
52- test_by_char<char >(loc_collation);
53- }
79+ test_by_char<char >(loc, loc_collation, loc_no_collation);
5480 }
5581 {
5682 TEST_CONTEXT (" wchar_t" );
57- test_by_char<wchar_t >(loc);
83+ test_by_char<wchar_t >(loc, loc_collation, loc_no_collation );
5884 }
5985 }
6086}
0 commit comments