Re: [PATCH v6 02/19] string: Add strprefix()

From: Alejandro Colomar

Date: Sat Nov 14 2026 - 14:00:51 EST


Hi Joe,

> Date: 2026-11-14 10:24:26-0800
> From: Joe Perches <joe@xxxxxxxxxxx>
>
> On Sat, 2026-11-14 at 17:59 +0100, Alejandro Colomar wrote:
> > > A negative of this is that it implies the return should
> > > always be tested even though it can be known that the
> > > return is effectively bool.
> >
> > I'm not sure I follow. The attribute is about the arguments, not the
> > return value. Perhaps I don't understand what you mean by "this".
>
> 'this' is the return of the strprefix function itself.
>
> It returns either a const char * or a char * or NULL.
>
> Because it can return NULL, then the return value
> could/should be tested.

Yes, it must be tested, when not used as a boolean.
The usual replacement of
s += str_has_prefix(s, prefix);
would be
s = strprefix(s, prefix) ?: s;

The boolean use remains trivial:

- return strstarts(name, ".init") ||
- strstarts(name, ".ARM.extab.init") ||
- strstarts(name, ".ARM.exidx.init");
+ return strprefix(name, ".init") ||
+ strprefix(name, ".ARM.extab.init") ||
+ strprefix(name, ".ARM.exidx.init");
and
- if (!str_has_prefix(p, "console=") && !strstr(p, " console=")) {
+ if (!strprefix(p, "console=") && !strstr(p, " console=")) {

The non-boolean use would look like this (actual diffs from the patch
set):

- prefix_len = str_has_prefix(name, PDSC_FW_COMPONENT_PREFIX);
- if (prefix_len)
- name += prefix_len;
+ name = strprefix(name, PDSC_FW_COMPONENT_PREFIX) ?: name;

(the old code, actually, didn't need that test, and could have been
simply 'name += str_has_prefix(name, PDSC_FW_COMPONENT_PREFIX);')

and
- len = str_has_prefix(*str, "brl,");
- if (len) {
+ p = strprefix(*str, "brl,");
+ if (p) {
*brl_options = "";
- *str += len;
+ *str = p;
return 0;
}

and
cmp = strstrip(option);

- len = str_has_prefix(cmp, "no");
- if (len)
+ p = strprefix(cmp, "no");
+ if (p) {
neg = 1;
-
- cmp += len;
+ cmp = p;
+ }


Have a lovely night!
Alex

--
<https://www.alejandro-colomar.es>

Attachment: signature.asc
Description: PGP signature