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