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

update logic of REST_StringHelper::isAExpression() #188

Closed
wants to merge 16 commits into from
Closed
Changes from 2 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
56 changes: 44 additions & 12 deletions source/framework/tools/src/TRestStringHelper.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,56 @@ using namespace std;
/// \brief Returns 1 only if valid mathematical expression keywords (or numbers)
/// are found in the string **in**. If not it returns 0.
///
/// By logic, mathematical expressions must have: +-*/e^% in the middle, or % in the end, or math functions in the beginning.
/// despite those symbols, the string should be purely numeric.
/// example:
/// 1+1 : expression
/// sin(1.5) : expression
/// 123456789 : not expression, It is a pure number that can be directly parsed.
Int_t REST_StringHelper::isAExpression(string in) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

REST_StringHelper::isAExpression(const string& in)

string temp = in;
vector<string> replace{"sqrt", "log", "exp", "gaus", "cos", "sin", "tan", "atan", "acos", "asin"};
for (int i = 0; i < replace.size(); i++) {
temp = Replace(temp, replace[i], "0", 0);
bool symbol = false;
bool numeric = false;

if (in.length() < 2) // minimum expression: 3%
return 0;

vector<string> funcs{"sqrt", "log", "exp", "gaus", "cos", "sin", "tan", "atan", "acos", "asin"};
for (int i = 0; i < funcs.size(); i++) {
Copy link
Member

@lobis lobis Apr 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for (const auto& item: funcs){
        if (in.find(item) != std::string::npos) {
            symbol = true;
            break;
        }
    }

if (in.find(funcs[i]) != std::string::npos) {
symbol = true;
break;
}
}

if (temp.length() == 0)
return 0;
else if (temp.length() == 1) {
if (temp.find_first_not_of("0123456789") == std::string::npos) {
return 1;
} else {
return 0;
if (!symbol) {
int pos = in.find_first_of("+-*/e^%");
if (pos > 0 && pos < in.size() - 1) {
symbol = true;
}
}

if (!symbol) {
int pos = in.find_first_of("%");
if (pos == in.size() - 1) {
symbol = true;
}
}

if (symbol) {
string temp = in;
for (int i = 0; i < funcs.size(); i++) {
temp = Replace(temp, funcs[i], "0", 0);
}
} else {
if (temp.find_first_not_of("-0123456789e+*/.,)( ^%") == std::string::npos) {
if (temp.find("/") == 0 || temp.find("./") == 0 || temp.find("../") == 0)
return 0; // identify path
return 1;
}
}
else
{
return 0;
}

return 0;
}
Expand Down Expand Up @@ -507,6 +535,10 @@ Bool_t REST_StringHelper::StringToBool(std::string in) {
}

Long64_t REST_StringHelper::StringToLong(std::string in) {
if (in.find_first_of("eE") != string::npos) {
// in case for scientific numbers
return (Long64_t)StringToDouble(in);
}
stringstream strIn;
strIn << in;
long long llNum;
Expand Down