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
parent
2fb9aa2a60
commit
198ff70bce
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -355,6 +355,11 @@ public:
|
|||
no_commutativity = true;
|
||||
}
|
||||
|
||||
/* Equivalent of MATLAB/Octave’s 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).
|
||||
|
|
|
@ -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 ®ime : 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)
|
||||
{
|
||||
|
|
|
@ -932,8 +932,6 @@ public:
|
|||
void set_pac_target_info_component_kind(PacTargetKind kind);
|
||||
// Add a resid statement
|
||||
void resid();
|
||||
// Equivalent of MATLAB’s 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
|
||||
|
|
Loading…
Reference in New Issue