Re: [PATCH 8/8] v4l2-utils: turn prefixes to a constexpr array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> On Apr 21, 2021, at 01:06, Hans Verkuil <hverkuil@xxxxxxxxx> wrote:
> 
>> On 21/04/2021 09:20, Rosen Penev wrote:
>> Allows usage of a single any_of instead of a raw loop.
>> 
>> Signed-off-by: Rosen Penev <rosenp@xxxxxxxxx>
>> ---
>> utils/v4l2-ctl/v4l2-ctl-common.cpp | 24 +++++-------------------
>> 1 file changed, 5 insertions(+), 19 deletions(-)
>> 
>> diff --git a/utils/v4l2-ctl/v4l2-ctl-common.cpp b/utils/v4l2-ctl/v4l2-ctl-common.cpp
>> index 17ad488dd..2b6dd6d13 100644
>> --- a/utils/v4l2-ctl/v4l2-ctl-common.cpp
>> +++ b/utils/v4l2-ctl/v4l2-ctl-common.cpp
>> @@ -116,28 +116,14 @@ void common_usage()
>>           );
>> }
>> 
>> -static const char *prefixes[] = {
>> -    "video",
>> -    "radio",
>> -    "vbi",
>> -    "swradio",
>> -    "v4l-subdev",
>> -    "v4l-touch",
>> -    "media",
>> -    nullptr
>> +static constexpr std::array<const char *, 7> prefixes{
>> +    "video", "radio", "vbi", "swradio", "v4l-subdev", "v4l-touch", "media",
>> };
>> 
>> static bool is_v4l_dev(const char *name)
>> {
>> -    for (unsigned i = 0; prefixes[i]; i++) {
>> -        unsigned l = strlen(prefixes[i]);
>> -
>> -        if (!memcmp(name, prefixes[i], l)) {
>> -            if (isdigit(name[l]))
>> -                return true;
>> -        }
>> -    }
>> -    return false;
>> +    return std::any_of(prefixes.begin(), prefixes.end(),
>> +               [=](const char *prefix) { return !strcmp(name, prefix) && isdigit(name[strlen(prefix)]); });
> 
> Yuck. Besides, it is wrong AFAIKS since strcmp is not the same as memcmp.
True. C++17 would make this better with std::string_view with its size member function which also happens to be constexpr. strlen IIRC is slow.
> 
> I like the original code, easier to understand than the replacement. So sue me :-)
I’ll convert to range loop.
> 
> Regards,
> 
>    Hans
> 
>> }
>> 
>> static int calc_node_val(const char *s)
>> @@ -146,7 +132,7 @@ static int calc_node_val(const char *s)
>> 
>>    s = std::strrchr(s, '/') + 1;
>> 
>> -    for (unsigned i = 0; prefixes[i]; i++) {
>> +    for (size_t i = 0; i < prefixes.size(); i++) {
>>        unsigned l = strlen(prefixes[i]);
>> 
>>        if (!memcmp(s, prefixes[i], l)) {
>> 
> 




[Index of Archives]     [Linux Input]     [Video for Linux]     [Gstreamer Embedded]     [Mplayer Users]     [Linux USB Devel]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Yosemite Backpacking]

  Powered by Linux