mirror of
https://github.com/google/googletest.git
synced 2024-12-27 18:31:21 +08:00
Fix RE::Init for Android and NetBSD.
This is a somewhat recent change for Android (I'm not clear on whether it's a recent change for NetBSD, or if Android was just very behind on its implementation), so while this worked fine as recently as API 32 devices, REG_GNU is required for API 34 (API 33 untested). A test actually caught this, but https://github.com/google/googletest/pull/4334 "fixed" the test rather than the implementation. This CL also reverts the test change so it can catch the failure. PiperOrigin-RevId: 571126374 Change-Id: I420dfcedea58f2c8b605f699515d744006c0a9d9
This commit is contained in:
parent
beb552fb47
commit
2dd1c13195
@ -697,13 +697,24 @@ bool RE::PartialMatch(const char* str, const RE& re) {
|
|||||||
void RE::Init(const char* regex) {
|
void RE::Init(const char* regex) {
|
||||||
pattern_ = regex;
|
pattern_ = regex;
|
||||||
|
|
||||||
|
// NetBSD (and Android, which takes its regex implemntation from NetBSD) does
|
||||||
|
// not include the GNU regex extensions (such as Perl style character classes
|
||||||
|
// like \w) in REG_EXTENDED. REG_EXTENDED is only specified to include the
|
||||||
|
// [[:alpha:]] style character classes. Enable REG_GNU wherever it is defined
|
||||||
|
// so users can use those extensions.
|
||||||
|
#if defined(REG_GNU)
|
||||||
|
constexpr int reg_flags = REG_EXTENDED | REG_GNU;
|
||||||
|
#else
|
||||||
|
constexpr int reg_flags = REG_EXTENDED;
|
||||||
|
#endif
|
||||||
|
|
||||||
// Reserves enough bytes to hold the regular expression used for a
|
// Reserves enough bytes to hold the regular expression used for a
|
||||||
// full match.
|
// full match.
|
||||||
const size_t full_regex_len = strlen(regex) + 10;
|
const size_t full_regex_len = strlen(regex) + 10;
|
||||||
char* const full_pattern = new char[full_regex_len];
|
char* const full_pattern = new char[full_regex_len];
|
||||||
|
|
||||||
snprintf(full_pattern, full_regex_len, "^(%s)$", regex);
|
snprintf(full_pattern, full_regex_len, "^(%s)$", regex);
|
||||||
is_valid_ = regcomp(&full_regex_, full_pattern, REG_EXTENDED) == 0;
|
is_valid_ = regcomp(&full_regex_, full_pattern, reg_flags) == 0;
|
||||||
// We want to call regcomp(&partial_regex_, ...) even if the
|
// We want to call regcomp(&partial_regex_, ...) even if the
|
||||||
// previous expression returns false. Otherwise partial_regex_ may
|
// previous expression returns false. Otherwise partial_regex_ may
|
||||||
// not be properly initialized can may cause trouble when it's
|
// not be properly initialized can may cause trouble when it's
|
||||||
@ -714,7 +725,7 @@ void RE::Init(const char* regex) {
|
|||||||
// regex. We change it to an equivalent form "()" to be safe.
|
// regex. We change it to an equivalent form "()" to be safe.
|
||||||
if (is_valid_) {
|
if (is_valid_) {
|
||||||
const char* const partial_regex = (*regex == '\0') ? "()" : regex;
|
const char* const partial_regex = (*regex == '\0') ? "()" : regex;
|
||||||
is_valid_ = regcomp(&partial_regex_, partial_regex, REG_EXTENDED) == 0;
|
is_valid_ = regcomp(&partial_regex_, partial_regex, reg_flags) == 0;
|
||||||
}
|
}
|
||||||
EXPECT_TRUE(is_valid_)
|
EXPECT_TRUE(is_valid_)
|
||||||
<< "Regular expression \"" << regex
|
<< "Regular expression \"" << regex
|
||||||
|
@ -418,8 +418,8 @@ TYPED_TEST(RETest, ImplicitConstructorWorks) {
|
|||||||
const RE simple(TypeParam("hello"));
|
const RE simple(TypeParam("hello"));
|
||||||
EXPECT_STREQ("hello", simple.pattern());
|
EXPECT_STREQ("hello", simple.pattern());
|
||||||
|
|
||||||
const RE normal(TypeParam(".*([[:alnum:]_]+)"));
|
const RE normal(TypeParam(".*(\\w+)"));
|
||||||
EXPECT_STREQ(".*([[:alnum:]_]+)", normal.pattern());
|
EXPECT_STREQ(".*(\\w+)", normal.pattern());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests that RE's constructors reject invalid regular expressions.
|
// Tests that RE's constructors reject invalid regular expressions.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user