Skip to content

Commit a3be5b6

Browse files
leoossaJenkins-dev
authored andcommitted
refactor(common/number): use ranges algorithm and string_view
Use ranges::all_of to check all characters in a string in place of manual iteration. Use modern string_view in place of old-fashioned const char*. New code is more compact, modernized, cleaner and easier to read and understand. Signed-off-by: Leonard Ossa <leonard.ossa@openvpn.com>
1 parent 2d6981b commit a3be5b6

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

openvpn/common/number.hpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#ifndef OPENVPN_COMMON_NUMBER_H
1515
#define OPENVPN_COMMON_NUMBER_H
1616

17+
#include <algorithm>
1718
#include <string>
1819
#include <limits>
1920

@@ -120,18 +121,10 @@ inline bool parse_number_validate(const std::string &numstr,
120121
return false;
121122
}
122123

123-
inline bool is_number(const char *str)
124+
inline bool is_number(std::string_view str)
124125
{
125-
char c;
126-
bool found_digit = false;
127-
while ((c = *str++))
128-
{
129-
if (c >= '0' && c <= '9')
130-
found_digit = true;
131-
else
132-
return false;
133-
}
134-
return found_digit;
126+
return !str.empty() && std::ranges::all_of(str, [](const unsigned char c)
127+
{ return std::isdigit(c); });
135128
}
136129

137130
} // namespace openvpn

0 commit comments

Comments
 (0)