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; << " type: " << typeid(cstr).name() << " len:" << len << nl;
Info<< " view: " << std::string_view(cstr) << 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; 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) Manipulators (without arguments)

View File

@ -95,10 +95,12 @@ Foam::tokenList Foam::ITstream::parse_chars
IOstreamOption streamOpt IOstreamOption streamOpt
) )
{ {
ISpanStream is(s, nbytes, streamOpt);
tokenList tokens; tokenList tokens;
if (s && nbytes > 0) // extra safety
{
ISpanStream is(s, nbytes, streamOpt);
parseStream(is, tokens); parseStream(is, tokens);
}
return tokens; return tokens;
} }
@ -107,10 +109,19 @@ Foam::tokenList Foam::ITstream::parse_chars
void Foam::ITstream::reset(const char* input, size_t nbytes) 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)); ISpanStream is(input, nbytes, static_cast<IOstreamOption>(*this));
parseStream(is, static_cast<tokenList&>(*this)); parseStream(is, static_cast<tokenList&>(*this));
ITstream::seek(0); // rewind() bypassing virtual ITstream::seek(0); // rewind() bypassing virtual
}
else
{
ITstream::seek(0); // rewind() bypassing virtual
tokenList::clear();
}
} }
@ -248,7 +259,10 @@ Foam::ITstream::ITstream
: :
ITstream(streamOpt, name) ITstream(streamOpt, name)
{ {
if (input)
{
reset(input, strlen(input)); reset(input, strlen(input));
}
} }

View File

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

View File

@ -123,20 +123,6 @@ public:
stream_type(static_cast<buffer_type*>(this)) 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 // Member Functions
@ -325,26 +311,6 @@ public:
ISpanStream(buffer.cdata(), buffer.size(), streamOpt) 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 // Member Functions
@ -402,20 +368,6 @@ public:
syncState(); 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 //- Rewind the stream, clearing any old errors
virtual void rewind() override virtual void rewind() override
{ {

View File

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

View File

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

View File

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