@auto_js/utility 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -24,7 +24,11 @@ class function_ref<auto(Args...) noexcept(Nx)->Result> {
24
24
 
25
25
  // NOLINTNEXTLINE(google-explicit-constructor)
26
26
  constexpr function_ref(signature_type* function) :
27
- function_ref{std::type_identity<signature_type>{}, static_cast<void*>(function)} {}
27
+ invoke_{[](void* ptr, Args... args) noexcept(Nx) -> Result {
28
+ auto& fn = *reinterpret_cast<signature_type*>(ptr);
29
+ return fn(std::forward<Args>(args)...);
30
+ }},
31
+ ptr_{reinterpret_cast<void*>(function)} {}
28
32
 
29
33
  template <std::invocable<Args...> Object>
30
34
  requires(type<Object> != type<function_ref>)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@auto_js/utility",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "author": "https://github.com/laverdet/",
5
5
  "homepage": "https://github.com/laverdet/isolated-vm/tree/experimental/packages/utility",
6
6
  "license": "ISC",
package/utility/facade.cc CHANGED
@@ -3,30 +3,37 @@ import std;
3
3
 
4
4
  namespace util {
5
5
 
6
- // Internal addition facade
7
- template <class difference_type_>
8
- class addition_facade {
6
+ // Implements `Type++` via `++Type`
7
+ export class incrementable_facade {
9
8
  public:
10
- using difference_type = difference_type_;
11
-
12
- // ++Type
13
- auto operator++(this auto& self) -> auto& { return self += 1; }
9
+ using difference_type = int;
14
10
 
15
11
  // Type++
16
- auto operator++(this auto& self, int) {
12
+ constexpr auto operator++(this auto& self, int) {
17
13
  auto result = self;
18
14
  ++self;
19
15
  return result;
20
16
  }
17
+ };
18
+
19
+ // Implements addition operators via `operator+=()`
20
+ export template <class difference_type_>
21
+ class addition_facade : public incrementable_facade {
22
+ public:
23
+ using incrementable_facade::operator++;
24
+ using difference_type = difference_type_;
25
+
26
+ // ++Type
27
+ constexpr auto operator++(this auto& self) -> auto& { return self += 1; }
21
28
 
22
29
  // Type + difference_type
23
- auto operator+(this const auto& self, difference_type offset) {
30
+ constexpr auto operator+(this const auto& self, difference_type offset) {
24
31
  auto result = self;
25
32
  return result += offset;
26
33
  }
27
34
 
28
35
  // difference_type + Type
29
- friend auto operator+(difference_type left, const auto& right) {
36
+ constexpr friend auto operator+(difference_type left, const auto& right) {
30
37
  return right + left;
31
38
  }
32
39
  };
@@ -47,26 +54,26 @@ class subtraction_facade<difference_type_, wide_size_type> {
47
54
  using difference_type = difference_type_;
48
55
 
49
56
  // Type -= difference_type
50
- auto operator-=(this auto& self, difference_type offset) -> auto& { return self += -offset; }
57
+ constexpr auto operator-=(this auto& self, difference_type offset) -> auto& { return self += -offset; }
51
58
 
52
59
  // --Type
53
- auto operator--(this auto& self) -> auto& { return self -= 1; }
60
+ constexpr auto operator--(this auto& self) -> auto& { return self -= 1; }
54
61
 
55
62
  // Type--
56
- auto operator--(this auto& self, int) {
63
+ constexpr auto operator--(this auto& self, int) {
57
64
  auto result = self;
58
65
  --self;
59
66
  return result;
60
67
  }
61
68
 
62
69
  // Type - difference_type
63
- auto operator-(this const auto& self, difference_type offset) {
70
+ constexpr auto operator-(this const auto& self, difference_type offset) {
64
71
  auto result = self;
65
72
  return result -= offset;
66
73
  }
67
74
 
68
75
  // Type - Type
69
- auto operator-(this const auto& self, decltype(self) right) -> difference_type {
76
+ constexpr auto operator-(this const auto& self, decltype(self) right) -> difference_type {
70
77
  return static_cast<difference_type>(wide_size_type{+self} - wide_size_type{+right});
71
78
  }
72
79
  };
@@ -97,7 +104,7 @@ class arithmetic_facade
97
104
  export template <class difference_type>
98
105
  class array_facade {
99
106
  public:
100
- auto operator[](this auto&& self, difference_type offset) -> decltype(auto) {
107
+ constexpr auto operator[](this auto&& self, difference_type offset) -> decltype(auto) {
101
108
  return *(std::forward<decltype(self)>(self) + offset);
102
109
  }
103
110
  };
package/utility/ranges.cc CHANGED
@@ -52,4 +52,15 @@ export constexpr auto into_range(meta_range auto&& range) {
52
52
  return std::forward<decltype(range)>(range).into_range();
53
53
  }
54
54
 
55
+ // Returns either a `std::tuple<size_type>` with the range's size, or `std::tuple<>` if it can't be
56
+ // determined in constant time.
57
+ export template <class Type>
58
+ constexpr auto maybe_range_size(const Type& range) {
59
+ if constexpr (std::ranges::sized_range<Type>) {
60
+ return std::tuple{std::ranges::size(range)};
61
+ } else {
62
+ return std::tuple{};
63
+ }
64
+ }
65
+
55
66
  } // namespace util
package/utility/string.cc CHANGED
@@ -233,14 +233,14 @@ class codepoint_forward_view<char16_t> {
233
233
 
234
234
  constexpr auto read() -> char32_t {
235
235
  // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
236
- auto char0 = char32_t{*pos_++};
236
+ auto char0 = static_cast<char32_t>(*pos_++);
237
237
  if (char0 >= 0xd800 && char0 < 0xdc00) {
238
238
  if (eof()) {
239
239
  // nb: Unpaired surrogate
240
240
  return char0;
241
241
  }
242
242
  // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
243
- auto char1 = char32_t{*pos_++};
243
+ auto char1 = static_cast<char32_t>(*pos_++);
244
244
  if (char1 >= 0xdc00 && char1 < 0xe000) {
245
245
  return ((char0 - 0xd800) * 0x400) + (char1 - 0xdc00) + 0x1'0000;
246
246
  } else {
@@ -142,4 +142,34 @@ constexpr auto slice(const Type& value) {
142
142
  return slice_t<const Type>{value};
143
143
  }
144
144
 
145
+ // Implements construction of containers with variadic elements
146
+ template <class Type>
147
+ struct inplace_container_constructor {
148
+ constexpr auto operator()(auto&&... args) const -> Type {
149
+ return Type{std::in_place, std::forward<decltype(args)>(args)...};
150
+ }
151
+ };
152
+
153
+ template <class Type, std::size_t Size>
154
+ struct inplace_container_constructor<std::array<Type, Size>> {
155
+ constexpr auto operator()(auto&&... args) const -> std::array<Type, Size> {
156
+ return std::array<Type, Size>{std::forward<decltype(args)>(args)...};
157
+ }
158
+ };
159
+
160
+ template <class Type, class Alloc>
161
+ struct inplace_container_constructor<std::vector<Type, Alloc>> {
162
+ constexpr auto operator()(auto&&... args) const -> std::vector<Type, Alloc> {
163
+ auto container = std::vector<Type, Alloc>{};
164
+ container.reserve(sizeof...(args));
165
+ (..., container.emplace_back(std::forward<decltype(args)>(args)));
166
+ return container;
167
+ }
168
+ };
169
+
170
+ export template <class Type>
171
+ auto make_inplace_container(auto&&... args) -> Type {
172
+ return inplace_container_constructor<Type>{}(std::forward<decltype(args)>(args)...);
173
+ }
174
+
145
175
  } // namespace util