Skip to content

Commit

Permalink
isValidSchemeName: Use regex
Browse files Browse the repository at this point in the history
As requested by Eelco Dolstra. I think it used to be simpler.
  • Loading branch information
roberth committed Dec 12, 2023
1 parent 2e451a6 commit 4eaeda6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
15 changes: 3 additions & 12 deletions src/libutil/url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "util.hh"
#include "split.hh"
#include "canon-path.hh"
#include "string.hh"

namespace nix {

Expand Down Expand Up @@ -187,17 +186,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);
}

}
5 changes: 5 additions & 0 deletions tests/unit/libutil/url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 "));
}

}

0 comments on commit 4eaeda6

Please sign in to comment.