Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #47, change Union::String::IsMatchesMask into a proper std::regex #50

Merged
merged 2 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 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 @@ -26,7 +37,7 @@ namespace NH::Bass
return m_DefaultTransition;
}

void TransitionInfo::AddTransitionEffect(TransitionEffect effect, double duration, const std::string& filter)
void TransitionInfo::AddTransitionEffect(const TransitionEffect effect, const double duration, const std::string& filter)
{
Transition* transition = filter.empty() ? &m_DefaultTransition : GetFilteredTransition(filter);
transition->Effect = effect;
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);
};

}