Commit 60c31415 authored by Mark Olesen's avatar Mark Olesen Committed by Andrew Heather
Browse files

ENH: ensure self-assignment and self-swapping are a no-op for string types

- simplifies their use when reordering lists etc.
  (word, fileName, keyType, wordRe)

- "unfriend" IO operators for string types. They require no internal access

- add compile/uncompile methods to keyType for symmetry with wordRe

- when outputting keyType/wordRe, be more explicit about them using
  writeQuoted()
parent df35627e
......@@ -84,7 +84,7 @@ public:
// - range: '[', ']' \n
//
// \note The presence of '{', '}' regex bounds is not considered
inline static bool meta(const char c);
inline static bool meta(char c);
// Constructors
......
......@@ -28,7 +28,7 @@ License
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
inline bool Foam::regExp::meta(const char c)
inline bool Foam::regExp::meta(char c)
{
return
(
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2016 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation
......@@ -35,9 +35,10 @@ License
void Foam::Ostream::decrIndent()
{
if (indentLevel_ == 0)
if (!indentLevel_)
{
cerr<< "Ostream::decrIndent() : attempt to decrement 0 indent level"
std::cerr
<< "Ostream::decrIndent() : attempt to decrement 0 indent level"
<< std::endl;
}
else
......@@ -56,17 +57,17 @@ Foam::Ostream& Foam::Ostream::write(const keyType& kw)
Foam::Ostream& Foam::Ostream::writeKeyword(const keyType& kw)
{
indent();
write(kw);
writeQuoted(kw, kw.isPattern());
label nSpaces = entryIndentation_ - label(kw.size());
// pattern is surrounded by quotes
// Account for quotes surrounding pattern
if (kw.isPattern())
{
nSpaces -= 2;
}
// could also increment by indentSize_ ...
// Could also increment by indentSize_ ...
if (nSpaces < 1)
{
nSpaces = 1;
......@@ -81,9 +82,9 @@ Foam::Ostream& Foam::Ostream::writeKeyword(const keyType& kw)
}
Foam::Ostream& Foam::Ostream::beginBlock(const keyType& keyword)
Foam::Ostream& Foam::Ostream::beginBlock(const keyType& kw)
{
indent(); write(keyword); write('\n');
indent(); writeQuoted(kw, kw.isPattern()); write('\n');
beginBlock();
return *this;
......
......@@ -185,7 +185,7 @@ public:
//- Write begin block group with the given name
// Increments indentation, adds newline.
virtual Ostream& beginBlock(const keyType& keyword);
virtual Ostream& beginBlock(const keyType& kw);
//- Write begin block group without a name
// Increments indentation, adds newline.
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation
......@@ -55,15 +55,6 @@ SourceFiles
namespace Foam
{
// Forward declaration of classes
class Ostream;
// Forward declaration of friend functions and operators
class SHA1;
class SHA1Digest;
Ostream& operator<<(Ostream&, const SHA1&);
/*---------------------------------------------------------------------------*\
Class SHA1 Declaration
\*---------------------------------------------------------------------------*/
......@@ -91,6 +82,7 @@ class SHA1
//- The input processing buffer
uint32_t buffer_[32];
// Private Member Functions
//- Process data block-wise, LEN must be a multiple of 64!
......@@ -172,14 +164,13 @@ public:
//- Convert to a SHA1Digest,
// calculate current %digest from appended data
inline operator SHA1Digest() const;
};
// Friend Operators
//- Output the %digest
inline friend Ostream& operator<<(Ostream&, const SHA1&);
// IOstream Operators
};
//- Output the %digest
inline Ostream& operator<<(Ostream& os, const SHA1& sha);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -136,7 +136,7 @@ inline Foam::SHA1::operator Foam::SHA1Digest() const
inline Foam::Ostream& Foam::operator<<(Ostream& os, const SHA1& sha)
{
return os << sha.digest();
return (os << sha.digest());
}
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2017 OpenFOAM Foundation
......@@ -62,10 +62,6 @@ typedef List<word> wordList;
class wordRe;
class fileName;
Istream& operator>>(Istream&, fileName&);
Ostream& operator<<(Ostream&, const fileName&);
/*---------------------------------------------------------------------------*\
Class fileName Declaration
\*---------------------------------------------------------------------------*/
......@@ -74,12 +70,6 @@ class fileName
:
public string
{
// Private Member Functions
//- Strip invalid characters
inline void stripInvalid();
public:
//- Enumerations to handle directory entry types.
......@@ -94,7 +84,10 @@ public:
// Static data members
//- The typeName
static const char* const typeName;
//- Debugging
static int debug;
//- An empty fileName
......@@ -161,6 +154,8 @@ public:
//- that ignores duplicate or trailing slashes.
static bool equals(const std::string& s1, const std::string& s2);
//- Strip invalid characters
inline void stripInvalid();
//- Cleanup filename
//
......@@ -353,15 +348,17 @@ public:
) const;
// Member operators
// Member Operators
// Assignment
//- Copy assignment, no character validation required
fileName& operator=(const fileName&) = default;
// Self-assignment is a no-op.
inline fileName& operator=(const fileName& str);
//- Move assignment, no character validation required
fileName& operator=(fileName&&) = default;
// Self-assignment is a no-op.
inline fileName& operator=(fileName&& str);
//- Copy assignment, no character validation required
inline fileName& operator=(const word& str);
......@@ -390,13 +387,16 @@ public:
//- Append a path element with '/' separator.
// No '/' separator is added if this or the argument are empty.
fileName& operator/=(const string& other);
};
// IOstream operators
// IOstream Operators
friend Istream& operator>>(Istream& is, fileName& fn);
friend Ostream& operator<<(Ostream& os, const fileName& fn);
};
//- Read operator
Istream& operator>>(Istream& is, fileName& val);
//- Write operator
Ostream& operator<<(Ostream& os, const fileName& val);
// Global Operators
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011 OpenFOAM Foundation
......@@ -25,32 +25,7 @@ License
\*---------------------------------------------------------------------------*/
#include <iostream> // for std::cerr
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
inline void Foam::fileName::stripInvalid()
{
// Skip stripping unless debug is active (to avoid costly operations)
if (debug && string::stripInvalid<fileName>(*this))
{
std::cerr
<< "fileName::stripInvalid() called for invalid fileName "
<< this->c_str() << std::endl;
if (debug > 1)
{
std::cerr
<< " For debug level (= " << debug
<< ") > 1 this is considered fatal" << std::endl;
std::abort();
}
removeRepeated('/');
removeTrailing('/');
}
}
#include <iostream> // For std::cerr
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
......@@ -134,6 +109,29 @@ inline bool Foam::fileName::valid(char c)
}
inline void Foam::fileName::stripInvalid()
{
// Only strip when debug is active (potentially costly operation)
if (debug && string::stripInvalid<fileName>(*this))
{
std::cerr
<< "fileName::stripInvalid() called for invalid fileName "
<< this->c_str() << std::endl;
if (debug > 1)
{
std::cerr
<< " For debug level (= " << debug
<< ") > 1 this is considered fatal" << std::endl;
std::abort();
}
removeRepeated('/');
removeTrailing('/');
}
}
inline bool Foam::fileName::isAbsolute(const std::string& str)
{
return !str.empty() && str[0] == '/';
......@@ -258,6 +256,28 @@ inline Foam::fileName& Foam::fileName::ext(const word& ending)
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
inline Foam::fileName& Foam::fileName::operator=(const fileName& str)
{
// Self-assignment is a no-op
if (this != &str)
{
assign(str);
}
return *this;
}
inline Foam::fileName& Foam::fileName::operator=(fileName&& str)
{
// Self-assignment is a no-op
if (this != &str)
{
assign(std::move(str));
}
return *this;
}
inline Foam::fileName& Foam::fileName::operator=(const word& str)
{
assign(str);
......
......@@ -72,9 +72,9 @@ Foam::Istream& Foam::operator>>(Istream& is, fileName& val)
}
Foam::Ostream& Foam::operator<<(Ostream& os, const fileName& fn)
Foam::Ostream& Foam::operator<<(Ostream& os, const fileName& val)
{
os.write(fn);
os.write(val);
os.check(FUNCTION_NAME);
return os;
}
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2015 OpenFOAM Foundation
......@@ -76,12 +76,13 @@ Foam::Istream& Foam::operator>>(Istream& is, keyType& val)
if (t.isWord())
{
val = t.wordToken();
val.uncompile(); // Non-regex
}
else if (t.isString())
{
// Assign from string, treat as regular expression
val = t.stringToken();
val.isPattern_ = true;
val.compile(); // As regex
// Flag empty strings as an error
if (val.empty())
......@@ -108,9 +109,9 @@ Foam::Istream& Foam::operator>>(Istream& is, keyType& val)
}
Foam::Ostream& Foam::operator<<(Ostream& os, const keyType& kw)
Foam::Ostream& Foam::operator<<(Ostream& os, const keyType& val)
{
os.write(kw);
os.writeQuoted(val, val.isPattern());
os.check(FUNCTION_NAME);
return os;
}
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation
......@@ -48,13 +48,9 @@ SourceFiles
namespace Foam
{
// Forward declarations
class keyType;
// Forward Declarations
class Istream;
class Ostream;
Istream& operator>>(Istream& is, keyType& kw);
Ostream& operator<<(Ostream& os, const keyType& kw);
/*---------------------------------------------------------------------------*\
Class keyType Declaration
......@@ -64,9 +60,9 @@ class keyType
:
public word
{
// Private data
// Private Data
//- Is the keyType a pattern (regular expression)
//- Treat keyType as a pattern (regular expression)
bool isPattern_;
......@@ -78,13 +74,13 @@ class keyType
public:
// Static data members
// Static Data Members
//- An empty keyType
static const keyType null;
// Public data types
// Public Data Types
//- Enumeration for search/match modes as bitmask
// eg, (keyType::REGEX | keyType::RECURSIVE)
......@@ -142,18 +138,40 @@ public:
// permit brace-brackets, which are valid for some regexs.
inline static bool valid(char c);
// Access
//- The keyType is treated as literal, not as pattern.
inline bool isLiteral() const;
//- The keyType is treated as a pattern, not as literal string.
inline bool isPattern() const;
//- Swap contents
// Infrastructure
//- Mark as regular expression
inline bool compile();
//- Mark as literal, instead of a regular expression.
// Optionally strip invalid word characters.
inline void uncompile(bool doStrip = false);
// Editing
//- Clear string and set as literal
inline void clear();
//- Swap contents. Self-swapping is a no-op.
inline void swap(keyType& s);
// Matching/Searching
//- Smart match as regular expression or as a string.
// Optionally force a literal match only
bool match(const std::string& text, bool literal = false) const;
bool match(const std::string& text, bool literal=false) const;
// Member Operators
......@@ -164,9 +182,11 @@ public:
//- Copy assignment, retaining type (literal or regex)
// Self-assignment is a no-op.
inline void operator=(const keyType& s);
//- Move assignment, retaining type (literal or regex)
// Self-assignment is a no-op.
inline void operator=(keyType&& s);
//- Assign as word, treat as literal
......@@ -177,13 +197,16 @@ public:
//- Assign as word, treat as literal
inline void operator=(const char* s);
};
// IOstream Operators
// IOstream Operators
friend Istream& operator>>(Istream& is, keyType& kw);
friend Ostream& operator<<(Ostream& os, const keyType& kw);
};
//- Read operator
Istream& operator>>(Istream& is, keyType& val);
//- Write operator
Ostream& operator<<(Ostream& os, const keyType& val);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2017 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation
......@@ -130,8 +130,40 @@ inline bool Foam::keyType::isPattern() const
}
inline bool Foam::keyType::compile()
{
isPattern_ = true;
return true;
}
inline void Foam::keyType::uncompile(bool doStrip)
{
// Only strip when debug is active (potentially costly operation)
if (isPattern_ && doStrip && word::debug)
{
string::stripInvalid<word>(*this);
}
isPattern_ = false;
}
inline void Foam::keyType::clear()
{
word::clear();
isPattern_ = false;
}
inline void Foam::keyType::swap(keyType& s)
{
// Self-swapping is a no-op
if (this == &s)
{
return;
}
word::swap(static_cast<word&>(s));
std::swap(isPattern_, s.isPattern_);
}
......@@ -147,6 +179,12 @@ inline bool Foam::keyType::operator()(const std::string& text) const
inline void Foam::keyType::operator=(const keyType& s)
{
// Self-assignment is a no-op
if (this == &s)
{
return;
}
assign(s); // Bypasses char checking
isPattern_ = s.isPattern_;
}
......@@ -154,6 +192,12 @@ inline void Foam::keyType::operator=(const keyType& s)
inline void Foam::keyType::operator=(keyType&& s)
{
// Self-assignment is a no-op
if (this == &s)
{
return;
}
clear();
swap(s);
}
......
......@@ -58,20 +58,16 @@ Description
namespace Foam
{
// Forward declarations
class Ostream;
class CStringList;
// Forward Declarations
template<class String> class SubStrings;
Ostream& operator<<(Ostream& os, const CStringList& list);
/*---------------------------------------------------------------------------*\
Class CStringList Declaration
\*---------------------------------------------------------------------------*/
class CStringList
{
// Private data
// Private Data