Skip to content

Commit

Permalink
refactor: Use ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
slavek-kucera authored May 27, 2024
1 parent 0d5aee8 commit ec6b856
Show file tree
Hide file tree
Showing 100 changed files with 516 additions and 644 deletions.
3 changes: 2 additions & 1 deletion benchmark/diagnostic_counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "diagnostic.h"
#include "nlohmann/json.hpp"
#include "protocol.h"
#include "utils/projectors.h"
#include "workspace_manager.h"

namespace hlasm_plugin::benchmark {
Expand Down Expand Up @@ -62,7 +63,7 @@ inline nlohmann::json get_top_messages(const std::unordered_map<std::string, uns

constexpr const auto cmp_msg = [](const auto& a, const auto& b) { return a.second > b.second; };

const auto last = std::partial_sort_copy(msgs.begin(), msgs.end(), top_msgs.begin(), top_msgs.end(), cmp_msg);
const auto [_, last] = std::ranges::partial_sort_copy(msgs, top_msgs, cmp_msg);
top_msgs.erase(last, top_msgs.end());

nlohmann::json result = nlohmann::json::object();
Expand Down
15 changes: 7 additions & 8 deletions language_server/src/lsp/feature_workspace_folders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,13 @@ void feature_workspace_folders::did_change_watched_files(const nlohmann::json& p
const auto& json_changes = params.at("changes");
std::vector<fs_change> changes;
changes.reserve(json_changes.size());
std::transform(
json_changes.begin(), json_changes.end(), std::back_inserter(changes), [](const nlohmann::json& change) {
auto uri = change.at("uri").get<std::string_view>();
auto type = change.at("type").get<long long>();
if (type < 1 || type > 3)
type = 0;
return fs_change { uri, static_cast<fs_change_type>(type) };
});
std::ranges::transform(json_changes, std::back_inserter(changes), [](const nlohmann::json& change) {
auto uri = change.at("uri").get<std::string_view>();
auto type = change.at("type").get<long long>();
if (type < 1 || type > 3)
type = 0;
return fs_change { uri, static_cast<fs_change_type>(type) };
});
ws_mngr_.did_change_watched_files(changes);
}
catch (const nlohmann::json::exception& j)
Expand Down
15 changes: 6 additions & 9 deletions language_server/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,15 @@ class main_program final : public json_sink,

auto separate_arguments(int argc, char** argv)
{
auto start = std::find_if(argv + !!argc, argv + argc, [](std::string_view arg) { return arg == "--hlasm-start"; });
auto end = std::find_if(start, argv + argc, [](std::string_view arg) { return arg == "--hlasm-end"; });
auto first = std::find_if(argv + !!argc, argv + argc, [](std::string_view arg) { return arg == "--hlasm-start"; });
auto last = std::find_if(first, argv + argc, [](std::string_view arg) { return arg == "--hlasm-end"; });

if (end == argv + argc)
{
start = argv + !!argc;
end = argv + argc;
}
if (last == argv + argc)
first = argv + !!argc;
else
++start;
++first;

return std::span<const char* const>(start, end - start);
return std::span<const char* const>(first, last - first);
}

void log_options(server_options opts)
Expand Down
15 changes: 6 additions & 9 deletions language_server/test/regress_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ TEST(regress_test, behaviour_error)
ws_mngr->idle_handler();

ASSERT_EQ(mess_p.notfs.size(), (size_t)3); // diags+open+parsing
auto publish_notif = std::find_if(mess_p.notfs.begin(), mess_p.notfs.end(), [&](nlohmann::json notif) {
return notif["method"] == "textDocument/publishDiagnostics";
});
auto publish_notif = std::ranges::find_if(
mess_p.notfs, [](const auto& notif) { return notif["method"] == "textDocument/publishDiagnostics"; });
ASSERT_NE(publish_notif, mess_p.notfs.end());
ASSERT_EQ((*publish_notif)["method"], "textDocument/publishDiagnostics");
auto diagnostics = (*publish_notif)["params"]["diagnostics"];
Expand Down Expand Up @@ -114,9 +113,8 @@ TEST(regress_test, behaviour_error)
s.message_received(notf);
ws_mngr->idle_handler();

auto notfs_it = std::find_if(mess_p.notfs.begin(), mess_p.notfs.end(), [](const auto& msg_notification) {
return msg_notification["method"] == "textDocument/publishDiagnostics";
});
auto notfs_it = std::ranges::find_if(
mess_p.notfs, [](const auto& msg) { return msg["method"] == "textDocument/publishDiagnostics"; });
ASSERT_NE(notfs_it, mess_p.notfs.end());
EXPECT_EQ((*notfs_it)["params"]["diagnostics"].size(), (size_t)0);

Expand Down Expand Up @@ -642,9 +640,8 @@ TEST(regress_test, check_diagnostic_tags)
ws_mngr->idle_handler();

ASSERT_EQ(mess_p.notfs.size(), (size_t)4); // diags+open+parsing+output
auto publish_notif = std::find_if(mess_p.notfs.begin(), mess_p.notfs.end(), [&](nlohmann::json notif) {
return notif["method"] == "textDocument/publishDiagnostics";
});
auto publish_notif = std::ranges::find_if(
mess_p.notfs, [](const auto& msg) { return msg["method"] == "textDocument/publishDiagnostics"; });
ASSERT_NE(publish_notif, mess_p.notfs.end());
ASSERT_EQ((*publish_notif)["method"], "textDocument/publishDiagnostics");
auto diagnostics = (*publish_notif)["params"]["diagnostics"];
Expand Down
3 changes: 1 addition & 2 deletions parser_library/src/analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ std::unique_ptr<processing::preprocessor> analyzer_options::get_preprocessor(pro
}
} tmp;

std::transform(
preprocessor_args.begin(), preprocessor_args.end(), std::back_inserter(tmp.pp), transform_preprocessor);
std::ranges::transform(preprocessor_args, std::back_inserter(tmp.pp), transform_preprocessor);

return std::make_unique<combined_preprocessor>(std::move(tmp));
}
Expand Down
8 changes: 3 additions & 5 deletions parser_library/src/checking/asm_instr_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1287,11 +1287,9 @@ bool process::check(std::span<const asm_operand* const> to_check,
&& process_operands->operand_identifier == "OVERRIDE") // everything parsed is parameter of operand
{
--e;
if (!std::all_of(process_operands->operand_parameters.cbegin(),
process_operands->operand_parameters.cend(),
[this, &add_diagnostic](const auto& parameter) {
return check_assembler_process_operand(parameter.get(), add_diagnostic);
}))
if (!std::ranges::all_of(process_operands->operand_parameters, [this, &add_diagnostic](const auto& parameter) {
return check_assembler_process_operand(parameter.get(), add_diagnostic);
}))
return false;
}
// everything else is an operand
Expand Down
7 changes: 3 additions & 4 deletions parser_library/src/checking/asm_instr_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ using namespace hlasm_plugin::parser_library::checking;
bool assembler_instruction::is_param_in_vector(
std::string_view parameter, const std::vector<std::string_view>& options) const
{
return std::find(options.cbegin(), options.cend(), parameter) != options.cend();
return std::ranges::find(options, parameter) != options.cend();
}

bool assembler_instruction::operands_size_corresponding(std::span<const asm_operand* const> to_check,
Expand Down Expand Up @@ -166,8 +166,7 @@ bool assembler_instruction::check_optable_operands(const std::vector<std::unique
if (input.size() == 2)
second = get_simple_operand(input[1].get());
// check first parameter
if (first == nullptr
|| std::find(optable_array.cbegin(), optable_array.cend(), first->operand_identifier) == optable_array.end())
if (first == nullptr || std::ranges::find(optable_array, first->operand_identifier) == optable_array.end())
{
// first parameter was wrong
add_diagnostic(diagnostic_op::error_A212_OPTABLE_first_op(instr_name, input[0]->operand_range));
Expand Down Expand Up @@ -238,7 +237,7 @@ bool assembler_instruction::check_codepage_parameter(
// decimal value
else
{
if (!std::all_of(input_str.cbegin(), input_str.cend(), [](unsigned char c) { return std::isdigit(c); }))
if (!std::ranges::all_of(input_str, [](unsigned char c) { return std::isdigit(c); }))
{
add_diagnostic(diagnostic_op::error_A215_CODEPAGE_format(name_of_instruction, input.operand_range));
return false;
Expand Down
9 changes: 4 additions & 5 deletions parser_library/src/checking/checker_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ inline bool has_one_comma(std::span<const asm_operand* const> to_check)

inline bool has_all_digits(std::string_view str)
{
return std::all_of(str.begin(), str.end(), [](unsigned char c) { return std::isdigit(c); });
return std::ranges::all_of(str, [](unsigned char c) { return std::isdigit(c); });
}

// function to convert numbers less than 64000 to hexadecimal
Expand All @@ -76,14 +76,13 @@ inline std::string dec_to_hexa(int to_convert)
{
result.push_back("0123456789ABCDEF"[to_convert % 16]);
} while (to_convert /= 16);
std::reverse(result.begin(), result.end());
std::ranges::reverse(result);
return result;
}

inline bool is_value_hexa(std::string_view to_test)
{
return !to_test.empty()
&& std::all_of(to_test.cbegin(), to_test.cend(), [](unsigned char c) { return std::isxdigit(c); });
return !to_test.empty() && std::ranges::all_of(to_test, [](unsigned char c) { return std::isxdigit(c); });
}

inline bool is_byte_value(const int to_test) { return (to_test <= 255 && to_test >= 0); }
Expand All @@ -99,7 +98,7 @@ inline bool is_ord_symbol(std::string_view to_test)
{
assert(!to_test.empty());
return !to_test.empty() && to_test.size() <= 63 && isalpha((unsigned char)to_test.front())
&& std::all_of(to_test.cbegin(), to_test.cend(), [](unsigned char c) { return std::isalnum(c); });
&& std::ranges::all_of(to_test, [](unsigned char c) { return std::isalnum(c); });
}

inline bool is_var_symbol(std::string_view to_test)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,7 @@ size_t data_def_type::get_number_of_values_in_nominal(const reduced_nominal_valu
if (type == 'C' || type == 'G') // C and G do not support multiple nominal values
return 1;
else if (std::holds_alternative<std::string>(nom.value))
{
const std::string& s = std::get<std::string>(nom.value);
return std::count(s.begin(), s.end(), ',') + 1;
}
return std::ranges::count(std::get<std::string>(nom.value), ',') + 1;
else
return std::get<size_t>(nom.value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ uint64_t data_def_type_Z::get_nominal_length(const reduced_nominal_value_t& op)

// each digit is assembled as one byte

return std::count_if(s.cbegin(), s.cend(), &is_digit);
return std::ranges::count_if(s, &is_digit);
}

uint32_t data_def_type_Z::get_nominal_length_attribute(const reduced_nominal_value_t& op) const
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ matched_special_value try_matching_special_value(

for (auto s : list)
{
if (auto to_match = num.substr(start, s.size());
!std::equal(to_match.begin(), to_match.end(), s.begin(), s.end(), [](unsigned char l, unsigned char r) {
if (!std::ranges::equal(num.substr(start, s.size()), s, [](unsigned char l, unsigned char r) {
return toupper(l) == toupper(r);
}))
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,24 +254,21 @@ uint64_t data_def_type_G::get_nominal_length(const reduced_nominal_value_t& op)
{
const std::string& s = std::get<std::string>(op.value);
return utils::length_utf32_no_validation(s)
- std::count_if(s.begin(), s.end(), [](char c) { return c == '<' || c == '>'; });
- std::ranges::count_if(s, [](char c) { return c == '<' || c == '>'; });
}
}

uint32_t data_def_type_G::get_nominal_length_attribute(const reduced_nominal_value_t& nom) const
{
if (!nom.present)
return 2;
else if (!std::holds_alternative<std::string>(nom.value))
return 0;
else
{
if (!std::holds_alternative<std::string>(nom.value))
return 0;
else
{
const std::string& s = std::get<std::string>(nom.value);
return (uint32_t)(utils::length_utf32_no_validation(s)
- std::count_if(s.begin(), s.end(), [](char c) { return c == '<' || c == '>'; }));
}
const std::string& s = std::get<std::string>(nom.value);
return (uint32_t)(utils::length_utf32_no_validation(s)
- std::ranges::count_if(s, [](char c) { return c == '<' || c == '>'; }));
}
}

Expand Down
5 changes: 3 additions & 2 deletions parser_library/src/config/assembler_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "compiler_options.h"
#include "nlohmann/json.hpp"
#include "utils/projectors.h"
#include "utils/string_operations.h"

namespace hlasm_plugin::parser_library::config {
Expand Down Expand Up @@ -125,7 +126,7 @@ bool instr_set_equivalent_valid(
utils::to_upper(instr_set_name);

return instr_set_name.size() == 0
|| std::ranges::any_of(equivalents, [&instr_set_name](auto item) { return instr_set_name == item.first; });
|| std::ranges::find(equivalents, instr_set_name, utils::first_element) != equivalents.end();
}
} // namespace

Expand All @@ -148,7 +149,7 @@ std::optional<instruction_set_version> find_instruction_set(
{
utils::to_upper(instr_set_name);

auto it = std::ranges::lower_bound(equivalents, instr_set_name, {}, [](const auto& instr) { return instr.first; });
auto it = std::ranges::lower_bound(equivalents, instr_set_name, {}, utils::first_element);

if (it == std::end(equivalents) || it->first != instr_set_name)
return std::nullopt;
Expand Down
7 changes: 3 additions & 4 deletions parser_library/src/config/proc_grps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "assembler_options.h"
#include "instruction_set_version.h"
#include "nlohmann/json.hpp"
#include "utils/projectors.h"

namespace hlasm_plugin::parser_library::config {

Expand Down Expand Up @@ -345,9 +346,7 @@ constexpr const auto preprocessor_list = preprocessor_list_generator<decltype(pr

constexpr auto find_preprocessor_deserializer(std::string_view name)
{
auto it = std::find_if(std::begin(preprocessor_list), std::end(preprocessor_list), [&name](const auto& entry) {
return entry.first == name;
});
auto it = std::ranges::find(preprocessor_list, name, utils::first_element);
return it != std::end(preprocessor_list) ? it->second : nullptr;
}

Expand All @@ -359,7 +358,7 @@ void add_single_preprocessor(std::vector<preprocessor_options>& preprocessors, c
else if (j.is_object())
j.at("name").get_to(p_name);

std::transform(p_name.begin(), p_name.end(), p_name.begin(), [](unsigned char c) { return (char)toupper(c); });
std::ranges::transform(p_name, p_name.begin(), [](unsigned char c) { return (char)toupper(c); });
if (auto deserializer = find_preprocessor_deserializer(p_name); deserializer)
deserializer(preprocessors, j);
else
Expand Down
8 changes: 3 additions & 5 deletions parser_library/src/context/id_index.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef CONTEXT_ID_INDEX_H
#define CONTEXT_ID_INDEX_H

#include <algorithm>
#include <cassert>
#include <compare>
#include <cstring>
Expand All @@ -39,7 +40,7 @@ class id_index
{
assert(s.size() < buffer_size);
if (std::is_constant_evaluated())
std::copy(s.begin(), s.end(), m_buffer);
std::ranges::copy(s, m_buffer);
else
std::memcpy(m_buffer, s.data(), s.size());
m_buffer[buffer_size - 1] = (char)s.size();
Expand Down Expand Up @@ -82,10 +83,7 @@ class id_index
}
}

constexpr bool operator==(const id_index& o) const noexcept
{
return std::equal(std::begin(m_buffer), std::end(m_buffer), std::begin(o.m_buffer), std::end(o.m_buffer));
}
constexpr bool operator==(const id_index& o) const noexcept { return std::ranges::equal(m_buffer, o.m_buffer); }

std::string_view to_string_view() const noexcept
{
Expand Down
2 changes: 1 addition & 1 deletion parser_library/src/context/id_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ using namespace hlasm_plugin::parser_library::context;
id_index id_storage::small_id(std::string_view value)
{
char buf[id_index::buffer_size];
char* end = std::transform(value.begin(), value.end(), buf, [](unsigned char c) { return utils::upper_cased[c]; });
const auto [_, end] = std::ranges::transform(value, buf, [](unsigned char c) { return utils::upper_cased[c]; });
return id_index(std::string_view(buf, end - buf));
}

Expand Down
10 changes: 5 additions & 5 deletions parser_library/src/context/instruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1988,7 +1988,7 @@ constexpr const auto combined_machine_instruction_table = []() {
const auto mn = mnemonic_codes[mnemo_i].name();
const auto [name, idx] = ma <= mn ? std::pair(ma, mach_i++) : std::pair(mn, m1 - mnemo_i++);
assert(name.size() <= limit);
std::copy(name.begin(), name.end(), names[out_i].begin());
std::ranges::copy(name, names[out_i].begin());
offsets[out_i] = (short)idx;
}

Expand All @@ -1997,7 +1997,7 @@ constexpr const auto combined_machine_instruction_table = []() {
const auto& m = machine_instructions[mach_i];
const auto name = m.name();
assert(name.size() <= limit);
std::copy(name.begin(), name.end(), names[out_i].begin());
std::ranges::copy(name, names[out_i].begin());
offsets[out_i] = (short)mach_i;
}

Expand All @@ -2006,7 +2006,7 @@ constexpr const auto combined_machine_instruction_table = []() {
const auto& mn = mnemonic_codes[mnemo_i];
const auto name = mn.name();
assert(name.size() <= limit);
std::copy(name.begin(), name.end(), names[out_i].begin());
std::ranges::copy(name, names[out_i].begin());
offsets[out_i] = (short)(m1 - mnemo_i);
}

Expand All @@ -2023,9 +2023,9 @@ std::pair<const machine_instruction*, const mnemonic_code*> instruction::find_ma
const auto& [mach_instr_names, mach_instr_offsets] = combined_machine_instruction_table;

std::array<char, combined_machine_instruction_name_limit> padded_name {};
std::copy(name.begin(), name.end(), padded_name.begin());
std::ranges::copy(name, padded_name.begin());

auto it = std::lower_bound(std::begin(mach_instr_names), std::end(mach_instr_names), padded_name);
auto it = std::ranges::lower_bound(mach_instr_names, padded_name);
if (it == std::end(mach_instr_names) || *it != padded_name)
return {};

Expand Down
4 changes: 1 addition & 3 deletions parser_library/src/context/literal_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,7 @@ void literal_pool::generate_pool(diagnosable_ctx& diags, index_t<using_collectio
alignment = (~top_alignment & top_alignment - 1) + 1;
}

std::stable_sort(m_pending_literals.begin(), m_pending_literals.end(), [](const auto& l, const auto& r) {
return l.alignment > r.alignment;
});
std::ranges::stable_sort(m_pending_literals, std::ranges::greater(), &pending_literal::alignment);

constexpr auto sectalign = doubleword;
ord_ctx.align(sectalign, li);
Expand Down
Loading

0 comments on commit ec6b856

Please sign in to comment.