ENH: refactor function arg splitting -> stringOps::splitFunctionArgs

This commit is contained in:
Mark Olesen
2021-05-18 08:21:55 +02:00
parent 44a243a94d
commit c9fda67b5f
10 changed files with 514 additions and 74 deletions

View File

@ -0,0 +1,3 @@
Test-splitFunctionArgs.C
EXE = $(FOAM_USER_APPBIN)/Test-splitFunctionArgs

View File

@ -0,0 +1,2 @@
/* EXE_INC = */
/* EXE_LIBS = */

View File

@ -0,0 +1,155 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
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/>.
Application
Test-splitFunctionArgs
Description
Test splitting of function name args
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "IOstreams.H"
#include "IOobject.H"
#include "IFstream.H"
#include "dictionary.H"
#include "stringOps.H"
#include "Tuple2.H"
using namespace Foam;
// Split out function name and any arguments
// - use as per functionObjectList
void testFunctionNameAndArgsSplit(const std::string& line)
{
word funcName;
wordRes args;
List<Tuple2<word, string>> namedArgs;
const auto lbracket = line.find('(');
if (lbracket == std::string::npos)
{
funcName = word::validate(line);
// No args
}
else
{
funcName = word::validate(line.substr(0, lbracket));
std::string params;
const auto rbracket = line.rfind(')');
if (rbracket != std::string::npos && lbracket < rbracket)
{
params = line.substr(lbracket+1, (rbracket - lbracket - 1));
}
else
{
params = line.substr(lbracket+1);
}
Info<<"parsing: " << params << nl;
stringOps::splitFunctionArgs(params, args, namedArgs);
}
Info<< nl
<< line << nl
<< "function: <" << funcName << '>' << nl
<< " args: " << args << nl
<< " named: " << namedArgs << nl;
}
// Split out any arguments
void testArgsSplit(const std::string& line)
{
wordRes args;
List<Tuple2<word, string>> namedArgs;
stringOps::splitFunctionArgs(line, args, namedArgs);
Info<< nl
<< line << nl
<< " args: " << args << nl
<< " named: " << namedArgs << nl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
argList::noBanner();
argList::noParallel();
argList::addArgument("file1 .. fileN");
argList args(argc, argv, false, true);
if (args.size() <= 1)
{
InfoErr<< "Provide a file or files to test" << nl;
}
else
{
for (label argi=1; argi < args.size(); ++argi)
{
IOobject::writeDivider(Info);
const auto inputFile = args.get<fileName>(argi);
IFstream is(inputFile);
string line;
while (is.getLine(line))
{
if (line.empty() || line[0] == '#')
{
continue;
}
if (line.starts_with("function:"))
{
auto trim = line.find(':');
++trim;
while (isspace(line[trim]))
{
++trim;
}
line.erase(0, trim);
testFunctionNameAndArgsSplit(line);
}
else
{
testArgsSplit(line);
}
}
}
}
return 0;
}
// ************************************************************************* //

View File

@ -0,0 +1,47 @@
# -----------------------------------------------------------------------------
# Test names for splitting. Comment character as per -*- sh -*- mode
#
# Prefix with "function: " to test with function name splitting
function: basic
function: func1( a , b );
function: func2(a, value=10);
# discard or flag bad/missing parameters?
function: func3(a , , , value=10);
function: func4();
function: func5( abc );
start=1, end=2
start=1, end=2,
start=100, end= 200, abc
value=100
start=1, end=2
origin=(0 0 0), scale=2, normal=(0 0 1)
# Canonical with named args
function: patchAverage(patch=inlet, p)
# Canonical with unnamed and named args
function: patchAverage(other, patch=inlet, pval)
function: patchAverage(patch=(inlet|outlet), p)
# General
(a, b)
allow=(inlet|outlet), deny=false, regular(value)
# -----------------------------------------------------------------------------