- // Element access
- char* data() { return str.data; }
- const char* data() const { return str.data; }
- char* c_str() { return str.data; }
- const char* c_str() const { return str.data; };
- reference at(size_type i)
- {
- if (i >= size())
- throw std::out_of_range("Out of range");
- return data()[i];
- }
- const_reference at(size_type i) const
- {
- if (i >= size())
- throw std::out_of_range("Out of range");
- return data()[i];
- }
- reference operator[](size_type i)
- {
- return data()[i];
- }
- const_reference operator[](size_type i) const
- {
- return data()[i];
- }
- // Conversion
- static string_data& to_string_data(string& s) { return s.str; }
- operator std::string() const { return std::string(this->c_str(), this->size()); }
-
- // Iterators
- iterator begin() { return data(); }
- iterator end() { return data() + size(); }
- const_iterator begin() const { return data(); }
- const_iterator end() const { return data() + size(); }
- const_iterator cbegin() const { return data(); }
- const_iterator cend() const { return data() + size(); }
- // (Missing, reverse iterators)
-
- // Operations
- void clear()
- {
- str.len = 0;
- str.data = &NUL;
- }
-
- bool equals(const char* data, std::size_t len) const
- {
- return this->size() == len
- && std::memcmp(this->c_str(), data, len) == 0;
- }
-
- bool operator==(string const& that) const
- {
- return this->equals(that.c_str(), that.size());
- }
- bool operator==(std::string const& that) const
- {
- return this->equals(that.c_str(), that.size());
- }
- bool operator==(const char* that) const
- {
- return this->equals(that, std::strlen(that));
- }
-
- template<class X>
- bool operator!=(X const& that) const
- {
- return not (*this == that);
- }
-
- // Compare:
- int compare(const char* data, std::size_t len) const
- {
- size_t n = std::min(this->size(), len);
- int res = memcmp(this->c_str(), data, n);
- if (res != 0)
- return res;
- else if (this->size() == len)
- return 0;
- else if (this->size() < len)
- return -1;
- else
- return 1;
- }
- int compare(string const& that) const
- {
- return this->compare(that.c_str(), that.size());
- }
- int compare(std::string const& that) const
- {
- return this->compare(that.c_str(), that.size());
- }
- int compare(const char* that) const
- {
- return this->compare(that, std::strlen(that));
- }
-
- // Define < <= >= > in term of compare():
- template<class X>
- bool operator<(X const& that) const
- {
- return this->compare(that) < 0;
- }
- template<class X>
- bool operator<=(X const& that) const
- {
- return this->compare(that) <= 0;
- }
- template<class X>
- bool operator>(X const& that) const
- {
- return this->compare(that) > 0;
- }
- template<class X>
- bool operator>=(X const& that) const
- {
- return this->compare(that) >= 0;
- }
-};
-
-inline
-bool operator==(std::string const& a, string const& b)
-{
- return b == a;
-}
-inline
-bool operator!=(std::string const& a, string const& b)
-{
- return b != a;
-}
-inline
-bool operator<(std::string const& a, string const& b)
-{
- return b > a;