X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fsupport%2Ftrivstring.cpp;h=d2325cbbabf37a4025e4e858f4b39a158a6efb4f;hb=26ba2a65838731ce639a09539f617cb0f0be3b22;hp=71085ee04ed020e208575a44d19c62b5d50cad6c;hpb=46f7b578b2bb1313afaacf3f75ffb15ccaae7bd2;p=lyx.git diff --git a/src/support/trivstring.cpp b/src/support/trivstring.cpp index 71085ee04e..d2325cbbab 100644 --- a/src/support/trivstring.cpp +++ b/src/support/trivstring.cpp @@ -13,7 +13,10 @@ #include "support/trivstring.h" #include "support/docstring.h" +#ifdef STD_STRING_USES_COW #include +#include +#include using namespace std; @@ -29,16 +32,34 @@ trivial_string::trivial_string(trivial_string const & that) : size_(that.s else if (size_ > 0) { data_ = new Char[size_ + 1]; copy(that.data_, that.data_ + size_ + 1, data_); + } else { + // Happens only for really big Char types + data_ = 0; + } +} + + +template +trivial_string::trivial_string(Char const * that, size_t n) : size_(n) +{ + if (use_sso()) { + copy(that, that + size_, data_sso()); + data_sso()[size_] = '\0'; + } else if (size_ > 0) { + data_ = new Char[size_ + 1]; + copy(that, that + size_, data_); + data_[size_] = '\0'; + } else { + // Happens only for really big Char types + data_ = 0; } - // else Happens only for really big Char types } template trivial_string::trivial_string(string const &); template trivial_string::trivial_string(docstring const &); template -trivial_string::trivial_string( - basic_string, allocator > const & that) +trivial_string::trivial_string(_stdstring const & that) : size_(that.length()) { if (use_sso()) { @@ -48,8 +69,10 @@ trivial_string::trivial_string( data_ = new Char[size_ + 1]; copy(that.begin(), that.end(), data_); data_[size_] = '\0'; + } else { + // Happens only for really big Char types + data_ = 0; } - // else Happens only for really big Char types } @@ -84,7 +107,7 @@ template trivial_string & trivial_string::operator=(docstring const &); template trivial_string & -trivial_string::operator=(basic_string, allocator > const & that) +trivial_string::operator=(_stdstring const & that) { if (!use_sso()) delete[] data_; @@ -136,20 +159,31 @@ int trivial_string::compare(trivial_string const & other) const } -template string trivial_string::str() const; -template docstring trivial_string::str() const; +template trivial_string trivial_string::substr(size_t, size_t) const; +template trivial_string trivial_string::substr(size_t, size_t) const; +template +trivial_string trivial_string::substr(size_t pos, size_t n) const +{ + if (pos > length()) + throw out_of_range("trivial_string::substr"); + if (n == _stdstring::npos) + n = length() - pos; + size_t const l = min(pos + n, length()); + return trivial_string(c_str() + pos, l - pos); +} + + +template trivial_string::operator string() const; +template trivial_string::operator docstring() const; template -basic_string, allocator > -trivial_string::str() const +trivial_string::operator _stdstring() const { if (use_sso()) - return basic_string, allocator >( - data_sso(), size_); + return _stdstring(data_sso(), size_); if (size_ > 0) - return basic_string, allocator >( - data_, size_); + return _stdstring(data_, size_); // Happens only for really big Char types - return basic_string, allocator >(); + return _stdstring(); } @@ -167,14 +201,62 @@ template Char const * trivial_string::c_str() const } +template char trivial_string::operator[](size_t) const; +template char_type trivial_string::operator[](size_t) const; +template Char trivial_string::operator[](size_t i) const +{ + return c_str()[i]; +} + + template bool operator<(trivial_string const &, trivial_string const &); template bool operator<(trivial_string const &, trivial_string const &); template -bool operator<(trivial_string const & lhs, trivial_string const &rhs) +bool operator<(trivial_string const & lhs, trivial_string const & rhs) { return lhs.compare(rhs) < 0; } + +template bool operator==(trivial_string const &, + trivial_string const &); +template bool operator==(trivial_string const &, + trivial_string const &); +template +bool operator==(trivial_string const & lhs, trivial_string const & rhs) +{ + return lhs.compare(rhs) == 0; +} + + +template bool operator==(trivial_string const &, char const *); +template bool operator==(trivial_string const &, char_type const *); +template +bool operator==(trivial_string const & lhs, Char const * rhs) +{ + return lhs.compare(trivial_string(rhs)) == 0; +} + + +template bool operator==(char const *, trivial_string const &); +template bool operator==(char_type const *, trivial_string const &); +template +bool operator==(Char const * lhs, trivial_string const & rhs) +{ + return rhs.compare(trivial_string(lhs)) == 0; +} + + +template ostream & operator<<(ostream &, trivial_string const &); +template odocstream & operator<<(odocstream &, trivial_string const &); +template +basic_ostream > & +operator<<(basic_ostream > & os, trivial_string const & s) +{ + return os << basic_string, allocator >(s); +} + } // namespace lyx +#endif