Skip to content

Commit

Permalink
Move almost all view functions on best::vec and best::strbuf behi…
Browse files Browse the repository at this point in the history
…nd `operator->` (#25)

This patch introduces `best::arrow<T>`, a helper for returning anything
out of an `operator->`. The doc comment on the type explains why it
exists and what C++ semantics it abuses.

`best::vec` and `best::strbuf` now have `operator->`s that effectively
return `best::span` and `best::str`, respectively. This allows the
removal of a ton of duplicated functions, and means that the owned types
can't "lag" behind the unowned types! This also means that e.g.
`best::box<T[]>` does not need to duplicate all of the functions from
`best::span`, like to `best::vec` used to.
  • Loading branch information
mcy authored Jul 15, 2024
1 parent 3b14093 commit 0d52b66
Show file tree
Hide file tree
Showing 10 changed files with 319 additions and 482 deletions.
4 changes: 4 additions & 0 deletions best/base/fwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ class pun;
// template <typename, best::option<size_t>>
// class span;

// best/func/arrow.h
template <typename>
class arrow;

// best/func/fnref.h
template <typename>
class fnref;
Expand Down
12 changes: 6 additions & 6 deletions best/cli/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ void normalize(best::strbuf& name, const auto& about) {
}

// TODO: also check the ends.
if (name.starts_with('-') || name.starts_with('_') ||
name.contains(&reserved_rune)) {
if (name->starts_with('-') || name->starts_with('_') ||
name->contains(&reserved_rune)) {
best::wtf("field {}::{}'s name ({:?}) contains reserved runes",
about.strukt->path(), about.field, name);
}
Expand Down Expand Up @@ -342,8 +342,8 @@ void cli::init() {
});

// Now, sort the flags so we can bisect through them later.
impl_->sorted_flags.sort(&impl::entry::key);
impl_->sorted_subs.sort(&impl::entry::key);
impl_->sorted_flags->sort(&impl::entry::key);
impl_->sorted_subs->sort(&impl::entry::key);

// Check for duplicates.
best::option<best::str> prev;
Expand Down Expand Up @@ -647,7 +647,7 @@ best::strbuf cli::usage(best::pretext<wtf8> exe, bool hidden) const {

best::format(out, "Usage: {}", exe);
if (!parents.is_empty()) {
parents.reverse();
parents->reverse();
for (auto sub : parents) {
best::format(out, " {}", sub);
}
Expand Down Expand Up @@ -809,7 +809,7 @@ best::strbuf cli::usage(best::pretext<wtf8> exe, bool hidden) const {
}

best::str prefix = e.key[{
.end = e.key.size() - e.key.split('.').last()->size(),
.end = e.key.size() - e.key->split('.').last()->size(),
}];
// Chop off everything past the last `.` to make the prefix.

Expand Down
1 change: 1 addition & 0 deletions best/container/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ cc_library(
":object",
":option",
":span",
"//best/func:arrow",
"//best/math:bit",
"//best/math:overflow",
"//best/memory:allocator",
Expand Down
10 changes: 5 additions & 5 deletions best/container/span_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,10 @@ best::test FrontAndBack = [](auto& t) {

best::test Swap = [](auto& t) {
best::vec<int> ints = {1, 2, 3, 4, 5};
ints.as_span().swap(1, 2);
ints->swap(1, 2);
t.expect_eq(ints, best::span{1, 3, 2, 4, 5});

ints.as_span().reverse();
ints->reverse();
t.expect_eq(ints, best::span{5, 4, 2, 3, 1});
};

Expand Down Expand Up @@ -332,13 +332,13 @@ best::test Sort = [](auto& t) {
best::mark_sort_header_used();

best::vec<int> ints = {5, 4, 3, 2, 1};
ints.as_span().sort();
ints->sort();
t.expect_eq(ints, best::span{1, 2, 3, 4, 5});

ints.as_span().stable_sort([](int x) { return best::count_ones(x); });
ints->stable_sort([](int x) { return best::count_ones(x); });
t.expect_eq(ints, best::span{1, 2, 4, 3, 5});

ints.as_span().sort([](int x, int y) { return y <=> x; });
ints->sort([](int x, int y) { return y <=> x; });
t.expect_eq(ints, best::span{5, 4, 3, 2, 1});
};

Expand Down
Loading

0 comments on commit 0d52b66

Please sign in to comment.