isValidSchemeName: Use regex

As requested by Eelco Dolstra. I think it used to be simpler.
This commit is contained in:
Robert Hensing 2023-12-12 17:43:54 +01:00
parent 2e451a663e
commit 4eaeda6604
2 changed files with 8 additions and 12 deletions

View file

@ -3,7 +3,6 @@
#include "util.hh" #include "util.hh"
#include "split.hh" #include "split.hh"
#include "canon-path.hh" #include "canon-path.hh"
#include "string.hh"
namespace nix { namespace nix {
@ -187,17 +186,9 @@ std::string fixGitURL(const std::string & url)
// https://www.rfc-editor.org/rfc/rfc3986#section-3.1 // https://www.rfc-editor.org/rfc/rfc3986#section-3.1
bool isValidSchemeName(std::string_view s) bool isValidSchemeName(std::string_view s)
{ {
if (s.empty()) return false; static std::regex regex(schemeNameRegex, std::regex::ECMAScript);
if (!isASCIIAlpha(s[0])) return false;
for (auto c : s.substr(1)) { return std::regex_match(s.begin(), s.end(), regex, std::regex_constants::match_default);
if (isASCIIAlpha(c)) continue;
if (isASCIIDigit(c)) continue;
if (c == '+') continue;
if (c == '-') continue;
if (c == '.') continue;
return false;
}
return true;
} }
} }

View file

@ -360,6 +360,11 @@ TEST(nix, isValidSchemeName) {
ASSERT_FALSE(isValidSchemeName(".file")); ASSERT_FALSE(isValidSchemeName(".file"));
ASSERT_FALSE(isValidSchemeName("-file")); ASSERT_FALSE(isValidSchemeName("-file"));
ASSERT_FALSE(isValidSchemeName("1file")); ASSERT_FALSE(isValidSchemeName("1file"));
// regex ok?
ASSERT_FALSE(isValidSchemeName("\nhttp"));
ASSERT_FALSE(isValidSchemeName("\nhttp\n"));
ASSERT_FALSE(isValidSchemeName("http\n"));
ASSERT_FALSE(isValidSchemeName("http "));
} }
} }