mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: support default values in string expansion
- syntax as per Bourne/Korn shell
${parameter:-defValue}
If parameter is unset or null, the \c defValue is substituted.
Otherwise, the value of parameter is substituted.
This commit is contained in:
@ -40,8 +40,9 @@ int main(int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
string test
|
string test
|
||||||
(
|
(
|
||||||
" $HOME kjhkjhkjh \" \\$HOME/tyetyery ${FOAM_RUN} \n ; hkjh ;$ with "
|
" $HOME kjhkjhkjh \" \\$HOME/tyetyery $; ${FOAM_RUN} \n $; hkjh;"
|
||||||
" $(DONOTSUBST) some other ${USER} entries "
|
" $(DONOTSUBST) some other <${USER}> with '${__UNKNOWN:-some default}'"
|
||||||
|
" value "
|
||||||
);
|
);
|
||||||
|
|
||||||
dictionary dict;
|
dictionary dict;
|
||||||
|
|||||||
@ -66,10 +66,23 @@ Foam::string& Foam::stringOps::inplaceExpand
|
|||||||
string::size_type endVar = begVar;
|
string::size_type endVar = begVar;
|
||||||
string::size_type delim = 0;
|
string::size_type delim = 0;
|
||||||
|
|
||||||
|
// The position of the ":-" default value
|
||||||
|
string::size_type altPos = string::npos;
|
||||||
|
|
||||||
if (s[begVar+1] == '{')
|
if (s[begVar+1] == '{')
|
||||||
{
|
{
|
||||||
endVar = s.find('}', begVar);
|
endVar = s.find('}', begVar);
|
||||||
delim = 1;
|
delim = 1;
|
||||||
|
|
||||||
|
// looks like ${parameter:-word}
|
||||||
|
if (endVar != string::npos)
|
||||||
|
{
|
||||||
|
altPos = s.find(":-", begVar);
|
||||||
|
if (altPos != string::npos && altPos > endVar)
|
||||||
|
{
|
||||||
|
altPos = string::npos;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -110,11 +123,26 @@ Foam::string& Foam::stringOps::inplaceExpand
|
|||||||
s.substr
|
s.substr
|
||||||
(
|
(
|
||||||
begVar + 1 + delim,
|
begVar + 1 + delim,
|
||||||
endVar - begVar - 2*delim
|
(
|
||||||
|
(altPos == string::npos ? endVar : altPos)
|
||||||
|
- begVar - 2*delim
|
||||||
|
)
|
||||||
),
|
),
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
|
|
||||||
|
std::string altValue;
|
||||||
|
if (altPos != string::npos)
|
||||||
|
{
|
||||||
|
// had ":-" default value
|
||||||
|
altValue = s.substr
|
||||||
|
(
|
||||||
|
altPos + 2,
|
||||||
|
endVar - altPos - 2*delim
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
HashTable<string, word, string::hash>::const_iterator fnd =
|
HashTable<string, word, string::hash>::const_iterator fnd =
|
||||||
mapping.find(varName);
|
mapping.find(varName);
|
||||||
|
|
||||||
@ -128,6 +156,17 @@ Foam::string& Foam::stringOps::inplaceExpand
|
|||||||
);
|
);
|
||||||
begVar += (*fnd).size();
|
begVar += (*fnd).size();
|
||||||
}
|
}
|
||||||
|
else if (altPos != string::npos)
|
||||||
|
{
|
||||||
|
// use alternative provided
|
||||||
|
s.std::string::replace
|
||||||
|
(
|
||||||
|
begVar,
|
||||||
|
endVar - begVar + 1,
|
||||||
|
altValue
|
||||||
|
);
|
||||||
|
begVar += altValue.size();
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
s.std::string::replace
|
s.std::string::replace
|
||||||
@ -312,10 +351,23 @@ Foam::string& Foam::stringOps::inplaceExpand
|
|||||||
string::size_type endVar = begVar;
|
string::size_type endVar = begVar;
|
||||||
string::size_type delim = 0;
|
string::size_type delim = 0;
|
||||||
|
|
||||||
|
// The position of the ":-" default value
|
||||||
|
string::size_type altPos = string::npos;
|
||||||
|
|
||||||
if (s[begVar+1] == '{')
|
if (s[begVar+1] == '{')
|
||||||
{
|
{
|
||||||
endVar = s.find('}', begVar);
|
endVar = s.find('}', begVar);
|
||||||
delim = 1;
|
delim = 1;
|
||||||
|
|
||||||
|
// looks like ${parameter:-word}
|
||||||
|
if (endVar != string::npos)
|
||||||
|
{
|
||||||
|
altPos = s.find(":-", begVar);
|
||||||
|
if (altPos != string::npos && altPos > endVar)
|
||||||
|
{
|
||||||
|
altPos = string::npos;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -350,11 +402,25 @@ Foam::string& Foam::stringOps::inplaceExpand
|
|||||||
s.substr
|
s.substr
|
||||||
(
|
(
|
||||||
begVar + 1 + delim,
|
begVar + 1 + delim,
|
||||||
endVar - begVar - 2*delim
|
(
|
||||||
|
(altPos == string::npos ? endVar : altPos)
|
||||||
|
- begVar - 2*delim
|
||||||
|
)
|
||||||
),
|
),
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
|
|
||||||
|
std::string altValue;
|
||||||
|
if (altPos != string::npos)
|
||||||
|
{
|
||||||
|
// had ":-" default value
|
||||||
|
altValue = s.substr
|
||||||
|
(
|
||||||
|
altPos + 2,
|
||||||
|
endVar - altPos - 2*delim
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const string varValue = getEnv(varName);
|
const string varValue = getEnv(varName);
|
||||||
if (varValue.size())
|
if (varValue.size())
|
||||||
{
|
{
|
||||||
@ -367,6 +433,17 @@ Foam::string& Foam::stringOps::inplaceExpand
|
|||||||
);
|
);
|
||||||
begVar += varValue.size();
|
begVar += varValue.size();
|
||||||
}
|
}
|
||||||
|
else if (altPos != string::npos)
|
||||||
|
{
|
||||||
|
// use alternative provided
|
||||||
|
s.std::string::replace
|
||||||
|
(
|
||||||
|
begVar,
|
||||||
|
endVar - begVar + 1,
|
||||||
|
altValue
|
||||||
|
);
|
||||||
|
begVar += altValue.size();
|
||||||
|
}
|
||||||
else if (allowEmpty)
|
else if (allowEmpty)
|
||||||
{
|
{
|
||||||
s.std::string::replace
|
s.std::string::replace
|
||||||
|
|||||||
@ -55,7 +55,17 @@ namespace stringOps
|
|||||||
// -# variables
|
// -# variables
|
||||||
// - "$VAR", "${VAR}"
|
// - "$VAR", "${VAR}"
|
||||||
//
|
//
|
||||||
// Any unknown entries are removed
|
// Supports default values as per the Bourne/Korn shell.
|
||||||
|
// \code
|
||||||
|
// "${parameter:-defValue}"
|
||||||
|
// \endcode
|
||||||
|
// If parameter is unset or null, the \c defValue is substituted.
|
||||||
|
// Otherwise, the value of parameter is substituted.
|
||||||
|
//
|
||||||
|
// Any unknown entries are removed silently.
|
||||||
|
//
|
||||||
|
// Malformed entries (eg, brace mismatch, sigil followed by bad character)
|
||||||
|
// are left as is.
|
||||||
//
|
//
|
||||||
// \note the leading sigil can be changed to avoid conflicts with other
|
// \note the leading sigil can be changed to avoid conflicts with other
|
||||||
// string expansions
|
// string expansions
|
||||||
@ -72,7 +82,17 @@ namespace stringOps
|
|||||||
// -# variables
|
// -# variables
|
||||||
// - "$VAR", "${VAR}"
|
// - "$VAR", "${VAR}"
|
||||||
//
|
//
|
||||||
// Any unknown entries are removed
|
// Supports default values as per the Bourne/Korn shell.
|
||||||
|
// \code
|
||||||
|
// "${parameter:-defValue}"
|
||||||
|
// \endcode
|
||||||
|
// If parameter is unset or null, the \c defValue is substituted.
|
||||||
|
// Otherwise, the value of parameter is substituted.
|
||||||
|
//
|
||||||
|
// Any unknown entries are removed silently.
|
||||||
|
//
|
||||||
|
// Malformed entries (eg, brace mismatch, sigil followed by bad character)
|
||||||
|
// are left as is.
|
||||||
//
|
//
|
||||||
// \note the leading sigil can be changed to avoid conflicts with other
|
// \note the leading sigil can be changed to avoid conflicts with other
|
||||||
// string expansions
|
// string expansions
|
||||||
@ -128,7 +148,18 @@ namespace stringOps
|
|||||||
// - leading "~user" : home directory for specified user
|
// - leading "~user" : home directory for specified user
|
||||||
// - leading "~OpenFOAM" : site/user OpenFOAM configuration directory
|
// - leading "~OpenFOAM" : site/user OpenFOAM configuration directory
|
||||||
//
|
//
|
||||||
// Any unknown entries are removed silently if allowEmpty is true
|
// Supports default values as per the Bourne/Korn shell.
|
||||||
|
// \code
|
||||||
|
// "${parameter:-defValue}"
|
||||||
|
// \endcode
|
||||||
|
// If parameter is unset or null, the \c defValue is substituted.
|
||||||
|
// Otherwise, the value of parameter is substituted.
|
||||||
|
//
|
||||||
|
// Any unknown entries are removed silently, if allowEmpty is true.
|
||||||
|
//
|
||||||
|
// Malformed entries (eg, brace mismatch, sigil followed by bad character)
|
||||||
|
// are left as is.
|
||||||
|
//
|
||||||
// \sa
|
// \sa
|
||||||
// Foam::findEtcFile
|
// Foam::findEtcFile
|
||||||
string expand
|
string expand
|
||||||
@ -149,7 +180,19 @@ namespace stringOps
|
|||||||
// - leading "~user" : home directory for specified user
|
// - leading "~user" : home directory for specified user
|
||||||
// - leading "~OpenFOAM" : site/user OpenFOAM configuration directory
|
// - leading "~OpenFOAM" : site/user OpenFOAM configuration directory
|
||||||
//
|
//
|
||||||
// Any unknown entries are removed silently if allowEmpty is true
|
// Supports default values as per the Bourne/Korn shell.
|
||||||
|
// \code
|
||||||
|
// "${parameter:-defValue}"
|
||||||
|
// \endcode
|
||||||
|
// If parameter is unset or null, the \c defValue is substituted.
|
||||||
|
// Otherwise, the value of parameter is substituted.
|
||||||
|
//
|
||||||
|
// Any unknown entries are removed silently, if allowEmpty is true.
|
||||||
|
//
|
||||||
|
// Malformed entries (eg, brace mismatch, sigil followed by bad character)
|
||||||
|
// are left as is.
|
||||||
|
//
|
||||||
|
// Any unknown entries are removed silently if allowEmpty is true.
|
||||||
// \sa
|
// \sa
|
||||||
// Foam::findEtcFile
|
// Foam::findEtcFile
|
||||||
string& inplaceExpand
|
string& inplaceExpand
|
||||||
|
|||||||
Reference in New Issue
Block a user