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 -------------------------------------------------------------------------------
11 License
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 
33 template<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 
46 template<class PrimitiveType>
48 (
49  const std::string& fmt,
50  const PrimitiveType& val
51 )
52 {
53  word output;
54  string_printf(output, fmt, val);
55  return output;
56 }
57 
58 
59 inline 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 
76 inline Foam::word::word(const string& s, bool doStrip)
77 :
78  string(s)
79 {
80  if (doStrip)
81  {
82  stripInvalid();
83  }
84 }
85 
86 
87 inline Foam::word::word(string&& s, bool doStrip)
88 :
89  string(std::move(s))
90 {
91  if (doStrip)
92  {
93  stripInvalid();
94  }
95 }
96 
97 
98 inline 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 
109 inline Foam::word::word(const std::string& s, bool doStrip)
110 :
111  string(s)
112 {
113  if (doStrip)
114  {
115  stripInvalid();
116  }
117 }
118 
119 
120 inline Foam::word::word(const char* s, bool doStrip)
121 :
122  string(s)
123 {
124  if (doStrip)
125  {
126  stripInvalid();
127  }
128 }
129 
130 
131 inline 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 
144 inline 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 
178 inline Foam::word Foam::word::ext() const
179 {
180  return string::ext();
181 }
182 
183 
184 inline Foam::word& Foam::word::ext(const word& ending)
185 {
186  string::ext(ending);
187  return *this;
188 }
189 
190 
191 inline Foam::word& Foam::word::replace_ext(const word& ending)
192 {
194  string::ext(ending);
195  return *this;
196 }
197 
198 
199 inline 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 
214 inline Foam::word& Foam::word::operator=(const word& s)
215 {
216  // Self-assignment is a no-op
217  if (this != &s)
218  {
219  assign(s);
220  }
221  return *this;
222 }
223 
224 
225 inline Foam::word& Foam::word::operator=(word&& s)
226 {
227  // Self-assignment is a no-op
228  if (this != &s)
229  {
230  assign(std::move(s));
231  }
232  return *this;
233 }
234 
235 
236 inline Foam::word& Foam::word::operator=(const string& s)
237 {
238  assign(s);
239  stripInvalid();
240  return *this;
241 }
242 
243 
244 inline Foam::word& Foam::word::operator=(string&& s)
245 {
246  assign(std::move(s));
247  stripInvalid();
248  return *this;
249 }
250 
251 
252 inline Foam::word& Foam::word::operator=(const std::string& s)
253 {
254  assign(s);
255  stripInvalid();
256  return *this;
257 }
258 
259 
260 inline Foam::word& Foam::word::operator=(std::string&& s)
261 {
262  assign(std::move(s));
263  stripInvalid();
264  return *this;
265 }
266 
267 
268 inline Foam::word& Foam::word::operator=(const char* s)
269 {
270  assign(s);
271  stripInvalid();
272  return *this;
273 }
274 
275 
276 // ************************************************************************* //
word()=default
Default construct.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
static std::string::size_type string_printf(std::string &output, const char *fmt, const PrimitiveType &val)
A printf-style formatter for a primitive.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
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
A class for handling words, derived from Foam::string.
Definition: word.H:63
word ext() const
Return file name extension (part after last .)
Definition: string.C:38
word lessExt() const
Return word without extension (part before last .)
Definition: wordI.H:192
void stripInvalid()
Strip invalid characters from this word.
Definition: wordI.H:137
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:68
static bool valid(char c)
Is this character valid for a word?
Definition: wordI.H:52
static word printf(const char *fmt, const PrimitiveType &val)
Use a printf-style formatter for a primitive.
void assign(Field< Tout > &result, const Field< T1 > &a, const UnaryOp &op)
Populate a field as the result of a unary operation on an input.
Definition: FieldOps.C:28
int debug
Static debugging option.
const dimensionedScalar c
Speed of light in a vacuum.
bool isspace(char c) noexcept
Test for whitespace (C-locale)
Definition: char.H:69
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:44
word & replace_ext(const word &ending)
Remove extension (if any) and append a new one.
Definition: wordI.H:184
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))
A class for handling character strings derived from std::string.
Definition: string.H:72
bool remove_ext()
Remove extension, return true if string changed.
Definition: stringI.H:93