Skip to content

Commit

Permalink
Add iterators (#14)
Browse files Browse the repository at this point in the history
This adds something like Rust's `Iterator` trait but in a way that is
suitable for C++ use. We have:

- `best::iter` and friends.
- `best::int_range`, which `best::bounds` is now a specialization of.
It's an iterator.
- `best::span::find()` and `best::text::find()` now have standardized
interfaces.
- `best::span::split()`, `best::text::split()`.
- `best::text::runes()` and `best::text::rune_indices()`.
- `best::pretext`, i.e., a `best::text` that has not been validated.

Removed `best::rune::iter`, since it was a stopgap solution until we got
here.
  • Loading branch information
mcy committed Jun 26, 2024
1 parent 4de86f6 commit 3475255
Show file tree
Hide file tree
Showing 37 changed files with 2,584 additions and 1,074 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pieces of functionality.
- `best/container` - Container types. We interpret any generic "wrapper" type as
a container.
- `best/func` - Helpers for manipulating functions as first-class objects.
- `best/iter` - Iterators and other functional programming types.
- `best/log` - Logging utilities.
- `best/math` - Numeric and integer utilities.
- `best/memory` - Low-level memory management.
Expand Down
20 changes: 17 additions & 3 deletions best/base/fwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@
//! best. Users SHOULD NOT forward-declared best types.

namespace best {
// best/container/bounds.h
struct bounds;

// best/container/choice.h
template <typename...>
class choice;
Expand Down Expand Up @@ -79,6 +76,21 @@ tap(Cb&&) -> tap<tags_internal_do_not_use::ctad_guard, best::as_auto<Cb>>;
template <typename Cb>
tap(best::bind_t, Cb&&) -> tap<tags_internal_do_not_use::ctad_guard, Cb&&>;

// best/func/tap.h
template <typename>
class iter;
template <typename Impl>
iter(Impl) -> iter<Impl>;
template <typename>
class iter_range;
template <typename Impl>
iter_range(Impl) -> iter_range<Impl>;
struct iter_range_end final {};

// best/iter/bounds.h
template <typename>
struct int_range;

// best/log/location.h
template <typename>
class track_location;
Expand Down Expand Up @@ -116,6 +128,8 @@ class formatter;
// best/text/str.h
template <typename>
class text;
template <typename>
class pretext;

// best/text/utf.h
struct utf8;
Expand Down
21 changes: 1 addition & 20 deletions best/container/BUILD
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
package(default_visibility = ["//visibility:public"])

cc_library(
name = "bounds",
hdrs = ["bounds.h"],
srcs = ["bounds.cc"],
deps = [
":simple_option",
"//best/log/internal:crash",
],
)

cc_test(
name = "bounds_test",
srcs = ["bounds_test.cc"],
linkopts = ["-rdynamic"],
deps = [
":bounds",
"//best/test",
],
)

cc_library(
name = "choice",
hdrs = [
Expand Down Expand Up @@ -167,6 +147,7 @@ cc_library(
deps = [
":option",
":result",
"//best/iter",
"//best/memory:bytes",
],
)
Expand Down
224 changes: 0 additions & 224 deletions best/container/bounds.h

This file was deleted.

14 changes: 8 additions & 6 deletions best/container/internal/simple_option.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ namespace best::container_internal {
template <typename T>
class option final {
public:
using type = T;

constexpr option() = default;
constexpr option(T value) : value_(BEST_MOVE(value)), has_(true) {}

Expand All @@ -59,13 +61,13 @@ class option final {
return has_ && value_ == that;
}

// TODO: BestFmt
template <typename Os>
friend Os& operator<<(Os& os, option opt) {
if (!opt.has_value()) {
return os << "none";
friend void BestFmt(auto& fmt, const option& opt)
requires requires { fmt.format(opt.value_); }
{
if (opt.has_) {
fmt.format("option({:!})", opt.value_);
} else {
return os << "option(" << *opt << ")";
fmt.format("none");
}
}

Expand Down
9 changes: 8 additions & 1 deletion best/container/option.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,14 @@ inline constexpr struct none_t {
/// Whether `T` is some `best::option<U>`.
template <typename T>
concept is_option =
best::same<best::as_auto<T>, best::option<typename best::as_auto<T>::type>>;
best::same<best::as_auto<T>,
best::option<typename best::as_auto<T>::type>> ||
requires {
requires best::is_object<typename best::as_auto<T>::type>;
requires best::same<
best::as_auto<T>,
best::container_internal::option<typename best::as_auto<T>::type>>;
};

/// # `best::option_type<T>`
///
Expand Down
2 changes: 1 addition & 1 deletion best/container/row.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ args(Args&&...) -> args<Args&&...>;

/// # `best::row<...>`
///
/// A list of heterogenous things. Note that the semantics of `best::row`
/// A list of heterogenous thing0s. Note that the semantics of `best::row`
/// for non-object types does not match `std::tuple`. Instead, a `best::row`
/// models a sequence of `best::object`s. In particular, a `best::row<T>` and
/// a `best::choice<T>` have the same internal semantics.
Expand Down
Loading

0 comments on commit 3475255

Please sign in to comment.