word.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) 2011 OpenFOAM Foundation
9  Copyright (C) 2017-2021 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 "word.H"
30 #include "debug.H"
31 #include <cctype>
32 #include <sstream>
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 const char* const Foam::word::typeName = "word";
37 
38 int Foam::word::debug(Foam::debug::debugSwitch(word::typeName, 0));
39 
41 
42 
43 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
44 
45 Foam::word Foam::word::validate(const std::string& s, const bool prefix)
46 {
47  word out;
48  out.resize(s.size() + (prefix ? 1 : 0));
49 
50  std::string::size_type len = 0;
51 
52  // As per validate, but optionally detect if the first character
53  // is a digit, which we'd like to avoid having since this will
54  // cause parse issues when read back later.
55  for (auto iter = s.cbegin(); iter != s.cend(); ++iter)
56  {
57  const char c = *iter;
58 
59  if (word::valid(c))
60  {
61  if (!len && prefix && isdigit(c))
62  {
63  // First valid character was a digit - prefix with '_'
64  out[len++] = '_';
65  }
66 
67  out[len++] = c;
68  }
69  }
70 
71  out.erase(len);
72 
73  return out;
74 }
75 
76 
78 (
79  const char* first,
80  const char* last,
81  const bool prefix
82 )
83 {
84  std::string::size_type len = (last - first) + (prefix ? 1 : 0);
85 
86  word out;
87  out.resize(len);
88 
89  for (len=0; first != last; ++first)
90  {
91  const char c = *first;
92 
93  if (word::valid(c))
94  {
95  if (!len && prefix && isdigit(c))
96  {
97  // First valid character was a digit - prefix with '_'
98  out[len++] = '_';
99  }
100 
101  out[len++] = c;
102  }
103  }
104 
105  out.erase(len);
106 
107  return out;
108 }
109 
110 
111 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
112 
114 {
115  const auto i = find_ext();
116 
117  if (i == npos)
118  {
119  return *this;
120  }
121 
122  return substr(0, i);
123 }
124 
125 
127 {
128  return string::ext();
129 }
130 
131 
133 {
134  string::ext(ending);
135  return *this;
136 }
137 
138 
139 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
140 
142 {
143  if (a.size())
144  {
145  if (b.size())
146  {
147  // Two non-empty words: can concatenate and perform camel case
148  word camelCase(a + b);
149  camelCase[a.size()] = char(toupper(b[0]));
150 
151  return camelCase;
152  }
153 
154  return a;
155  }
156 
157  // Or, if the first string is empty (or both are empty)
158 
159  return b;
160 }
161 
162 
163 // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
164 
165 Foam::word Foam::name(const void* ptr)
166 {
167  std::ostringstream buf;
168  buf << "0x" << std::hex << uintptr_t(ptr);
169 
170  return word(buf.str(), false); // Needs no stripping
171 }
172 
173 
174 // ************************************************************************* //
Foam::word::lessExt
word lessExt() const
Return word without extension (part before last .)
Definition: word.C:113
hex
const cellModel & hex
Definition: createBlockMesh.H:1
debug.H
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
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::operator&
tmp< GeometricField< Type, fvPatchField, volMesh > > operator&(const fvMatrix< Type > &, const DimensionedField< Type, volMesh > &)
Foam::debug::debugSwitch
int debugSwitch(const char *name, const int deflt=0)
Lookup debug switch or add default value.
Definition: debug.C:225
Foam::word::validate
static word validate(const std::string &s, const bool prefix=false)
Construct validated word (no invalid characters).
Definition: word.C:45
Foam::word::ext
word ext() const
Return file name extension (part after last .)
Definition: word.C:126
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::string::ext
word ext() const
Return file name extension (part after last .)
Definition: string.C:45
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:59
Foam::word::typeName
static const char *const typeName
The typeName.
Definition: word.H:74
Foam::word::debug
static int debug
Debugging.
Definition: word.H:77
Foam::word::null
static const word null
An empty word.
Definition: word.H:80
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
word.H