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 <sstream>
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 const char* const Foam::word::typeName = "word";
37 
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 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
112 
113 Foam::word Foam::operator&(const word& a, const word& b)
114 {
115  if (a.size())
116  {
117  if (b.size())
118  {
119  // Two non-empty words: can concatenate and perform camel case
120  word camelCase(a + b);
121  camelCase[a.size()] = char(toupper(b[0]));
122 
123  return camelCase;
124  }
125 
126  return a;
127  }
128 
129  // Or, if the first string is empty (or both are empty)
131  return b;
132 }
133 
134 
135 // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
136 
137 Foam::word Foam::name(const void* ptr)
138 {
139  std::ostringstream buf;
140  buf.setf(std::ios_base::hex, std::ios_base::basefield);
141 
142  buf << "0x"; // Same as setf(std::ios::showbase)
143  buf << uintptr_t(ptr);
144 
145  return word(buf.str(), false); // Needs no stripping
146 }
147 
148 
149 // ************************************************************************* //
static word validate(const std::string &s, const bool prefix=false)
Construct validated word (no invalid characters).
Definition: word.C:38
IOstream & hex(IOstream &io)
Definition: IOstream.H:548
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 INVALID.
Definition: exprTraits.C:52
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))