Add String and Str iterators

This commit is contained in:
David Tolnay 2020-11-25 20:50:32 -08:00
parent 01ef29a15c
commit ff7f5fb8cc
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
5 changed files with 61 additions and 0 deletions

View File

@ -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,

View File

@ -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 &);

View File

@ -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 &);

View File

@ -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<Str>;
// Not necessarily ABI compatible with &str. Codegen will translate to

View File

@ -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<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 @@ 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;