4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
10 * Full author contact details are available in file CREDITS.
13 #ifndef LYX_DOCSTRING_H
14 #define LYX_DOCSTRING_H
16 #include "support/types.h"
23 /// String type for storing the main text in UCS4 encoding
24 typedef std::basic_string<char_type> docstring;
26 /// Creates a docstring from a C string of ASCII characters
27 docstring const from_ascii(char const *);
29 /// Creates a docstring from a std::string of ASCII characters
30 docstring const from_ascii(std::string const &);
32 /// Creates a std::string of ASCII characters from a docstring
33 std::string const to_ascii(docstring const &);
35 /// Creates a docstring from a UTF8 string. This should go eventually.
36 docstring const from_utf8(std::string const &);
38 /// Creates a UTF8 string from a docstring. This should go eventually.
39 std::string const to_utf8(docstring const &);
41 /// convert \p s from the encoding of the locale to ucs4.
42 docstring const from_local8bit(std::string const & s);
44 /// Exception thrown by to_local8bit if the string could not be converted
45 class to_local8bit_failure : public std::bad_cast {
47 to_local8bit_failure() throw() : std::bad_cast() {}
48 virtual ~to_local8bit_failure() throw() {}
49 virtual const char* what() const throw();
53 * Convert \p s from ucs4 to the encoding of the locale.
54 * This may fail and throw an exception, the caller is expected to act
57 std::string const to_local8bit(docstring const & s);
59 /// convert \p s from the encoding of the file system to ucs4.
60 docstring const from_filesystem8bit(std::string const & s);
62 /// Compare a docstring with a C string of ASCII characters
63 bool operator==(lyx::docstring const &, char const *);
65 /// Compare a C string of ASCII characters with a docstring
66 inline bool operator==(char const * l, lyx::docstring const & r) { return r == l; }
68 /// Compare a docstring with a C string of ASCII characters
69 inline bool operator!=(lyx::docstring const & l, char const * r) { return !(l == r); }
71 /// Compare a C string of ASCII characters with a docstring
72 inline bool operator!=(char const * l, lyx::docstring const & r) { return !(r == l); }
74 /// Concatenate a docstring and a C string of ASCII characters
75 lyx::docstring operator+(lyx::docstring const &, char const *);
77 /// Concatenate a C string of ASCII characters and a docstring
78 lyx::docstring operator+(char const *, lyx::docstring const &);
80 /// Concatenate a docstring and a single ASCII character
81 lyx::docstring operator+(lyx::docstring const & l, char r);
83 /// Concatenate a single ASCII character and a docstring
84 lyx::docstring operator+(char l, lyx::docstring const & r);
86 /// Append a C string of ASCII characters to a docstring
87 lyx::docstring & operator+=(lyx::docstring &, char const *);
89 /// Append a single ASCII character to a docstring
90 lyx::docstring & operator+=(lyx::docstring & l, char r);
95 #if SIZEOF_WCHAR_T != 4 && defined(__GNUC__) && defined(__GNUC_MINOR__) && __GNUC__ == 3 && __GNUC_MINOR__ < 4
96 // Missing char_traits methods in gcc 3.3 and older. Taken from gcc 4.2svn.
99 template<typename T> void
100 char_traits<T>::assign(char_type & c1, char_type const & c2)
106 template<typename T> bool
107 char_traits<T>::eq(char_type const & c1, char_type const & c2)
113 template<typename T> bool
114 char_traits<T>::lt(char_type const & c1, char_type const & c2)
120 template<typename T> int
121 char_traits<T>::compare(char_type const * s1, char_type const * s2, std::size_t n)
123 for (std::size_t i = 0; i < n; ++i)
124 if (lt(s1[i], s2[i]))
126 else if (lt(s2[i], s1[i]))
132 template<typename T> std::size_t
133 char_traits<T>::length(char_type const * p)
136 while (!eq(p[i], char_type()))
142 template<typename T> typename char_traits<T>::char_type const *
143 char_traits<T>::find(char_type const * s, size_t n, char_type const & a)
145 for (std::size_t i = 0; i < n; ++i)
152 template<typename T> typename char_traits<T>::char_type *
153 char_traits<T>::move(char_type * s1, char_type const * s2, std::size_t n)
155 return static_cast<T *>(std::memmove(s1, s2, n * sizeof(char_type)));
159 template<typename T> typename char_traits<T>::char_type *
160 char_traits<T>::copy(char_type * s1, char_type const * s2, std::size_t n)
162 std::copy(s2, s2 + n, s1);
167 template<typename T> typename char_traits<T>::char_type *
168 char_traits<T>::assign(char_type * s, std::size_t n, char_type a)
170 std::fill_n(s, n, a);
175 template<typename T> typename char_traits<T>::char_type
176 char_traits<T>::to_char_type(int_type const & c)
178 return static_cast<char_type>(c);
182 template<typename T> typename char_traits<T>::int_type
183 char_traits<T>::to_int_type(char_type const & c)
185 return static_cast<int_type>(c);
189 template<typename T> bool
190 char_traits<T>::eq_int_type(int_type const & c1, int_type const & c2)
196 template<typename T> typename char_traits<T>::int_type
197 char_traits<T>::eof()
199 return static_cast<int_type>(EOF);
203 template<typename T> typename char_traits<T>::int_type
204 char_traits<T>::not_eof(int_type const & c)
206 return !eq_int_type(c, eof()) ? c : to_int_type(char_type());