X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fsupport%2Ftrivstring.cpp;h=d2325cbbabf37a4025e4e858f4b39a158a6efb4f;hb=cf14e814124ccbc8155fa1dde98d03be319c0e87;hp=6fda216674ebb33c1738c26540ad389cf774b370;hpb=5b336954d7eb234f424164a8e4d039db74002afa;p=lyx.git diff --git a/src/support/trivstring.cpp b/src/support/trivstring.cpp index 6fda216674..d2325cbbab 100644 --- a/src/support/trivstring.cpp +++ b/src/support/trivstring.cpp @@ -16,6 +16,7 @@ #ifdef STD_STRING_USES_COW #include #include +#include using namespace std; @@ -31,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()) { @@ -50,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 } @@ -86,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_; @@ -138,19 +159,31 @@ int trivial_string::compare(trivial_string const & other) 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 -trivial_string::operator basic_string, allocator >() 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(); } @@ -168,6 +201,14 @@ 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 &, @@ -186,7 +227,7 @@ template bool operator==(trivial_string const &, template bool operator==(trivial_string const & lhs, trivial_string const & rhs) { - return lhs.compare(rhs) == 0; + return lhs.compare(rhs) == 0; } @@ -195,7 +236,7 @@ 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; + return lhs.compare(trivial_string(rhs)) == 0; } @@ -204,7 +245,7 @@ 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; + return rhs.compare(trivial_string(lhs)) == 0; }