Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

Commit

Permalink
#168: unique smart pointers in regex
Browse files Browse the repository at this point in the history
instead of new/delete.
  • Loading branch information
hugbug committed Feb 25, 2016
1 parent 6bd5223 commit 27eb78f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
6 changes: 2 additions & 4 deletions daemon/util/Util.cpp
Expand Up @@ -1577,7 +1577,7 @@ RegEx::RegEx(const char *pattern, int matchBufSize)
m_matchBufSize = matchBufSize;
if (matchBufSize > 0)
{
m_matches = new regmatch_t[matchBufSize];
m_matches = std::make_unique<regmatch_t[]>(matchBufSize);
}
else
{
Expand All @@ -1592,14 +1592,13 @@ RegEx::~RegEx()
{
#ifdef HAVE_REGEX_H
regfree(&m_context);
delete[] m_matches;
#endif
}

bool RegEx::Match(const char *str)
{
#ifdef HAVE_REGEX_H
return m_valid ? regexec(&m_context, str, m_matchBufSize, m_matches, 0) == 0 : false;
return m_valid ? regexec(&m_context, str, m_matchBufSize, m_matches.get(), 0) == 0 : false;
#else
return false;
#endif
Expand Down Expand Up @@ -1640,7 +1639,6 @@ int RegEx::GetMatchLen(int index)
#endif
}


WildMask::WildMask(const char *pattern, bool wantsPositions)
{
m_pattern = pattern;
Expand Down
4 changes: 2 additions & 2 deletions daemon/util/Util.h
Expand Up @@ -217,7 +217,7 @@ class RegEx
private:
#ifdef HAVE_REGEX_H
regex_t m_context;
regmatch_t* m_matches;
std::unique_ptr<regmatch_t[]> m_matches;
#endif
bool m_valid;
int m_matchBufSize;
Expand All @@ -226,7 +226,7 @@ class RegEx
RegEx(const char *pattern, int matchBufSize = 100);
~RegEx();
bool IsValid() { return m_valid; }
bool Match(const char *str);
bool Match(const char* str);
int GetMatchCount();
int GetMatchStart(int index);
int GetMatchLen(int index);
Expand Down
40 changes: 40 additions & 0 deletions tests/util/UtilTest.cpp
Expand Up @@ -83,3 +83,43 @@ TEST_CASE("Util: WildMask", "[Util][Quick]")
REQUIRE(mask.Match(".par2"));
REQUIRE_FALSE(mask.Match("par2"));
}

TEST_CASE("Util: RegEx", "[Util][Quick]")
{
RegEx regExRar(".*\\.rar$");
RegEx regExRarMultiSeq(".*\\.[r-z][0-9][0-9]$");
RegEx regExSevenZip(".*\\.7z$|.*\\.7z\\.[0-9]+$");
RegEx regExNumExt(".*\\.[0-9]+$");

REQUIRE(regExRar.Match("filename.rar"));
REQUIRE(regExRar.Match("filename.part001.rar"));
REQUIRE_FALSE(regExRar.Match("filename.rar.txt"));

REQUIRE_FALSE(regExRarMultiSeq.Match("filename.rar"));
REQUIRE(regExRarMultiSeq.Match("filename.r01"));
REQUIRE(regExRarMultiSeq.Match("filename.r99"));
REQUIRE_FALSE(regExRarMultiSeq.Match("filename.r001"));
REQUIRE(regExRarMultiSeq.Match("filename.s01"));
REQUIRE(regExRarMultiSeq.Match("filename.t99"));

REQUIRE(regExSevenZip.Match("filename.7z"));
REQUIRE_FALSE(regExSevenZip.Match("filename.7z.rar"));
REQUIRE(regExSevenZip.Match("filename.7z.1"));
REQUIRE(regExSevenZip.Match("filename.7z.001"));
REQUIRE(regExSevenZip.Match("filename.7z.123"));
REQUIRE(regExSevenZip.Match("filename.7z.999"));

REQUIRE(regExNumExt.Match("filename.7z.1"));
REQUIRE(regExNumExt.Match("filename.7z.9"));
REQUIRE(regExNumExt.Match("filename.7z.001"));
REQUIRE(regExNumExt.Match("filename.7z.123"));
REQUIRE(regExNumExt.Match("filename.7z.999"));

const char* testStr = "My.Show.Name.S01E02.ABC.720";
RegEx seasonEpisode(".*S([0-9]+)E([0-9]+).*");
REQUIRE(seasonEpisode.IsValid());
REQUIRE(seasonEpisode.Match(testStr));
REQUIRE(seasonEpisode.GetMatchCount() == 3);
REQUIRE(seasonEpisode.GetMatchStart(1) == 14);
REQUIRE(seasonEpisode.GetMatchLen(1) == 2);
}

0 comments on commit 27eb78f

Please sign in to comment.