3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "support/trivstring.h"
14 #include "support/docstring.h"
16 #ifdef STD_STRING_USES_COW
25 template trivial_string<char>::trivial_string(trivial_string const &);
26 template trivial_string<char_type>::trivial_string(trivial_string const &);
27 template<typename Char>
28 trivial_string<Char>::trivial_string(trivial_string const & that) : size_(that.size_)
31 copy(that.data_sso(), that.data_sso() + size_ + 1, data_sso());
33 data_ = new Char[size_ + 1];
34 copy(that.data_, that.data_ + size_ + 1, data_);
36 // Happens only for really big Char types
42 template<typename Char>
43 trivial_string<Char>::trivial_string(Char const * that, size_t n) : size_(n)
46 copy(that, that + size_, data_sso());
47 data_sso()[size_] = '\0';
48 } else if (size_ > 0) {
49 data_ = new Char[size_ + 1];
50 copy(that, that + size_, data_);
53 // Happens only for really big Char types
59 template trivial_string<char>::trivial_string(string const &);
60 template trivial_string<char_type>::trivial_string(docstring const &);
61 template<typename Char>
62 trivial_string<Char>::trivial_string(_stdstring const & that)
63 : size_(that.length())
66 copy(that.begin(), that.end(), data_sso());
67 data_sso()[size_] = '\0';
68 } else if (size_ > 0) {
69 data_ = new Char[size_ + 1];
70 copy(that.begin(), that.end(), data_);
73 // Happens only for really big Char types
79 template trivial_string<char> &
80 trivial_string<char>::operator=(trivial_string const &);
81 template trivial_string<char_type> &
82 trivial_string<char_type>::operator=(trivial_string const &);
83 template<typename Char>
84 trivial_string<Char> & trivial_string<Char>::operator=(trivial_string const & that)
92 copy(that.data_sso(), that.data_sso() + size_ + 1, data_sso());
94 data_ = new Char[size_ + 1];
95 copy(that.data_, that.data_ + size_ + 1, data_);
97 // Happens only for really big Char types
104 template trivial_string<char> &
105 trivial_string<char>::operator=(string const &);
106 template trivial_string<char_type> &
107 trivial_string<char_type>::operator=(docstring const &);
108 template<typename Char>
109 trivial_string<Char> &
110 trivial_string<Char>::operator=(_stdstring const & that)
116 copy(that.begin(), that.end(), data_sso());
117 data_sso()[size_] = '\0';
118 } else if (size_ > 0) {
119 data_ = new Char[size_ + 1];
120 copy(that.begin(), that.end(), data_);
122 // Happens only for really big Char types
130 trivial_string<char>::swap(trivial_string<char> &);
132 trivial_string<char_type>::swap(trivial_string<char_type> &);
133 template<typename Char>
134 void trivial_string<Char>::swap(trivial_string & that)
136 size_t const sizetmp = that.size_;
139 Char * const datatmp = that.data_;
145 template<typename Char>
146 int trivial_string<Char>::compare(trivial_string const & other) const
148 size_t const lsize = this->length();
149 size_t const rsize = other.length();
150 size_t const len = min(lsize, rsize);
151 int r = char_traits<Char>::compare(c_str(), other.c_str(), len);
155 else if (lsize < rsize)
162 template trivial_string<char> trivial_string<char>::substr(size_t, size_t) const;
163 template trivial_string<char_type> trivial_string<char_type>::substr(size_t, size_t) const;
164 template<typename Char>
165 trivial_string<Char> trivial_string<Char>::substr(size_t pos, size_t n) const
168 throw out_of_range("trivial_string::substr");
169 if (n == _stdstring::npos)
171 size_t const l = min(pos + n, length());
172 return trivial_string(c_str() + pos, l - pos);
176 template trivial_string<char>::operator string() const;
177 template trivial_string<char_type>::operator docstring() const;
178 template<typename Char>
179 trivial_string<Char>::operator _stdstring() const
182 return _stdstring(data_sso(), size_);
184 return _stdstring(data_, size_);
185 // Happens only for really big Char types
190 template<typename Char> Char const * trivial_string<Char>::c_str() const
196 // Happens only for really big Char types
197 static const Char empty_char = '\0';
202 template char trivial_string<char>::operator[](size_t) const;
203 template char_type trivial_string<char_type>::operator[](size_t) const;
204 template <typename Char> Char trivial_string<Char>::operator[](size_t i) const
210 template bool operator<(trivial_string<char> const &,
211 trivial_string<char> const &);
212 template bool operator<(trivial_string<char_type> const &,
213 trivial_string<char_type> const &);
214 template <typename Char>
215 bool operator<(trivial_string<Char> const & lhs, trivial_string<Char> const & rhs)
217 return lhs.compare(rhs) < 0;
221 template bool operator==(trivial_string<char> const &,
222 trivial_string<char> const &);
223 template bool operator==(trivial_string<char_type> const &,
224 trivial_string<char_type> const &);
225 template <typename Char>
226 bool operator==(trivial_string<Char> const & lhs, trivial_string<Char> const & rhs)
228 return lhs.compare(rhs) == 0;
232 template bool operator==(trivial_string<char> const &, char const *);
233 template bool operator==(trivial_string<char_type> const &, char_type const *);
234 template <typename Char>
235 bool operator==(trivial_string<Char> const & lhs, Char const * rhs)
237 return lhs.compare(trivial_string<Char>(rhs)) == 0;
241 template bool operator==(char const *, trivial_string<char> const &);
242 template bool operator==(char_type const *, trivial_string<char_type> const &);
243 template <typename Char>
244 bool operator==(Char const * lhs, trivial_string<Char> const & rhs)
246 return rhs.compare(trivial_string<Char>(lhs)) == 0;
250 template ostream & operator<<(ostream &, trivial_string<char> const &);
251 template odocstream & operator<<(odocstream &, trivial_string<char_type> const &);
252 template <typename Char>
253 basic_ostream<Char, char_traits<Char> > &
254 operator<<(basic_ostream<Char, char_traits<Char> > & os, trivial_string<Char> const & s)
256 return os << basic_string<Char, char_traits<Char>, allocator<Char> >(s);