]> git.lyx.org Git - lyx.git/blob - src/support/trivstring.cpp
Provide proper fallback if a bibliography processor is not found
[lyx.git] / src / support / trivstring.cpp
1 /**
2  * \file trivstring.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Georg Baum
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "support/trivstring.h"
14 #include "support/docstring.h"
15
16 #ifdef STD_STRING_USES_COW
17 #include <algorithm>
18 #include <ostream>
19 #include <stdexcept>
20
21 using namespace std;
22
23 namespace lyx {
24
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_)
29 {
30         if (use_sso())
31                 copy(that.data_sso(), that.data_sso() + size_ + 1, data_sso());
32         else if (size_ > 0) {
33                 data_ = new Char[size_ + 1];
34                 copy(that.data_, that.data_ + size_ + 1, data_);
35         } else {
36                 // Happens only for really big Char types
37                 data_ = 0;
38         }
39 }
40
41
42 template<typename Char>
43 trivial_string<Char>::trivial_string(Char const * that, size_t n) : size_(n)
44 {
45         if (use_sso()) {
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_);
51                 data_[size_] = '\0';
52         } else {
53                 // Happens only for really big Char types
54                 data_ = 0;
55         }
56 }
57
58
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())
64 {
65         if (use_sso()) {
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_);
71                 data_[size_] = '\0';
72         } else {
73                 // Happens only for really big Char types
74                 data_ = 0;
75         }
76 }
77
78
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)
85 {
86         if (&that == this)
87                 return *this;
88         if (!use_sso())
89                 delete[] data_;
90         size_ = that.size_;
91         if (use_sso())
92                 copy(that.data_sso(), that.data_sso() + size_ + 1, data_sso());
93         else if (size_ > 0) {
94                 data_ = new Char[size_ + 1];
95                 copy(that.data_, that.data_ + size_ + 1, data_);
96         } else {
97                 // Happens only for really big Char types
98                 data_ = 0;
99         }
100         return *this;
101 }
102
103
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)
111 {
112         if (!use_sso())
113                 delete[] data_;
114         size_ = that.size();
115         if (use_sso()) {
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_);
121         } else {
122                 // Happens only for really big Char types
123                 data_ = 0;
124         }
125         return *this;
126 }
127
128
129 template void
130 trivial_string<char>::swap(trivial_string<char> &);
131 template void
132 trivial_string<char_type>::swap(trivial_string<char_type> &);
133 template<typename Char>
134 void trivial_string<Char>::swap(trivial_string & that)
135 {
136         size_t const sizetmp = that.size_;
137         that.size_ = size_;
138         size_ = sizetmp;
139         Char * const datatmp = that.data_;
140         that.data_ = data_;
141         data_ = datatmp;
142 }
143
144
145 template<typename Char>
146 int trivial_string<Char>::compare(trivial_string const & other) const
147 {
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);
152         if (r == 0) {
153                 if (lsize > rsize)
154                         r = 1;
155                 else if (lsize < rsize)
156                         r = -1;
157         }
158         return r;
159 }
160
161
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
166 {
167         if (pos > length())
168                 throw out_of_range("trivial_string::substr");
169         if (n == _stdstring::npos)
170                 n = length() - pos;
171         size_t const l = min(pos + n, length());
172         return trivial_string(c_str() + pos, l - pos);
173 }
174
175
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
180 {
181         if (use_sso())
182                 return _stdstring(data_sso(), size_);
183         if (size_ > 0)
184                 return _stdstring(data_, size_);
185         // Happens only for really big Char types
186         return _stdstring();
187 }
188
189
190 template char const * trivial_string<char>::c_str() const;
191 template char_type const * trivial_string<char_type>::c_str() const;
192 template<typename Char> Char const * trivial_string<Char>::c_str() const
193 {
194         if (use_sso())
195                 return data_sso();
196         if (size_ > 0)
197                 return data_;
198         // Happens only for really big Char types
199         static const Char empty_char = '\0';
200         return &empty_char;
201 }
202
203
204 template char trivial_string<char>::operator[](size_t) const;
205 template char_type trivial_string<char_type>::operator[](size_t) const;
206 template <typename Char> Char trivial_string<Char>::operator[](size_t i) const
207 {
208         return c_str()[i];
209 }
210
211
212 template bool operator<(trivial_string<char> const &,
213                         trivial_string<char> const &);
214 template bool operator<(trivial_string<char_type> const &,
215                         trivial_string<char_type> const &);
216 template <typename Char>
217 bool operator<(trivial_string<Char> const & lhs, trivial_string<Char> const & rhs)
218 {
219         return lhs.compare(rhs) < 0;
220 }
221
222
223 template bool operator==(trivial_string<char> const &,
224                          trivial_string<char> const &);
225 template bool operator==(trivial_string<char_type> const &,
226                          trivial_string<char_type> const &);
227 template <typename Char>
228 bool operator==(trivial_string<Char> const & lhs, trivial_string<Char> const & rhs)
229 {
230         return lhs.compare(rhs) == 0;
231 }
232
233
234 template bool operator==(trivial_string<char> const &, char const *);
235 template bool operator==(trivial_string<char_type> const &, char_type const *);
236 template <typename Char>
237 bool operator==(trivial_string<Char> const & lhs, Char const * rhs)
238 {
239         return lhs.compare(trivial_string<Char>(rhs)) == 0;
240 }
241
242
243 template bool operator==(char const *, trivial_string<char> const &);
244 template bool operator==(char_type const *, trivial_string<char_type> const &);
245 template <typename Char>
246 bool operator==(Char const * lhs, trivial_string<Char> const & rhs)
247 {
248         return rhs.compare(trivial_string<Char>(lhs)) == 0;
249 }
250
251
252 template ostream & operator<<(ostream &, trivial_string<char> const &);
253 template odocstream & operator<<(odocstream &, trivial_string<char_type> const &);
254 template <typename Char>
255 basic_ostream<Char, char_traits<Char> > &
256 operator<<(basic_ostream<Char, char_traits<Char> > & os, trivial_string<Char> const & s)
257 {
258         return os << basic_string<Char, char_traits<Char>, allocator<Char> >(s);
259 }
260
261 } // namespace lyx
262 #endif