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-2021 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
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 label fieldWidth,
39 const std::string& str,
40 size_t pos,
41 size_t len
42)
43{
44 if (fieldWidth < 1)
45 {
46 fieldWidth = 1;
47 }
48
50
51 const auto trimPoints = stringOps::findTrim(str, pos, len);
52
53 pos = trimPoints.first;
54 len = (trimPoints.second - trimPoints.first);
55
56 if (!len)
57 {
58 return string();
59 }
60
62
64 {
65 expressions::fieldExprDriver driver(fieldWidth);
66 driver.parse(str, pos, len);
67 result = std::move(driver.result());
68 }
69
70 if (!result.hasValue() || !result.size())
71 {
73 << "Failed evaluation: "
74 << str.substr(pos, len) << nl;
75
76 return string();
77 }
78
80 if (result.size() <= 1)
81 {
82 result.writeValue(os);
83 }
84 else
85 {
86 result.writeField(os);
87 }
88
89 return os.str();
90}
91
92
94(
95 const std::string& str,
96 size_t pos,
97 size_t len
98)
99{
100 return stringOps::evaluate(1, str, pos, len);
101}
102
103
104// ************************************************************************* //
Input/output from string buffers.
Output to string buffer, using a OSstream. Always UNCOMPRESSED.
Definition: StringStream.H:231
const exprResult & result() const noexcept
Const access to expression result.
Definition: exprDriver.H:391
A polymorphic field/result from evaluating an expression.
Definition: exprResult.H:127
label size() const
The field or object size.
Definition: exprResultI.H:281
void writeValue(Ostream &os) const
Write the single value, or the first value from field.
Definition: exprResult.C:661
bool hasValue() const
Has a value?
Definition: exprResultI.H:228
void writeField(Ostream &os, const word &keyword="") const
Write the field, optionally as an entry.
Definition: exprResult.C:629
Driver for generic primitive field expressions.
virtual unsigned parse(const std::string &expr, size_t pos=0, size_t len=std::string::npos)
Execute the parser.
A class for handling character strings derived from std::string.
Definition: string.H:79
OBJstream os(runTime.globalPath()/outputName)
string evaluate(label fieldWidth, const std::string &s, size_t pos=0, size_t len=std::string::npos)
String evaluation with specified (positive, non-zero) field width.
dimensionedScalar pos(const dimensionedScalar &ds)
messageStream InfoErr
Information stream (stderr output on master, null elsewhere)
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
String expression evaluation.