From 2b1436066221bf8a599e1b7e2695c378ef01f066 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Sun, 18 Dec 2016 23:21:51 +0100 Subject: [PATCH] ENH: additional startsWith(), endsWith() string methods - As the names describe, check if the string starts or ends with a particular value. Always true if the given text is empty or if the string is identical to the given text. --- applications/test/string/Test-string.C | 72 +++++++++++++++++++ .../primitives/strings/string/string.C | 29 +++++++- .../primitives/strings/string/string.H | 52 +++++++++----- 3 files changed, 132 insertions(+), 21 deletions(-) diff --git a/applications/test/string/Test-string.C b/applications/test/string/Test-string.C index a8276c2926..ec7defadd3 100644 --- a/applications/test/string/Test-string.C +++ b/applications/test/string/Test-string.C @@ -34,6 +34,8 @@ Description #include "int.H" #include "uint.H" #include "scalar.H" +#include "Switch.H" +#include "stringList.H" using namespace Foam; @@ -166,6 +168,76 @@ int main(int argc, char *argv[]) << Foam::name("formatted >%08d<", val) << "\n"; } + // test startsWith, endsWith methods + { + string empty; //; + string input1 = "shorter input"; + string input2 = "longer input text"; + + stringList checks{"match", "long", "", "short", "text", "s", "l", "t"}; + + Info<< nl; + Info<< "check startsWith:" << nl + << "~~~~~~~~~~~~~~~~~" << nl; + + Info<<"input: " << empty << nl; + for (const string& test : checks) + { + Info<< " startsWith(" << test << ") = " + << Switch(empty.startsWith(test)) << nl; + } + Info<<"input: " << input1 << nl; + for (const string& test : checks) + { + Info<< " startsWith(" << test << ") = " + << Switch(input1.startsWith(test)) << nl; + } + Info<<"input: " << input2 << nl; + for (const string& test : checks) + { + Info<< " startsWith(" << test << ") = " + << Switch(input2.startsWith(test)) << nl; + } + + + Info<< nl; + Info<< "check endsWith:" << nl + << "~~~~~~~~~~~~~~~~~" << nl; + + Info<<"input: " << empty << nl; + for (const string& test : checks) + { + Info<< " endsWith(" << test << ") = " + << Switch(empty.endsWith(test)) << nl; + } + Info<<"input: " << input1 << nl; + for (const string& test : checks) + { + Info<< " endsWith(" << test << ") = " + << Switch(input1.endsWith(test)) << nl; + } + Info<<"input: " << input2 << nl; + for (const string& test : checks) + { + Info<< " endsWith(" << test << ") = " + << Switch(input2.endsWith(test)) << nl; + } + + Info<< nl; + Info<< "check endsWith as applied to field names:" << nl + << "~~~~~~~~~~~~~~~~~" << nl; + + string input3 = "field_0"; + string input4 = "_0"; + + Info<size(); + const size_type txtLen = text.size(); + + return + ( + !txtLen + || (strLen >= txtLen && !compare(0, txtLen, text)) + ); +} + + +bool Foam::string::endsWith(const std::string& text) const +{ + const size_type strLen = this->size(); + const size_type txtLen = text.size(); + + return + ( + !txtLen + || (strLen >= txtLen && !compare(strLen - txtLen, npos, text)) + ); +} + + // ************************************************************************* // diff --git a/src/OpenFOAM/primitives/strings/string/string.H b/src/OpenFOAM/primitives/strings/string/string.H index 67d6b2b688..0b05a868b3 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 | + \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -104,49 +104,53 @@ public: inline string(); //- Construct from std::string - inline string(const std::string&); + inline string(const std::string& str); //- Construct as copy of character array - inline string(const char*); + inline string(const char* str); //- Construct as copy of specified number of characters - inline string(const char*, const size_type); + inline string(const char* str, const size_type len); //- Construct from a single character - inline string(const char); + inline string(const char c); //- Construct from Istream - string(Istream&); + string(Istream& is); // Member Functions //- Count and return the number of a given character in the string - size_type count(const char) const; + size_type count(const char c) const; //- Is this string type valid? template - static inline bool valid(const string&); + static inline bool valid(const string& str); //- Does this string have particular meta-characters? // The meta characters can be optionally quoted. template - static inline bool meta(const string&, const char quote='\\'); + static inline bool meta(const string& str, const char quote = '\\'); //- Strip invalid characters from the given string template - static inline bool stripInvalid(string&); + static inline bool stripInvalid(string& str); //- Return a valid String from the given string template - static inline String validate(const string&); + static inline String validate(const string& str); //- Return a String with quoted meta-characters from the given string template - static inline string quotemeta(const string&, const char quote='\\'); + static inline string quotemeta + ( + const string& str, + const char quote = '\\' + ); //- True when strings match literally - inline bool match(const std::string&) const; + inline bool match(const std::string& str) const; //- Avoid masking the normal std::string replace using std::string::replace; @@ -186,16 +190,26 @@ public: string& expand(const bool allowEmpty = false); //- Remove repeated characters returning true if string changed - bool removeRepeated(const char); + bool removeRepeated(const char character); //- Return string with repeated characters removed - string removeRepeated(const char) const; + string removeRepeated(const char character) const; //- Remove trailing character returning true if string changed - bool removeTrailing(const char); + bool removeTrailing(const char character); //- Return string with trailing character removed - string removeTrailing(const char) const; + string removeTrailing(const char character) const; + + //- 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. + bool startsWith(const std::string& text) const; + + //- True if the string ends with the given text. + // Always true if the given text is empty or if the string + // is identical to the given text. + bool endsWith(const std::string& text) const; // Member Operators @@ -216,8 +230,8 @@ public: // IOstream Operators - friend Istream& operator>>(Istream&, string&); - friend Ostream& operator<<(Ostream&, const string&); + friend Istream& operator>>(Istream& is, string& s); + friend Ostream& operator<<(Ostream& os, const string& s); };