Commit 806b6684 authored by Mark Olesen's avatar Mark Olesen
Browse files

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
parent fa8d9727
......@@ -67,7 +67,7 @@ int main(int argc, char *argv[])
{
entry* e = dict1.add
(
Foam::name("entry%d", i),
word::printf("entry%d", i),
string("entry" + Foam::name(i))
);
entryInfo(e);
......@@ -76,7 +76,7 @@ int main(int argc, char *argv[])
{
entry* e = tmpdict.add
(
Foam::name("subentry%d", i),
word::printf("subentry%d", i),
string("subentry" + Foam::name(i))
);
entryInfo(e);
......@@ -85,7 +85,7 @@ int main(int argc, char *argv[])
{
entry* e = dict1.add
(
Foam::name("dict%d", i),
word::printf("dict%d", i),
tmpdict
);
entryInfo(e);
......
......@@ -64,14 +64,14 @@ int main(int argc, char *argv[])
// Test Foam::name with formatting string
{
word formatted = Foam::name("formatted=<%X>", 0xdeadbeef);
word formatted = word::printf("formatted=<%X>", 0xdeadbeef);
Info<<"formatted: " << formatted << nl;
}
Info<<"formatted: "
<< Foam::name("formatted not checked for validity=<%X>", 0xdeadbeef)
<< word::printf("formatted not checked for validity=<%X>", 0xdeadbeef)
<< nl
<< endl
<< endl;
Info<< "string:" << test << nl << "hash:"
......@@ -227,7 +227,7 @@ int main(int argc, char *argv[])
cout<< "\ntest Foam::name()\n";
Info<< "hash: = " << Foam::name("0x%012X", string::hash()(s2)) << endl;
Info<< "hash: = " << word::printf("0x%012X", string::hash()(s2)) << endl;
// test formatting on int
{
......@@ -236,7 +236,7 @@ int main(int argc, char *argv[])
Info<< "int " << val << " as word >"
<< Foam::name(val) << "< or "
<< Foam::name("formatted >%08d<", val) << "\n";
<< word::printf("formatted >%08d<", val) << "\n";
}
// test formatting on scalar
......@@ -244,7 +244,7 @@ int main(int argc, char *argv[])
scalar val = 3.1415926535897931;
Info<< "scalar " << val << " as word >"
<< Foam::name(val) << "< or "
<< Foam::name("formatted >%.9f<", val) << "\n";
<< word::printf("formatted >%.9f<", val) << "\n";
}
// test formatting on uint
......@@ -254,7 +254,7 @@ int main(int argc, char *argv[])
Info<< "uint64 " << val << " as word >"
<< Foam::name(val) << "< or "
<< Foam::name("formatted >%08d<", val) << "\n";
<< word::printf("formatted >%08d<", val) << "\n";
}
// test startsWith, endsWith methods
......
......@@ -155,7 +155,7 @@ int main(int argc, char *argv[])
if (withVTK && Pstream::master())
{
const word outputName =
Foam::name("forces_%06d.vtp", runTime.timeIndex());
word::printf("forces_%06d.vtp", runTime.timeIndex());
Info<<" " << outputName << endl;
......
......@@ -226,14 +226,16 @@ int main(int argc, char *argv[])
// State/response = what comes back from FEM
{
const word outputName = Foam::name("state_%06d.vtp", index);
const word outputName = word::printf("state_%06d.vtp", index);
Info<<" " << outputName << endl;
state.writeVTP(outputName, movement().axis());
}
{
const word outputName = Foam::name("geom_%06d.vtp", index);
const word outputName = word::printf("geom_%06d.vtp", index);
Info<<" " << outputName << endl;
movement().writeVTP(outputName, state, mesh, patchLst, points0);
......
......@@ -133,7 +133,7 @@ void printSourceFileAndLine
else
{
string cwdLine(line.replaceAll(cwd() + '/', ""));
string homeLine(cwdLine.replaceAll(home(), '~'));
string homeLine(cwdLine.replaceAll(home(), "~"));
os << " at " << homeLine.c_str();
}
......
......@@ -33,6 +33,14 @@ License
namespace Foam
{
// Convert a single character to a word with length 1
inline static Foam::word charToWord(char c)
{
return Foam::word(std::string(1, c), false);
}
// Adjust stream format based on the flagMask
inline static void processFlags(Istream& is, int flagMask)
{
......@@ -45,7 +53,8 @@ inline static void processFlags(Istream& is, int flagMask)
is.format(IOstream::BINARY);
}
}
}
} // End anonymous namespace
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
......@@ -315,7 +324,7 @@ Foam::Istream& Foam::UIPstream::read(token& t)
{
if (isalpha(c))
{
t = word(c);
t = charToWord(c);
return *this;
}
......
......@@ -33,6 +33,19 @@ License
// Truncate error message for readability
static const unsigned errLen = 80;
// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
namespace
{
// Convert a single character to a word with length 1
inline static Foam::word charToWord(char c)
{
return Foam::word(std::string(1, c), false);
}
} // End anonymous namespace
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
......@@ -210,7 +223,7 @@ Foam::Istream& Foam::ISstream::read(token& t)
if (read(nextC).bad())
{
// Return lone '#' as word
t = token(word(c));
t = charToWord(c);
}
else if (nextC == token::BEGIN_BLOCK)
{
......@@ -246,7 +259,7 @@ Foam::Istream& Foam::ISstream::read(token& t)
if (read(nextC).bad())
{
// Return lone '$' as word
t = token(word(c));
t = charToWord(c);
}
else if (nextC == token::BEGIN_BLOCK)
{
......
......@@ -191,7 +191,7 @@ Foam::error::operator Foam::dictionary() const
dictionary errDict;
string oneLineMessage(message());
oneLineMessage.replaceAll('\n', ' ');
oneLineMessage.replaceAll("\n", " ");
errDict.add("type", word("Foam::error"));
errDict.add("message", oneLineMessage);
......
......@@ -45,9 +45,9 @@ namespace Foam
Foam::word Foam::graph::wordify(const Foam::string& sname)
{
string wname = sname;
wname.replace(' ', '_');
wname.replace('(', '_');
wname.replace(')', "");
wname.replace(" ", "_");
wname.replace("(", "_");
wname.replace(")", "");
return word(wname);
}
......
......@@ -23,8 +23,6 @@ License
\*---------------------------------------------------------------------------*/
#include "stringOps.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
......@@ -64,18 +62,6 @@ word name(const Scalar val)
}
word name(const char* fmt, const Scalar val)
{
return stringOps::name(fmt, val);
}
word name(const std::string& fmt, const Scalar val)
{
return stringOps::name(fmt, val);
}
Scalar ScalarRead(const char* buf)
{
char* endptr = nullptr;
......
......@@ -105,17 +105,10 @@ public:
// * * * * * * * * * * * * * * * IO/Conversion * * * * * * * * * * * * * * * //
//- Return a string representation of a Scalar
//- Return a word representation of a Scalar.
// Uses stringstream instead of std::to_string for more consistent formatting.
word name(const Scalar val);
//- Return a word representation of a Scalar, using printf-style formatter.
// The representation is not checked for valid word characters.
word name(const char* fmt, const Scalar val);
//- Return a word representation of a Scalar, using printf-style formatter.
// The representation is not checked for valid word characters.
word name(const std::string& fmt, const Scalar val);
//- Parse entire buffer as a float/double, skipping leading/trailing whitespace.
// \return Parsed value or FatalIOError on any problem
Scalar ScalarRead(const char* buf);
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2014-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -24,21 +24,6 @@ License
\*---------------------------------------------------------------------------*/
#include "int32.H"
#include "stringOps.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Foam::word Foam::name(const char* fmt, const int32_t val)
{
return stringOps::name(fmt, val);
}
Foam::word Foam::name(const std::string& fmt, const int32_t val)
{
return stringOps::name(fmt, val);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -62,16 +62,6 @@ inline word name(const int32_t val)
}
//- Return a word representation of an int32, using printf-style formatter.
// The representation is not checked for valid word characters.
word name(const char* fmt, const int32_t val);
//- Return a word representation of an int32, using printf-style formatter.
// The representation is not checked for valid word characters.
word name(const std::string& fmt, const int32_t val);
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
//- Read int32_t from stream
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2014-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -24,21 +24,6 @@ License
\*---------------------------------------------------------------------------*/
#include "int64.H"
#include "stringOps.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Foam::word Foam::name(const char* fmt, const int64_t val)
{
return stringOps::name(fmt, val);
}
Foam::word Foam::name(const std::string& fmt, const int64_t val)
{
return stringOps::name(fmt, val);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -63,16 +63,6 @@ inline word name(const int64_t val)
}
//- Return a word representation of an int64, using printf-style formatter.
// The representation is not checked for valid word characters.
word name(const char* fmt, const int64_t);
//- Return a word representation of an int64, using printf-style formatter.
// The representation is not checked for valid word characters.
word name(const std::string& fmt, const int64_t);
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
//- Read int64_t from stream
......
......@@ -24,21 +24,6 @@ License
\*---------------------------------------------------------------------------*/
#include "uint32.H"
#include "stringOps.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Foam::word Foam::name(const char* fmt, const uint32_t val)
{
return stringOps::name(fmt, val);
}
Foam::word Foam::name(const std::string& fmt, const uint32_t val)
{
return stringOps::name(fmt, val);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -62,16 +62,6 @@ inline word name(const uint32_t val)
}
//- Return a word representation of a uint32, using printf-style formatter.
// The representation is not checked for valid word characters.
word name(const char* fmt, const uint32_t);
//- Return a word representation of a uint32, using printf-style formatter.
// The representation is not checked for valid word characters.
word name(const std::string& fmt, const uint32_t);
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
//- Read uint32_t from stream
......
......@@ -24,21 +24,6 @@ License
\*---------------------------------------------------------------------------*/
#include "uint64.H"
#include "stringOps.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Foam::word Foam::name(const char* fmt, const uint64_t val)
{
return stringOps::name(fmt, val);
}
Foam::word Foam::name(const std::string& fmt, const uint64_t val)
{
return stringOps::name(fmt, val);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -62,16 +62,6 @@ inline word name(const uint64_t val)
}
//- Return a word representation of a uint64_t, using printf-style formatter.
// The representation is not checked for valid word characters.
word name(const char* fmt, const uint64_t);
//- Return a word representation of a uint64_t, using printf-style formatter.
// The representation is not checked for valid word characters.
word name(const std::string& fmt, const uint64_t);
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
//- Read uint64_t from stream.
......
......@@ -42,8 +42,7 @@ inline void Foam::hashedWordList::rehash(const bool unique)
inline Foam::hashedWordList::hashedWordList()
:
List<word>(),
indices_()
List<word>()
{}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment