Message ID | 20210421072035.4188497-8-rosenp@gmail.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | [1/8] clang-tidy: use auto | expand |
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. I like the original code, easier to understand than the replacement. So sue me :-) 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)) { >
> 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)) { >> >
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)]); }); } 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)) {
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(-)