mirror of
https://github.com/microsoft/vcpkg.git
synced 2025-06-07 01:24:47 +08:00
[vcpkg] Optimize string split slightly. (#11433)
This commit is contained in:
parent
9a0652b870
commit
0116b86e38
@ -173,9 +173,7 @@ namespace vcpkg::Strings
|
||||
|
||||
void trim_all_and_remove_whitespace_strings(std::vector<std::string>* strings);
|
||||
|
||||
std::vector<std::string> split(const std::string& s, const std::string& delimiter);
|
||||
|
||||
std::vector<std::string> split(const std::string& s, const std::string& delimiter, size_t max_count);
|
||||
std::vector<std::string> split(const std::string& s, const char delimiter);
|
||||
|
||||
std::vector<StringView> find_all_enclosed(StringView input, StringView left_delim, StringView right_delim);
|
||||
|
||||
|
@ -2,15 +2,16 @@
|
||||
|
||||
#include <vcpkg/base/strings.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <stdint.h>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
TEST_CASE ("b32 encoding", "[strings]")
|
||||
{
|
||||
using u64 = std::uint64_t;
|
||||
using u64 = uint64_t;
|
||||
|
||||
std::vector<std::pair<std::uint64_t, std::string>> map;
|
||||
std::vector<std::pair<u64, std::string>> map;
|
||||
|
||||
map.emplace_back(0, "AAAAAAAAAAAAA");
|
||||
map.emplace_back(1, "BAAAAAAAAAAAA");
|
||||
@ -24,10 +25,19 @@ TEST_CASE ("b32 encoding", "[strings]")
|
||||
map.emplace_back(0x1405'64E7'FE7E'A88C, "MEK5H774ELBIB");
|
||||
map.emplace_back(0xFFFF'FFFF'FFFF'FFFF, "777777777777P");
|
||||
|
||||
std::string result;
|
||||
for (const auto& pr : map)
|
||||
{
|
||||
result = vcpkg::Strings::b32_encode(pr.first);
|
||||
REQUIRE(vcpkg::Strings::b32_encode(pr.first) == pr.second);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE ("split by char", "[strings]")
|
||||
{
|
||||
using vcpkg::Strings::split;
|
||||
using result_t = std::vector<std::string>;
|
||||
REQUIRE(split(",,,,,,", ',').empty());
|
||||
REQUIRE(split(",,a,,b,,", ',') == result_t{"a", "b"});
|
||||
REQUIRE(split("hello world", ' ') == result_t{"hello", "world"});
|
||||
REQUIRE(split(" hello world ", ' ') == result_t{"hello", "world"});
|
||||
REQUIRE(split("no delimiters", ',') == result_t{"no delimiters"});
|
||||
}
|
||||
|
@ -341,7 +341,7 @@ int main(const int argc, const char* const* const argv)
|
||||
const auto vcpkg_feature_flags_env = System::get_environment_variable("VCPKG_FEATURE_FLAGS");
|
||||
if (const auto v = vcpkg_feature_flags_env.get())
|
||||
{
|
||||
auto flags = Strings::split(*v, ",");
|
||||
auto flags = Strings::split(*v, ',');
|
||||
if (std::find(flags.begin(), flags.end(), "binarycaching") != flags.end()) GlobalState::g_binary_caching = true;
|
||||
}
|
||||
|
||||
|
@ -834,10 +834,10 @@ namespace vcpkg::Files
|
||||
{
|
||||
#if defined(_WIN32)
|
||||
static constexpr StringLiteral EXTS[] = {".cmd", ".exe", ".bat"};
|
||||
auto paths = Strings::split(System::get_environment_variable("PATH").value_or_exit(VCPKG_LINE_INFO), ";");
|
||||
auto paths = Strings::split(System::get_environment_variable("PATH").value_or_exit(VCPKG_LINE_INFO), ';');
|
||||
#else // ^^^ defined(_WIN32) // !defined(_WIN32) vvv
|
||||
static constexpr StringLiteral EXTS[] = {""};
|
||||
auto paths = Strings::split(System::get_environment_variable("PATH").value_or_exit(VCPKG_LINE_INFO), ":");
|
||||
auto paths = Strings::split(System::get_environment_variable("PATH").value_or_exit(VCPKG_LINE_INFO), ':');
|
||||
#endif // ^^^ !defined(_WIN32)
|
||||
|
||||
std::vector<fs::path> ret;
|
||||
|
@ -157,63 +157,23 @@ void Strings::trim_all_and_remove_whitespace_strings(std::vector<std::string>* s
|
||||
Util::erase_remove_if(*strings, [](const std::string& s) { return s.empty(); });
|
||||
}
|
||||
|
||||
std::vector<std::string> Strings::split(const std::string& s, const std::string& delimiter)
|
||||
std::vector<std::string> Strings::split(const std::string& s, const char delimiter)
|
||||
{
|
||||
std::vector<std::string> output;
|
||||
|
||||
if (delimiter.empty())
|
||||
auto first = s.begin();
|
||||
const auto last = s.end();
|
||||
for (;;)
|
||||
{
|
||||
first = std::find_if(first, last, [=](const char c) { return c != delimiter; });
|
||||
if (first == last)
|
||||
{
|
||||
output.push_back(s);
|
||||
return output;
|
||||
}
|
||||
|
||||
const size_t delimiter_length = delimiter.length();
|
||||
size_t i = 0;
|
||||
for (size_t pos = s.find(delimiter); pos != std::string::npos; pos = s.find(delimiter, pos))
|
||||
{
|
||||
output.push_back(s.substr(i, pos - i));
|
||||
pos += delimiter_length;
|
||||
i = pos;
|
||||
auto next = std::find(first, last, delimiter);
|
||||
output.emplace_back(first, next);
|
||||
first = next;
|
||||
}
|
||||
|
||||
// Add the rest of the string after the last delimiter, unless there is nothing after it
|
||||
if (i != s.length())
|
||||
{
|
||||
output.push_back(s.substr(i, s.length()));
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
std::vector<std::string> Strings::split(const std::string& s, const std::string& delimiter, size_t max_count)
|
||||
{
|
||||
std::vector<std::string> output;
|
||||
|
||||
Checks::check_exit(VCPKG_LINE_INFO, max_count >= 1);
|
||||
|
||||
if (delimiter.empty())
|
||||
{
|
||||
output.push_back(s);
|
||||
return output;
|
||||
}
|
||||
|
||||
const size_t delimiter_length = delimiter.length();
|
||||
size_t i = 0;
|
||||
for (size_t pos = s.find(delimiter); pos != std::string::npos; pos = s.find(delimiter, pos))
|
||||
{
|
||||
if (output.size() == max_count - 1) break;
|
||||
output.push_back(s.substr(i, pos - i));
|
||||
pos += delimiter_length;
|
||||
i = pos;
|
||||
}
|
||||
|
||||
// Add the rest of the string after the last delimiter, unless there is nothing after it
|
||||
if (i != s.length())
|
||||
{
|
||||
output.push_back(s.substr(i, s.length()));
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
std::vector<StringView> Strings::find_all_enclosed(StringView input, StringView left_delim, StringView right_delim)
|
||||
@ -223,7 +183,7 @@ std::vector<StringView> Strings::find_all_enclosed(StringView input, StringView
|
||||
|
||||
std::vector<StringView> output;
|
||||
|
||||
while (true)
|
||||
for (;;)
|
||||
{
|
||||
it_left = std::search(it_right, input.end(), left_delim.begin(), left_delim.end());
|
||||
if (it_left == input.end()) break;
|
||||
|
@ -267,7 +267,7 @@ namespace vcpkg
|
||||
|
||||
if (k && !k->empty())
|
||||
{
|
||||
auto vars = Strings::split(*k, ";");
|
||||
auto vars = Strings::split(*k, ';');
|
||||
|
||||
for (auto&& var : vars)
|
||||
{
|
||||
|
@ -1057,7 +1057,7 @@ namespace vcpkg::Build
|
||||
VCPKG_LINE_INFO, "Unknown setting for VCPKG_BUILD_TYPE: %s", variable_value);
|
||||
break;
|
||||
case VcpkgTripletVar::ENV_PASSTHROUGH:
|
||||
passthrough_env_vars = Strings::split(variable_value, ";");
|
||||
passthrough_env_vars = Strings::split(variable_value, ';');
|
||||
break;
|
||||
case VcpkgTripletVar::PUBLIC_ABI_OVERRIDE:
|
||||
public_abi_override = variable_value.empty() ? nullopt : Optional<std::string>{variable_value};
|
||||
|
@ -174,7 +174,7 @@ namespace vcpkg::CMakeVars
|
||||
const auto ec_data = System::cmd_execute_and_capture_output(cmd_launch_cmake);
|
||||
Checks::check_exit(VCPKG_LINE_INFO, ec_data.exit_code == 0, ec_data.output);
|
||||
|
||||
const std::vector<std::string> lines = Strings::split(ec_data.output, "\n");
|
||||
const std::vector<std::string> lines = Strings::split(ec_data.output, '\n');
|
||||
|
||||
const auto end = lines.cend();
|
||||
|
||||
@ -192,7 +192,7 @@ namespace vcpkg::CMakeVars
|
||||
{
|
||||
const std::string& line = *block_start;
|
||||
|
||||
std::vector<std::string> s = Strings::split(line, "=");
|
||||
std::vector<std::string> s = Strings::split(line, '=');
|
||||
Checks::check_exit(VCPKG_LINE_INFO,
|
||||
s.size() == 1 || s.size() == 2,
|
||||
"Expected format is [VARIABLE_NAME=VARIABLE_VALUE], but was [%s]",
|
||||
|
@ -30,7 +30,7 @@ namespace vcpkg::Commands::Autocomplete
|
||||
{
|
||||
Metrics::g_metrics.lock()->set_send_metrics(false);
|
||||
const std::string to_autocomplete = Strings::join(" ", args.command_arguments);
|
||||
const std::vector<std::string> tokens = Strings::split(to_autocomplete, " ");
|
||||
const std::vector<std::string> tokens = Strings::split(to_autocomplete, ' ');
|
||||
|
||||
std::smatch match;
|
||||
|
||||
|
@ -407,7 +407,7 @@ namespace vcpkg::Commands::CI
|
||||
auto it_exclusions = options.settings.find(OPTION_EXCLUDE);
|
||||
if (it_exclusions != options.settings.end())
|
||||
{
|
||||
auto exclusions = Strings::split(it_exclusions->second, ",");
|
||||
auto exclusions = Strings::split(it_exclusions->second, ',');
|
||||
exclusions_set.insert(exclusions.begin(), exclusions.end());
|
||||
}
|
||||
|
||||
|
@ -48,8 +48,8 @@ namespace vcpkg::Commands::PortHistory
|
||||
auto output = run_git_command(paths, cmd);
|
||||
|
||||
auto commits = Util::fmap(
|
||||
Strings::split(output.output, "\n"), [](const std::string& line) -> auto {
|
||||
auto parts = Strings::split(line, " ");
|
||||
Strings::split(output.output, '\n'), [](const std::string& line) -> auto {
|
||||
auto parts = Strings::split(line, ' ');
|
||||
return std::make_pair(parts[0], parts[1]);
|
||||
});
|
||||
|
||||
|
@ -55,7 +55,7 @@ namespace vcpkg
|
||||
auto override_vars = evaluation_context.cmake_context.find("VCPKG_DEP_INFO_OVERRIDE_VARS");
|
||||
if (override_vars != evaluation_context.cmake_context.end())
|
||||
{
|
||||
auto cmake_list = Strings::split(override_vars->second, ";");
|
||||
auto cmake_list = Strings::split(override_vars->second, ';');
|
||||
for (auto& override_id : cmake_list)
|
||||
{
|
||||
if (!override_id.empty())
|
||||
|
Loading…
Reference in New Issue
Block a user