From 71de630722ea90fc0eed436d4a541a9513011d72 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Sun, 10 Nov 2019 10:50:49 +0100
Subject: [PATCH] ENH: tune efficiency of stringOps::trim

- move left/right positions prior to substr
---
 applications/test/string/Test-string.C        |  2 +-
 .../primitives/strings/stringOps/stringOps.C  | 45 ++++++++++++-------
 .../primitives/strings/stringOps/stringOps.H  | 14 +++---
 3 files changed, 37 insertions(+), 24 deletions(-)

diff --git a/applications/test/string/Test-string.C b/applications/test/string/Test-string.C
index 8fd98245d95..9b8e1688eb0 100644
--- a/applications/test/string/Test-string.C
+++ b/applications/test/string/Test-string.C
@@ -70,7 +70,7 @@ int main(int argc, char *argv[])
         " $(DONOTSUBST) some other <${USER}> with '${__UNKNOWN:-some default}'"
         " value "
         " or with '${HOME:+Home was set}' via :+ alternative"
-        " or with '${__UNKNOWN:+unknown}' empty"
+        " or with '${__UNKNOWN:+unknown}' empty  "
     );
 
     setEnv("FOAM_CASE", cwd(), true);
diff --git a/src/OpenFOAM/primitives/strings/stringOps/stringOps.C b/src/OpenFOAM/primitives/strings/stringOps/stringOps.C
index 21479555c32..1515c5191d9 100644
--- a/src/OpenFOAM/primitives/strings/stringOps/stringOps.C
+++ b/src/OpenFOAM/primitives/strings/stringOps/stringOps.C
@@ -742,12 +742,12 @@ std::string::size_type Foam::stringOps::count(const char* str, const char c)
 
 Foam::string Foam::stringOps::expand
 (
-    const std::string& original,
+    const std::string& str,
     const HashTable<string, word, string::hash>& mapping,
     const char sigil
 )
 {
-    string s(original);
+    string s(str);
     inplaceExpand(s, mapping);
     return s;
 }
@@ -877,12 +877,12 @@ void Foam::stringOps::inplaceExpand
 
 Foam::string Foam::stringOps::expand
 (
-    const std::string& original,
+    const std::string& str,
     const dictionary& dict,
     const char sigil
 )
 {
-    string s(original);
+    string s(str);
     inplaceExpand(s, dict, sigil);
     return s;
 }
@@ -917,11 +917,11 @@ void Foam::stringOps::inplaceExpand
 
 Foam::string Foam::stringOps::expand
 (
-    const std::string& original,
+    const std::string& str,
     const bool allowEmpty
 )
 {
-    string s(original);
+    string s(str);
     inplaceExpand(s, allowEmpty);
     return s;
 }
@@ -1036,11 +1036,24 @@ void Foam::stringOps::inplaceTrimRight(std::string& s)
 }
 
 
-Foam::string Foam::stringOps::trim(const std::string& original)
+Foam::string Foam::stringOps::trim(const std::string& str)
 {
-    string s(original);
-    inplaceTrim(s);
-    return s;
+    std::string::size_type beg = 0;
+    std::string::size_type end = str.size();
+
+    // Right
+    while (beg < end && std::isspace(str[end-1]))
+    {
+        --end;
+    }
+
+    // Left
+    while (beg < end && std::isspace(str[beg]))
+    {
+        ++beg;
+    }
+
+    return str.substr(beg, end-beg);
 }
 
 
@@ -1051,9 +1064,9 @@ void Foam::stringOps::inplaceTrim(std::string& s)
 }
 
 
-Foam::string Foam::stringOps::removeComments(const std::string& original)
+Foam::string Foam::stringOps::removeComments(const std::string& str)
 {
-    string s(original);
+    string s(str);
     inplaceRemoveComments(s);
     return s;
 }
@@ -1135,9 +1148,9 @@ void Foam::stringOps::inplaceRemoveComments(std::string& s)
 }
 
 
-Foam::string Foam::stringOps::lower(const std::string& original)
+Foam::string Foam::stringOps::lower(const std::string& str)
 {
-    string s(original);
+    string s(str);
     inplaceLower(s);
     return s;
 }
@@ -1155,9 +1168,9 @@ void Foam::stringOps::inplaceLower(std::string& s)
 }
 
 
-Foam::string Foam::stringOps::upper(const std::string& original)
+Foam::string Foam::stringOps::upper(const std::string& str)
 {
-    string s(original);
+    string s(str);
     inplaceUpper(s);
     return s;
 }
diff --git a/src/OpenFOAM/primitives/strings/stringOps/stringOps.H b/src/OpenFOAM/primitives/strings/stringOps/stringOps.H
index a357f8a169e..11167b02f0b 100644
--- a/src/OpenFOAM/primitives/strings/stringOps/stringOps.H
+++ b/src/OpenFOAM/primitives/strings/stringOps/stringOps.H
@@ -83,7 +83,7 @@ namespace stringOps
     //  \sa stringOps::inplaceExpand() for details
     string expand
     (
-        const std::string& original,
+        const std::string& str,
         const HashTable<string, word, string::hash>& mapping,
         const char sigil = '$'
     );
@@ -206,7 +206,7 @@ namespace stringOps
     //  \sa stringOps::inplaceExpand(std::string&, const dictionary&, char)
     string expand
     (
-        const std::string& original,
+        const std::string& str,
         const dictionary& dict,
         const char sigil = '$'
     );
@@ -232,7 +232,7 @@ namespace stringOps
     //  stringOps::inplaceExpand(std::string&, bool);
     string expand
     (
-        const std::string& original,
+        const std::string& str,
         const bool allowEmpty = false
     );
 
@@ -271,27 +271,27 @@ namespace stringOps
     void inplaceTrimRight(std::string& s);
 
     //- Return string trimmed of leading and trailing whitespace
-    string trim(const std::string& original);
+    string trim(const std::string& str);
 
     //- Trim leading and trailing whitespace inplace
     void inplaceTrim(std::string& s);
 
 
     //- Return string with C/C++ comments removed
-    string removeComments(const std::string& original);
+    string removeComments(const std::string& str);
 
     //- Remove C/C++ comments inplace
     void inplaceRemoveComments(std::string& s);
 
 
     //- Return string transformed with std::tolower on each character
-    string lower(const std::string& original);
+    string lower(const std::string& str);
 
     //- Inplace transform string with std::tolower on each character
     void inplaceLower(std::string& s);
 
     //- Return string transformed with std::toupper on each character
-    string upper(const std::string& original);
+    string upper(const std::string& str);
 
     //- Inplace transform string with std::toupper on each character
     void inplaceUpper(std::string& s);
-- 
GitLab