RE: [PATCH 1/2] lib/strtox: introduce kstrtoull_suffix() helper

From: David Laight
Date: Wed Dec 20 2023 - 03:32:29 EST


From: Qu Wenruo
> Sent: 19 December 2023 21:18
...
> > How about:
> > suffix = *endptr;
> > if (!strchr(suffixes, suffix))
> > return -ENIVAL;
> > shift = strcspn("KkMmGgTtPp", suffix)/2 * 10 + 10;
>
> This means the caller has to provide the suffix string in this
> particular order.

No, The strchr() checks that the suffix is one the caller wanted.
The strcspn() is against a fixed list - so the order can be
selected to make the code shorter.

Actually strcspn() isn't the function you need.
There might be a function like strchr() that returns a count
but I can't remember its name and it may not be in kernel.
You might have to write:
shift = 0;
for (const char *sfp = "KkMmGgTtPp"; suffix != *sfp; sfp++, shift++) {
if (!*sfp)
return -EINVAL;
}
shift = shift/2 + 1 * 10;

David

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