Skip to content

Commit

Permalink
core: fix recursive variables in RHS of variable assignment
Browse files Browse the repository at this point in the history
fixes #33
  • Loading branch information
vaxerski committed Mar 20, 2024
1 parent a685493 commit cef1b47
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,14 +519,14 @@ CParseResult CConfig::parseLine(std::string line, bool dynamic) {
return result;
}

if (*LHS.begin() == '$')
return parseVariable(LHS, RHS, dynamic);
const bool ISVARIABLE = *LHS.begin() == '$';

// limit unwrapping iterations to 100. if exceeds, raise error
for (size_t i = 0; i < 100; ++i) {
bool anyMatch = false;
for (auto& var : impl->variables) {
const auto LHSIT = LHS.find("$" + var.name);
// don't parse LHS variables if this is a variable...
const auto LHSIT = ISVARIABLE ? std::string::npos : LHS.find("$" + var.name);
const auto RHSIT = RHS.find("$" + var.name);

if (LHSIT != std::string::npos)
Expand All @@ -551,6 +551,9 @@ CParseResult CConfig::parseLine(std::string line, bool dynamic) {
}
}

if (*LHS.begin() == '$')
return parseVariable(LHS, RHS, dynamic);

bool found = false;
for (auto& h : impl->handlers) {
if (!h.options.allowFlags && h.name != LHS)
Expand Down
4 changes: 4 additions & 0 deletions tests/config/config.conf
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ specialAnonymous {

testCategory:testValueHex = 0xFFfFaAbB

$RECURSIVE1 = a
$RECURSIVE2 = $RECURSIVE1b
testStringRecursive = $RECURSIVE2c

testStringQuotes = "Hello World!"
#testDefault = 123

Expand Down
5 changes: 5 additions & 0 deletions tests/parse/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ int main(int argc, char** argv, char** envp) {
config.addConfigValue("testEnv", "");
config.addConfigValue("testVar", (Hyprlang::INT)0);
config.addConfigValue("testStringQuotes", "");
config.addConfigValue("testStringRecursive", "");
config.addConfigValue("testCategory:testValueInt", (Hyprlang::INT)0);
config.addConfigValue("testCategory:testValueHex", (Hyprlang::INT)0xA);
config.addConfigValue("testCategory:nested1:testValueNest", (Hyprlang::INT)0);
Expand Down Expand Up @@ -178,12 +179,16 @@ int main(int argc, char** argv, char** envp) {
// test variables
std::cout << " → Testing variables\n";
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testVar")), 13371337);
EXPECT(std::any_cast<const char*>(config.getConfigValue("testStringRecursive")), std::string{"abc"});

// test dynamic variables
std::cout << " → Testing dynamic variables\n";
EXPECT(config.parseDynamic("$MY_VAR_2 = 420").error, false);
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testVar")), 1337420);

EXPECT(config.parseDynamic("$RECURSIVE1 = d").error, false);
EXPECT(std::any_cast<const char*>(config.getConfigValue("testStringRecursive")), std::string{"dbc"});

// test env variables
std::cout << " → Testing env variables\n";
EXPECT(std::any_cast<const char*>(config.getConfigValue("testEnv")), std::string{getenv("SHELL")});
Expand Down

0 comments on commit cef1b47

Please sign in to comment.