[ovs-dev] [PATCH 03/27] uuid: New function uuid_is_partial_match().
Ben Pfaff
blp at ovn.org
Wed May 3 15:22:38 UTC 2017
On Tue, May 02, 2017 at 07:06:06PM -0700, Andy Zhou wrote:
> On Sun, Apr 30, 2017 at 4:22 PM, Ben Pfaff <blp at ovn.org> wrote:
> > This will have another caller in an upcoming commit.
> >
> > Signed-off-by: Ben Pfaff <blp at ovn.org>
>
> Acked-by: Andy Zhou <azhou at ovn.org>
Thanks!
...
> > diff --git a/lib/uuid.c b/lib/uuid.c
> > index 636492bcbe5b..0b20c24faecd 100644
> > --- a/lib/uuid.c
> > +++ b/lib/uuid.c
> > @@ -242,6 +242,17 @@ uuid_is_partial_string(const char *s)
> > return i;
> > }
> >
> > +/* Compares 'match' to the string representation of 'uuid'. If 'match' equals
> > + * or is a prefix of this string representation, returns strlen(match);
> > + * otherwise, returns 0. (Thus, the caller can */
>
> The last sentence is not finished.
Oops. Now I've deleted it.
> > +int
> > +uuid_is_partial_match(const struct uuid *uuid, const char *match)
> > +{
> > + char uuid_s[UUID_LEN + 1];
> > + snprintf(uuid_s, sizeof uuid_s, UUID_FMT, UUID_ARGS(uuid));
> > + size_t match_len = strlen(match);
> > + return !strncmp(uuid_s, match, match_len) ? match_len : 0;
> > +}
>
> I am not sure if the following optimization makes sense, since the
> main use case if mainly for match to be << UUID_LEN. On the other
> hand, it is cheap to check.
Thanks for the suggestion.
My own instinct is that the expensive part here is probably the
snprintf(). I think I'll leave this as it is until we find that there's
a performance problem of some kind.
> diff --git a/lib/uuid.c b/lib/uuid.c
> index 0b20c24faecd..5fa7ed91e852 100644
> --- a/lib/uuid.c
> +++ b/lib/uuid.c
> @@ -249,8 +249,11 @@ int
> uuid_is_partial_match(const struct uuid *uuid, const char *match)
> {
> char uuid_s[UUID_LEN + 1];
> - snprintf(uuid_s, sizeof uuid_s, UUID_FMT, UUID_ARGS(uuid));
> size_t match_len = strlen(match);
> + if (match_len > UUID_LEN) {
> + return 0;
> + }
> + snprintf(uuid_s, sizeof uuid_s, UUID_FMT, UUID_ARGS(uuid));
> return !strncmp(uuid_s, match, match_len) ? match_len : 0;
> }
Thanks again, I'll apply this in a minute.
More information about the dev
mailing list