Skip to content

Commit

Permalink
Revert changing main parse function. Add templates for strings of arr…
Browse files Browse the repository at this point in the history
…ays or vectors
  • Loading branch information
rlatosky committed Feb 4, 2025
1 parent fb15df6 commit a8c5f35
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 18 deletions.
50 changes: 38 additions & 12 deletions src/libraries/JANA/Services/JParameterManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,16 @@ class JParameterManager : public JService {
template<typename T>
static inline void Parse(const std::string& value, std::vector<T>& out);

template<>
static inline void Parse(const std::string& value, std::vector<std::string>& out);

template<typename T, size_t arrSize>
static inline void Parse(const std::string& value, std::array<T,arrSize>& out);

template<size_t arrSize>
static inline void Parse(const std::string& value, std::array<std::string,arrSize>& out);


/*
Template specialization done for double and float numbers in order to match the
precision of the number with the string
Expand Down Expand Up @@ -352,10 +359,7 @@ void JParameterManager::Parse(const std::string& s, T& out) {
/// The stream operator is not only redundant here, but it also splits the string (see Issue #191)
template <>
inline void JParameterManager::Parse(const std::string& value, std::string& out) {
std::string formatted_val = value;
if (formatted_val.find('\\') != std::string::npos)
formatted_val.erase(formatted_val.find('\\'));
out = formatted_val;
out = value;
}


Expand All @@ -374,16 +378,25 @@ inline void JParameterManager::Parse(const std::string& value,std::array<T,N> &v
std::stringstream ss(value);
std::string temp = ""; // creating a temp var allows us to store the string s where the escape character was used
int indx = 0;
// while (getline(ss, s, ',')) {
// T t;
// Parse(s, t);
// val[indx++]= t;
// }
while (getline(ss, s, ',')) {
T t;
Parse(s, t);
val[indx++]= t;
}
}

// @brief Template to parse a string and return in an array of strings
template<size_t N>
inline void JParameterManager::Parse(const std::string& value,std::array<std::string,N> &val) {
std::string s;
std::stringstream ss(value);
std::string temp = ""; // creating a temp var allows us to store the string s where the escape character was used
int indx = 0;
while (getline(ss, s, ',')) {
std::string t;
if (s.find('\\') != std::string::npos) {
s.erase(s.find('\\'));
temp = s + ',';
temp += s + ',';
continue;
}
if (!temp.empty()) {
Expand All @@ -404,13 +417,26 @@ inline void JParameterManager::Parse(const std::string& value, std::vector<T> &v
std::stringstream ss(value);
std::string s;
val.clear(); // clearing the input vector to ensure no dulication which can be caused due to val.push_back(t);
// we find an escape character
std::string temp = ""; // creating a temp var allows us to store the string s where the escape character was used
while (getline(ss, s, ',')) {
T t;
Parse(s, t);
val.push_back(t);
}
}

/// @brief Specialization for std::vector<std::string> with escape commas
template<>
inline void JParameterManager::Parse(const std::string& value, std::vector<std::string> &val) {
std::stringstream ss(value);
std::string s;
val.clear(); // clearing the input vector to ensure no dulication which can be caused due to val.push_back(t);
std::string temp = ""; // creating a temp var allows us to store the string s where the escape character was used
while (getline(ss, s, ',')) {
std::string t;
if (s.find('\\') != std::string::npos) {
s.erase(s.find('\\'));
temp = s + ',';
temp += s + ',';
continue;
}
if (!temp.empty()) {
Expand Down
11 changes: 5 additions & 6 deletions src/programs/unit_tests/Services/JParameterManagerTests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@ TEST_CASE("JParameterManager_ArrayParams") {
REQUIRE(vals[1] == "whitespace in middle");
REQUIRE(vals[2] == " also with whitespace padding ");
}

SECTION("Writing a array of strings") {
std::array<std::string,3> inputs = {"first", "second one" , " third one "};
jpm.SetDefaultParameter("test", inputs);
Expand Down Expand Up @@ -346,28 +345,28 @@ TEST_CASE("JParameterManager_ArrayParams") {
}
SECTION("Reading a array of functions with commas") {
// As of Mon Jan 27, JParameterManager does not allow an escape key to prevent splitting on the next comma)
jpm.SetParameter("test", "phi-fmod(phi\\,5),theta-fmod(theta\\,10),omega-fmod(omega\\,15)"); // Issue #380 (Feature request)
jpm.SetParameter("test", "theta-fmod(phi-fmod(phi\\,5)\\,7),theta-fmod(theta\\,10),omega-fmod(omega\\,15)"); // Issue #380 (Feature request)
std::array<std::string, 3> vals;
jpm.GetParameter("test", vals);

REQUIRE(vals[0] == "phi-fmod(phi,5)");
REQUIRE(vals[0] == "theta-fmod(phi-fmod(phi,5),7)");
REQUIRE(vals[1] == "theta-fmod(theta,10)");
REQUIRE(vals[2] == "omega-fmod(omega,15)");
}
SECTION("Writing a array of functions with commas") {
std::array<std::string, 3> inputs = {
"phi-fmod(phi\\,5)",
"theta-fmod(phi-fmod(phi\\,5)\\,7)",
"theta-fmod(theta\\,10)",
"omega-fmod(omega\\,15)"
};

jpm.SetDefaultParameter("test", inputs);
std::array<float,3> outputs;
auto param = jpm.GetParameter("test", outputs);
REQUIRE(param->GetValue() == "phi-fmod(phi\\,5),theta-fmod(theta\\,10),omega-fmod(omega\\,15)");
REQUIRE(param->GetValue() == "theta-fmod(phi-fmod(phi\\,5)\\,7),theta-fmod(theta\\,10),omega-fmod(omega\\,15)");

std::array<float,3> temp;
jpm.Parse(jpm.Stringify("phi-fmod(phi\\,5),theta-fmod(theta\\,10),omega-fmod(omega\\,15)"), temp);
jpm.Parse(jpm.Stringify("theta-fmod(phi-fmod(phi\\,5)\\,7),theta-fmod(theta\\,10),omega-fmod(omega\\,15)"), temp);
REQUIRE(temp == outputs);
}
}
Expand Down

0 comments on commit a8c5f35

Please sign in to comment.