diff --git a/applications/test/string/Test-string.C b/applications/test/string/Test-string.C index cb6333b3d4dbd07b9396339e81b6251836945d65..208e1c297f615aa3a4632d3c67072a67c5935836 100644 --- a/applications/test/string/Test-string.C +++ b/applications/test/string/Test-string.C @@ -69,6 +69,16 @@ int main(int argc, char *argv[]) Info<<"trimRight: " << stringOps::trimRight(test) << endl; Info<<"trim: " << stringOps::trim(test) << endl; + { + fileName test1("libFooBar.so"); + + Info<< nl; + Info<< "trim filename: " << test1 << nl; + + test1.removeStart("lib"); + Info<<"without leading 'lib': " << test1 << nl; + } + Info<< nl; Info<<"camel-case => " << (word("camel") & "case") << nl; for (const auto& s : { " text with \"spaces'", "08/15 value" }) diff --git a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCode.C b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCode.C index 291d1d05f55a7937cd32c7ceca788de135788ab9..2a96a9dafb0508d01111df5f76743139fabab91d 100644 --- a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCode.C +++ b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCode.C @@ -93,12 +93,11 @@ void Foam::dynamicCode::checkSecurity Foam::word Foam::dynamicCode::libraryBaseName(const fileName& libPath) { word libName(libPath.name(true)); - libName.erase(0, 3); // Remove leading 'lib' from name + libName.removeStart("lib"); // Remove leading 'lib' from name return libName; } - // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * // void Foam::dynamicCode::copyAndFilter diff --git a/src/OpenFOAM/primitives/strings/string/string.C b/src/OpenFOAM/primitives/strings/string/string.C index 3b0d6acfbf6eb1f157b72f1d491bbb945093f4d4..5f03edeba1c97c0168e8cb149e4b31d3c9582906 100644 --- a/src/OpenFOAM/primitives/strings/string/string.C +++ b/src/OpenFOAM/primitives/strings/string/string.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -167,6 +167,48 @@ Foam::string Foam::string::removeTrailing(const char character) const } +bool Foam::string::removeStart(const std::string& text) +{ + const size_type txtLen = text.size(); + if (!txtLen) + { + return true; + } + + const size_type strLen = this->size(); + if (strLen >= txtLen && !compare(0, txtLen, text)) + { + this->erase(0, txtLen); + return true; + } + else + { + return false; + } +} + + +bool Foam::string::removeEnd(const std::string& text) +{ + const size_type txtLen = text.size(); + if (!txtLen) + { + return true; + } + + const size_type strLen = this->size(); + if (strLen >= txtLen && !compare(strLen - txtLen, npos, text)) + { + this->resize(strLen - txtLen); + return true; + } + else + { + return false; + } +} + + bool Foam::string::startsWith(const std::string& text) const { const size_type strLen = this->size(); diff --git a/src/OpenFOAM/primitives/strings/string/string.H b/src/OpenFOAM/primitives/strings/string/string.H index 0b05a868b3d3e3f9766512b0a66a509094dbe889..7010427059effe434f7dd4f30494ef5c286c4915 100644 --- a/src/OpenFOAM/primitives/strings/string/string.H +++ b/src/OpenFOAM/primitives/strings/string/string.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 | Copyright (C) 2016 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -201,6 +201,14 @@ public: //- Return string with trailing character removed string removeTrailing(const char character) const; + //- Remove the given text from the start of the string. + // Always true if the removal occurred or the given text is empty. + bool removeStart(const std::string& text); + + //- Remove the given text from the end of the string. + // Always true if the removal occurred or the given text is empty. + bool removeEnd(const std::string& text); + //- True if the string starts with the given text. // Always true if the given text is empty or if the string // is identical to the given text.