stringOpsEvaluate.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2019 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "stringOpsEvaluate.H"
29 #include "stringOps.H"
30 #include "StringStream.H"
31 #include "fieldExprDriver.H"
32 #include "error.H"
33 
34 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
35 
37 (
38  const std::string& str,
39  size_t pos,
40  size_t len
41 )
42 {
44 
45  const auto trimPoints = stringOps::findTrim(str, pos, len);
46 
47  pos = trimPoints.first;
48  len = (trimPoints.second - trimPoints.first);
49 
50  if (!len)
51  {
52  return "";
53  }
54 
56 
58  {
60  driver.parse(str, pos, len);
61  result = std::move(driver.result());
62  }
63 
64  if (!result.hasValue() || !result.size())
65  {
66  InfoErr
67  << "Failed evaluation: "
68  << str.substr(pos, len) << nl;
69 
70  return "";
71  }
72 
73  OStringStream os;
74  result.writeValue(os);
75 
76  return os.str();
77 }
78 
79 
80 // ************************************************************************* //
stringOpsEvaluate.H
String expression evaluation.
Foam::expressions::exprResult::size
label size() const
The field or object size.
Definition: exprResultI.H:295
Foam::expressions::fieldExpr::parseDriver
Driver for generic primitive field expressions.
Definition: fieldExprDriver.H:82
StringStream.H
Input/output from string buffers.
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:73
Foam::InfoErr
messageStream InfoErr
Information stream (uses stderr - output is on the master only)
Foam::expressions::exprResult
A polymorphic field/result from evaluating an expression.
Definition: exprResult.H:128
error.H
Foam::expressions::exprResult::writeValue
void writeValue(Ostream &os) const
Write the single value, or the first value from field.
Definition: exprResult.C:642
Foam::stringOps::findTrim
std::pair< size_t, size_t > findTrim(const std::string &s, size_t pos=0, size_t len=std::string::npos)
Find (first, last) non-space locations in string or sub-string.
Foam::expressions::exprResult::hasValue
bool hasValue() const
Has a value?
Definition: exprResultI.H:237
fieldExprDriver.H
Foam::Detail::StringStreamAllocator::str
Foam::string str() const
Get the string - as Foam::string rather than std::string.
Definition: StringStream.H:91
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::OStringStream
Output to string buffer, using a OSstream.
Definition: StringStream.H:196
Foam::expressions::exprDriver::result
virtual const exprResult & result() const
Const access to expression result.
Definition: exprDriver.H:305
stringOps.H
Foam::expressions::fieldExpr::parseDriver::parse
virtual unsigned parse(const std::string &expr, size_t pos=0, size_t len=std::string::npos)
Execute the parser.
Definition: fieldExprDriver.C:96
Foam::stringOps::evaluate
string evaluate(const std::string &s, size_t pos=0, size_t len=std::string::npos)
Definition: stringOpsEvaluate.C:37
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177