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 /// convert \p s from ucs4 to the encoding of the file system.
63 std::string const to_filesystem8bit(docstring const & s);
65 /// Compare a docstring with a C string of ASCII characters
66 bool operator==(lyx::docstring const &, char const *);
68 /// Compare a C string of ASCII characters with a docstring
69 inline bool operator==(char const * l, lyx::docstring const & r) { return r == l; }
71 /// Compare a docstring with a C string of ASCII characters
72 inline bool operator!=(lyx::docstring const & l, char const * r) { return !(l == r); }
74 /// Compare a C string of ASCII characters with a docstring
75 inline bool operator!=(char const * l, lyx::docstring const & r) { return !(r == l); }
77 /// Concatenate a docstring and a C string of ASCII characters
78 lyx::docstring operator+(lyx::docstring const &, char const *);
80 /// Concatenate a C string of ASCII characters and a docstring
81 lyx::docstring operator+(char const *, lyx::docstring const &);
83 /// Concatenate a docstring and a single ASCII character
84 lyx::docstring operator+(lyx::docstring const & l, char r);
86 /// Concatenate a single ASCII character and a docstring
87 lyx::docstring operator+(char l, lyx::docstring const & r);
89 /// Append a C string of ASCII characters to a docstring
90 lyx::docstring & operator+=(lyx::docstring &, char const *);
92 /// Append a single ASCII character to a docstring
93 lyx::docstring & operator+=(lyx::docstring & l, char r);
98 #if SIZEOF_WCHAR_T != 4 && defined(__GNUC__) && defined(__GNUC_MINOR__) && __GNUC__ == 3 && __GNUC_MINOR__ < 4
99 // Missing char_traits methods in gcc 3.3 and older. Taken from gcc 4.2svn.
102 template<typename T> void
103 char_traits<T>::assign(char_type & c1, char_type const & c2)
109 template<typename T> bool
110 char_traits<T>::eq(char_type const & c1, char_type const & c2)
116 template<typename T> bool
117 char_traits<T>::lt(char_type const & c1, char_type const & c2)
123 template<typename T> int
124 char_traits<T>::compare(char_type const * s1, char_type const * s2, std::size_t n)
126 for (std::size_t i = 0; i < n; ++i)
127 if (lt(s1[i], s2[i]))
129 else if (lt(s2[i], s1[i]))
135 template<typename T> std::size_t
136 char_traits<T>::length(char_type const * p)
139 while (!eq(p[i], char_type()))
145 template<typename T> typename char_traits<T>::char_type const *
146 char_traits<T>::find(char_type const * s, size_t n, char_type const & a)
148 for (std::size_t i = 0; i < n; ++i)
155 template<typename T> typename char_traits<T>::char_type *
156 char_traits<T>::move(char_type * s1, char_type const * s2, std::size_t n)
158 return static_cast<T *>(std::memmove(s1, s2, n * sizeof(char_type)));
162 template<typename T> typename char_traits<T>::char_type *
163 char_traits<T>::copy(char_type * s1, char_type const * s2, std::size_t n)
165 std::copy(s2, s2 + n, s1);
170 template<typename T> typename char_traits<T>::char_type *
171 char_traits<T>::assign(char_type * s, std::size_t n, char_type a)
173 std::fill_n(s, n, a);
178 template<typename T> typename char_traits<T>::char_type
179 char_traits<T>::to_char_type(int_type const & c)
181 return static_cast<char_type>(c);
185 template<typename T> typename char_traits<T>::int_type
186 char_traits<T>::to_int_type(char_type const & c)
188 return static_cast<int_type>(c);
192 template<typename T> bool
193 char_traits<T>::eq_int_type(int_type const & c1, int_type const & c2)
199 template<typename T> typename char_traits<T>::int_type
200 char_traits<T>::eof()
202 return static_cast<int_type>(EOF);
206 template<typename T> typename char_traits<T>::int_type
207 char_traits<T>::not_eof(int_type const & c)
209 return !eq_int_type(c, eof()) ? c : to_int_type(char_type());