[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [libvirt] [PATCH 1/2] util: introduce virStringMatch



On Fri, May 12, 2017 at 06:44:16AM -0400, John Ferlan wrote:
> 
> 
> On 05/11/2017 11:49 AM, Pavel Hrdina wrote:
> > Simply tries to match the provided regex on a string and returns
> > the result.  Useful if caller don't care about the matched substring
> > and want to just test if some pattern patches a string.
> > 
> > Signed-off-by: Pavel Hrdina <phrdina redhat com>
> > ---
> >  src/libvirt_private.syms |  1 +
> >  src/util/virstring.c     | 34 ++++++++++++++++++++++++++++++++++
> >  src/util/virstring.h     |  3 +++
> >  tests/virstringtest.c    | 47 +++++++++++++++++++++++++++++++++++++++++++++++
> >  4 files changed, 85 insertions(+)
> > 
> 
> Something to consider seeing as this function could be "reused" if made
> a bit more generic...
> 
> > diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
> > index afb9100c50..d32c6e7549 100644
> > --- a/src/libvirt_private.syms
> > +++ b/src/libvirt_private.syms
> > @@ -2621,6 +2621,7 @@ virStringListHasString;
> >  virStringListJoin;
> >  virStringListLength;
> >  virStringListRemove;
> > +virStringMatch;
> >  virStringReplace;
> >  virStringSearch;
> >  virStringSortCompare;
> > diff --git a/src/util/virstring.c b/src/util/virstring.c
> > index 335e773d78..b95a8926bd 100644
> > --- a/src/util/virstring.c
> > +++ b/src/util/virstring.c
> > @@ -979,6 +979,40 @@ virStringSearch(const char *str,
> >  }
> >  
> >  /**
> > + * virStringMatch:
> > + * @str: string to match
> > + * @regexp: POSIX Extended regular expression pattern used for matching
> > + *
> > + * Performs a POSIX extended regex search against a string.
> > + * Returns 0 on match, -1 on error, 1 on no match.
> > + */
> > +int
> > +virStringMatch(const char *str,
> > +               const char *regexp)
> > +{
> > +    regex_t re;
> > +    int ret = -1;
> > +    int rv;
> > +
> > +    VIR_DEBUG("match '%s' for '%s'", str, regexp);
> > +
> > +    if ((rv = regcomp(&re, regexp, REG_EXTENDED | REG_NOSUB)) != 0) {
> 
> Why not pass the last arg as @flags

In most cases there is no need to change those @flags, other possible
are REG_ICASE and REG_NEWLINE.

> > +        char error[100];
> > +        regerror(rv, &re, error, sizeof(error));
> > +        virReportError(VIR_ERR_INTERNAL_ERROR,
> > +                       _("error while compiling regular expression '%s': %s"),
> > +                       regexp, error);
> > +        return -1;
> > +    }
> > +
> > +    if ((ret = regexec(&re, str, 0, NULL, 0)) != 0)
> 
> and perhaps the 3rd/4th args as params...
> 
> Could lead to some reuse of this particular function by others that also
> call regcomp and regexec (virStorageBackendLogicalParseVolExtents has an
> example of the args to regexec)

There is virStringSearch() that does exactly what you are referring to.
However, virStringSearch() doesn't allow more than one match group,
which virStorageBackendLogicalParseVolExtents uses.

The purpose of virStringMatch is to match the regex in cases where
you don't need the matched groups.  We might extend/improve
virStringSearch().

Pavel

Attachment: signature.asc
Description: Digital signature


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]