Add String and Str iterators
diff --git a/.clang-tidy b/.clang-tidy
index 9962a64..1d45713 100644
--- a/.clang-tidy
+++ b/.clang-tidy
@@ -8,6 +8,7 @@
-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 7d7d6e8..b0a99b4 100644
--- a/book/src/binding/str.md
+++ b/book/src/binding/str.md
@@ -29,6 +29,13 @@
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 ac44fb2..47a2b96 100644
--- a/book/src/binding/string.md
+++ b/book/src/binding/string.md
@@ -32,6 +32,16 @@
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 5725be9..8624233 100644
--- a/include/cxx.h
+++ b/include/cxx.h
@@ -48,6 +48,16 @@
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 @@
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<Str>;
// Not necessarily ABI compatible with &str. Codegen will translate to
diff --git a/src/cxx.cc b/src/cxx.cc
index 2a8ba97..ef5db8f 100644
--- a/src/cxx.cc
+++ b/src/cxx.cc
@@ -122,6 +122,24 @@
size_t String::length() const noexcept { return cxxbridge1$string$len(this); }
+String::iterator String::begin() noexcept {
+ return const_cast<char *>(this->data());
+}
+
+String::iterator String::end() noexcept {
+ return const_cast<char *>(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 @@
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;