mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: provide formatting version of Foam::name() (issue #253)
- there are some cases in which the C-style sprintf is much more
convenient, albeit problematic for buffer overwrites.
Provide a formatting version of Foam::name() for language
primitives that is buffer-safe.
Returns a Foam::word, so that further output will be unquoted, but
without any checking that the characters are indeed entirely valid
word characters.
Example use,
i = 1234;
s = Foam::name("%08d", i);
produces '00001234'
Alternative using string streams:
std::ostringstream buf;
buf.fill('0');
buf << setw(8) << i;
s = buf.str();
Note that the format specification can also be slightly more complex:
Foam::name("output%08d.vtk", i);
Foam::name("timing=%.2fs", time);
It remains the caller's responsibility to ensure that the format mask
is valid.
This commit is contained in:
@ -3,7 +3,7 @@
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation
|
||||
\\/ M anipulation |
|
||||
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -36,6 +36,7 @@ SourceFiles
|
||||
#define stringOps_H
|
||||
|
||||
#include "string.H"
|
||||
#include "word.H"
|
||||
#include "dictionary.H"
|
||||
#include "HashTable.H"
|
||||
|
||||
@ -292,6 +293,21 @@ namespace stringOps
|
||||
string& inplaceTrim(string&);
|
||||
|
||||
|
||||
//- Return a word representation of the primitive,
|
||||
// using printf-style formatter.
|
||||
// The representation is not checked for valid word characters -
|
||||
// it is assumed that the caller knows what they are doing
|
||||
template<class PrimitiveType>
|
||||
Foam::word name(const char* fmt, const PrimitiveType& val);
|
||||
|
||||
//- Return a word representation of the primitive,
|
||||
// using printf-style formatter.
|
||||
// The representation is not checked for valid word characters -
|
||||
// it is assumed that the caller knows what they are doing
|
||||
template<class PrimitiveType>
|
||||
Foam::word name(const std::string& fmt, const PrimitiveType& val);
|
||||
|
||||
|
||||
} // End namespace stringOps
|
||||
|
||||
|
||||
@ -299,6 +315,13 @@ namespace stringOps
|
||||
|
||||
} // End namespace Foam
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
#ifdef NoRepository
|
||||
#include "stringOpsTemplates.C"
|
||||
#endif
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user