|
1 | 1 | #include "../include/cxx.h"
|
| 2 | +#include <algorithm> |
2 | 3 | #include <cassert>
|
3 | 4 | #include <cstring>
|
4 | 5 | #include <exception>
|
@@ -140,6 +141,30 @@ String::const_iterator String::cend() const noexcept {
|
140 | 141 | return this->data() + this->size();
|
141 | 142 | }
|
142 | 143 |
|
| 144 | +bool String::operator==(const String &rhs) const noexcept { |
| 145 | + return rust::Str(*this) == rust::Str(rhs); |
| 146 | +} |
| 147 | + |
| 148 | +bool String::operator!=(const String &rhs) const noexcept { |
| 149 | + return rust::Str(*this) != rust::Str(rhs); |
| 150 | +} |
| 151 | + |
| 152 | +bool String::operator<(const String &rhs) const noexcept { |
| 153 | + return rust::Str(*this) < rust::Str(rhs); |
| 154 | +} |
| 155 | + |
| 156 | +bool String::operator<=(const String &rhs) const noexcept { |
| 157 | + return rust::Str(*this) <= rust::Str(rhs); |
| 158 | +} |
| 159 | + |
| 160 | +bool String::operator>(const String &rhs) const noexcept { |
| 161 | + return rust::Str(*this) > rust::Str(rhs); |
| 162 | +} |
| 163 | + |
| 164 | +bool String::operator>=(const String &rhs) const noexcept { |
| 165 | + return rust::Str(*this) >= rust::Str(rhs); |
| 166 | +} |
| 167 | + |
143 | 168 | String::String(unsafe_bitcopy_t, const String &bits) noexcept
|
144 | 169 | : repr(bits.repr) {}
|
145 | 170 |
|
@@ -186,6 +211,39 @@ Str::const_iterator Str::cbegin() const noexcept { return this->ptr; }
|
186 | 211 |
|
187 | 212 | Str::const_iterator Str::cend() const noexcept { return this->ptr + this->len; }
|
188 | 213 |
|
| 214 | +bool Str::operator==(const Str &rhs) const noexcept { |
| 215 | + return this->len == rhs.len && |
| 216 | + std::equal(this->begin(), this->end(), rhs.begin()); |
| 217 | +} |
| 218 | + |
| 219 | +bool Str::operator!=(const Str &rhs) const noexcept { return !(*this == rhs); } |
| 220 | + |
| 221 | +bool Str::operator<(const Str &rhs) const noexcept { |
| 222 | + return std::lexicographical_compare(this->begin(), this->end(), rhs.begin(), |
| 223 | + rhs.end()); |
| 224 | +} |
| 225 | + |
| 226 | +bool Str::operator<=(const Str &rhs) const noexcept { |
| 227 | + // std::mismatch(this->begin(), this->end(), rhs.begin(), rhs.end()), except |
| 228 | + // without Undefined Behavior on C++11 if rhs is shorter than *this. |
| 229 | + const_iterator liter = this->begin(), lend = this->end(), riter = rhs.begin(), |
| 230 | + rend = rhs.end(); |
| 231 | + while (liter != lend && riter != rend && *liter == *riter) { |
| 232 | + ++liter, ++riter; |
| 233 | + } |
| 234 | + if (liter == lend) { |
| 235 | + return true; // equal or *this is a prefix of rhs |
| 236 | + } else if (riter == rend) { |
| 237 | + return false; // rhs is a prefix of *this |
| 238 | + } else { |
| 239 | + return *liter <= *riter; |
| 240 | + } |
| 241 | +} |
| 242 | + |
| 243 | +bool Str::operator>(const Str &rhs) const noexcept { return rhs < *this; } |
| 244 | + |
| 245 | +bool Str::operator>=(const Str &rhs) const noexcept { return rhs <= *this; } |
| 246 | + |
189 | 247 | std::ostream &operator<<(std::ostream &os, const Str &s) {
|
190 | 248 | os.write(s.data(), s.size());
|
191 | 249 | return os;
|
|
0 commit comments