[libvirt] [PATCHv2 2/5] Add functions dealing with control characters in strings

Ján Tomko jtomko at redhat.com
Wed Apr 15 15:27:22 UTC 2015


On Wed, Apr 15, 2015 at 12:00:51PM +0200, Peter Krempa wrote:
> On Tue, Apr 14, 2015 at 13:28:47 +0200, Ján Tomko wrote:
> > Add virStringHasControlChars that checks if the string has
> > any control characters other than \t\r\n,
> > and virStringStripControlChars that removes them in-place.
> > ---
> >  src/libvirt_private.syms |  2 ++
> >  src/util/virstring.c     | 39 +++++++++++++++++++++++++++++++++++++++
> >  src/util/virstring.h     |  2 ++
> >  tests/virstringtest.c    | 39 +++++++++++++++++++++++++++++++++++++++
> >  4 files changed, 82 insertions(+)

> > +
> > +bool
> > +virStringHasControlChars(const char *str)
> > +{
> > +    if (!str)
> > +        return false;
> > +
> > +    return strcspn(str, control_chars) != strlen(str);
> > +}

> > + */
> > +void
> > +virStringStripControlChars(char *str)
> > +{
> > +    size_t len, i, j;
> > +
> > +    if (!virStringHasControlChars(str))
> > +        return;
> 
> The check above calls strlen and iterates through the string looking for
> the characters.

Right, the check can do just:
  return str[strcspn(str, control_chars)] != '\0';

Jan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20150415/05d0141e/attachment-0001.sig>


More information about the libvir-list mailing list