ENH: increase some string_view coverage

- remove some stdFoam::span<char> handling, which was just a stop-gap
  measure
This commit is contained in:
Mark Olesen
2025-09-29 10:18:11 +02:00
parent 9223d238bd
commit 2396828a60
8 changed files with 94 additions and 150 deletions

View File

@ -59,11 +59,6 @@ int main(int argc, char *argv[])
<< " type: " << typeid(cstr).name() << " len:" << len << nl;
Info<< " view: " << std::string_view(cstr) << nl;
Info<< " span: "
<< stdFoam::span<const char>(cstr, len) << nl;
Info<< " span: "
<< stdFoam::span<char>(const_cast<char*>(cstr), len) << nl;
}
}

View File

@ -341,22 +341,6 @@ inline Ostream& operator<<(Ostream& os, std::string_view s)
return os;
}
//- Write operator for character span. Output like string data
inline Ostream& operator<<(Ostream& os, stdFoam::span<char> s)
{
os.writeQuoted(s.data(), s.size(), true); // quoted
os.check("Foam::operator<<(Ostream&, stdFoam::span<char>)");
return os;
}
//- Write operator for const character span. Output like string data
inline Ostream& operator<<(Ostream& os, stdFoam::span<const char> s)
{
os.writeQuoted(s.data(), s.size(), true); // quoted
os.check("Foam::operator<<(Ostream&, stdFoam::span<const char>)");
return os;
}
/*---------------------------------------------------------------------------*\
Manipulators (without arguments)

View File

@ -95,10 +95,12 @@ Foam::tokenList Foam::ITstream::parse_chars
IOstreamOption streamOpt
)
{
ISpanStream is(s, nbytes, streamOpt);
tokenList tokens;
if (s && nbytes > 0) // extra safety
{
ISpanStream is(s, nbytes, streamOpt);
parseStream(is, tokens);
}
return tokens;
}
@ -106,12 +108,21 @@ Foam::tokenList Foam::ITstream::parse_chars
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
void Foam::ITstream::reset(const char* input, size_t nbytes)
{
tokenList tokens;
if (input && nbytes > 0) // extra safety
{
ISpanStream is(input, nbytes, static_cast<IOstreamOption>(*this));
parseStream(is, static_cast<tokenList&>(*this));
ITstream::seek(0); // rewind() bypassing virtual
}
else
{
ITstream::seek(0); // rewind() bypassing virtual
tokenList::clear();
}
}
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
@ -247,9 +258,12 @@ Foam::ITstream::ITstream
)
:
ITstream(streamOpt, name)
{
if (input)
{
reset(input, strlen(input));
}
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //

View File

@ -77,7 +77,8 @@ class ITstream
// but leave any excess capacity (ie, like reserve).
void reserveCapacity(const label newCapacity);
//- Convert input sequence into a list of tokens,
//- Convert input sequence into a list of tokens.
// Includes nullptr guard
static tokenList parse_chars
(
const char* s,
@ -87,6 +88,7 @@ class ITstream
//- Convert input sequence into a list of tokens,
//- using the existing stream format. Rewinds the stream
// Includes nullptr guard
void reset(const char* input, size_t nbytes);
//- Failsafe read-access to token at specified location
@ -175,32 +177,6 @@ public:
reset(s.data(), s.size());
}
//- Construct token list by parsing the input character sequence
// Uses static parse function internally.
explicit ITstream
(
stdFoam::span<char> s,
IOstreamOption streamOpt = IOstreamOption()
)
:
ITstream(streamOpt)
{
reset(s.data(), s.size());
}
//- Construct token list by parsing the input character sequence
// Uses static parse function internally.
explicit ITstream
(
stdFoam::span<const char> s,
IOstreamOption streamOpt = IOstreamOption()
)
:
ITstream(streamOpt)
{
reset(s.data(), s.size());
}
// Additional constructors
@ -255,17 +231,6 @@ public:
return parse_chars(input.cdata(), input.size(), streamOpt);
}
//- Create token list by parsing the input string
//- until no good tokens remain.
static tokenList parse
(
const std::string& input,
IOstreamOption streamOpt = IOstreamOption()
)
{
return parse_chars(input.data(), input.size(), streamOpt);
}
//- Create token list by parsing the input character sequence
//- until no good tokens remain.
static tokenList parse
@ -273,9 +238,16 @@ public:
const char* input,
IOstreamOption streamOpt = IOstreamOption()
)
{
if (input)
{
return parse_chars(input, strlen(input), streamOpt);
}
else
{
return tokenList();
}
}
//- Create token list by parsing the input character sequence
//- until no good tokens remain.
@ -288,28 +260,6 @@ public:
return parse_chars(s.data(), s.size(), streamOpt);
}
//- Create token list by parsing the input character sequence
//- until no good tokens remain.
static tokenList parse
(
stdFoam::span<char> s,
IOstreamOption streamOpt = IOstreamOption()
)
{
return parse_chars(s.data(), s.size(), streamOpt);
}
//- Create token list by parsing the input character sequence
//- until no good tokens remain.
static tokenList parse
(
stdFoam::span<const char> s,
IOstreamOption streamOpt = IOstreamOption()
)
{
return parse_chars(s.data(), s.size(), streamOpt);
}
// Member Functions
@ -387,7 +337,7 @@ public:
label& tokenIndex() noexcept { return tokenIndex_; }
//- Set the token index (no checks). \return the previous value
label tokenIndex(const label num) noexcept
label tokenIndex(label num) noexcept
{
label old(tokenIndex_);
tokenIndex_ = num;

View File

@ -123,20 +123,6 @@ public:
stream_type(static_cast<buffer_type*>(this))
{}
//- Construct (shallow copy) from span character content
explicit ispanstream(stdFoam::span<char> s)
:
buffer_type(const_cast<char*>(s.data()), s.size()),
stream_type(static_cast<buffer_type*>(this))
{}
//- Construct (shallow copy) from span character content
explicit ispanstream(stdFoam::span<const char> s)
:
buffer_type(const_cast<char*>(s.data()), s.size()),
stream_type(static_cast<buffer_type*>(this))
{}
// Member Functions
@ -325,26 +311,6 @@ public:
ISpanStream(buffer.cdata(), buffer.size(), streamOpt)
{}
//- Construct (shallow copy) from span character content
explicit ISpanStream
(
stdFoam::span<const char> s,
IOstreamOption streamOpt = IOstreamOption()
)
:
ISpanStream(s.data(), s.size(), streamOpt)
{}
//- Construct (shallow copy) from span character content
explicit ISpanStream
(
stdFoam::span<char> s,
IOstreamOption streamOpt = IOstreamOption()
)
:
ISpanStream(s.data(), s.size(), streamOpt)
{}
// Member Functions
@ -402,20 +368,6 @@ public:
syncState();
}
//- Reset input area to use data from span character content
void reset(stdFoam::span<char> s)
{
stream_.reset(s.data(), s.size());
syncState();
}
//- Reset input area to use data from span character content
void reset(stdFoam::span<const char> s)
{
stream_.reset(s.data(), s.size());
syncState();
}
//- Rewind the stream, clearing any old errors
virtual void rewind() override
{

View File

@ -58,6 +58,8 @@ Description
#include <algorithm> // std::copy
#include <utility> // std::initializer_list
#include <vector>
#include <string_view>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -93,6 +95,11 @@ class CStringList
// \return location one-past end of dest (ie, the next destination)
static inline char* stringCopy(char* dest, const char* src);
//- Copy string characters into dest as NUL-terminated string.
//
// \return location one-past end of dest (ie, the next destination)
static inline char* stringCopy(char *dest, std::string_view src);
//- Copy string characters into dest as NUL-terminated string.
// Forces conversion of std::sub_match to string form
//
@ -133,9 +140,17 @@ public:
template<class StringType>
inline explicit CStringList(const UList<StringType>& input);
//- Copy construct from a list of string_views
// Copies the input characters.
inline explicit CStringList(const UList<std::string_view>& input);
//- Copy construct from a list of string_views
// Copies the input characters.
inline explicit CStringList(const std::vector<std::string_view>& input);
//- Copy construct from a list of sub-string references
// Copies the input characters.
explicit CStringList(const SubStrings& input);
inline explicit CStringList(const SubStrings& input);
//- Destructor. Invokes clear() to free memory.
@ -157,6 +172,9 @@ public:
//- Return the number of C-strings (ie, argc)
int size() const noexcept { return argc_; }
//- The flattened character content, with interspersed nul-chars
inline std::string_view view() const;
//- The flattened character content, with interspersed nul-chars
const char* cdata_bytes() const noexcept { return data_; }

View File

@ -28,16 +28,6 @@ License
#include "CStringList.H"
#include "Ostream.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::CStringList::CStringList(const SubStrings& input)
:
CStringList()
{
resetContent(input);
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
// Largely identical to resetContent() except with 'c-string'

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2024 OpenCFD Ltd.
Copyright (C) 2016-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -35,6 +35,14 @@ inline char* Foam::CStringList::stringCopy(char* dest, const char* src)
}
inline char* Foam::CStringList::stringCopy(char *dest, std::string_view src)
{
dest = std::copy_n(src.data(), src.size(), dest);
*(dest++) = '\0';
return dest;
}
inline char* Foam::CStringList::stringCopy(char *dest, const std::string& src)
{
dest = std::copy_n(src.data(), src.size(), dest);
@ -90,6 +98,33 @@ inline Foam::CStringList::CStringList(const UList<StringType>& input)
}
inline Foam::CStringList::CStringList(const UList<std::string_view>& input)
:
CStringList()
{
resetContent(input);
}
inline Foam::CStringList::CStringList
(
const std::vector<std::string_view>& input
)
:
CStringList()
{
resetContent(input);
}
inline Foam::CStringList::CStringList(const SubStrings& input)
:
CStringList()
{
resetContent(input);
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
inline Foam::CStringList::~CStringList()
@ -100,6 +135,12 @@ inline Foam::CStringList::~CStringList()
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
inline std::string_view Foam::CStringList::view() const
{
return std::string_view(data_, nbytes_);
}
inline void Foam::CStringList::clear()
{
argc_ = 0;