From 2396828a60097eb3401c790c58685de1182e735c Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Mon, 29 Sep 2025 10:18:11 +0200 Subject: [PATCH] ENH: increase some string_view coverage - remove some stdFoam::span handling, which was just a stop-gap measure --- .../test/string_view1/Test-string_view1.cxx | 5 -- src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H | 16 ---- src/OpenFOAM/db/IOstreams/Tstreams/ITstream.C | 28 +++++-- src/OpenFOAM/db/IOstreams/Tstreams/ITstream.H | 74 +++---------------- .../db/IOstreams/memory/ISpanStream.H | 48 ------------ .../primitives/strings/lists/CStringList.H | 20 ++++- .../primitives/strings/lists/CStringList.cxx | 10 --- .../primitives/strings/lists/CStringListI.H | 43 ++++++++++- 8 files changed, 94 insertions(+), 150 deletions(-) diff --git a/applications/test/string_view1/Test-string_view1.cxx b/applications/test/string_view1/Test-string_view1.cxx index bbb24dc8fe..87c420ea80 100644 --- a/applications/test/string_view1/Test-string_view1.cxx +++ b/applications/test/string_view1/Test-string_view1.cxx @@ -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(cstr, len) << nl; - Info<< " span: " - << stdFoam::span(const_cast(cstr), len) << nl; } } diff --git a/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H b/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H index beb8687bb1..38c09dd2ee 100644 --- a/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H +++ b/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H @@ -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 s) -{ - os.writeQuoted(s.data(), s.size(), true); // quoted - os.check("Foam::operator<<(Ostream&, stdFoam::span)"); - return os; -} - -//- Write operator for const character span. Output like string data -inline Ostream& operator<<(Ostream& os, stdFoam::span s) -{ - os.writeQuoted(s.data(), s.size(), true); // quoted - os.check("Foam::operator<<(Ostream&, stdFoam::span)"); - return os; -} - /*---------------------------------------------------------------------------*\ Manipulators (without arguments) diff --git a/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.C b/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.C index a487f0fe21..a4a3df399a 100644 --- a/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.C +++ b/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.C @@ -95,10 +95,12 @@ Foam::tokenList Foam::ITstream::parse_chars IOstreamOption streamOpt ) { - ISpanStream is(s, nbytes, streamOpt); - tokenList tokens; - parseStream(is, tokens); + if (s && nbytes > 0) // extra safety + { + ISpanStream is(s, nbytes, streamOpt); + parseStream(is, tokens); + } return tokens; } @@ -107,10 +109,19 @@ Foam::tokenList Foam::ITstream::parse_chars void Foam::ITstream::reset(const char* input, size_t nbytes) { - ISpanStream is(input, nbytes, static_cast(*this)); + tokenList tokens; + if (input && nbytes > 0) // extra safety + { + ISpanStream is(input, nbytes, static_cast(*this)); - parseStream(is, static_cast(*this)); - ITstream::seek(0); // rewind() bypassing virtual + parseStream(is, static_cast(*this)); + 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) { - reset(input, strlen(input)); + if (input) + { + reset(input, strlen(input)); + } } diff --git a/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.H b/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.H index cf2e2239bb..d0c127bb53 100644 --- a/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.H +++ b/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.H @@ -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 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 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 @@ -274,7 +239,14 @@ public: IOstreamOption streamOpt = IOstreamOption() ) { - return parse_chars(input, strlen(input), streamOpt); + if (input) + { + return parse_chars(input, strlen(input), streamOpt); + } + else + { + return tokenList(); + } } //- Create token list by parsing the input character sequence @@ -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 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 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; diff --git a/src/OpenFOAM/db/IOstreams/memory/ISpanStream.H b/src/OpenFOAM/db/IOstreams/memory/ISpanStream.H index ae73dbf42c..89c11699bc 100644 --- a/src/OpenFOAM/db/IOstreams/memory/ISpanStream.H +++ b/src/OpenFOAM/db/IOstreams/memory/ISpanStream.H @@ -123,20 +123,6 @@ public: stream_type(static_cast(this)) {} - //- Construct (shallow copy) from span character content - explicit ispanstream(stdFoam::span s) - : - buffer_type(const_cast(s.data()), s.size()), - stream_type(static_cast(this)) - {} - - //- Construct (shallow copy) from span character content - explicit ispanstream(stdFoam::span s) - : - buffer_type(const_cast(s.data()), s.size()), - stream_type(static_cast(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 s, - IOstreamOption streamOpt = IOstreamOption() - ) - : - ISpanStream(s.data(), s.size(), streamOpt) - {} - - //- Construct (shallow copy) from span character content - explicit ISpanStream - ( - stdFoam::span 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 s) - { - stream_.reset(s.data(), s.size()); - syncState(); - } - - //- Reset input area to use data from span character content - void reset(stdFoam::span s) - { - stream_.reset(s.data(), s.size()); - syncState(); - } - //- Rewind the stream, clearing any old errors virtual void rewind() override { diff --git a/src/OpenFOAM/primitives/strings/lists/CStringList.H b/src/OpenFOAM/primitives/strings/lists/CStringList.H index c4977a298d..fd24374be1 100644 --- a/src/OpenFOAM/primitives/strings/lists/CStringList.H +++ b/src/OpenFOAM/primitives/strings/lists/CStringList.H @@ -58,6 +58,8 @@ Description #include // std::copy #include // std::initializer_list +#include +#include // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -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 inline explicit CStringList(const UList& input); + //- Copy construct from a list of string_views + // Copies the input characters. + inline explicit CStringList(const UList& input); + + //- Copy construct from a list of string_views + // Copies the input characters. + inline explicit CStringList(const std::vector& 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_; } diff --git a/src/OpenFOAM/primitives/strings/lists/CStringList.cxx b/src/OpenFOAM/primitives/strings/lists/CStringList.cxx index 0eead0d803..e7b182f49f 100644 --- a/src/OpenFOAM/primitives/strings/lists/CStringList.cxx +++ b/src/OpenFOAM/primitives/strings/lists/CStringList.cxx @@ -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' diff --git a/src/OpenFOAM/primitives/strings/lists/CStringListI.H b/src/OpenFOAM/primitives/strings/lists/CStringListI.H index 87dbc7bac3..9d1a8840fe 100644 --- a/src/OpenFOAM/primitives/strings/lists/CStringListI.H +++ b/src/OpenFOAM/primitives/strings/lists/CStringListI.H @@ -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& input) } +inline Foam::CStringList::CStringList(const UList& input) +: + CStringList() +{ + resetContent(input); +} + + +inline Foam::CStringList::CStringList +( + const std::vector& 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;