mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-14 10:16:15 +02:00
isValidSchemeName: Use regex
As requested by Eelco Dolstra. I think it used to be simpler.
This commit is contained in:
parent
2e451a663e
commit
4eaeda6604
2 changed files with 8 additions and 12 deletions
|
@ -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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 "));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue