ENH: add stringOps namespace with a collection of string-related ops

This commit is contained in:
Mark Olesen
2011-02-22 10:20:55 +01:00
parent 02f3459402
commit ba873744f3
8 changed files with 560 additions and 55 deletions

View File

@ -78,6 +78,7 @@ $(strings)/fileName/fileNameIO.C
$(strings)/keyType/keyType.C
$(strings)/wordRe/wordRe.C
$(strings)/lists/hashedWordList.C
$(strings)/stringOps/stringOps.C
primitives/hashes/Hasher/Hasher.C

View File

@ -31,6 +31,7 @@ License
#include "SHA1Digest.H"
#include "OSHA1stream.H"
#include "codeStreamTools.H"
#include "stringOps.H"
#include "dlLibraryTable.H"
#include "OSspecific.H"
#include "Time.H"
@ -87,19 +88,18 @@ bool Foam::functionEntries::codeStream::execute
string codeInclude = "";
if (codeDict.found("codeInclude"))
{
codeInclude = codeStreamTools::stripLeading(codeDict["codeInclude"]);
codeInclude = stringOps::trimLeft(codeDict["codeInclude"]);
}
string code = codeStreamTools::stripLeading(codeDict["code"]);
string code = stringOps::trimLeft(codeDict["code"]);
string codeOptions = "";
if (codeDict.found("codeOptions"))
{
codeOptions = codeStreamTools::stripLeading(codeDict["codeOptions"]);
codeOptions = stringOps::trimLeft(codeDict["codeOptions"]);
}
// Create name out of contents
// Create name from the contents
SHA1Digest sha;
{
OSHA1stream os;

View File

@ -84,13 +84,11 @@ SourceFiles
namespace Foam
{
class ISstream;
namespace functionEntries
{
/*---------------------------------------------------------------------------*\
Class codeStream Declaration
Class codeStream Declaration
\*---------------------------------------------------------------------------*/
class codeStream

View File

@ -211,20 +211,6 @@ bool Foam::codeStreamTools::copyFilesContents(const fileName& dir) const
}
Foam::string Foam::codeStreamTools::stripLeading(const string& s)
{
label sz = s.size();
if (sz > 0 && s[0] == '\n')
{
return s(1, sz-1);
}
else
{
return s;
}
}
bool Foam::codeStreamTools::writeDigest
(
const fileName& dir,

View File

@ -120,8 +120,6 @@ public:
static void* findLibrary(const fileName& libPath);
static string stripLeading(const string&);
static bool writeDigest(const fileName& dir, const SHA1Digest& sha1);
static SHA1Digest readDigest(const fileName& dir);
static bool upToDate(const fileName& dir, const SHA1Digest& sha1);

View File

@ -96,75 +96,79 @@ Foam::string& Foam::string::replaceAll
// Expand all occurences of environment variables and initial tilde sequences
Foam::string& Foam::string::expand(const bool recurse, const bool allowEmptyVar)
{
size_type startEnvar = 0;
size_type begVar = 0;
// Expand $VARS
// Repeat until nothing more is found
while
(
(startEnvar = find('$', startEnvar)) != npos
&& startEnvar < size()-1
(begVar = find('$', begVar)) != npos
&& begVar < size()-1
)
{
if (startEnvar == 0 || operator[](startEnvar-1) != '\\')
if (begVar == 0 || operator[](begVar-1) != '\\')
{
// Find end of first occurrence
size_type endEnvar = startEnvar;
size_type nd = 0;
size_type endVar = begVar;
size_type delim = 0;
if (operator[](startEnvar+1) == '{')
if (operator[](begVar+1) == '{')
{
endEnvar = find('}', startEnvar);
nd = 1;
endVar = find('}', begVar);
delim = 1;
}
else
{
iterator iter = begin() + startEnvar + 1;
iterator iter = begin() + begVar + 1;
while (iter != end() && (isalnum(*iter) || *iter == '_'))
while
(
iter != end()
&& (isalnum(*iter) || *iter == '_')
)
{
++iter;
++endEnvar;
++endVar;
}
}
if (endEnvar != npos && endEnvar != startEnvar)
if (endVar != npos && endVar != begVar)
{
string enVar = substr
string varName = substr
(
startEnvar + 1 + nd,
endEnvar - startEnvar - 2*nd
begVar + 1 + delim,
endVar - begVar - 2*delim
);
string enVarString = getEnv(enVar);
string varValue = getEnv(varName);
if (enVarString.size())
if (varValue.size())
{
if (recurse)
{
enVarString.expand(recurse, allowEmptyVar);
varValue.expand(recurse, allowEmptyVar);
}
std::string::replace
(
startEnvar,
endEnvar - startEnvar + 1,
enVarString
begVar,
endVar - begVar + 1,
varValue
);
startEnvar += enVarString.size();
begVar += varValue.size();
}
else if (allowEmptyVar)
{
std::string::replace
(
startEnvar,
endEnvar - startEnvar + 1,
begVar,
endVar - begVar + 1,
""
);
}
else
{
FatalErrorIn("string::expand(const bool, const bool)")
<< "Unknown variable name " << enVar << '.'
<< "Unknown variable name " << varName << '.'
<< exit(FatalError);
}
}
@ -175,7 +179,7 @@ Foam::string& Foam::string::expand(const bool recurse, const bool allowEmptyVar)
}
else
{
startEnvar++;
++begVar;
}
}
@ -191,10 +195,10 @@ Foam::string& Foam::string::expand(const bool recurse, const bool allowEmptyVar)
word user;
fileName file;
if ((startEnvar = find('/')) != npos)
if ((begVar = find('/')) != npos)
{
user = substr(1, startEnvar - 1);
file = substr(startEnvar + 1);
user = substr(1, begVar - 1);
file = substr(begVar + 1);
}
else
{
@ -215,7 +219,7 @@ Foam::string& Foam::string::expand(const bool recurse, const bool allowEmptyVar)
}
else if (operator[](0) == '.')
{
// Expand initial '.' and './' into cwd
// Expand a lone '.' and an initial './' into cwd
if (size() == 1)
{
*this = cwd();

View File

@ -0,0 +1,359 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2011 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "stringOps.H"
#include "OSspecific.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Foam::string Foam::stringOps::expand
(
const string& original,
const HashTable<string, word, string::hash>& mapping,
const char sigil
)
{
string str(original);
return inplaceExpand(str, mapping);
}
Foam::string& Foam::stringOps::inplaceExpand
(
string& s,
const HashTable<string, word, string::hash>& mapping,
const char sigil
)
{
string::size_type begVar = 0;
// Expand $VAR or ${VAR}
// Repeat until nothing more is found
while
(
(begVar = s.find(sigil, begVar)) != string::npos
&& begVar < s.size()-1
)
{
if (begVar == 0 || s[begVar-1] != '\\')
{
// Find end of first occurrence
string::size_type endVar = begVar;
string::size_type delim = 0;
if (s[begVar+1] == '{')
{
endVar = s.find('}', begVar);
delim = 1;
}
else
{
string::iterator iter = s.begin() + begVar + 1;
while
(
iter != s.end()
&& (isalnum(*iter) || *iter == '_')
)
{
++iter;
++endVar;
}
}
if (endVar != string::npos && endVar != begVar)
{
string varName = s.substr
(
begVar + 1 + delim,
endVar - begVar - 2*delim
);
HashTable<string, word, string::hash>::const_iterator fnd =
mapping.find(varName);
if (fnd != HashTable<string, word, string::hash>::end())
{
s.std::string::replace
(
begVar,
endVar - begVar + 1,
*fnd
);
begVar += (*fnd).size();
}
else
{
s.std::string::replace
(
begVar,
endVar - begVar + 1,
""
);
}
}
else
{
break;
}
}
else
{
++begVar;
}
}
return s;
}
Foam::string Foam::stringOps::expandEnv
(
const string& original,
const bool recurse,
const bool allowEmptyVar
)
{
string str(original);
return inplaceExpandEnv(str, recurse, allowEmptyVar);
}
// Expand all occurences of environment variables and initial tilde sequences
Foam::string& Foam::stringOps::inplaceExpandEnv
(
string& s,
const bool recurse,
const bool allowEmptyVar
)
{
string::size_type begVar = 0;
// Expand $VARS
// Repeat until nothing more is found
while
(
(begVar = s.find('$', begVar)) != string::npos
&& begVar < s.size()-1
)
{
if (begVar == 0 || s[begVar-1] != '\\')
{
// Find end of first occurrence
string::size_type endVar = begVar;
string::size_type delim = 0;
if (s[begVar+1] == '{')
{
endVar = s.find('}', begVar);
delim = 1;
}
else
{
string::iterator iter = s.begin() + begVar + 1;
while
(
iter != s.end()
&& (isalnum(*iter) || *iter == '_')
)
{
++iter;
++endVar;
}
}
if (endVar != string::npos && endVar != begVar)
{
string varName = s.substr
(
begVar + 1 + delim,
endVar - begVar - 2*delim
);
string varValue = getEnv(varName);
if (varValue.size())
{
if (recurse)
{
varValue.expand(recurse, allowEmptyVar);
}
s.std::string::replace
(
begVar,
endVar - begVar + 1,
varValue
);
begVar += varValue.size();
}
else if (allowEmptyVar)
{
s.std::string::replace
(
begVar,
endVar - begVar + 1,
""
);
}
else
{
FatalErrorIn("string::expand(const bool, const bool)")
<< "Unknown variable name " << varName << '.'
<< exit(FatalError);
}
}
else
{
break;
}
}
else
{
++begVar;
}
}
if (!s.empty())
{
if (s[0] == '~')
{
// Expand initial ~
// ~/ => home directory
// ~OpenFOAM => site/user OpenFOAM configuration directory
// ~user => home directory for specified user
word user;
fileName file;
if ((begVar = s.find('/')) != string::npos)
{
user = s.substr(1, begVar - 1);
file = s.substr(begVar + 1);
}
else
{
user = s.substr(1);
}
// NB: be a bit lazy and expand ~unknownUser as an
// empty string rather than leaving it untouched.
// otherwise add extra test
if (user == "OpenFOAM")
{
s = findEtcFile(file);
}
else
{
s = home(user)/file;
}
}
else if (s[0] == '.')
{
// Expand a lone '.' and an initial './' into cwd
if (s.size() == 1)
{
s = cwd();
}
else if (s[1] == '/')
{
s.std::string::replace(0, 1, cwd());
}
}
}
return s;
}
Foam::string Foam::stringOps::trimLeft(const string& s)
{
if (!s.empty())
{
string::size_type beg = 0;
while (isspace(s[beg]))
{
++beg;
}
if (beg)
{
return s.substr(beg);
}
}
return s;
}
Foam::string& Foam::stringOps::inplaceTrimLeft(string& s)
{
if (!s.empty())
{
string::size_type beg = 0;
while (isspace(s[beg]))
{
++beg;
}
if (beg)
{
s.erase(0, beg);
}
}
return s;
}
Foam::string Foam::stringOps::trimRight(const string& s)
{
notImplemented("string stringOps::trimRight(const string&)");
return s;
}
Foam::string& Foam::stringOps::inplaceTrimRight(string& s)
{
notImplemented("string& stringOps::inplaceTrimRight(string&)");
return s;
}
Foam::string Foam::stringOps::trim(const string& original)
{
notImplemented("string stringOps::trim(const string&)");
return original;
}
Foam::string& Foam::stringOps::inplaceTrim(string& s)
{
notImplemented("string& stringOps::inplaceTrim(string&)");
return s;
}
// ************************************************************************* //

View File

@ -0,0 +1,159 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2011 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Namespace
Foam::stringOps
Description
Collection of static functions to do various simple string-related
operations
SourceFiles
stringOps.C
\*---------------------------------------------------------------------------*/
#ifndef stringOps_H
#define stringOps_H
#include "string.H"
#include "HashTable.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
/*---------------------------------------------------------------------------*\
Namespace stringOps Declaration
\*---------------------------------------------------------------------------*/
namespace stringOps
{
//- Expand occurences of variables according to the mapping
// Expansion includes:
// -# variables
// - "$VAR", "${VAR}"
//
// \note the leading sigil can be changed to avoid conflicts with other
// string expansions
string expand
(
const string&,
const HashTable<string, word, string::hash>& mapping,
const char sigil = '$'
);
//- Inplace expand occurences of variables according to the mapping
// Expansion includes:
// -# variables
// - "$VAR", "${VAR}"
//
// \note the leading sigil can be changed to avoid conflicts with other
// string expansions
string& inplaceExpand
(
string&,
const HashTable<string, word, string::hash>& mapping,
const char sigil = '$'
);
//- Expand initial tildes and all occurences of environment variables
// Expansion includes:
// -# environment variables
// - "$VAR", "${VAR}"
// -# current directory
// - leading "./" : the current directory
// -# tilde expansion
// - leading "~/" : home directory
// - leading "~user" : home directory for specified user
// - leading "~OpenFOAM" : site/user OpenFOAM configuration directory
//
// \sa
// Foam::findEtcFile
string expandEnv
(
const string&,
const bool recurse=false,
const bool allowEmptyVar = false
);
//- Expand initial tildes and all occurences of environment variables
// Expansion includes:
// -# environment variables
// - "$VAR", "${VAR}"
// -# current directory
// - leading "./" : the current directory
// -# tilde expansion
// - leading "~/" : home directory
// - leading "~user" : home directory for specified user
// - leading "~OpenFOAM" : site/user OpenFOAM configuration directory
//
// \sa
// Foam::findEtcFile
string& inplaceExpandEnv
(
string&,
const bool recurse=false,
const bool allowEmptyVar = false
);
//- Return string trimmed of leading whitespace
string trimLeft(const string&);
//- Trim leading whitespace inplace
string& inplaceTrimLeft(string&);
//- Return string trimmed of trailing whitespace
// NOT IMPLEMENTED
string trimRight(const string&);
//- Trim trailing whitespace inplace
// NOT IMPLEMENTED
string& inplaceTrimRight(string&);
//- Return string trimmed of leading and trailing whitespace
// NOT IMPLEMENTED
string trim(const string&);
//- Trim leading and trailing whitespace inplace
// NOT IMPLEMENTED
string& inplaceTrim(string&);
} // End namespace stringOps
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //