diff --git a/src/OpenFOAM/primitives/strings/word/word.C b/src/OpenFOAM/primitives/strings/word/word.C index 2f418d721d2540ebb798ad0d2033bf412f294b8c..3d829ae61e73ae46e41f02feaea182aa94b6592a 100644 --- a/src/OpenFOAM/primitives/strings/word/word.C +++ b/src/OpenFOAM/primitives/strings/word/word.C @@ -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++] = '_'; } diff --git a/src/OpenFOAM/primitives/strings/word/word.H b/src/OpenFOAM/primitives/strings/word/word.H index ebff4c523509b848e6306c4ce87ee8f0968fcfc5..0661255341cc1b4ba11e235935c75aa12eb97889 100644 --- a/src/OpenFOAM/primitives/strings/word/word.H +++ b/src/OpenFOAM/primitives/strings/word/word.H @@ -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