Skip to content

Commit 01a5df6

Browse files
authored
Merge pull request #517 from dtolnay/striterator
Add String and Str iterators
2 parents 01ef29a + ff7f5fb commit 01a5df6

File tree

5 files changed

+61
-0
lines changed

5 files changed

+61
-0
lines changed

.clang-tidy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Checks:
88
-cppcoreguidelines-macro-usage,
99
-cppcoreguidelines-owning-memory,
1010
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
11+
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
1112
-cppcoreguidelines-pro-type-const-cast,
1213
-cppcoreguidelines-pro-type-member-init,
1314
-cppcoreguidelines-pro-type-reinterpret-cast,

book/src/binding/str.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ public:
2929
const char *data() const noexcept;
3030
size_t size() const noexcept;
3131
size_t length() const noexcept;
32+
33+
using iterator = const char *;
34+
using const_iterator = const char *;
35+
const_iterator begin() const noexcept;
36+
const_iterator end() const noexcept;
37+
const_iterator cbegin() const noexcept;
38+
const_iterator cend() const noexcept;
3239
};
3340
3441
std::ostream &operator<<(std::ostream &, const Str &);

book/src/binding/string.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ public:
3232
const char *data() const noexcept;
3333
size_t size() const noexcept;
3434
size_t length() const noexcept;
35+
36+
using iterator = char *;
37+
iterator begin() noexcept;
38+
iterator end() noexcept;
39+
40+
using const_iterator = const char *;
41+
const_iterator begin() const noexcept;
42+
const_iterator end() const noexcept;
43+
const_iterator cbegin() const noexcept;
44+
const_iterator cend() const noexcept;
3545
};
3646
3747
std::ostream &operator<<(std::ostream &, const String &);

include/cxx.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ class String final {
4848
size_t size() const noexcept;
4949
size_t length() const noexcept;
5050

51+
using iterator = char *;
52+
iterator begin() noexcept;
53+
iterator end() noexcept;
54+
55+
using const_iterator = const char *;
56+
const_iterator begin() const noexcept;
57+
const_iterator end() const noexcept;
58+
const_iterator cbegin() const noexcept;
59+
const_iterator cend() const noexcept;
60+
5161
// Internal API only intended for the cxxbridge code generator.
5262
String(unsafe_bitcopy_t, const String &) noexcept;
5363

@@ -78,6 +88,13 @@ class Str final {
7888
Str(const Str &) noexcept = default;
7989
~Str() noexcept = default;
8090

91+
using iterator = const char *;
92+
using const_iterator = const char *;
93+
const_iterator begin() const noexcept;
94+
const_iterator end() const noexcept;
95+
const_iterator cbegin() const noexcept;
96+
const_iterator cend() const noexcept;
97+
8198
private:
8299
friend impl<Str>;
83100
// Not necessarily ABI compatible with &str. Codegen will translate to

src/cxx.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,24 @@ size_t String::size() const noexcept { return cxxbridge1$string$len(this); }
122122

123123
size_t String::length() const noexcept { return cxxbridge1$string$len(this); }
124124

125+
String::iterator String::begin() noexcept {
126+
return const_cast<char *>(this->data());
127+
}
128+
129+
String::iterator String::end() noexcept {
130+
return const_cast<char *>(this->data()) + this->size();
131+
}
132+
133+
String::const_iterator String::begin() const noexcept { return this->cbegin(); }
134+
135+
String::const_iterator String::end() const noexcept { return this->cend(); }
136+
137+
String::const_iterator String::cbegin() const noexcept { return this->data(); }
138+
139+
String::const_iterator String::cend() const noexcept {
140+
return this->data() + this->size();
141+
}
142+
125143
String::String(unsafe_bitcopy_t, const String &bits) noexcept
126144
: repr(bits.repr) {}
127145

@@ -158,6 +176,14 @@ Str::operator std::string() const {
158176
return std::string(this->data(), this->size());
159177
}
160178

179+
Str::const_iterator Str::begin() const noexcept { return this->cbegin(); }
180+
181+
Str::const_iterator Str::end() const noexcept { return this->cend(); }
182+
183+
Str::const_iterator Str::cbegin() const noexcept { return this->ptr; }
184+
185+
Str::const_iterator Str::cend() const noexcept { return this->ptr + this->len; }
186+
161187
std::ostream &operator<<(std::ostream &os, const Str &s) {
162188
os.write(s.data(), s.size());
163189
return os;

0 commit comments

Comments
 (0)