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-2019 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 <cctype>
30 #include <iostream> // For std::cerr
31 
32 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
33 
34 template<class PrimitiveType>
36 (
37  const char* fmt,
38  const PrimitiveType& val
39 )
40 {
41  word output;
42  string_printf(output, fmt, val);
43  return output;
44 }
45 
46 
47 template<class PrimitiveType>
49 (
50  const std::string& fmt,
51  const PrimitiveType& val
52 )
53 {
54  word output;
55  string_printf(output, fmt, val);
56  return output;
57 }
58 
59 
60 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
61 
62 inline Foam::word::word(const string& s, bool doStrip)
63 :
64  string(s)
65 {
66  if (doStrip)
67  {
68  stripInvalid();
69  }
70 }
71 
72 
73 inline Foam::word::word(string&& s, bool doStrip)
74 :
75  string(std::move(s))
76 {
77  if (doStrip)
78  {
79  stripInvalid();
80  }
81 }
82 
83 
84 inline Foam::word::word(std::string&& s, bool doStrip)
85 :
86  string(std::move(s))
87 {
88  if (doStrip)
89  {
90  stripInvalid();
91  }
92 }
93 
94 
95 inline Foam::word::word(const std::string& s, bool doStrip)
96 :
97  string(s)
98 {
99  if (doStrip)
100  {
101  stripInvalid();
102  }
103 }
104 
105 
106 inline Foam::word::word(const char* s, bool doStrip)
107 :
108  string(s)
109 {
110  if (doStrip)
111  {
112  stripInvalid();
113  }
114 }
115 
116 
117 inline Foam::word::word(const char* s, size_type len, bool doStrip)
118 :
119  string(s, len)
120 {
121  if (doStrip)
122  {
123  stripInvalid();
124  }
125 }
126 
127 
128 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
129 
130 inline bool Foam::word::valid(char c)
131 {
132  return
133  (
134  !isspace(c)
135  && c != '"' // string quote
136  && c != '\'' // string quote
137  && c != '/' // path separator
138  && c != ';' // end statement
139  && c != '{' // beg subdict
140  && c != '}' // end subdict
141  );
142 }
143 
144 
146 {
147  // Only strip when debug is active (potentially costly operation)
148  if (debug && string::stripInvalid<word>(*this))
149  {
150  std::cerr
151  << "word::stripInvalid() called for word "
152  << this->c_str() << std::endl;
153 
154  if (debug > 1)
155  {
156  std::cerr
157  << " For debug level (= " << debug
158  << ") > 1 this is considered fatal" << std::endl;
159  std::exit(1);
160  }
161  }
162 }
163 
164 
165 inline bool Foam::word::hasExt() const
166 {
167  return string::hasExt();
168 }
169 
170 
172 {
173  return string::removeExt();
174 }
175 
176 
177 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
178 
180 {
181  // Self-assignment is a no-op
182  if (this != &s)
183  {
184  assign(s);
185  }
186  return *this;
187 }
188 
189 
191 {
192  // Self-assignment is a no-op
193  if (this != &s)
194  {
195  assign(std::move(s));
196  }
197  return *this;
198 }
199 
200 
201 inline Foam::word& Foam::word::operator=(const string& s)
202 {
203  assign(s);
204  stripInvalid();
205  return *this;
206 }
207 
208 
210 {
211  assign(std::move(s));
212  stripInvalid();
213  return *this;
214 }
215 
216 
217 inline Foam::word& Foam::word::operator=(const std::string& s)
218 {
219  assign(s);
220  stripInvalid();
221  return *this;
222 }
223 
224 
225 inline Foam::word& Foam::word::operator=(std::string&& s)
226 {
227  assign(std::move(s));
228  stripInvalid();
229  return *this;
230 }
231 
232 
233 inline Foam::word& Foam::word::operator=(const char* s)
234 {
235  assign(s);
236  stripInvalid();
237  return *this;
238 }
239 
240 
241 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::word::operator=
word & operator=(const word &s)
Copy assignment, no character validation required.
Definition: wordI.H:179
Foam::string::hasExt
bool hasExt() const
Return true if it has an extension or simply ends with a '.'.
Definition: stringI.H:56
Foam::output
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:66
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
s
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))
Definition: gmvOutputSpray.H:25
Foam::string::removeExt
bool removeExt()
Remove extension, returning true if string changed.
Definition: stringI.H:76
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:73
Foam::word::hasExt
bool hasExt() const
Return true if it has an extension or simply ends with a '.'.
Definition: wordI.H:165
Foam::word::removeExt
bool removeExt()
Remove extension, returning true if string changed.
Definition: wordI.H:171
Foam::word::stripInvalid
void stripInvalid()
Strip invalid characters from this word.
Definition: wordI.H:145
size_type
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:76
Foam::word::valid
static bool valid(char c)
Is this character valid for a word?
Definition: wordI.H:130
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::word::word
word()=default
Construct null.
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::word::printf
static word printf(const char *fmt, const PrimitiveType &val)
Use a printf-style formatter for a primitive.
Foam::FieldOps::assign
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:35
Foam::isspace
bool isspace(char c)
Test for horizontal whitespace.
Definition: char.H:63