ENH: interface consistency in CStringList

- in preparation for broadcasting versions
This commit is contained in:
Mark Olesen
2022-03-11 10:24:47 +01:00
parent 71772f17ab
commit e59ca1226e
4 changed files with 56 additions and 50 deletions

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017 OpenCFD Ltd.
Copyright (C) 2017-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -34,10 +34,15 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const CStringList& list)
{
const int n = list.size();
for (int i = 0; i<n; ++i)
for (int i = 0, space = 0; i < n; ++i)
{
if (i) os << ' ';
if (list[i]) os << list[i];
const char* p = list.get(i);
if (p && *p)
{
if (space++) os << ' ';
os << p;
}
}
return os;

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2021 OpenCFD Ltd.
Copyright (C) 2016-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -48,8 +48,8 @@ Description
\*---------------------------------------------------------------------------*/
#ifndef CStringList_H
#define CStringList_H
#ifndef Foam_CStringList_H
#define Foam_CStringList_H
#include "fileNameList.H"
#include "stringList.H"
@ -76,7 +76,7 @@ class CStringList
//- Overall length of the raw content
// Does not include the final nul-character
size_t len_;
size_t nbytes_;
//- List of strings, including trailing nullptr
char** argv_;
@ -90,9 +90,8 @@ class CStringList
//- Copy string characters into dest as NUL-terminated string.
// Forces conversion of std::sub_match to string form
//
// \return the location one-past the end of dest, which is used
// for the next destination
static inline char* stringCopy(char *dest, const std::string& str);
// \return location one-past end of dest (ie, the next destination)
static inline char* stringCopy(char *dest, const std::string& src);
//- Copy the input list of strings.
// \return number of arguments (argc)
@ -112,7 +111,7 @@ public:
// Constructors
//- Default construct, adding content later (via reset).
inline CStringList();
inline constexpr CStringList() noexcept;
//- Copy construct from a list of strings
// Copies the input characters.
@ -138,15 +137,31 @@ public:
// Access
//- True if the size is zero.
//- True if the size (ie, argc) is zero.
inline bool empty() const noexcept;
//- Return the number of C-strings (ie, argc)
inline int size() const noexcept;
//- The flattened character content, with interspersed nul-chars
const char* cdata_bytes() const noexcept { return data_; }
//- Overall length of the flattened character (data) content
//- including interspersed nul-chars but not the trailing nul-char
size_t size_bytes() const noexcept { return nbytes_; }
//- Same as cdata_bytes()
const char* data() const noexcept { return data_; }
//- Same as size_bytes()
size_t length() const noexcept { return nbytes_; }
//- Return string element at the given index. No bounds checking.
const char* get(int i) const;
//- Return the list of C-strings (ie, argv)
// The position at argc is a nullptr
inline char** strings() const;
inline char** strings() const noexcept;
//- Return the sublist of C-strings (ie, argv) starting at the
//- specified offset.
@ -154,13 +169,6 @@ public:
inline char** strings(int start) const;
//- Overall length of the flattened character (data) content
inline size_t length() const;
//- The flattened character content, with interspersed nul-chars
inline const char* data() const;
// Edit
//- Clear contents and free memory

View File

@ -27,11 +27,13 @@ License
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
inline char* Foam::CStringList::stringCopy(char *dest, const std::string& str)
inline char* Foam::CStringList::stringCopy(char *dest, const std::string& src)
{
for (auto iter = str.cbegin(); iter != str.cend(); ++iter)
// Like string::copy() but without extra checks
const size_t len = src.length();
for (size_t i = 0; i < len; ++i)
{
*dest = *iter;
*dest = src[i];
++dest;
}
*(dest++) = '\0';
@ -58,10 +60,10 @@ inline int Foam::CStringList::count(const char * const argv[])
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
inline Foam::CStringList::CStringList()
inline constexpr Foam::CStringList::CStringList() noexcept
:
argc_(0),
len_(0),
nbytes_(0),
argv_(nullptr),
data_(nullptr)
{}
@ -98,7 +100,7 @@ inline Foam::CStringList::~CStringList()
inline void Foam::CStringList::clear()
{
argc_ = 0;
len_ = 0;
nbytes_ = 0;
if (data_)
{
@ -125,13 +127,7 @@ inline int Foam::CStringList::size() const noexcept
}
inline size_t Foam::CStringList::length() const
{
return len_;
}
inline char** Foam::CStringList::strings() const
inline char** Foam::CStringList::strings() const noexcept
{
return argv_;
}
@ -143,12 +139,6 @@ inline char** Foam::CStringList::strings(int start) const
}
inline const char* Foam::CStringList::data() const
{
return data_;
}
template<class StringType>
inline int Foam::CStringList::reset(const UList<StringType>& input)
{
@ -165,6 +155,12 @@ inline int Foam::CStringList::reset(const SubStrings<StringType>& input)
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
inline const char* Foam::CStringList::get(int i) const
{
return argv_[i];
}
inline const char* Foam::CStringList::operator[](int i) const
{
return argv_[i];

View File

@ -30,10 +30,7 @@ License
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
template<class ListType>
int Foam::CStringList::resetContent
(
const ListType& input
)
int Foam::CStringList::resetContent(const ListType& input)
{
clear();
@ -48,12 +45,12 @@ int Foam::CStringList::resetContent
// Count overall required string length, including each trailing nul char
for (const auto& str : input)
{
len_ += str.length() + 1;
nbytes_ += str.length() + 1;
}
--len_; // No final nul in overall count
--nbytes_; // Do not include final nul char in overall count
argv_ = new char*[input.size()+1]; // Extra +1 for terminating nullptr
data_ = new char[len_+1]; // Extra +1 for terminating nul char
data_ = new char[nbytes_+1]; // Extra +1 for terminating nul char
argv_[0] = data_; // Starts here
@ -75,14 +72,14 @@ template<class StringType>
Foam::List<StringType>
Foam::CStringList::asList(int argc, const char * const argv[])
{
List<StringType> lst(argc);
List<StringType> list(argc);
for (int i=0; i < argc; ++i)
{
lst[i] = argv[i];
list[i] = argv[i];
}
return lst;
return list;
}