Skip to content

Commit ec072c1

Browse files
author
Jake Champion
committed
refactor: use std::isalnum instead of implementing our own version
1 parent a3b88a5 commit ec072c1

File tree

1 file changed

+6
-16
lines changed

1 file changed

+6
-16
lines changed

c-dependencies/js-compute-runtime/builtins/backend.cpp

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <cctype>
12
#include <iostream>
23
#include <optional>
34

@@ -11,34 +12,23 @@
1112
#include "host_call.h"
1213
#include "js/Conversions.h"
1314

14-
bool isDigit(char character) { return character >= 48 && character <= 57; }
15-
bool isLowerAlpha(char character) { return character >= 97 && character <= 122; }
16-
bool isUpperAlpha(char character) { return character >= 65 && character <= 90; }
17-
bool isAlphaNumeric(char character) {
18-
if (!isDigit(character) && !isLowerAlpha(character) && !isUpperAlpha(character)) {
19-
return false;
20-
}
21-
return true;
22-
}
2315
bool isDash(char character) { return character == 45; }
2416
bool isDot(char character) { return character == 46; }
2517

2618
bool isNotAlphaNumericOrDash(char character) {
27-
return !isDigit(character) && !isLowerAlpha(character) && !isUpperAlpha(character) &&
28-
!isDash(character);
19+
return !std::isalnum(character) && !isDash(character);
2920
}
3021

3122
bool isNotAlphaNumericDotOrDash(char character) {
32-
return !isDigit(character) && !isLowerAlpha(character) && !isUpperAlpha(character) &&
33-
!isDash(character) && !isDot(character);
23+
return !std::isalnum(character) && !isDash(character) && !isDot(character);
3424
}
3525

3626
bool isValidHost(std::string host) {
3727
// ValidHostRegex =
3828
// "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])(:[0-9]+)$";
3929
auto firstCharacter = host.front();
4030
// check first character is in the regex [a-zA-Z0-9]
41-
if (!isAlphaNumeric(firstCharacter)) {
31+
if (!std::isalnum(firstCharacter)) {
4232
return false;
4333
}
4434
// split the hostname from the port
@@ -47,7 +37,7 @@ bool isValidHost(std::string host) {
4737

4838
auto lastCharacter = hostname.back();
4939
// check last character is in the regex [a-zA-Z0-9]
50-
if (!isAlphaNumeric(lastCharacter)) {
40+
if (!std::isalnum(lastCharacter)) {
5141
return false;
5242
}
5343

@@ -58,7 +48,7 @@ bool isValidHost(std::string host) {
5848
}
5949

6050
// check the character before the last . is in the regex [a-zA-Z0-9]
61-
if (!isAlphaNumeric(hostname.at(lastDot - 1))) {
51+
if (!std::isalnum(hostname.at(lastDot - 1))) {
6252
return false;
6353
}
6454
// check all other characters before the last . are in the regex [a-zA-Z0-9\-.]

0 commit comments

Comments
 (0)