Loading...
Searching...
No Matches
wordI.H
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) 2011-2017 OpenFOAM Foundation
9 Copyright (C) 2018-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
29#include <iostream> // For std::cerr
30
31// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
32
33template<class PrimitiveType>
35(
36 const char* fmt,
37 const PrimitiveType& val
38)
39{
40 word output;
41 string_printf(output, fmt, val);
42 return output;
43}
44
45
46template<class PrimitiveType>
48(
49 const std::string& fmt,
50 const PrimitiveType& val
51)
53 word output;
54 string_printf(output, fmt, val);
55 return output;
56}
57
58
59inline bool Foam::word::valid(char c)
60{
61 return
62 (
63 !isspace(c)
64 && c != '"' // string quote
65 && c != '\'' // string quote
66 && c != '/' // path separator
67 && c != ';' // end statement
68 && c != '{' // beg block (eg, subdict)
69 && c != '}' // end block (eg, subdict)
70 );
71}
72
73
74// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
75
76inline Foam::word::word(const string& s, bool doStrip)
77:
78 string(s)
79{
80 if (doStrip)
81 {
83 }
84}
85
86
87inline Foam::word::word(string&& s, bool doStrip)
88:
89 string(std::move(s))
90{
91 if (doStrip)
92 {
94 }
95}
96
97
98inline Foam::word::word(std::string&& s, bool doStrip)
99:
100 string(std::move(s))
101{
102 if (doStrip)
103 {
104 stripInvalid();
105 }
106}
107
108
109inline Foam::word::word(const std::string& s, bool doStrip)
110:
111 string(s)
112{
113 if (doStrip)
114 {
115 stripInvalid();
116 }
117}
118
119
120inline Foam::word::word(const char* s, bool doStrip)
121:
122 string(s)
123{
124 if (doStrip)
125 {
126 stripInvalid();
127 }
128}
129
130
131inline Foam::word::word(const char* s, size_type len, bool doStrip)
132:
133 string(s, len)
134{
135 if (doStrip)
136 {
138 }
139}
140
141
142// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
143
144inline void Foam::word::stripInvalid()
145{
146 // Only strip when debug is active (potentially costly operation)
147 if (debug && string::stripInvalid<word>(*this))
148 {
149 std::cerr
150 << "word::stripInvalid() called for word "
151 << this->c_str() << std::endl;
152
153 if (debug > 1)
154 {
155 std::cerr
156 << " For debug level (= " << debug
157 << ") > 1 this is considered fatal" << std::endl;
158 std::exit(1);
159 }
160 }
161}
162
163
164// FUTURE?
165// inline Foam::word Foam::word::stem() const
166// {
167// const auto i = find_ext(); // Or just rfind('.')
168//
169// if (i == std::string::npos)
170// {
171// return *this;
172// }
173//
174// return substr(0, i);
175// }
176
178inline Foam::word Foam::word::ext() const
179{
180 return string::ext();
181}
182
183
184inline Foam::word& Foam::word::ext(const word& ending)
185{
186 string::ext(ending);
187 return *this;
188}
189
190
191inline Foam::word& Foam::word::replace_ext(const word& ending)
194 string::ext(ending);
195 return *this;
196}
197
198
199inline Foam::word Foam::word::lessExt() const
200{
201 const auto i = find_ext(); // Or just rfind('.')
202
203 if (i == std::string::npos)
204 {
205 return *this;
206 }
208 return substr(0, i);
209}
210
211
212// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
213
215{
216 // Self-assignment is a no-op
217 if (this != &s)
219 assign(s);
220 }
221 return *this;
222}
223
224
225inline Foam::word& Foam::word::operator=(word&& s)
226{
227 // Self-assignment is a no-op
228 if (this != &s)
230 assign(std::move(s));
231 }
232 return *this;
233}
234
235
236inline Foam::word& Foam::word::operator=(const string& s)
238 assign(s);
239 stripInvalid();
240 return *this;
241}
242
243
244inline Foam::word& Foam::word::operator=(string&& s)
246 assign(std::move(s));
247 stripInvalid();
248 return *this;
249}
250
251
252inline Foam::word& Foam::word::operator=(const std::string& s)
254 assign(s);
255 stripInvalid();
256 return *this;
257}
258
259
260inline Foam::word& Foam::word::operator=(std::string&& s)
262 assign(std::move(s));
263 stripInvalid();
264 return *this;
265}
266
267
268inline Foam::word& Foam::word::operator=(const char* s)
269{
270 assign(s);
271 stripInvalid();
272 return *this;
273}
274
275
276// ************************************************************************* //
graph_traits< Graph >::vertices_size_type size_type
A class for handling character strings derived from std::string.
Definition string.H:76
bool remove_ext()
Remove extension, return true if string changed.
Definition stringI.H:93
string()=default
Default construct.
static bool stripInvalid(std::string &str)
Strip invalid characters from the given string.
Definition stringI.H:164
word ext() const
Return file name extension (part after last .).
Definition string.C:38
static std::string::size_type string_printf(std::string &output, const char *fmt, const PrimitiveType &val)
A printf-style formatter for a primitive.
static std::string::size_type find_ext(const std::string &str)
Find position of a file extension dot, return npos on failure.
Definition stringI.H:24
A class for handling words, derived from Foam::string.
Definition word.H:66
static word printf(const char *fmt, const PrimitiveType &val)
Use a printf-style formatter for a primitive.
word & replace_ext(const word &ending)
Remove extension (if any) and append a new one.
Definition wordI.H:184
word()=default
Default construct.
static bool valid(char c)
Is this character valid for a word?
Definition wordI.H:52
word ext() const
Return file name extension (part after last .).
Definition wordI.H:171
word & operator=(const word &s)
Copy assignment, no character validation required.
Definition wordI.H:207
static int debug
Debugging.
Definition word.H:79
void stripInvalid()
Strip invalid characters from this word.
Definition wordI.H:137
word lessExt() const
Return word without extension (part before last .).
Definition wordI.H:192
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
constexpr bool isspace(char c) noexcept
Test for whitespace (C-locale only).
Definition char.H:77