Skip to content

Commit 9df865c

Browse files
authored
refactor: to use std::ranges where applicable (#542)
1 parent 2edea6d commit 9df865c

File tree

7 files changed

+35
-34
lines changed

7 files changed

+35
-34
lines changed

.clang-tidy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Enable ALL the things! Except not really useful ones, needs to be a string
33
Checks: >
44
*,
5-
-*-use-ranges,
5+
-misc-no-recursion,
66
-*c++98-compat-*,
77
-altera-*,
88
-bugprone-easily-swappable-parameters,

source/builtin/builtin.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// SPDX-License-Identifier: MIT-0
33

44
#include <algorithm>
5-
#include <cctype>
65
#include <functional>
76
#include <iterator>
7+
#include <ranges>
88
#include <string>
99
#include <utility>
1010
#include <vector>
@@ -144,7 +144,7 @@ const builtin rest {
144144
if (maybe_string_or_array->is(array)) {
145145
if (const auto& arr = maybe_string_or_array->as<array_object>()->value; arr.size() > 1) {
146146
array_object::value_type rest;
147-
std::copy(arr.cbegin() + 1, arr.cend(), std::back_inserter(rest));
147+
std::ranges::copy(arr | std::ranges::views::drop(1), std::back_inserter(rest));
148148
return allocate<array_object>(std::move(rest));
149149
}
150150
return null();

source/code/code.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
#include <array>
55
#include <cstdint>
6+
#include <iterator>
67
#include <optional>
78
#include <ostream>
9+
#include <ranges>
810
#include <stdexcept>
911
#include <string>
1012
#include <utility>
@@ -251,7 +253,7 @@ auto flatten(const std::vector<std::vector<T>>& arrs) -> std::vector<T>
251253
{
252254
std::vector<T> result;
253255
for (const auto& arr : arrs) {
254-
std::copy(arr.cbegin(), arr.cend(), std::back_inserter(result));
256+
std::ranges::copy(arr, std::back_inserter(result));
255257
}
256258
return result;
257259
}

source/compiler/compiler.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <cstddef>
88
#include <iterator>
99
#include <optional>
10+
#include <ranges>
1011
#include <stdexcept>
1112
#include <string>
1213
#include <utility>
@@ -76,7 +77,7 @@ auto compiler::add_instructions(const instructions& ins) -> std::size_t
7677
{
7778
auto& scope = m_scopes[m_scope_index];
7879
const auto pos = scope.instrs.size();
79-
std::copy(ins.cbegin(), ins.cend(), std::back_inserter(scope.instrs));
80+
std::ranges::copy(ins, std::back_inserter(scope.instrs));
8081
return pos;
8182
}
8283

@@ -118,9 +119,9 @@ auto compiler::replace_last_pop_with_return() -> void
118119
auto compiler::replace_instruction(const std::size_t pos, const instructions& instr) -> void
119120
{
120121
// cppcheck-suppress variableScope
121-
auto& scope = m_scopes[m_scope_index];
122+
auto& target_instrs = m_scopes[m_scope_index].instrs;
122123
for (auto idx = 0UL; const auto& inst : instr) {
123-
scope.instrs[pos + idx] = inst;
124+
target_instrs[pos + idx] = inst;
124125
idx++;
125126
}
126127
}
@@ -549,7 +550,7 @@ auto flatten(const std::vector<std::vector<T>>& arrs) -> std::vector<T>
549550
{
550551
std::vector<T> result;
551552
for (const auto& arr : arrs) {
552-
std::copy(arr.cbegin(), arr.cend(), std::back_inserter(result));
553+
std::ranges::copy(arr, std::back_inserter(result));
553554
}
554555
return result;
555556
}

source/compiler/compiler.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ struct compiler final : visitor
4545

4646
[[nodiscard]] auto add_constant(const object* obj) -> std::size_t;
4747
[[nodiscard]] auto add_instructions(const instructions& ins) -> std::size_t;
48+
4849
auto emit(opcodes opcode, const operands& operands = {}) -> std::size_t;
4950

5051
auto emit(const opcodes opcode, const std::size_t operand) -> std::size_t

source/object/object.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <ios>
88
#include <iterator>
99
#include <ostream>
10+
#include <ranges>
1011
#include <sstream>
1112
#include <string>
1213
#include <utility>
@@ -106,7 +107,7 @@ auto multiply_sequence_helper(const T* source, const integer_object::value_type
106107
{
107108
typename T::value_type target;
108109
for (integer_object::value_type i = 0; i < count; i++) {
109-
std::copy(source->value.cbegin(), source->value.cend(), std::back_inserter(target));
110+
std::ranges::copy(source->value, std::back_inserter(target));
110111
}
111112
return allocate<T>(std::move(target));
112113
}
@@ -779,11 +780,8 @@ auto array_object::operator==(const object& other) const -> const object*
779780
if (other_value.size() != value.size()) {
780781
return fals();
781782
}
782-
const auto eq = std::equal(value.cbegin(),
783-
value.cend(),
784-
other_value.cbegin(),
785-
other_value.cend(),
786-
[](const object* a, const object* b) { return object_eq(*a, *b); });
783+
const auto eq = std::ranges::equal(
784+
value, other_value, [](const object* a, const object* b) -> bool { return object_eq(*a, *b); });
787785
return native_bool_to_object(eq);
788786
}
789787
return fals();
@@ -802,7 +800,7 @@ auto array_object::operator+(const object& other) const -> const object*
802800
if (other.is(array)) {
803801
value_type concat = value;
804802
const auto& other_value = other.as<array_object>()->value;
805-
std::copy(other_value.cbegin(), other_value.cend(), std::back_inserter(concat));
803+
std::ranges::copy(other_value, std::back_inserter(concat));
806804
return allocate<array_object>(std::move(concat));
807805
}
808806
return nullptr;
@@ -812,9 +810,9 @@ auto operator<<(std::ostream& strm, const hashable::key_type& t) -> std::ostream
812810
{
813811
std::visit(
814812
overloaded {
815-
[&](const int64_t val) { strm << val; },
816-
[&](const std::string& val) { strm << '"' << val << '"'; },
817-
[&](const bool val) { strm << std::boolalpha << val; },
813+
[&](const int64_t val) -> void { strm << val; },
814+
[&](const std::string& val) -> void { strm << '"' << val << '"'; },
815+
[&](const bool val) -> void { strm << std::boolalpha << val; },
818816
},
819817
t);
820818
return strm;
@@ -843,14 +841,13 @@ auto hash_object::operator==(const object& other) const -> const object*
843841
if (other_value.size() != value.size()) {
844842
return fals();
845843
}
846-
const auto eq = std::all_of(value.cbegin(),
847-
value.cend(),
848-
[&other_value](const auto& pair)
849-
{
850-
const auto& [key, value] = pair;
851-
auto it = other_value.find(key);
852-
return it != other_value.cend() && object_eq(*(it->second), *value);
853-
});
844+
const auto eq = std::ranges::all_of(value,
845+
[&other_value](const auto& pair) -> auto
846+
{
847+
const auto& [key, value] = pair;
848+
auto it = other_value.find(key);
849+
return it != other_value.cend() && object_eq(*(it->second), *value);
850+
});
854851
return native_bool_to_object(eq);
855852
}
856853
return object::operator==(other);

source/vm/vm.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -648,14 +648,14 @@ auto require_eq(const std::variant<T...>& expected, const object*& actual, const
648648
using enum object::object_type;
649649
std::visit(
650650
overloaded {
651-
[&](const int64_t exp) { require_is(exp, actual, input); },
652-
[&](const double exp) { require_is(exp, actual, input); },
653-
[&](const bool exp) { require_is(exp, actual, input); },
654-
[&](const null_type& /*null*/) { REQUIRE(actual->is_null()); },
655-
[&](const std::string& exp) { require_is(exp, actual, input); },
656-
[&](const ::error& exp) { require_is(exp, actual, input); },
657-
[&](const std::vector<int>& exp) { require_array_object(exp, actual, input); },
658-
[&](const ::hash& exp) { require_hash_object(exp, actual, input); },
651+
[&](const int64_t exp) -> auto { require_is(exp, actual, input); },
652+
[&](const double exp) -> auto { require_is(exp, actual, input); },
653+
[&](const bool exp) -> auto { require_is(exp, actual, input); },
654+
[&](const null_type& /*null*/) -> auto { REQUIRE(actual->is_null()); },
655+
[&](const std::string& exp) -> auto { require_is(exp, actual, input); },
656+
[&](const ::error& exp) -> auto { require_is(exp, actual, input); },
657+
[&](const std::vector<int>& exp) -> auto { require_array_object(exp, actual, input); },
658+
[&](const ::hash& exp) -> auto { require_hash_object(exp, actual, input); },
659659
},
660660
expected);
661661
}

0 commit comments

Comments
 (0)