Skip to content

Commit 75aeb9e

Browse files
committed
Use example from issue
1 parent a4f4281 commit 75aeb9e

File tree

1 file changed

+40
-15
lines changed

1 file changed

+40
-15
lines changed

test/test_std_regex.cpp

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,50 @@
1212

1313
/// Smoke test that std::regex works with generated locales
1414
template<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)) {
2427
TEST_EQ(pieces.size(), 1u);
2528
TEST_EQ(pieces[0].str(), text);
29+
30+
pattern.imbue(l);
31+
pattern = s_pattern;
32+
if TEST(std::regex_match(text, pieces, pattern)) {
33+
TEST_EQ(pieces.size(), 1u);
34+
TEST_EQ(pieces[0].str(), text);
35+
}
36+
37+
// Set via global locale
38+
const std::locale oldLoc = std::locale::global(l);
39+
std::basic_regex<CharType> globalPattern{s_pattern};
40+
if TEST(std::regex_match(text, pieces, globalPattern)) {
41+
TEST_EQ(pieces.size(), 1u);
42+
TEST_EQ(pieces[0].str(), text);
43+
}
44+
std::locale::global(oldLoc);
45+
}
46+
}
47+
48+
template<typename CharType>
49+
void test_by_char(const std::locale& loc, const std::locale& loc_collation, const std::locale& loc_no_collation)
50+
{
51+
test_by_char_impl<CharType>(loc);
52+
{
53+
TEST_CONTEXT("without collation");
54+
test_by_char_impl<CharType>(loc_no_collation);
55+
}
56+
{
57+
TEST_CONTEXT("just collation");
58+
test_by_char_impl<CharType>(loc_collation);
2659
}
2760
}
2861

@@ -42,19 +75,11 @@ void test_main(int /*argc*/, char** /*argv*/)
4275
const std::locale loc_collation = gen("en_US.UTF-8");
4376
{
4477
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-
}
78+
test_by_char<char>(loc, loc_collation, loc_no_collation);
5479
}
5580
{
5681
TEST_CONTEXT("wchar_t");
57-
test_by_char<wchar_t>(loc);
82+
test_by_char<wchar_t>(loc, loc_collation, loc_no_collation);
5883
}
5984
}
6085
}

0 commit comments

Comments
 (0)