Re: [PATCH v3] string.h: Add str_has_prefix() helper

From: Steven Rostedt
Date: Sat Dec 22 2018 - 12:47:45 EST


On Sat, 22 Dec 2018 18:28:18 +0900
Namhyung Kim <namhyung@xxxxxxxxxx> wrote:

> >
> > for (i = 0; i < hist_data->attrs->n_actions; i++) {
> > str = hist_data->attrs->action_str[i];
> >
> > - if (str_has_prefix(str, "onmatch(")) {
> > - char *action_str = str + sizeof("onmatch(") - 1;
> > + if ((len = str_has_prefix(str, "onmatch("))) {
> > + char *action_str = str + len;
>
> IMHO, returning (match) length might confuse people that it might
> support partial match and returns the length actually matched rather
> than full match. If I knew it always returns the length of prefix (or
> 0) why it matters? Using strlen() in the next line will have same
> effect of compiler optimization for the constant strings, no?
>
> if (str_has_prefix(str, "onmatch(")) {
> char *action_str = str + strlen("onmatch(");

The reason to return the length was to get rid of the need for
duplicating the strlen("xxxx") because it's a burden to keep the two
the same. Not only that, a lot of places just do "str + 7" because it's
easier to type.

Yes, it has the same effect on the compiler, but that's not what we are
trying to solve. We are trying to get rid of the duplication, because
that requires humans to get it right, and humans are not good at that.

-- Steve