diff options
author | vladlosev <vladlosev@861a406c-534a-0410-8894-cb66d6ee9925> | 2008-11-20 01:40:35 +0000 |
---|---|---|
committer | vladlosev <vladlosev@861a406c-534a-0410-8894-cb66d6ee9925> | 2008-11-20 01:40:35 +0000 |
commit | 3d7042176307f0d7700a3640f3b3bcc8790b8fcd (patch) | |
tree | ec4a9020570acc6d09366e5b305b9d162c1a6026 /src/gtest-port.cc | |
parent | b6a296d0f7caff7140f422e49f5398c9ef17504d (diff) | |
download | googletest-3d7042176307f0d7700a3640f3b3bcc8790b8fcd.tar.gz googletest-3d7042176307f0d7700a3640f3b3bcc8790b8fcd.tar.bz2 googletest-3d7042176307f0d7700a3640f3b3bcc8790b8fcd.zip |
Value-parameterized tests and many bugfixes
Diffstat (limited to 'src/gtest-port.cc')
-rw-r--r-- | src/gtest-port.cc | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/src/gtest-port.cc b/src/gtest-port.cc index 000a4ab1..9878cae0 100644 --- a/src/gtest-port.cc +++ b/src/gtest-port.cc @@ -56,25 +56,49 @@ namespace internal { // Implements RE. Currently only needed for death tests. RE::~RE() { - regfree(®ex_); + regfree(&partial_regex_); + regfree(&full_regex_); free(const_cast<char*>(pattern_)); } -// Returns true iff str contains regular expression re. +// Returns true iff regular expression re matches the entire str. +bool RE::FullMatch(const char* str, const RE& re) { + if (!re.is_valid_) return false; + + regmatch_t match; + return regexec(&re.full_regex_, str, 1, &match, 0) == 0; +} + +// Returns true iff regular expression re matches a substring of str +// (including str itself). bool RE::PartialMatch(const char* str, const RE& re) { if (!re.is_valid_) return false; regmatch_t match; - return regexec(&re.regex_, str, 1, &match, 0) == 0; + return regexec(&re.partial_regex_, str, 1, &match, 0) == 0; } // Initializes an RE from its string representation. void RE::Init(const char* regex) { pattern_ = strdup(regex); - is_valid_ = regcomp(®ex_, regex, REG_EXTENDED) == 0; + + // Reserves enough bytes to hold the regular expression used for a + // full match. + const size_t full_regex_len = strlen(regex) + 10; + char* const full_pattern = new char[full_regex_len]; + + snprintf(full_pattern, full_regex_len, "^(%s)$", regex); + is_valid_ = regcomp(&full_regex_, full_pattern, REG_EXTENDED) == 0; + // We want to call regcomp(&partial_regex_, ...) even if the + // previous expression returns false. Otherwise partial_regex_ may + // not be properly initialized can may cause trouble when it's + // freed. + is_valid_ = (regcomp(&partial_regex_, regex, REG_EXTENDED) == 0) && is_valid_; EXPECT_TRUE(is_valid_) << "Regular expression \"" << regex << "\" is not a valid POSIX Extended regular expression."; + + delete[] full_pattern; } #endif // GTEST_HAS_DEATH_TEST |