Skip to content

Commit

Permalink
Billy CRs
Browse files Browse the repository at this point in the history
* put is_hex_digit and is_word_char into parserbase
* try_parse_external_*_version -> try_extract_external_*_version
* add ZERO_MAC constant
* add consts to `last`
* make `is_character_ref` a function
  • Loading branch information
strega-nil committed Mar 16, 2022
1 parent d4b470d commit 399b13a
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 77 deletions.
9 changes: 9 additions & 0 deletions include/vcpkg/base/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ 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
1 change: 0 additions & 1 deletion include/vcpkg/base/strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <limits.h>

#include <algorithm>
#include <charconv>
#include <vector>

namespace vcpkg::Strings::details
Expand Down
4 changes: 2 additions & 2 deletions include/vcpkg/versions.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ namespace vcpkg

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

VCPKG_FORMAT_WITH_TO_STRING(vcpkg::VersionSpec);
21 changes: 6 additions & 15 deletions src/vcpkg/base/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,23 +482,14 @@ namespace vcpkg::Json
return Parse::ParserBase::next();
}

static constexpr bool is_digit(char32_t code_point) noexcept
{
return code_point >= '0' && code_point <= '9';
}
static constexpr bool is_hex_digit(char32_t code_point) noexcept
{
return is_digit(code_point) || (code_point >= 'a' && code_point <= 'f') ||
(code_point >= 'A' && code_point <= 'F');
}
static bool is_number_start(char32_t code_point) noexcept
{
return code_point == '-' || is_digit(code_point);
return code_point == '-' || is_ascii_digit(code_point);
}

static unsigned char from_hex_digit(char32_t code_point) noexcept
{
if (is_digit(code_point))
if (is_ascii_digit(code_point))
{
return static_cast<unsigned char>(code_point) - '0';
}
Expand Down Expand Up @@ -664,7 +655,7 @@ namespace vcpkg::Json
floating = true;
current = next();
}
else if (is_digit(current))
else if (is_ascii_digit(current))
{
add_error("Unexpected digits after a leading zero");
return Value();
Expand All @@ -682,7 +673,7 @@ namespace vcpkg::Json
}
}

while (is_digit(current))
while (is_ascii_digit(current))
{
number_to_parse.push_back(static_cast<char>(current));
current = next();
Expand All @@ -692,12 +683,12 @@ namespace vcpkg::Json
floating = true;
number_to_parse.push_back('.');
current = next();
if (!is_digit(current))
if (!is_ascii_digit(current))
{
add_error("Expected digits after the decimal point");
return Value();
}
while (is_digit(current))
while (is_ascii_digit(current))
{
number_to_parse.push_back(static_cast<char>(current));
current = next();
Expand Down
4 changes: 2 additions & 2 deletions src/vcpkg/binarycaching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2163,7 +2163,7 @@ std::string vcpkg::format_version_for_nugetref(StringView version, StringView ab
// and ignores random extra stuff from the end

ParsedExternalVersion parsed_version;
if (try_parse_external_date_version(parsed_version, version))
if (try_extract_external_date_version(parsed_version, version))
{
parsed_version.normalize();
return fmt::format(
Expand All @@ -2174,7 +2174,7 @@ std::string vcpkg::format_version_for_nugetref(StringView version, StringView ab
{
version = version.substr(1);
}
if (try_parse_external_dot_version(parsed_version, version))
if (try_extract_external_dot_version(parsed_version, version))
{
parsed_version.normalize();
return fmt::format(
Expand Down
4 changes: 1 addition & 3 deletions src/vcpkg/commands.xdownload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ namespace vcpkg::Commands::X_Download

static bool is_hex(StringView sha)
{
return std::all_of(sha.begin(), sha.end(), [](char ch) {
return (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
});
return std::all_of(sha.begin(), sha.end(), Parse::ParserBase::is_hex_digit);
}
static bool is_sha512(StringView sha) { return sha.size() == 128 && is_hex(sha); }

Expand Down
80 changes: 38 additions & 42 deletions src/vcpkg/export.ifw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,33 @@ namespace vcpkg::Export::IFW
using Dependencies::ExportPlanType;
using Install::InstallDir;

// requires: after_prefix <= semi
// requires: *semi == ';'
static bool is_character_ref(const char* after_prefix, const char* semi)
{
if (after_prefix == semi)
{
return false;
}

if (*after_prefix == '#')
{
++after_prefix;
if (*after_prefix == 'x')
{
++after_prefix;
// hex character escape: &#xABC;
return after_prefix != semi && std::all_of(after_prefix, semi, Parse::ParserBase::is_hex_digit);
}

// decimal character escape: &#123;
return after_prefix != semi && std::all_of(after_prefix, semi, Parse::ParserBase::is_ascii_digit);
}

// word character escape: &amp;
return std::all_of(after_prefix, semi, Parse::ParserBase::is_word_char);
}

std::string safe_rich_from_plain_text(StringView text)
{
// looking for `&`, not followed by:
Expand All @@ -23,72 +50,41 @@ namespace vcpkg::Export::IFW
// - `<numbers, letters, or _>;`
// (basically, an HTML character entity reference)
using P = Parse::ParserBase;
constexpr static auto is_word = [](char ch) { return P::is_alphanum(ch) || ch == '_'; };
constexpr static auto is_hex_digit = [](char ch) {
return P::is_ascii_digit(ch) || (ch >= 'A' && ch <= 'F') || (ch >= 'a' && ch <= 'f');
};
constexpr static char escaped_amp[] = "&amp;";
constexpr static StringLiteral escaped_amp = "&amp;";

auto first = text.begin();
auto last = text.end();
const auto last = text.end();

std::string result;
for (;;)
{
auto amp = std::find(first, last, '&');
result.append(first, amp);
first = amp;
if (first == last) break;
if (first == last)
{
break;
}

++first; // skip amp
if (first == last)
{
result.append(escaped_amp);
break;
}
else if (*first == '#')
{
// &#123;
++first; // skip hash
auto semi = std::find(first, last, ';');
bool is_char_ref = false;
if (semi != last && semi != first)
{
if (*first == 'x' && semi != first + 1 && std::all_of(first + 1, semi, is_hex_digit))
{
is_char_ref = true;
}
else if (std::all_of(first, semi, P::is_ascii_digit))
{
is_char_ref = true;
}
}

if (is_char_ref)
{
result.append(amp, semi);
}
else
{
result.append(escaped_amp);
result.push_back('#');
result.append(first, semi);
}
first = semi;
}
else
{
// &lt;
auto semi = std::find(first, last, ';');
if (semi != last && semi != first && std::all_of(first, semi, is_word))

if (semi != last && is_character_ref(first, semi))
{
result.append(amp, semi);
first = amp;
}
else
{
result.append(escaped_amp);
result.append(first, semi);
result.append(escaped_amp.begin(), escaped_amp.end());
}
result.append(first, semi);
first = semi;
}
}
Expand Down
14 changes: 6 additions & 8 deletions src/vcpkg/metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,17 @@ namespace vcpkg

Optional<StringView> find_first_nonzero_mac(StringView sv)
{
constexpr static auto is_hex_digit = [](char ch) {
return Parse::ParserBase::is_ascii_digit(ch) || (ch >= 'A' && ch <= 'F') || (ch >= 'a' && ch <= 'f');
};
static constexpr StringLiteral ZERO_MAC = "00-00-00-00-00-00";

auto first = sv.begin();
auto last = sv.end();
const auto last = sv.end();

while (first != last)
{
// XX-XX-XX-XX-XX-XX
// 1 2 3 4 5 6
// size = 6 * 2 + 5 = 17
first = std::find_if(first, last, is_hex_digit);
first = std::find_if(first, last, Parse::ParserBase::is_hex_digit);
if (last - first < 17)
{
break;
Expand All @@ -55,21 +53,21 @@ namespace vcpkg
}
is_first = false;

if (!is_hex_digit(*end_of_mac))
if (!Parse::ParserBase::is_hex_digit(*end_of_mac))
{
is_valid = false;
break;
}
++end_of_mac;

if (!is_hex_digit(*end_of_mac))
if (!Parse::ParserBase::is_hex_digit(*end_of_mac))
{
is_valid = false;
break;
}
++end_of_mac;
}
if (is_valid && StringView{first, end_of_mac} != "00-00-00-00-00-00")
if (is_valid && StringView{first, end_of_mac} != ZERO_MAC)
{
return StringView{first, end_of_mac};
}
Expand Down
2 changes: 1 addition & 1 deletion src/vcpkg/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace vcpkg
return nullopt;
}

if (try_parse_external_dot_version(parsed_version, StringView{first, last}) &&
if (try_extract_external_dot_version(parsed_version, StringView{first, last}) &&
!parsed_version.minor.empty())
{
break;
Expand Down
6 changes: 3 additions & 3 deletions src/vcpkg/versions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ namespace vcpkg
ExpectedL<DateVersion> DateVersion::try_parse(StringView version)
{
ParsedExternalVersion parsed;
if (!try_parse_external_date_version(parsed, version))
if (!try_extract_external_date_version(parsed, version))
{
return format_invalid_date_version(version);
}
Expand Down Expand Up @@ -426,7 +426,7 @@ namespace vcpkg
}

// /(\d\d\d\d)-(\d\d)-(\d\d).*/
bool try_parse_external_date_version(ParsedExternalVersion& out, StringView version)
bool try_extract_external_date_version(ParsedExternalVersion& out, StringView version)
{
using P = vcpkg::Parse::ParserBase;
// a b c d - e f - g h <end>
Expand All @@ -453,7 +453,7 @@ namespace vcpkg
}

// /(\d+)(\.\d+|$)(\.\d+)?.*/
bool try_parse_external_dot_version(ParsedExternalVersion& out, StringView version)
bool try_extract_external_dot_version(ParsedExternalVersion& out, StringView version)
{
using P = vcpkg::Parse::ParserBase;
auto first = version.begin();
Expand Down

0 comments on commit 399b13a

Please sign in to comment.