STYLE: two-parameter Foam::name replaced by word::printf (issue #724)

- reduces some ambiguity and clarifies the expected output and
  behaviour.

STYLE: reduce some automatic conversions of char to string
This commit is contained in:
Mark Olesen
2018-02-08 12:00:54 +01:00
parent fa8d972796
commit 806b668418
36 changed files with 233 additions and 251 deletions

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2016-2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -39,6 +39,7 @@ See also
SourceFiles
string.C
stringIO.C
stringTemplates.C
\*---------------------------------------------------------------------------*/
@ -90,6 +91,24 @@ protected:
// A wrapped version of find_last_of("./") with additional logic.
inline std::string::size_type find_ext() const;
//- A printf-style formatter for a primitive.
template<class PrimitiveType>
static std::string::size_type string_printf
(
std::string& output,
const char* fmt,
const PrimitiveType& val
);
//- A printf-style formatter for a primitive.
template<class PrimitiveType>
static std::string::size_type string_printf
(
std::string& output,
const std::string& fmt,
const PrimitiveType& val
);
//- Return file name extension (part after last .)
word ext() const;
@ -128,8 +147,7 @@ public:
class hash
{
public:
hash()
{}
hash() = default;
//- Hash for string.
// Uses Foam::string instead of std::string for automatic conversions.
@ -152,9 +170,9 @@ public:
inline string(const char* str, const size_type len);
//- Construct from a single character
inline string(const char c);
inline explicit string(const char c);
//- Construct from copies of a single character
//- Construct fill copies of a single character
inline string(const size_type len, const char c);
//- Move construct from std::string
@ -292,6 +310,10 @@ public:
#include "stringI.H"
#ifdef NoRepository
#include "stringTemplates.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif

View File

@ -23,7 +23,6 @@ License
\*---------------------------------------------------------------------------*/
// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
inline std::string::size_type Foam::string::find_ext(const std::string& str)
@ -68,6 +67,8 @@ inline bool Foam::string::removeExt()
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
inline Foam::string::string()
:
std::string()
{}

View File

@ -0,0 +1,72 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
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 <cstdio>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Could also consider generalizing with C++11 variadic templates
template<class PrimitiveType>
std::string::size_type Foam::string::string_printf
(
std::string& output,
const char* fmt,
const PrimitiveType& val
)
{
// Use snprintf with zero to establish the size (without '\0') required
int n = ::snprintf(nullptr, 0, fmt, val);
if (n > 0)
{
output.resize(n+1);
char* buf = &(output[0]);
// Print directly into buffer, no stripping desired
n = ::snprintf(buf, n+1, fmt, val);
output.resize(n);
}
else
{
output.clear();
}
return output.size();
}
template<class PrimitiveType>
std::string::size_type Foam::string::string_printf
(
std::string& output,
const std::string& fmt,
const PrimitiveType& val
)
{
return string_printf(output, fmt.c_str(), val);
}
// ************************************************************************* //