lkml.org 
[lkml]   [2018]   [Dec]   [22]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: [PATCH v3] string.h: Add str_has_prefix() helper
On Sat, 22 Dec 2018 18:28:18 +0900
Namhyung Kim <namhyung@kernel.org> 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

\
 
 \ /
  Last update: 2018-12-22 18:48    [W:0.063 / U:1.196 seconds]
©2003-2018 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site