Simplify strsplit() using std::string_view

By the way:
– improve the semantics by having a consistent treatment of empty substring
  components (previously, only the last one was ignored)
– move it to DataTree to make it more accessible from elsewhere (even though
  ideally it should be in a “utilities” namespace).
master
Sébastien Villemot 2022-10-11 14:30:53 +02:00
parent 2fb9aa2a60
commit 198ff70bce
No known key found for this signature in database
GPG Key ID: 2CECE9350ECEBE4A
4 changed files with 24 additions and 23 deletions

View File

@ -945,6 +945,23 @@ DataTree::writePowerDerivHeader(ostream &output) const
output << "double getPowerDeriv(double x, double p, int k);" << endl;
}
vector<string>
DataTree::strsplit(string_view str, char delim)
{
vector<string> result;
while (true)
{
size_t idx {str.find(delim)};
if (auto sub {str.substr(0, idx)};
!sub.empty())
result.emplace_back(sub);
if (idx == string_view::npos)
break;
str.remove_prefix(idx+1);
}
return result;
}
string
DataTree::packageDir(const string &package)
{

View File

@ -355,6 +355,11 @@ public:
no_commutativity = true;
}
/* Equivalent of MATLAB/Octaves strsplit, except that it ignores empty
substring components (MATLAB/Octave adds them to the output); in
particular, returns an empty vector given an empty string. */
static vector<string> strsplit(string_view str, char delim);
/*! Takes a MATLAB/Octave package name (possibly with several levels nested using dots),
and returns the name of the corresponding filesystem directory (which
is created by the function if it does not exist).

View File

@ -2587,8 +2587,8 @@ ParsingDriver::add_model_equal(expr_t arg1, expr_t arg2)
// If the equation has a “bind” or “relax” tag (occbin case)
if (!eq_tags.contains("name"))
error("An equation with a 'bind' or 'relax' tag must have a 'name' tag");
auto regimes_bind = strsplit(eq_tags["bind"], ',');
auto regimes_relax = strsplit(eq_tags["relax"], ',');
auto regimes_bind = DataTree::strsplit(eq_tags["bind"], ',');
auto regimes_relax = DataTree::strsplit(eq_tags["relax"], ',');
auto regimes_all = regimes_bind;
regimes_all.insert(regimes_all.end(), regimes_relax.begin(), regimes_relax.end()); // Concatenate the two vectors
for (const auto &regime : regimes_all)
@ -3732,25 +3732,6 @@ ParsingDriver::set_pac_target_info_component_kind(PacTargetKind kind)
get<3>(pac_target_info_component) = kind;
}
vector<string>
ParsingDriver::strsplit(const string &str, char delim)
{
vector<string> result;
size_t idx = 0;
while (idx < str.size())
{
size_t idx2 = str.find(delim, idx);
if (idx2 == string::npos)
{
result.push_back(str.substr(idx));
break;
}
result.push_back(str.substr(idx, idx2-idx));
idx = idx2 + 1;
}
return result;
}
bool
ParsingDriver::isSymbolIdentifier(const string &str)
{

View File

@ -932,8 +932,6 @@ public:
void set_pac_target_info_component_kind(PacTargetKind kind);
// Add a resid statement
void resid();
// Equivalent of MATLABs strsplit. Returns an empty vector given an empty string.
static vector<string> strsplit(const string &str, char delim);
// Returns true iff the string is a legal symbol identifier (see NAME token in lexer)
static bool isSymbolIdentifier(const string &str);
// Given an Occbin regime name, returns the corresponding auxiliary parameter