mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: consistency improvements for keyType and wordRe
- simplify compile/uncompile, reading, assignment - implicit construct wordRe from keyType (was explicit) to simplify future API changes. - make Foam::isspace consistent with std::isspace (C-locale) by including vertical tab and form feed ENH: improve #ifeq float/label comparisons
This commit is contained in:
committed by
Andrew Heather
parent
57c1fceabf
commit
96a1b86fb9
@ -6,7 +6,7 @@
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2011 OpenFOAM Foundation
|
||||
Copyright (C) 2019 OpenCFD Ltd.
|
||||
Copyright (C) 2019-2021 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -41,7 +41,7 @@ using namespace Foam;
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
stringList strLst
|
||||
stringList strings
|
||||
{
|
||||
"hello",
|
||||
"heello",
|
||||
@ -53,51 +53,57 @@ int main(int argc, char *argv[])
|
||||
"okkey",
|
||||
"okkkey",
|
||||
};
|
||||
labelList matches;
|
||||
|
||||
wordRes reLst(IStringStream("( okey \"[hy]e+.*\" )")());
|
||||
wordRes matcher1(IStringStream("( okey \"[hy]e+.*\" )")());
|
||||
|
||||
Info<< "stringList " << strLst << nl;
|
||||
Info<< "stringList " << strings << nl;
|
||||
|
||||
labelList matches = findStrings(regExp(".*ee.*"), strLst);
|
||||
|
||||
Info<< "matches found for regexp .*ee.* :" << nl << matches << nl;
|
||||
|
||||
forAll(matches, i)
|
||||
{
|
||||
Info<< " -> " << strLst[matches[i]] << nl;
|
||||
keyType key(".*ee.*", keyType::REGEX);
|
||||
matches = findMatchingStrings(regExp(key), strings);
|
||||
|
||||
Info<< "matches found for regexp " << key << " :" << nl
|
||||
<< matches << nl;
|
||||
|
||||
for (const label idx : matches)
|
||||
{
|
||||
Info<< " -> " << strings[idx] << nl;
|
||||
}
|
||||
}
|
||||
|
||||
Info<< "Match found using ListOps = "
|
||||
<< ListOps::found(strLst, regExp(".*ee.*")) << nl;
|
||||
<< ListOps::found(strings, regExp(".*ee.*")) << nl;
|
||||
|
||||
Info<< "First index = "
|
||||
<< ListOps::find(strLst, regExp(".*ee.*")) << nl;
|
||||
<< ListOps::find(strings, regExp(".*ee.*")) << nl;
|
||||
|
||||
Info<< endl;
|
||||
|
||||
matches = findStrings(reLst, strLst);
|
||||
matches = findMatchingStrings(matcher1, strings);
|
||||
|
||||
Info<< "matching " << flatOutput(reLst) << " => "
|
||||
<< reLst.matching(strLst) << nl;
|
||||
Info<< "matches found for " << flatOutput(reLst) << " => "
|
||||
Info<< "matching " << flatOutput(matcher1) << " => "
|
||||
<< matcher1.matching(strings) << nl;
|
||||
Info<< "matches found for " << flatOutput(matcher1) << " => "
|
||||
<< matches << nl;
|
||||
forAll(matches, i)
|
||||
|
||||
for (const label idx : matches)
|
||||
{
|
||||
Info<< " -> " << strLst[matches[i]] << nl;
|
||||
Info<< " -> " << strings[idx] << nl;
|
||||
}
|
||||
Info<< endl;
|
||||
|
||||
stringList subLst = subsetStrings(regExp(".*ee.*"), strLst);
|
||||
stringList subLst = subsetStrings(regExp(".*ee.*"), strings);
|
||||
Info<< "subset stringList: " << subLst << nl;
|
||||
|
||||
subLst = subsetStrings(reLst, strLst);
|
||||
subLst = subsetStrings(matcher1, strings);
|
||||
Info<< "subset stringList: " << subLst << nl;
|
||||
|
||||
inplaceSubsetStrings(reLst, strLst);
|
||||
Info<< "subsetted stringList: " << strLst << nl;
|
||||
inplaceSubsetStrings(matcher1, strings);
|
||||
Info<< "subsetted stringList: " << strings << nl;
|
||||
|
||||
inplaceSubsetStrings(regExp(".*l.*"), strLst);
|
||||
Info<< "subsetted stringList: " << strLst << nl;
|
||||
inplaceSubsetStrings(regExp(".*l.*"), strings);
|
||||
Info<< "subsetted stringList: " << strings << nl;
|
||||
|
||||
Info<< "\nEnd\n" << endl;
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
\\ / A nd | www.openfoam.com
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2017 OpenCFD Ltd.
|
||||
Copyright (C) 2017-2021 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -38,8 +38,12 @@ Description
|
||||
using namespace Foam;
|
||||
|
||||
// Simple utility
|
||||
template<class String>
|
||||
void printSubStrings(const String& str, const SubStrings<String>& split)
|
||||
template<class StringType>
|
||||
void printSubStrings
|
||||
(
|
||||
const StringType& str,
|
||||
const SubStrings<StringType>& split
|
||||
)
|
||||
{
|
||||
Info<< "string {" << str.size() << " chars} = " << str << nl
|
||||
<< split.size() << " elements {" << split.length() << " chars}"
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2011-2016 OpenFOAM Foundation
|
||||
Copyright (C) 2017-2020 OpenCFD Ltd.
|
||||
Copyright (C) 2017-2021 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -32,6 +32,7 @@ Description
|
||||
#include "IOstreams.H"
|
||||
#include "IOobject.H"
|
||||
#include "IFstream.H"
|
||||
#include "ITstream.H"
|
||||
#include "List.H"
|
||||
#include "Tuple2.H"
|
||||
#include "keyType.H"
|
||||
@ -51,11 +52,84 @@ word typeOf(wordRe::compOption retval)
|
||||
}
|
||||
|
||||
|
||||
Ostream& printInfo(const wordRe& wre)
|
||||
{
|
||||
if (wre.isPattern())
|
||||
{
|
||||
Info<< "wordRe(regex) ";
|
||||
}
|
||||
else
|
||||
{
|
||||
Info<< "wordRe(plain) ";
|
||||
}
|
||||
Info<< wre;
|
||||
return Info;
|
||||
}
|
||||
|
||||
|
||||
// Could use something like this for reading wordRes
|
||||
void exptl_reading(Istream& is, wordRes& list)
|
||||
{
|
||||
token tok(is);
|
||||
|
||||
bool ok = ((tok.isWord() || tok.isQuotedString()) && !tok.isCompound());
|
||||
if (ok)
|
||||
{
|
||||
list.resize(1);
|
||||
ok = list[0].assign(tok);
|
||||
}
|
||||
if (!ok)
|
||||
{
|
||||
if (tok.good())
|
||||
{
|
||||
is.putBack(tok);
|
||||
}
|
||||
list.readList(is);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool testReadList_wordRes(const std::string& input)
|
||||
{
|
||||
ITstream is("input", input);
|
||||
wordRes list;
|
||||
|
||||
exptl_reading(is, list);
|
||||
|
||||
const label nTrailing = is.nRemainingTokens();
|
||||
|
||||
Info<< "input:<<<<" << nl << input.c_str() << nl
|
||||
<< ">>>> with " << nTrailing << " tokens remaining" << nl
|
||||
<< "list: " << flatOutput(list) << nl;
|
||||
|
||||
return !nTrailing;
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
// Main program:
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (true)
|
||||
{
|
||||
Info<< "Test input for wordRes" << nl << nl;
|
||||
|
||||
testReadList_wordRes
|
||||
(
|
||||
"("
|
||||
"this \"x.*\" \"file[a-b]\" xvalues \"xv.*\""
|
||||
")"
|
||||
);
|
||||
|
||||
testReadList_wordRes
|
||||
(
|
||||
"\".*\""
|
||||
);
|
||||
|
||||
Info<< nl << nl;
|
||||
}
|
||||
|
||||
wordRe wre;
|
||||
std::string s1("this .* file");
|
||||
Foam::string s2("this .* file");
|
||||
@ -64,13 +138,13 @@ int main(int argc, char *argv[])
|
||||
keyType keyre("x.*", keyType::REGEX);
|
||||
|
||||
wordRes wordrelist
|
||||
{
|
||||
({
|
||||
{"this", wordRe::LITERAL},
|
||||
{"x.*", wordRe::REGEX},
|
||||
{"file[a-b]", wordRe::REGEX},
|
||||
{"xvalues", wordRe::LITERAL},
|
||||
{"xv.*", wordRe::REGEX},
|
||||
};
|
||||
});
|
||||
|
||||
if (false)
|
||||
{
|
||||
@ -169,45 +243,45 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
Info<< nl;
|
||||
|
||||
wordRe(s1, wordRe::DETECT).info(Info) << nl;
|
||||
wordRe(s2).info(Info) << nl;
|
||||
wordRe(s2, wordRe::DETECT).info(Info) << nl;
|
||||
wordRe(s3, wordRe::REGEX).info(Info) << nl;
|
||||
printInfo(wordRe(s1, wordRe::DETECT)) << nl;
|
||||
printInfo(wordRe(s2)) << nl;
|
||||
printInfo(wordRe(s2, wordRe::DETECT)) << nl;
|
||||
printInfo(wordRe(s3, wordRe::REGEX)) << nl;
|
||||
|
||||
wre = "this .* file";
|
||||
|
||||
Info<<"substring: " << wre.substr(4) << nl;
|
||||
|
||||
wre.info(Info) << nl;
|
||||
printInfo(wre) << nl;
|
||||
wre = s1;
|
||||
wre.info(Info) << nl;
|
||||
printInfo(wre) << nl;
|
||||
wre.uncompile();
|
||||
wre.info(Info) << nl;
|
||||
printInfo(wre) << nl;
|
||||
|
||||
wre = "something";
|
||||
wre.info(Info) << " before" << nl;
|
||||
printInfo(wre) << " before" << nl;
|
||||
wre.uncompile();
|
||||
wre.info(Info) << " uncompiled" << nl;
|
||||
printInfo(wre) << " uncompiled" << nl;
|
||||
wre.compile(wordRe::DETECT);
|
||||
wre.info(Info) << " after DETECT" << nl;
|
||||
printInfo(wre) << " after DETECT" << nl;
|
||||
wre.compile(wordRe::ICASE);
|
||||
wre.info(Info) << " after ICASE" << nl;
|
||||
printInfo(wre) << " after ICASE" << nl;
|
||||
wre.compile(wordRe::DETECT_ICASE);
|
||||
wre.info(Info) << " after DETECT_ICASE" << nl;
|
||||
printInfo(wre) << " after DETECT_ICASE" << nl;
|
||||
|
||||
wre = "something .* value";
|
||||
wre.info(Info) << " before" << nl;
|
||||
printInfo(wre) << " before" << nl;
|
||||
wre.uncompile();
|
||||
wre.info(Info) << " uncompiled" << nl;
|
||||
printInfo(wre) << " uncompiled" << nl;
|
||||
wre.compile(wordRe::DETECT);
|
||||
wre.info(Info) << " after DETECT" << nl;
|
||||
printInfo(wre) << " after DETECT" << nl;
|
||||
wre.uncompile();
|
||||
wre.info(Info) << " uncompiled" << nl;
|
||||
printInfo(wre) << " uncompiled" << nl;
|
||||
wre.compile();
|
||||
wre.info(Info) << " re-compiled" << nl;
|
||||
printInfo(wre) << " re-compiled" << nl;
|
||||
|
||||
wre.set("something .* value", wordRe::LITERAL);
|
||||
wre.info(Info) << " set as LITERAL" << nl;
|
||||
printInfo(wre) << " set as LITERAL" << nl;
|
||||
|
||||
IOobject::writeDivider(Info);
|
||||
|
||||
@ -220,7 +294,7 @@ int main(int argc, char *argv[])
|
||||
const wordRe& wre = rawList[elemI].first();
|
||||
const string& str = rawList[elemI].second();
|
||||
|
||||
wre.info(Info)
|
||||
printInfo(wre)
|
||||
<< " equals:" << (wre == str)
|
||||
<< "(" << wre.match(str, true) << ")"
|
||||
<< " match:" << wre.match(str)
|
||||
@ -230,11 +304,10 @@ int main(int argc, char *argv[])
|
||||
wordRe wre2;
|
||||
wre2.set(wre, wordRe::ICASE);
|
||||
|
||||
wre2.info(Info)
|
||||
printInfo(wre2)
|
||||
<< " match:" << wre2.match(str)
|
||||
<< " str=" << str
|
||||
<< nl;
|
||||
|
||||
}
|
||||
|
||||
Info<< "\nEnd\n" << endl;
|
||||
|
||||
Reference in New Issue
Block a user