parametersystem: parseKey/transformKey can now be implementation details

This commit is contained in:
Arne Morten Kvarving 2024-09-02 14:02:15 +02:00
parent 427b619ca5
commit 7584f588a7
2 changed files with 46 additions and 51 deletions

View File

@ -34,6 +34,18 @@
namespace {
std::string parseKey(std::string& s)
{
unsigned i;
for (i = 0; i < s.size(); ++ i)
if (std::isspace(s[i]) || s[i] == '=')
break;
std::string ret = s.substr(0, i);
s = s.substr(i);
return ret;
}
std::string parseQuotedValue(std::string& s, const std::string& errorPrefix)
{
if (s.empty() || s[0] != '"')
@ -92,6 +104,40 @@ void removeLeadingSpace(std::string& s)
s = s.substr(i);
}
std::string transformKey(const std::string& s,
bool capitalizeFirstLetter,
const std::string& errorPrefix = "")
{
std::string result;
if (s.empty())
throw std::runtime_error(errorPrefix+"Empty parameter names are invalid");
if (!std::isalpha(s[0]))
throw std::runtime_error(errorPrefix+"Parameter name '" + s + "' is invalid: First character must be a letter");
if (capitalizeFirstLetter)
result += static_cast<char>(std::toupper(s[0]));
else
result += s[0];
for (unsigned i = 1; i < s.size(); ++i) {
if (s[i] == '-') {
++ i;
if (s.size() <= i || !std::isalpha(s[i]))
throw std::runtime_error(errorPrefix+"Invalid parameter name '" + s + "'");
result += static_cast<char>(std::toupper(s[i]));
}
else if (!std::isalnum(s[i]))
throw std::runtime_error(errorPrefix+"Invalid parameter name '" + s + "'");
else
result += s[i];
}
return result;
}
} // anonymous namespace
namespace Opm::Parameters {
@ -593,49 +639,4 @@ int getTtyWidth()
return ttyWidth;
}
std::string parseKey(std::string& s)
{
unsigned i;
for (i = 0; i < s.size(); ++ i)
if (std::isspace(s[i]) || s[i] == '=')
break;
std::string ret = s.substr(0, i);
s = s.substr(i);
return ret;
}
std::string transformKey(const std::string& s,
bool capitalizeFirstLetter,
const std::string& errorPrefix)
{
std::string result;
if (s.empty())
throw std::runtime_error(errorPrefix+"Empty parameter names are invalid");
if (!std::isalpha(s[0]))
throw std::runtime_error(errorPrefix+"Parameter name '" + s + "' is invalid: First character must be a letter");
if (capitalizeFirstLetter)
result += static_cast<char>(std::toupper(s[0]));
else
result += s[0];
for (unsigned i = 1; i < s.size(); ++i) {
if (s[i] == '-') {
++ i;
if (s.size() <= i || !std::isalpha(s[i]))
throw std::runtime_error(errorPrefix+"Invalid parameter name '" + s + "'");
result += static_cast<char>(std::toupper(s[i]));
}
else if (!std::isalnum(s[i]))
throw std::runtime_error(errorPrefix+"Invalid parameter name '" + s + "'");
else
result += s[i];
}
return result;
}
} // namespace Opm::Parameters

View File

@ -215,12 +215,6 @@ std::string breakLines(const std::string& msg,
int getTtyWidth();
std::string parseKey(std::string& s);
std::string transformKey(const std::string& s,
bool capitalizeFirstLetter,
const std::string& errorPrefix = "");
void getFlattenedKeyList(std::list<std::string>& dest,
const Dune::ParameterTree& tree,
const std::string& prefix = "");