Skip to content
Snippets Groups Projects
Commit 58eab581 authored by Mark OLESEN's avatar Mark OLESEN
Browse files

ENH: add optional parameter to word::validated (issue #518)

- can now suppress prefixing an underscore to leading digits.
parent 32ee4947
Branches
Tags
1 merge request!122Feature post release cleaning
......@@ -35,10 +35,10 @@ const Foam::word Foam::word::null;
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
Foam::word Foam::word::validated(const std::string& s)
Foam::word Foam::word::validated(const std::string& s, const bool prefix)
{
std::string::size_type count = 0;
bool prefix = false;
bool extra = false;
// Count number of valid characters and detect if the first character
// happens to be a digit, which we'd like to avoid having since this
......@@ -49,10 +49,10 @@ Foam::word Foam::word::validated(const std::string& s)
if (word::valid(c))
{
if (!count && isdigit(c))
if (prefix && !count && isdigit(c))
{
// First valid character was a digit - prefix with '_'
prefix = true;
extra = true;
++count;
}
......@@ -60,7 +60,7 @@ Foam::word Foam::word::validated(const std::string& s)
}
}
if (count == s.size() && !prefix)
if (count == s.size() && !extra)
{
return word(s, false); // Already checked, can just return as word
}
......@@ -70,7 +70,7 @@ Foam::word Foam::word::validated(const std::string& s)
count = 0;
// Copy valid content.
if (prefix)
if (extra)
{
out[count++] = '_';
}
......
......@@ -113,8 +113,9 @@ public:
inline static bool valid(char c);
//- Construct a validated word, in which all invalid characters have
// been stripped out and any leading digit is '_'-prefixed.
static word validated(const std::string& s);
// been stripped out. Normally also prefix any leading digit
// with '_' to have words that work nicely as dictionary keywords.
static word validated(const std::string& s, const bool prefix=true);
// File-like functions
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment