X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fsupport%2Ftrivstring.cpp;h=c780aa7f6d4eab9037fee46c7b5c41e0e17b8a82;hb=9c55af4a223ce4db29d643251109e245665344bd;hp=6fda216674ebb33c1738c26540ad389cf774b370;hpb=5b336954d7eb234f424164a8e4d039db74002afa;p=lyx.git diff --git a/src/support/trivstring.cpp b/src/support/trivstring.cpp index 6fda216674..c780aa7f6d 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,24 +159,34 @@ 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(); } -template char const * trivial_string::c_str() const; -template char_type const * trivial_string::c_str() const; template Char const * trivial_string::c_str() const { if (use_sso()) @@ -168,6 +199,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 &,