Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[regex-ripout] part 2/n #431

Merged
merged 17 commits into from
Mar 16, 2022
83 changes: 83 additions & 0 deletions azure-pipelines/end-to-end-tests-dir/autocomplete.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
. $PSScriptRoot/../end-to-end-tests-prelude.ps1

$publicCommands = @(
'install'
'search'
'remove'
'list'
'update'
'hash'
'help'
'integrate'
'export'
'edit'
'create'
'owns'
'cache'
'version'
'contact'
'upgrade'
)

$privateCommands = @(
'build'
'buildexternal'
'ci'
'depend-info'
'env'
'portsdiff'
)

function getOptionsForPrefix($prefix, $commands) {
$commands | Sort-Object | ? { $_.StartsWith($prefix) }
}
function arraysEqual($arr1, $arr2) {
if ($arr1.Length -ne $arr2.Length) {
$False
} else {
for ($i = 0; $i -lt $arr1.Length; ++$i) {
if ($arr1[$i] -ne $arr2[$i]) {
return $False
}
}
}
$True
}

$publicPrefixesToTest = @(
'in'
's'
'rem'
'h'
'upgra'
'e'
)
$privatePrefixesToTest = @(
'b'
'build'
'buildext'
'ci'
'dep'
'en'
'port'
'notprefix'
)

function testPrefixes($toTest, $commands) {
$toTest | % {
$expected = getOptionsForPrefix $_ $commands
$found = Run-Vcpkg autocomplete $_
Throw-IfFailed
if (-not (arraysEqual @($expected) @($found))) {
Write-Host "unexpected result from vcpkg autocomplete:
expected: `"$($expected -join '" "')`"
found : `"$($found -join '" "')`""
throw
}
}
}

testPrefixes $publicPrefixesToTest $publicCommands
testPrefixes $privatePrefixesToTest $privateCommands

# autocomplete is currently broken, so we only test the parts that are correct
6 changes: 6 additions & 0 deletions include/vcpkg/base/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ namespace vcpkg::Parse
static constexpr bool is_alphadash(char32_t ch) { return is_icase_alpha(ch) || ch == '-'; }
static constexpr bool is_alphanumdash(char32_t ch) { return is_alphanum(ch) || ch == '-'; }

static constexpr bool is_hex_digit(char32_t ch)
{
return is_ascii_digit(ch) || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
}
static constexpr bool is_word_char(char32_t ch) { return is_alphanum(ch) || ch == '_'; }

StringView skip_whitespace() { return match_zero_or_more(is_whitespace); }
StringView skip_tabs_spaces()
{
Expand Down
68 changes: 5 additions & 63 deletions include/vcpkg/base/strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,74 +236,16 @@ namespace vcpkg::Strings

// Equivalent to one of the `::strto[T]` functions. Returns `nullopt` if there is an error.
template<class T>
Optional<T> strto(ZStringView sv);
Optional<T> strto(StringView sv);

template<>
inline Optional<double> strto<double>(ZStringView sv)
{
char* endptr = nullptr;
double res = strtod(sv.c_str(), &endptr);
if (endptr == sv.c_str())
{
// no digits
return nullopt;
}
// else, we may have HUGE_VAL but we expect the caller to deal with that
return res;
}

Optional<int> strto<int>(StringView);
template<>
inline Optional<long> strto<long>(ZStringView sv)
{
char* endptr = nullptr;
long res = strtol(sv.c_str(), &endptr, 10);
if (endptr == sv.c_str())
{
// no digits
return nullopt;
}
if (errno == ERANGE)
{
// out of bounds
return nullopt;
}

return res;
}

Optional<long> strto<long>(StringView);
template<>
inline Optional<long long> strto<long long>(ZStringView sv)
{
char* endptr = nullptr;
long long res = strtoll(sv.c_str(), &endptr, 10);
if (endptr == sv.c_str())
{
// no digits
return nullopt;
}
if (errno == ERANGE)
{
// out of bounds
return nullopt;
}

return res;
}

Optional<long long> strto<long long>(StringView);
template<>
inline Optional<int> strto<int>(ZStringView sv)
{
auto res = strto<long>(sv);
if (auto r = res.get())
{
if (*r < INT_MIN || *r > INT_MAX)
{
return nullopt;
}
return static_cast<int>(*r);
}
return nullopt;
}
Optional<double> strto<double>(StringView);

const char* search(StringView haystack, StringView needle);

Expand Down
4 changes: 2 additions & 2 deletions include/vcpkg/binarycaching.private.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace vcpkg
// - v?<X>.<Y><whatever> -> <X>.<Y>.0-vcpkg<abitag>
// - v?<X>.<Y>.<Z><whatever> -> <X>.<Y>.<Z>-vcpkg<abitag>
// - anything else -> 0.0.0-vcpkg<abitag>
std::string reformat_version(StringView version, StringView abi_tag);
std::string format_version_for_nugetref(StringView version, StringView abi_tag);

struct NugetReference
{
Expand All @@ -33,7 +33,7 @@ namespace vcpkg
const std::string& abi_tag,
const std::string& prefix)
{
return {Strings::concat(prefix, spec.dir()), reformat_version(raw_version, abi_tag)};
return {Strings::concat(prefix, spec.dir()), format_version_for_nugetref(raw_version, abi_tag)};
}
inline NugetReference make_nugetref(const Dependencies::InstallPlanAction& action, const std::string& prefix)
{
Expand Down
2 changes: 2 additions & 0 deletions include/vcpkg/export.ifw.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ namespace vcpkg::Export::IFW
Optional<std::string> maybe_installer_file_path;
};

std::string safe_rich_from_plain_text(StringView text);

void do_export(const std::vector<Dependencies::ExportPlanAction>& export_plan,
const std::string& export_id,
const Options& ifw_options,
Expand Down
10 changes: 8 additions & 2 deletions include/vcpkg/export.prefab.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ namespace vcpkg::Export::Prefab
std::string to_string();
void to_string(std::string& out);

friend bool operator==(const NdkVersion& lhs, const NdkVersion& rhs)
{
return lhs.m_major == rhs.m_major && lhs.m_minor == rhs.m_minor && lhs.m_patch == rhs.m_patch;
}
friend bool operator!=(const NdkVersion& lhs, const NdkVersion& rhs) { return !(lhs == rhs); }

private:
int m_major;
int m_minor;
Expand Down Expand Up @@ -74,6 +80,6 @@ namespace vcpkg::Export::Prefab
const VcpkgPaths& paths,
const Options& prefab_options,
const Triplet& triplet);
Optional<std::string> find_ndk_version(const std::string& content);
Optional<NdkVersion> to_version(const std::string& version);
Optional<StringView> find_ndk_version(StringView content);
Optional<NdkVersion> to_version(StringView version);
}
2 changes: 2 additions & 0 deletions include/vcpkg/metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ namespace vcpkg
void flush(Filesystem& fs);
};

Optional<StringView> find_first_nonzero_mac(StringView sv);

extern LockGuarded<Metrics> g_metrics;
}
3 changes: 3 additions & 0 deletions include/vcpkg/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <vcpkg/fwd/tools.h>
#include <vcpkg/fwd/vcpkgpaths.h>

#include <vcpkg/base/optional.h>
#include <vcpkg/base/stringliteral.h>

#include <string>
Expand Down Expand Up @@ -44,5 +45,7 @@ namespace vcpkg
virtual const std::string& get_tool_version(const VcpkgPaths& paths, StringView tool) const = 0;
};

Optional<std::array<int, 3>> parse_tool_version_string(StringView string_version);

std::unique_ptr<ToolCache> get_tool_cache(RequireExactVersions abiToolVersionHandling);
}
17 changes: 17 additions & 0 deletions include/vcpkg/versions.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,23 @@ namespace vcpkg
None,
Minimum
};

// this is for version parsing that isn't in vcpkg ports
// stuff like tools, nuget, etc.
struct ParsedExternalVersion
{
StringView major;
StringView minor;
StringView patch;

void normalize();
};

StringView normalize_external_version_zeros(StringView sv);
// /(\d\d\d\d)-(\d\d)-(\d\d).*/
bool try_extract_external_date_version(ParsedExternalVersion& out, StringView version);
// /(\d+)(\.\d+|$)(\.\d+)?.*/
bool try_extract_external_dot_version(ParsedExternalVersion& out, StringView version);
}

VCPKG_FORMAT_WITH_TO_STRING(vcpkg::VersionSpec);
34 changes: 17 additions & 17 deletions src/vcpkg-test/binarycaching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,30 +206,30 @@ TEST_CASE ("CacheStatus operations", "[BinaryCache]")
REQUIRE(lhs_lines.size() == rhs_lines.size()); \
}

TEST_CASE ("reformat_version semver-ish", "[reformat_version]")
TEST_CASE ("format_version_for_nugetref semver-ish", "[format_version_for_nugetref]")
{
REQUIRE(reformat_version("0.0.0", "abitag") == "0.0.0-vcpkgabitag");
REQUIRE(reformat_version("1.0.1", "abitag") == "1.0.1-vcpkgabitag");
REQUIRE(reformat_version("1.01.000", "abitag") == "1.1.0-vcpkgabitag");
REQUIRE(reformat_version("1.2", "abitag") == "1.2.0-vcpkgabitag");
REQUIRE(reformat_version("v52", "abitag") == "52.0.0-vcpkgabitag");
REQUIRE(reformat_version("v09.01.02", "abitag") == "9.1.2-vcpkgabitag");
REQUIRE(reformat_version("1.1.1q", "abitag") == "1.1.1-vcpkgabitag");
REQUIRE(reformat_version("1", "abitag") == "1.0.0-vcpkgabitag");
REQUIRE(format_version_for_nugetref("0.0.0", "abitag") == "0.0.0-vcpkgabitag");
REQUIRE(format_version_for_nugetref("1.0.1", "abitag") == "1.0.1-vcpkgabitag");
REQUIRE(format_version_for_nugetref("1.01.000", "abitag") == "1.1.0-vcpkgabitag");
REQUIRE(format_version_for_nugetref("1.2", "abitag") == "1.2.0-vcpkgabitag");
REQUIRE(format_version_for_nugetref("v52", "abitag") == "52.0.0-vcpkgabitag");
REQUIRE(format_version_for_nugetref("v09.01.02", "abitag") == "9.1.2-vcpkgabitag");
REQUIRE(format_version_for_nugetref("1.1.1q", "abitag") == "1.1.1-vcpkgabitag");
REQUIRE(format_version_for_nugetref("1", "abitag") == "1.0.0-vcpkgabitag");
}

TEST_CASE ("reformat_version date", "[reformat_version]")
TEST_CASE ("format_version_for_nugetref date", "[format_version_for_nugetref]")
{
REQUIRE(reformat_version("2020-06-26", "abitag") == "2020.6.26-vcpkgabitag");
REQUIRE(reformat_version("20-06-26", "abitag") == "0.0.0-vcpkgabitag");
REQUIRE(reformat_version("2020-06-26-release", "abitag") == "2020.6.26-vcpkgabitag");
REQUIRE(reformat_version("2020-06-26000", "abitag") == "2020.6.26-vcpkgabitag");
REQUIRE(format_version_for_nugetref("2020-06-26", "abitag") == "2020.6.26-vcpkgabitag");
REQUIRE(format_version_for_nugetref("20-06-26", "abitag") == "0.0.0-vcpkgabitag");
REQUIRE(format_version_for_nugetref("2020-06-26-release", "abitag") == "2020.6.26-vcpkgabitag");
REQUIRE(format_version_for_nugetref("2020-06-26000", "abitag") == "2020.6.26-vcpkgabitag");
}

TEST_CASE ("reformat_version generic", "[reformat_version]")
TEST_CASE ("format_version_for_nugetref generic", "[format_version_for_nugetref]")
{
REQUIRE(reformat_version("apr", "abitag") == "0.0.0-vcpkgabitag");
REQUIRE(reformat_version("", "abitag") == "0.0.0-vcpkgabitag");
REQUIRE(format_version_for_nugetref("apr", "abitag") == "0.0.0-vcpkgabitag");
REQUIRE(format_version_for_nugetref("", "abitag") == "0.0.0-vcpkgabitag");
}

TEST_CASE ("generate_nuspec", "[generate_nuspec]")
Expand Down
Loading