Skip to content

Commit

Permalink
sliders: ensure slider lookup is case insensitive
Browse files Browse the repository at this point in the history
jsfx variable names are not case sensitive. This should be considered when looking up sliders.
  • Loading branch information
JoepVanlier committed Jul 6, 2024
1 parent e439185 commit ef3f2d0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
13 changes: 11 additions & 2 deletions sources/ysfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ ysfx_t *ysfx_new(ysfx_config_t *config)

auto var_resolver = [](void *userdata, const char *name) -> EEL_F * {
ysfx_t *fx = (ysfx_t *)userdata;
auto it = fx->source.slider_alias.find(name);

/* Not very efficient */
std::string lower_name{name};
std::transform(lower_name.begin(), lower_name.end(), lower_name.begin(), [](unsigned char c){ return std::tolower(c); });

auto it = fx->source.slider_alias.find(lower_name);
if (it != fx->source.slider_alias.end())
return fx->var.slider[it->second];
return nullptr;
Expand Down Expand Up @@ -255,7 +260,11 @@ bool ysfx_load_file(ysfx_t *fx, const char *filepath, uint32_t loadopts)
for (uint32_t i = 0; i < ysfx_max_sliders; ++i) {
if (main->header.sliders[i].exists) {
if (!main->header.sliders[i].var.empty())
fx->source.slider_alias.insert({main->header.sliders[i].var, i});
{
std::string data = main->header.sliders[i].var;
std::transform(data.begin(), data.end(), data.begin(), [](unsigned char c){ return std::tolower(c); });
fx->source.slider_alias.insert({data, i});
}
}
}

Expand Down
29 changes: 29 additions & 0 deletions tests/ysfx_test_slider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,35 @@ TEST_CASE("slider manipulation", "[sliders]")
REQUIRE(ysfx_slider_get_value(fx.get(), 1) == 3);
}

SECTION("slider case insensitivity")
{
const char *text =
"desc:example" "\n"
"out_pin:output" "\n"
"slider1:fOo=1<1,3,0.1>the slider 1" "\n"
"slider2:bar=2<1,3,0.1>the slider 2" "\n"
"@init" "\n"
"foo=2;" "\n"
"bAr=3;" "\n"
"@sample" "\n"
"spl0=0.0;" "\n";

scoped_new_dir dir_fx("${root}/Effects");
scoped_new_txt file_main("${root}/Effects/example.jsfx", text);

ysfx_config_u config{ysfx_config_new()};
ysfx_u fx{ysfx_new(config.get())};

REQUIRE(ysfx_load_file(fx.get(), file_main.m_path.c_str(), 0));
REQUIRE(ysfx_compile(fx.get(), 0));

REQUIRE(ysfx_slider_get_value(fx.get(), 0) == 1);
REQUIRE(ysfx_slider_get_value(fx.get(), 1) == 2);
ysfx_init(fx.get());
REQUIRE(ysfx_slider_get_value(fx.get(), 0) == 2);
REQUIRE(ysfx_slider_get_value(fx.get(), 1) == 3);
}

SECTION("slider visibility")
{
const char *text =
Expand Down

0 comments on commit ef3f2d0

Please sign in to comment.