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
No related branches found
No related tags found
1 merge request!122Feature post release cleaning
...@@ -35,10 +35,10 @@ const Foam::word Foam::word::null; ...@@ -35,10 +35,10 @@ const Foam::word Foam::word::null;
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // // * * * * * * * * * * * * * 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; std::string::size_type count = 0;
bool prefix = false; bool extra = false;
// Count number of valid characters and detect if the first character // 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 // 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) ...@@ -49,10 +49,10 @@ Foam::word Foam::word::validated(const std::string& s)
if (word::valid(c)) if (word::valid(c))
{ {
if (!count && isdigit(c)) if (prefix && !count && isdigit(c))
{ {
// First valid character was a digit - prefix with '_' // First valid character was a digit - prefix with '_'
prefix = true; extra = true;
++count; ++count;
} }
...@@ -60,7 +60,7 @@ Foam::word Foam::word::validated(const std::string& s) ...@@ -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 return word(s, false); // Already checked, can just return as word
} }
...@@ -70,7 +70,7 @@ Foam::word Foam::word::validated(const std::string& s) ...@@ -70,7 +70,7 @@ Foam::word Foam::word::validated(const std::string& s)
count = 0; count = 0;
// Copy valid content. // Copy valid content.
if (prefix) if (extra)
{ {
out[count++] = '_'; out[count++] = '_';
} }
......
...@@ -113,8 +113,9 @@ public: ...@@ -113,8 +113,9 @@ public:
inline static bool valid(char c); inline static bool valid(char c);
//- Construct a validated word, in which all invalid characters have //- Construct a validated word, in which all invalid characters have
// been stripped out and any leading digit is '_'-prefixed. // been stripped out. Normally also prefix any leading digit
static word validated(const std::string& s); // with '_' to have words that work nicely as dictionary keywords.
static word validated(const std::string& s, const bool prefix=true);
// File-like functions // File-like functions
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment