Skip to content

Commit

Permalink
Fix #47, change Union::String::IsMatchesMask into a proper std::regex
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmacha committed Oct 19, 2024
1 parent 38da23f commit 3bcc535
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
23 changes: 17 additions & 6 deletions src/NH/Bass/TransitionInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,26 @@
#include <NH/Bass/MusicTheme.h>

#include <utility>
#include <regex>

namespace NH::Bass
{
const Transition& TransitionInfo::GetTransition(const std::string& targetTheme) const
{
for (const auto& [key, transition]: m_FilteredTransitions)
{
String regex = String(key.c_str()).Replace(",", "|").Replace(" ", "\\s*").Replace("*", ".*");;
if (regex.IsMatchesMask(targetTheme.c_str()))
String regexString = String(key.c_str())
// interpret comma as an OR operator, "THEME_1,THEME_2" -> "THEME_1|THEME_2"
.Replace(",", "|")
// interpret a space as zero or more whitespace characters
.Replace(" ", "\\s*")
// nasty hack to avoid replacement of .* -> ..* by the next step
.Replace(".*", "*")
// interpret wildcard (*) as any match (.*) in the regex syntax
.Replace("*", ".*");

const std::regex regex(regexString);
if (std::smatch match; std::regex_match(targetTheme, match, regex))
{
return transition;
}
Expand All @@ -35,7 +46,7 @@ namespace NH::Bass

void TransitionInfo::AddMidiFile(const MidiFile& file, const std::string& filter)
{
Transition* transition = filter == "" ? &m_DefaultTransition : GetFilteredTransition(filter);
Transition* transition = filter.empty() ? &m_DefaultTransition : GetFilteredTransition(filter);
Transition::TimePoint timePoint{-1};
for (const auto& [key, start, end] : file.GetTones())
{
Expand All @@ -58,18 +69,18 @@ namespace NH::Bass

void TransitionInfo::AddTimePoint(const Transition::TimePoint& timePoint, const std::string& filter)
{
Transition* transition = filter == "" ? &m_DefaultTransition : GetFilteredTransition(filter);
Transition* transition = filter.empty() ? &m_DefaultTransition : GetFilteredTransition(filter);
transition->TimePoints.push_back(timePoint);
}

void TransitionInfo::AddJingle(std::shared_ptr<AudioFile> jingle, const double delay, const std::string& filter)
{
Transition* transition = filter == "" ? &m_DefaultTransition : GetFilteredTransition(filter);
Transition* transition = filter.empty() ? &m_DefaultTransition : GetFilteredTransition(filter);
transition->Jingle = std::move(jingle);
transition->JingleDelay = delay;
}

Transition* TransitionInfo::GetFilteredTransition(std::string filter)
Transition* TransitionInfo::GetFilteredTransition(const std::string& filter)
{
if (!m_FilteredTransitions.contains(filter))
{
Expand Down
4 changes: 2 additions & 2 deletions src/NH/Bass/TransitionInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ namespace NH::Bass

[[nodiscard]] const Transition& GetTransition(const std::string& targetTheme) const;
[[nodiscard]] const Transition& GetDefaultTransition() const;
[[nodiscard]] std::string GetSourceTheme() const { return m_SourceTheme; }
[[nodiscard]] const std::string& GetSourceTheme() const { return m_SourceTheme; }

private:
Transition* GetFilteredTransition(std::string filter);
Transition* GetFilteredTransition(const std::string& filter);
};

}

0 comments on commit 3bcc535

Please sign in to comment.