diff --git a/applications/test/string/Test-string.C b/applications/test/string/Test-string.C index ec7defadd36072c0b3ab9dd462f7761016ed9743..cb6333b3d4dbd07b9396339e81b6251836945d65 100644 --- a/applications/test/string/Test-string.C +++ b/applications/test/string/Test-string.C @@ -69,6 +69,13 @@ int main(int argc, char *argv[]) Info<<"trimRight: " << stringOps::trimRight(test) << endl; Info<<"trim: " << stringOps::trim(test) << endl; + Info<< nl; + Info<<"camel-case => " << (word("camel") & "case") << nl; + for (const auto& s : { " text with \"spaces'", "08/15 value" }) + { + Info<<"validated \"" << s << "\" => " << word::validated(s) << nl; + } + Info<< nl; // test sub-strings via iterators string::const_iterator iter = test.end(); diff --git a/src/OpenFOAM/primitives/strings/word/word.C b/src/OpenFOAM/primitives/strings/word/word.C index c8a2c1f1b74e108248790779407fb256333cc843..bbd4050a338a486438ae9cb628311710c8e63e72 100644 --- a/src/OpenFOAM/primitives/strings/word/word.C +++ b/src/OpenFOAM/primitives/strings/word/word.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -32,4 +32,63 @@ const char* const Foam::word::typeName = "word"; int Foam::word::debug(Foam::debug::debugSwitch(word::typeName, 0)); const Foam::word Foam::word::null; + +// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // + +Foam::word Foam::word::validated(const std::string& s) +{ + std::string::size_type count = 0; + bool prefix = 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 + // will cause parse issues when read back later. + for (std::string::const_iterator it = s.cbegin(); it != s.cend(); ++it) + { + const char c = *it; + + if (word::valid(c)) + { + if (!count && isdigit(c)) + { + // First valid character was a digit - prefix with '_' + prefix = true; + ++count; + } + + ++count; + } + } + + if (count == s.size() && !prefix) + { + return word(s, false); // Already checked, can just return as word + } + + word out; + out.resize(count); + count = 0; + + // Copy valid content. + if (prefix) + { + out[count++] = '_'; + } + + for (std::string::const_iterator it = s.cbegin(); it != s.cend(); ++it) + { + const char c = *it; + + if (word::valid(c)) + { + out[count++] = c; + } + } + + out.resize(count); + + return out; +} + + // ************************************************************************* // diff --git a/src/OpenFOAM/primitives/strings/word/word.H b/src/OpenFOAM/primitives/strings/word/word.H index 8f29bdc943d8738eab07689e58c8e75f411e096a..1bc258bac9616738fc7a045b020c78cee85c7e3d 100644 --- a/src/OpenFOAM/primitives/strings/word/word.H +++ b/src/OpenFOAM/primitives/strings/word/word.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -48,9 +48,9 @@ namespace Foam // Forward declaration of friend functions and operators class word; -inline word operator&(const word&, const word&); -Istream& operator>>(Istream&, word&); -Ostream& operator<<(Ostream&, const word&); +inline word operator&(const word& a, const word& b); +Istream& operator>>(Istream& is, word& w); +Ostream& operator<<(Ostream& os, const word& w); /*---------------------------------------------------------------------------*\ @@ -84,55 +84,60 @@ public: inline word(); //- Construct as copy - inline word(const word&); + inline word(const word& w); //- Construct as copy of character array - inline word(const char*, const bool doStripInvalid=true); + inline word(const char* s, const bool doStripInvalid=true); //- Construct as copy with a maximum number of characters inline word ( - const char*, + const char* s, const size_type, const bool doStripInvalid ); //- Construct as copy of string - inline word(const string&, const bool doStripInvalid=true); + inline word(const string& s, const bool doStripInvalid=true); //- Construct as copy of std::string - inline word(const std::string&, const bool doStripInvalid=true); + inline word(const std::string& s, const bool doStripInvalid=true); //- Construct from Istream - word(Istream&); + word(Istream& is); // Member functions - //- Is this character valid for a word - inline static bool valid(char); + //- Is this character valid for a word? + 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); // Member operators // Assignment - inline void operator=(const word&); - inline void operator=(const string&); - inline void operator=(const std::string&); - inline void operator=(const char*); + inline void operator=(const word& w); + inline void operator=(const string& s); + inline void operator=(const std::string& s); + inline void operator=(const char* s); // Friend Operators - //- Join word a and bm capitalising first letter of b + //- Join word a and b, capitalising the first letter of b + // (so-called camelCase) friend word operator&(const word& a, const word& b); // IOstream operators - friend Istream& operator>>(Istream&, word&); - friend Ostream& operator<<(Ostream&, const word&); + friend Istream& operator>>(Istream& is, word& w); + friend Ostream& operator<<(Ostream& os, const word& w); }; diff --git a/src/OpenFOAM/primitives/strings/word/wordI.H b/src/OpenFOAM/primitives/strings/word/wordI.H index a4d15df4e78639e28364d19dbe1ddc9790e4b082..e19356f912d57214a5e6665cd9babb702f5cfd26 100644 --- a/src/OpenFOAM/primitives/strings/word/wordI.H +++ b/src/OpenFOAM/primitives/strings/word/wordI.H @@ -31,8 +31,7 @@ License inline void Foam::word::stripInvalid() { - // skip stripping unless debug is active to avoid - // costly operations + // skip stripping unless debug is active (to avoid costly operations) if (debug && string::stripInvalid<word>(*this)) { std::cerr @@ -132,29 +131,29 @@ inline bool Foam::word::valid(char c) // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // -inline void Foam::word::operator=(const word& q) +inline void Foam::word::operator=(const word& w) { - string::operator=(q); + string::operator=(w); } -inline void Foam::word::operator=(const string& q) +inline void Foam::word::operator=(const string& s) { - string::operator=(q); + string::operator=(s); stripInvalid(); } -inline void Foam::word::operator=(const std::string& q) +inline void Foam::word::operator=(const std::string& s) { - string::operator=(q); + string::operator=(s); stripInvalid(); } -inline void Foam::word::operator=(const char* q) +inline void Foam::word::operator=(const char* s) { - string::operator=(q); + string::operator=(s); stripInvalid(); } @@ -176,6 +175,5 @@ inline Foam::word Foam::operator&(const word& a, const word& b) } } -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // ************************************************************************* // diff --git a/src/conversion/ccm/reader/ccmReader.C b/src/conversion/ccm/reader/ccmReader.C index 217a1e8347f923bb8f6c5837fa51520815fd048a..66b8bd95054ebb4777b672582d6e28dffb216b87 100644 --- a/src/conversion/ccm/reader/ccmReader.C +++ b/src/conversion/ccm/reader/ccmReader.C @@ -166,77 +166,6 @@ std::string Foam::ccm::reader::ccmReadOptstr } -Foam::word Foam::ccm::reader::validateWord -( - const std::string& str -) -{ - std::string::size_type ngood = 0; - bool prefix = false; - bool first = true; - - for - ( - std::string::const_iterator iter = str.begin(); - iter != str.end(); - ++iter - ) - { - if (word::valid(*iter)) - { - ++ngood; - if (first) - { - first = false; - - // Start with a digit? need to prefix with '_' - if (isdigit(*iter)) - { - prefix = true; - ++ngood; - } - } - } - } - - if (ngood == str.size() && !prefix) - { - return str; - } - - Foam::word out; - out.resize(ngood); - ngood = 0; - - Foam::word::iterator iter2 = out.begin(); - for - ( - std::string::const_iterator iter1 = str.begin(); - iter1 != str.end(); - ++iter1 - ) - { - register char c = *iter1; - - if (Foam::word::valid(c)) - { - if (prefix) - { - prefix = false; - *(iter2++) = '_'; - ++ngood; - } - *(iter2++) = c; - ++ngood; - } - } - - out.resize(ngood); - - return out; -} - - // Read map data and check error void Foam::ccm::reader::readMap ( @@ -435,7 +364,7 @@ void Foam::ccm::reader::readProblemDescription_boundaryRegion } else { - dict.add(opt, validateWord(str)); + dict.add(opt, word::validated(str)); } } @@ -476,7 +405,7 @@ void Foam::ccm::reader::readProblemDescription_boundaryRegion if (!str.empty()) { - dict.add(opt, validateWord(str)); + dict.add(opt, word::validated(str)); } } @@ -541,7 +470,7 @@ void Foam::ccm::reader::readProblemDescription_cellTable str = "zone_" + ::Foam::name(Id); } - dict.add(opt, validateWord(str)); + dict.add(opt, word::validated(str)); } @@ -553,7 +482,7 @@ void Foam::ccm::reader::readProblemDescription_cellTable if (!str.empty()) { - dict.add(opt, validateWord(str)); + dict.add(opt, word::validated(str)); } } diff --git a/src/conversion/ccm/reader/ccmReader.H b/src/conversion/ccm/reader/ccmReader.H index aa15978dd35314388bf4de9c407fe4c41c19a710..14408d3f944bf40fbc26426c609b91cf3c415eb1 100644 --- a/src/conversion/ccm/reader/ccmReader.H +++ b/src/conversion/ccm/reader/ccmReader.H @@ -320,9 +320,6 @@ private: // return empty string on failure std::string ccmReadOptstr(const char* opt, ccmID node); - //- Strip invalid characters, prefix leading digit with '_' - static word validateWord(const std::string&); - //- Read map data and check error void readMap(const ccmID& mapId, labelList& data); diff --git a/src/conversion/fire/FIREMeshReader.C b/src/conversion/fire/FIREMeshReader.C index ef8576cf5f543665a282d97ecf9250a20cbf607c..e79d70b060d28d3025c135696e0c5aa50f82bdad 100644 --- a/src/conversion/fire/FIREMeshReader.C +++ b/src/conversion/fire/FIREMeshReader.C @@ -31,80 +31,6 @@ License // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // -Foam::word Foam::fileFormats::FIREMeshReader::validateWord -( - const std::string& str -) -{ - std::string::size_type ngood = 0; - bool prefix = false; - bool first = true; - - for - ( - std::string::const_iterator iter = str.begin(); - iter != str.end(); - ++iter - ) - { - if (word::valid(*iter)) - { - ++ngood; - if (first) - { - first = false; - - // start with a digit? need to prefix with '_' - if (isdigit(*iter)) - { - prefix = true; - } - } - } - } - - if (prefix) - { - ++ngood; - } - else if (ngood == str.size()) - { - return str; - } - - Foam::word out; - out.resize(ngood); - ngood = 0; - - Foam::word::iterator iter2 = out.begin(); - for - ( - std::string::const_iterator iter1 = str.begin(); - iter1 != str.end(); - ++iter1 - ) - { - register char c = *iter1; - - if (Foam::word::valid(c)) - { - if (prefix) - { - prefix = false; - *(iter2++) = '_'; - ++ngood; - } - *(iter2++) = c; - ++ngood; - } - } - - out.resize(ngood); - - return out; -} - - void Foam::fileFormats::FIREMeshReader::readPoints ( ISstream& is, @@ -229,7 +155,7 @@ void Foam::fileFormats::FIREMeshReader::readSelections(ISstream& is) // index starting at 1 const label selId = ++nCellSelections; - cellTable_.setName(selId, validateWord(name)); + cellTable_.setName(selId, word::validated(name)); cellTable_.setMaterial(selId, "fluid"); for (label i = 0; i < count; ++i) @@ -244,7 +170,7 @@ void Foam::fileFormats::FIREMeshReader::readSelections(ISstream& is) // index starting at 0 const label selId = nFaceSelections++; - faceNames.append(validateWord(name)); + faceNames.append(word::validated(name)); for (label i = 0; i < count; ++i) { diff --git a/src/conversion/fire/FIREMeshReader.H b/src/conversion/fire/FIREMeshReader.H index 0c95cb5fa21eb39a0110168d8d4d90e0436478ac..b3e7f0b551b460983c7f3a2f75a0e6a83b0dc9c8 100644 --- a/src/conversion/fire/FIREMeshReader.H +++ b/src/conversion/fire/FIREMeshReader.H @@ -83,10 +83,6 @@ protected: void operator=(const FIREMeshReader&) = delete; - //- Validate word (eg, avoid leading digits) - static word validateWord(const std::string&); - - //- Read the mesh from the file(s) virtual bool readGeometry(const scalar scaleFactor = 1.0); diff --git a/src/conversion/fire/checkFireEdges.C b/src/conversion/fire/checkFireEdges.C index eb7c3bf9da11bc5bf539062a256357bf83b5852b..3969d1b62a49a1095cd1319822560b63559feab8 100644 --- a/src/conversion/fire/checkFireEdges.C +++ b/src/conversion/fire/checkFireEdges.C @@ -194,7 +194,7 @@ Foam::label Foam::checkFireEdges thisEdge.flip(); } - if (&points) + if (notNull(points)) { forAll(thisEdge, keyI) { @@ -220,7 +220,7 @@ Foam::label Foam::checkFireEdges { labelList keys = strayPoints.sortedToc(); - if (&points) + if (notNull(points)) { forAll(keys, keyI) { @@ -257,10 +257,9 @@ Foam::label Foam::checkFireEdges { label nPoints = -1; - if (&points) + if (notNull(points)) { nPoints = points.size(); - } else { @@ -287,10 +286,7 @@ Foam::label Foam::checkFireEdges } -Foam::label Foam::checkFireEdges -( - const polyMesh& mesh -) +Foam::label Foam::checkFireEdges(const polyMesh& mesh) { return checkFireEdges(mesh.faces(), mesh.pointFaces(), mesh.points()); } diff --git a/src/conversion/fire/checkFireEdges.H b/src/conversion/fire/checkFireEdges.H index 0f0bffe2c884c4632707499e3000e1349abc0e57..d5586378716033759ebbe7d47f7bddb1244d1a62 100644 --- a/src/conversion/fire/checkFireEdges.H +++ b/src/conversion/fire/checkFireEdges.H @@ -50,9 +50,9 @@ class polyMesh; //- check edge connectivity label checkFireEdges ( - const faceList&, + const faceList& faces, const labelListList& pointFaces, - const UList<point>& = UList<point>::null() + const UList<point>& points = UList<point>::null() ); @@ -60,12 +60,12 @@ label checkFireEdges label checkFireEdges ( const faceList&, - const UList<point>& = UList<point>::null() + const UList<point>& points = UList<point>::null() ); //- check edge connectivity -label checkFireEdges(const polyMesh&); +label checkFireEdges(const polyMesh& mesh); // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //