From: Rosen Penev <rosenp@gmail.com>
To: Hans Verkuil <hverkuil@xs4all.nl>
Cc: linux-media@vger.kernel.org
Subject: Re: [PATCH 8/8] v4l2-utils: turn prefixes to a constexpr array
Date: Wed, 21 Apr 2021 02:23:44 -0700 [thread overview]
Message-ID: <21827EC0-E8B2-46F2-B943-A09A2D31EA45@gmail.com> (raw)
In-Reply-To: <566ba9fe-509c-26b4-19a3-845a45093ba1@xs4all.nl>
> On Apr 21, 2021, at 01:06, Hans Verkuil <hverkuil@xs4all.nl> 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@gmail.com>
>> ---
>> 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)) {
>>
>
prev parent reply other threads:[~2021-04-21 9:23 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-04-21 7:20 [PATCH 1/8] clang-tidy: use auto Rosen Penev
2021-04-21 7:20 ` [PATCH 2/8] clang-tidy: use nullptr Rosen Penev
2021-04-21 7:20 ` [PATCH 3/8] remove unused ARRAY_SIZE Rosen Penev
2021-04-21 7:20 ` [PATCH 4/8] cec-tuner: std::array conversions Rosen Penev
2021-04-21 7:20 ` [PATCH 5/8] v4l2-info: remove a strange sizeof usage Rosen Penev
2021-04-21 8:23 ` Hans Verkuil
2021-04-21 9:19 ` Rosen Penev
2021-04-21 9:33 ` Hans Verkuil
2021-04-21 9:39 ` Rosen Penev
2021-04-21 16:01 ` Nicolas Dufresne
2021-04-21 7:20 ` [PATCH 6/8] v4l2-utils: turn fb_formats to constexpr array Rosen Penev
2021-04-21 7:20 ` [PATCH 7/8] v4l2-utils: turn mbus_names into const vector Rosen Penev
2021-04-21 8:02 ` Hans Verkuil
2021-04-21 9:25 ` Rosen Penev
2021-04-21 7:20 ` [PATCH 8/8] v4l2-utils: turn prefixes to a constexpr array Rosen Penev
2021-04-21 8:06 ` Hans Verkuil
2021-04-21 9:23 ` Rosen Penev [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=21827EC0-E8B2-46F2-B943-A09A2D31EA45@gmail.com \
--to=rosenp@gmail.com \
--cc=hverkuil@xs4all.nl \
--cc=linux-media@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).