tree-wide: add global ascii_isdigit() + ascii_isalpha()
We now have a local implementation in string-util-fundamental.c, but
it's useful at a lot of other places, hence let's give it a more
expressive name and share it across the tree.
Follow-up for: 8d9156660d
This commit is contained in:
@@ -26,12 +26,10 @@ char *bus_label_escape(const char *s) {
|
||||
|
||||
for (f = s, t = r; *f; f++) {
|
||||
|
||||
/* Escape everything that is not a-zA-Z0-9. We also
|
||||
* escape 0-9 if it's the first character */
|
||||
/* Escape everything that is not a-zA-Z0-9. We also escape 0-9 if it's the first character */
|
||||
|
||||
if (!(*f >= 'A' && *f <= 'Z') &&
|
||||
!(*f >= 'a' && *f <= 'z') &&
|
||||
!(f > s && *f >= '0' && *f <= '9')) {
|
||||
if (!ascii_isalpha(*f) &&
|
||||
!(f > s && ascii_isdigit(*f))) {
|
||||
*(t++) = '_';
|
||||
*(t++) = hexchar(*f >> 4);
|
||||
*(t++) = hexchar(*f);
|
||||
|
||||
@@ -32,7 +32,7 @@ static bool env_name_is_valid_n(const char *e, size_t n) {
|
||||
if (n <= 0)
|
||||
return false;
|
||||
|
||||
if (e[0] >= '0' && e[0] <= '9')
|
||||
if (ascii_isdigit(e[0]))
|
||||
return false;
|
||||
|
||||
/* POSIX says the overall size of the environment block cannot
|
||||
|
||||
@@ -75,10 +75,8 @@ int gethostname_full(GetHostnameFlags flags, char **ret) {
|
||||
bool valid_ldh_char(char c) {
|
||||
/* "LDH" → "Letters, digits, hyphens", as per RFC 5890, Section 2.3.1 */
|
||||
|
||||
return
|
||||
(c >= 'a' && c <= 'z') ||
|
||||
(c >= 'A' && c <= 'Z') ||
|
||||
(c >= '0' && c <= '9') ||
|
||||
return ascii_isalpha(c) ||
|
||||
ascii_isdigit(c) ||
|
||||
c == '-';
|
||||
}
|
||||
|
||||
|
||||
@@ -210,7 +210,7 @@ int parse_size(const char *t, uint64_t base, uint64_t *size) {
|
||||
e++;
|
||||
|
||||
/* strtoull() itself would accept space/+/- */
|
||||
if (*e >= '0' && *e <= '9') {
|
||||
if (ascii_isdigit(*e)) {
|
||||
unsigned long long l2;
|
||||
char *e2;
|
||||
|
||||
@@ -599,7 +599,7 @@ int parse_fractional_part_u(const char **p, size_t digits, unsigned *res) {
|
||||
|
||||
/* accept any number of digits, strtoull is limited to 19 */
|
||||
for (size_t i = 0; i < digits; i++,s++) {
|
||||
if (*s < '0' || *s > '9') {
|
||||
if (!ascii_isdigit(*s)) {
|
||||
if (i == 0)
|
||||
return -EINVAL;
|
||||
|
||||
|
||||
@@ -833,7 +833,7 @@ bool ifname_valid_full(const char *p, IfnameValidFlags flags) {
|
||||
if (!ifname_valid_char(*t))
|
||||
return false;
|
||||
|
||||
numeric = numeric && (*t >= '0' && *t <= '9');
|
||||
numeric = numeric && ascii_isdigit(*t);
|
||||
}
|
||||
|
||||
/* It's fully numeric but didn't parse as valid ifindex above? if so, it must be too large or zero or
|
||||
|
||||
@@ -624,7 +624,7 @@ int vtnr_from_tty(const char *tty) {
|
||||
if (!startswith(tty, "tty") )
|
||||
return -EINVAL;
|
||||
|
||||
if (tty[3] < '0' || tty[3] > '9')
|
||||
if (!ascii_isdigit(tty[3]))
|
||||
return -EINVAL;
|
||||
|
||||
r = safe_atoi(tty+3, &i);
|
||||
|
||||
@@ -1411,9 +1411,8 @@ int verify_timezone(const char *name, int log_level) {
|
||||
return -EINVAL;
|
||||
|
||||
for (p = name; *p; p++) {
|
||||
if (!(*p >= '0' && *p <= '9') &&
|
||||
!(*p >= 'a' && *p <= 'z') &&
|
||||
!(*p >= 'A' && *p <= 'Z') &&
|
||||
if (!ascii_isdigit(*p) &&
|
||||
!ascii_isalpha(*p) &&
|
||||
!IN_SET(*p, '-', '_', '+', '/'))
|
||||
return -EINVAL;
|
||||
|
||||
|
||||
@@ -814,15 +814,13 @@ bool valid_user_group_name(const char *u, ValidUserFlags flags) {
|
||||
* Note that other systems are even more restrictive, and don't permit underscores or uppercase characters.
|
||||
*/
|
||||
|
||||
if (!(u[0] >= 'a' && u[0] <= 'z') &&
|
||||
!(u[0] >= 'A' && u[0] <= 'Z') &&
|
||||
if (!ascii_isalpha(u[0]) &&
|
||||
u[0] != '_')
|
||||
return false;
|
||||
|
||||
for (i = u+1; *i; i++)
|
||||
if (!(*i >= 'a' && *i <= 'z') &&
|
||||
!(*i >= 'A' && *i <= 'Z') &&
|
||||
!(*i >= '0' && *i <= '9') &&
|
||||
if (!ascii_isalpha(*i) &&
|
||||
!ascii_isdigit(*i) &&
|
||||
!IN_SET(*i, '_', '-'))
|
||||
return false;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user