RE: [RFC PATCH v1 07/12] efi: Replace strstarts() by str_has_prefix().

From: David Laight
Date: Fri Dec 11 2020 - 04:47:43 EST


From: Arvind Sankar
> Sent: 10 December 2020 18:14
...
> I wasn't aware of str_has_prefix() at the time. It does seem useful to
> eliminate the duplication of the string literal, I like the
> skip_prefix() API suggestion, maybe even
>
> bool str_skip_prefix(const char **s, const char *pfx)
> {
> size_t len = str_has_prefix(*s, pfx);
> *s += len;
> return !!len;
> }
> ...
> if (str_skip_prefix(&option, prefix)) { ... }
>
> to avoid the intermediate variable.

That'll generate horrid code - the 'option' variable has to be
repeatedly reloaded from memory (unless it is all inlined).

Perhaps the #define

#define str_skip_prefix(str, prefix) \
{( \
size_t _pfx_len = strlen(prefix)); \
memcmp(str, pfx, _pfx_len) ? 0 : ((str) += _pfx_len, 1); \
)}

There's probably something that'll let you use sizeof() instead
of strlen() for quoted strings (if only sizeof pfx != sizeof (char *)).

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)