ENH: add single-parameter PtrListOps::names (ie, no name filtering)

ENH: adjust fileName component method

- the location \c npos returns the last component
This commit is contained in:
Mark Olesen
2021-10-11 23:27:51 +02:00
parent 5014398c45
commit 7ad75fa18e
6 changed files with 31 additions and 5 deletions

View File

@ -224,7 +224,8 @@ void testDirname(const fileName& input)
<< " path:" << input.path()
<< " name:\"" << input.name() << '"'
<< " ext:\"" << input.ext() << '"'
<< " components: " << flatOutput(input.components()) << nl;
<< " components: " << flatOutput(input.components())
<< " last: " << input.component(string::npos) << nl;
}

View File

@ -43,6 +43,7 @@ SourceFiles
#ifndef PtrListOps_H
#define PtrListOps_H
#include "predicates.H"
#include "PtrList.H"
#include "ListOps.H"
@ -169,6 +170,12 @@ List<word> names
);
//- List of names generated by calling \c name() for each list item
//- no filtering (ie, predicates::always)
template<class T>
List<word> names(const UPtrList<T>& list);
//- Find first list item with 'name()' that matches, -1 on failure
template<class T, class UnaryMatchPredicate>
label firstMatching

View File

@ -166,6 +166,16 @@ Foam::List<Foam::word> Foam::PtrListOps::names
}
template<class T>
Foam::List<Foam::word> Foam::PtrListOps::names
(
const UPtrList<T>& list
)
{
return PtrListOps::names(list, predicates::always());
}
template<class T, class UnaryMatchPredicate>
Foam::label Foam::PtrListOps::firstMatching
(

View File

@ -485,9 +485,16 @@ Foam::word Foam::fileName::component
{
const auto parsed = stringOps::split<string>(*this, delim);
if (cmpt < parsed.size())
if (parsed.size())
{
return parsed[cmpt].str();
if (cmpt == std::string::npos)
{
return parsed.last().str();
}
else if (cmpt < parsed.size())
{
return parsed[cmpt].str();
}
}
return word();

View File

@ -358,7 +358,8 @@ public:
// \endverbatim
wordList components(const char delim = '/') const;
//- Return a single component of the path
//- Return a single component of the path or empty if out of range
// The location \c npos returns the last component
word component(const size_type cmpt, const char delim = '/') const;

View File

@ -269,7 +269,7 @@ Foam::coordinateSystems::lookup(const word& name) const
Foam::wordList Foam::coordinateSystems::names() const
{
return PtrListOps::names(*this, predicates::always{});
return PtrListOps::names(*this); // match any/all
}