1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2024-09-19 23:03:53 -04:00

isValidSchemeName: Use regex

As requested by Eelco Dolstra. I think it used to be simpler.

(cherry picked from commit 4eaeda6604)
This commit is contained in:
Robert Hensing 2023-12-12 17:43:54 +01:00
parent 598b0e2317
commit ebdb6926fd
2 changed files with 8 additions and 12 deletions

View file

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

View file

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