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

From: Qu Wenruo
Date: Wed Dec 20 2023 - 04:32:30 EST




On 2023/12/20 19:01, David Laight wrote:
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.

Ah, got it.
Although in that case, the 1st parameter should be ("KkMmGgTtPpEe"), as
we still support "Ee", just not by default.

Thanks,
Qu

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)