mirror of
https://gitee.com/openharmony/third_party_rust_cxx
synced 2025-02-17 06:57:37 +00:00
Add String and Str iterators
This commit is contained in:
parent
01ef29a15c
commit
ff7f5fb8cc
@ -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,
|
||||
|
@ -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 &);
|
||||
|
@ -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 &);
|
||||
|
@ -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
|
||||
|
26
src/cxx.cc
26
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<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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user