From 677e314279f95a3acbcb8a5a9734e3d83481b51e Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Thu, 12 Dec 2019 18:49:31 +0100 Subject: [PATCH] string trim --- applications/test/tmp/Test-tmp.C | 39 ++++++++++++++----- src/OSspecific/POSIX/regExp/regExpPosix.H | 22 +++++++++-- src/OSspecific/POSIX/regExp/regExpPosixI.H | 1 - .../primitives/strings/regex/regExpCxx.H | 7 +++- .../primitives/strings/stringOps/stringOps.H | 11 ++++++ 5 files changed, 64 insertions(+), 16 deletions(-) diff --git a/applications/test/tmp/Test-tmp.C b/applications/test/tmp/Test-tmp.C index db7de00cbc..88b081630a 100644 --- a/applications/test/tmp/Test-tmp.C +++ b/applications/test/tmp/Test-tmp.C @@ -2,13 +2,10 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\/ M anipulation | -------------------------------------------------------------------------------- \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2011 OpenFOAM Foundation - Copyright (C) 2011-2019 OpenCFD Ltd. + Copyright (C) 2018-2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -73,7 +70,7 @@ int main() f1 = f2 + f3 + f2 + f3; } - Info<<"f1 = " << f1 << nl; + Info<< "f1 = " << f1 << nl; } { @@ -83,7 +80,7 @@ int main() if (tfld1.valid()) { - Info<<"tmp: " << tfld1() << nl; + Info<< "tmp: " << tfld1() << nl; } // Hold on to the old content for a bit @@ -94,18 +91,42 @@ int main() printInfo(tfld2); if (tfld2.valid()) { - Info<<"tmp: " << tfld2() << nl; + Info<< "tmp: " << tfld2() << nl; } tfld2.clear(); - Info<<"After clear : "; + Info<< "After clear : "; printInfo(tfld2); tfld2.cref(f1); - Info<<"Reset const-ref : "; + Info<< "Reset to const-ref : "; printInfo(tfld2); + + Info<< "Clear const-ref does not affect tmp: "; + tfld2.clear(); + printInfo(tfld2); + + Info<< "Reset const-ref affects tmp: "; + tfld2.reset(); + printInfo(tfld2); + + + // Reset tfld2 from tfld1 + Info<< "Fld2 : "; + printInfo(tfld2); + + for (label i = 0; i < 2; ++i) + { + tfld2.reset + ( + tmp::NewFrom(4, Zero) + ); + + Info<< "Reset to some other tmp content : "; + printInfo(tfld2); + } } Info<< "\nEnd" << endl; diff --git a/src/OSspecific/POSIX/regExp/regExpPosix.H b/src/OSspecific/POSIX/regExp/regExpPosix.H index 4f3f85249f..09074d5fda 100644 --- a/src/OSspecific/POSIX/regExp/regExpPosix.H +++ b/src/OSspecific/POSIX/regExp/regExpPosix.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2004-2011, 2017-2019 OpenCFD Ltd. + Copyright (C) 2017-2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -49,6 +49,13 @@ SourceFiles #include #include +// Transitional feature - support std::smatch as per C++11 regex +#undef Foam_regExpPosix_cxx + +#ifdef Foam_regExpPosix_cxx +#include +#endif + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam @@ -71,8 +78,16 @@ class regExpPosix public: - //- Type for matches - typedef SubStrings results_type; + // Public Types + + #ifdef Foam_regExpPosix_cxx + //- Type for matches, as per C++11 regex + typedef std::smatch results_type; + #else + //- Type for matches, use OpenFOAM SubStrings container + typedef SubStrings results_type; + #endif + // Static Member Data @@ -190,7 +205,6 @@ public: //- Assign and compile pattern from string. // Matching is case sensitive. inline void operator=(const std::string& pattern); - }; diff --git a/src/OSspecific/POSIX/regExp/regExpPosixI.H b/src/OSspecific/POSIX/regExp/regExpPosixI.H index 3d8df849af..532d7e688c 100644 --- a/src/OSspecific/POSIX/regExp/regExpPosixI.H +++ b/src/OSspecific/POSIX/regExp/regExpPosixI.H @@ -27,7 +27,6 @@ License #include - // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // inline bool Foam::regExpPosix::meta(char c) diff --git a/src/OpenFOAM/primitives/strings/regex/regExpCxx.H b/src/OpenFOAM/primitives/strings/regex/regExpCxx.H index c7323df607..0522912347 100644 --- a/src/OpenFOAM/primitives/strings/regex/regExpCxx.H +++ b/src/OpenFOAM/primitives/strings/regex/regExpCxx.H @@ -84,8 +84,11 @@ class regExpCxx public: - //- Type for matches - typedef std::smatch results_type; + // Public Types + + //- Type for matches + typedef std::smatch results_type; + // Static Member Data diff --git a/src/OpenFOAM/primitives/strings/stringOps/stringOps.H b/src/OpenFOAM/primitives/strings/stringOps/stringOps.H index 11b0e15da3..b4a54b2b0c 100644 --- a/src/OpenFOAM/primitives/strings/stringOps/stringOps.H +++ b/src/OpenFOAM/primitives/strings/stringOps/stringOps.H @@ -48,6 +48,7 @@ SourceFiles #include "stringOpsSort.H" #include "stringOpsEvaluate.H" #include "wordRes.H" +#include // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -256,6 +257,16 @@ namespace stringOps // Return true if a replacement was successful. bool inplaceReplaceVar(std::string& s, const word& varName); + //- Find first and last non-space locations in string or sub-string + //- and return as a (pos, len) pair. + // In the future this might become a std::smatch or a std::stringview + std::pair + findTrim + ( + const std::string& s, + size_t pos = 0, + size_t len = std::string::npos + ); //- Return string trimmed of leading whitespace string trimLeft(const std::string& s);