diff --git a/.clang-tidy b/.clang-tidy index 9962a64a..1d457136 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -8,6 +8,7 @@ Checks: -cppcoreguidelines-macro-usage, -cppcoreguidelines-owning-memory, -cppcoreguidelines-pro-bounds-array-to-pointer-decay, + -cppcoreguidelines-pro-bounds-pointer-arithmetic, -cppcoreguidelines-pro-type-const-cast, -cppcoreguidelines-pro-type-member-init, -cppcoreguidelines-pro-type-reinterpret-cast, diff --git a/book/src/binding/str.md b/book/src/binding/str.md index 7d7d6e8a..b0a99b43 100644 --- a/book/src/binding/str.md +++ b/book/src/binding/str.md @@ -29,6 +29,13 @@ public: const char *data() const noexcept; size_t size() const noexcept; size_t length() const noexcept; + + using iterator = const char *; + using const_iterator = const char *; + const_iterator begin() const noexcept; + const_iterator end() const noexcept; + const_iterator cbegin() const noexcept; + const_iterator cend() const noexcept; }; std::ostream &operator<<(std::ostream &, const Str &); diff --git a/book/src/binding/string.md b/book/src/binding/string.md index ac44fb20..47a2b964 100644 --- a/book/src/binding/string.md +++ b/book/src/binding/string.md @@ -32,6 +32,16 @@ public: const char *data() const noexcept; size_t size() const noexcept; size_t length() const noexcept; + + using iterator = char *; + iterator begin() noexcept; + iterator end() noexcept; + + using const_iterator = const char *; + const_iterator begin() const noexcept; + const_iterator end() const noexcept; + const_iterator cbegin() const noexcept; + const_iterator cend() const noexcept; }; std::ostream &operator<<(std::ostream &, const String &); diff --git a/include/cxx.h b/include/cxx.h index 5725be96..86242335 100644 --- a/include/cxx.h +++ b/include/cxx.h @@ -48,6 +48,16 @@ public: size_t size() const noexcept; size_t length() const noexcept; + using iterator = char *; + iterator begin() noexcept; + iterator end() noexcept; + + using const_iterator = const char *; + const_iterator begin() const noexcept; + const_iterator end() const noexcept; + const_iterator cbegin() const noexcept; + const_iterator cend() const noexcept; + // Internal API only intended for the cxxbridge code generator. String(unsafe_bitcopy_t, const String &) noexcept; @@ -78,6 +88,13 @@ public: Str(const Str &) noexcept = default; ~Str() noexcept = default; + using iterator = const char *; + using const_iterator = const char *; + const_iterator begin() const noexcept; + const_iterator end() const noexcept; + const_iterator cbegin() const noexcept; + const_iterator cend() const noexcept; + private: friend impl; // Not necessarily ABI compatible with &str. Codegen will translate to diff --git a/src/cxx.cc b/src/cxx.cc index 2a8ba97b..ef5db8fa 100644 --- a/src/cxx.cc +++ b/src/cxx.cc @@ -122,6 +122,24 @@ size_t String::size() const noexcept { return cxxbridge1$string$len(this); } size_t String::length() const noexcept { return cxxbridge1$string$len(this); } +String::iterator String::begin() noexcept { + return const_cast(this->data()); +} + +String::iterator String::end() noexcept { + return const_cast(this->data()) + this->size(); +} + +String::const_iterator String::begin() const noexcept { return this->cbegin(); } + +String::const_iterator String::end() const noexcept { return this->cend(); } + +String::const_iterator String::cbegin() const noexcept { return this->data(); } + +String::const_iterator String::cend() const noexcept { + return this->data() + this->size(); +} + String::String(unsafe_bitcopy_t, const String &bits) noexcept : repr(bits.repr) {} @@ -158,6 +176,14 @@ Str::operator std::string() const { return std::string(this->data(), this->size()); } +Str::const_iterator Str::begin() const noexcept { return this->cbegin(); } + +Str::const_iterator Str::end() const noexcept { return this->cend(); } + +Str::const_iterator Str::cbegin() const noexcept { return this->ptr; } + +Str::const_iterator Str::cend() const noexcept { return this->ptr + this->len; } + std::ostream &operator<<(std::ostream &os, const Str &s) { os.write(s.data(), s.size()); return os;