1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011 OpenFOAM Foundation
Copyright (C) 2017-2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "word.H"
#include "debug.H"
#include <cctype>
#include <cstdint>
#include <sstream>
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
const char* const Foam::word::typeName = "word";
int Foam::word::debug(Foam::debug::debugSwitch(word::typeName, 0));
const Foam::word Foam::word::null;
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
Foam::word Foam::word::validate(const std::string& s, const bool prefix)
{
word out;
out.resize(s.size() + (prefix ? 1 : 0));
std::string::size_type len = 0;
// As per validate, but optionally detect if the first character
// is a digit, which we'd like to avoid having since this will
// cause parse issues when read back later.
for (auto iter = s.cbegin(); iter != s.cend(); ++iter)
{
const char c = *iter;
if (word::valid(c))
{
if (!len && prefix && isdigit(c))
{
// First valid character was a digit - prefix with '_'
out[len++] = '_';
}
out[len++] = c;
}
}
out.resize(len);
return out;
}
Foam::word Foam::word::validate
(
const char* first,
const char* last,
const bool prefix
)
{
std::string::size_type len = (last - first) + (prefix ? 1 : 0);
word out;
out.resize(len);
for (len=0; first != last; ++first)
{
const char c = *first;
if (word::valid(c))
{
if (!len && prefix && isdigit(c))
{
// First valid character was a digit - prefix with '_'
out[len++] = '_';
}
out[len++] = c;
}
}
out.resize(len);
return out;
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::word Foam::word::lessExt() const
{
const size_type i = find_ext();
if (i == npos)
{
return *this;
}
return substr(0, i);
}
Foam::word Foam::word::ext() const
{
return string::ext();
}
Foam::word& Foam::word::ext(const word& ending)
{
string::ext(ending);
return *this;
}
bool Foam::word::hasExt(const word& ending) const
{
return string::hasExt(ending);
}
bool Foam::word::hasExt(const wordRe& ending) const
{
return string::hasExt(ending);
}
// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
Foam::word Foam::operator&(const word& a, const word& b)
{
if (a.size())
{
if (b.size())
{
// Two non-empty words: can concatenate and perform camel case
word camelCase(a + b);
camelCase[a.size()] = char(toupper(b[0]));
return camelCase;
}
return a;
}
// Or, if the first string is empty (or both are empty)
return b;
}
// * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
Foam::word Foam::name(const void* ptr)
{
std::ostringstream buf;
buf << "0x" << std::hex << uintptr_t(ptr);
return word(buf.str(), false); // Needs no stripping
}
// ************************************************************************* //
|