ENH: prevent conversion of string to regExp in stringListOps (closes #739)

* For most cases, this conversion would be largely unintentional
  and also less efficient. If the regex is desirable, the caller
  should invoke it explicitly.
  For example,

      findStrings(regExp(str), listOfStrings);

  Or use one of the keyType, wordRe, wordRes variants instead.
  If string is to be used as a plain (non-regex) matcher,
  this can be directly invoked

      findMatchingStrings(str, listOfStrings);

  or using the ListOps instead:

      findIndices(listOfStrings, str);

* provide function interfaces for keyType.
This commit is contained in:
Mark Olesen
2018-02-22 09:28:03 +01:00
parent f959927910
commit ec38e7a408
16 changed files with 264 additions and 226 deletions

View File

@ -101,22 +101,26 @@ const Foam::coordinateSystems& Foam::coordinateSystems::New
Foam::labelList Foam::coordinateSystems::findIndices(const keyType& key) const
{
labelList indices;
if (key.isPattern())
if (key.empty())
{
indices = findStrings(key, toc());
// no-op
}
else if (key.isPattern())
{
indices = findStrings(key, this->toc());
}
else
{
indices.setSize(size());
label nFound = 0;
indices.setSize(this->size());
label count = 0;
forAll(*this, i)
{
if (key == operator[](i).name())
{
indices[nFound++] = i;
indices[count++] = i;
}
}
indices.setSize(nFound);
indices.setSize(count);
}
return indices;
@ -125,13 +129,16 @@ Foam::labelList Foam::coordinateSystems::findIndices(const keyType& key) const
Foam::label Foam::coordinateSystems::findIndex(const keyType& key) const
{
if (key.isPattern())
if (key.empty())
{
// no-op
}
else if (key.isPattern())
{
labelList indices = findIndices(key);
// return first element
if (!indices.empty())
{
return indices[0];
return indices.first(); // first match
}
}
else
@ -145,6 +152,7 @@ Foam::label Foam::coordinateSystems::findIndex(const keyType& key) const
}
}
// Not found
return -1;
}

View File

@ -120,12 +120,12 @@ Foam::label Foam::triSurfaceLoader::select(const wordRe& mat)
if (mat.isPattern())
{
foundIds = findMatchingStrings(mat, available_);
foundIds = findStrings(mat, available_);
sort(foundIds);
}
else
{
const word& plain = static_cast<const word&>(mat);
const word& plain = mat;
if (available_.found(plain))
{
foundIds.append(available_[plain]);
@ -164,7 +164,7 @@ Foam::label Foam::triSurfaceLoader::select(const UList<wordRe>& matcher)
{
if (mat.isPattern())
{
labelList indices = findMatchingStrings(mat, available_);
labelList indices = findStrings(mat, available_);
sort(indices);
for (const label idx : indices)
@ -177,7 +177,7 @@ Foam::label Foam::triSurfaceLoader::select(const UList<wordRe>& matcher)
}
else
{
const word& plain = static_cast<const word&>(mat);
const word& plain = mat;
if (available_.found(plain))
{
const label idx = available_[plain];