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 /// normalize \p s to precomposed form c
66 docstring const normalize_c(docstring const & s);
68 /// Compare a docstring with a C string of ASCII characters
69 bool operator==(lyx::docstring const &, char const *);
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 /// Compare a docstring with a C string of ASCII characters
75 inline bool operator!=(lyx::docstring const & l, char const * r) { return !(l == r); }
77 /// Compare a C string of ASCII characters with a docstring
78 inline bool operator!=(char const * l, lyx::docstring const & r) { return !(r == l); }
80 /// Concatenate a docstring and a C string of ASCII characters
81 lyx::docstring operator+(lyx::docstring const &, char const *);
83 /// Concatenate a C string of ASCII characters and a docstring
84 lyx::docstring operator+(char const *, lyx::docstring const &);
86 /// Concatenate a docstring and a single ASCII character
87 lyx::docstring operator+(lyx::docstring const & l, char r);
89 /// Concatenate a single ASCII character and a docstring
90 lyx::docstring operator+(char l, lyx::docstring const & r);
92 /// Append a C string of ASCII characters to a docstring
93 lyx::docstring & operator+=(lyx::docstring &, char const *);
95 /// Append a single ASCII character to a docstring
96 lyx::docstring & operator+=(lyx::docstring & l, char r);
101 #if ! defined(USE_WCHAR_T) && defined(__GNUC__) && defined(__GNUC_MINOR__) && __GNUC__ == 3 && __GNUC_MINOR__ < 4
102 // Missing char_traits methods in gcc 3.3 and older. Taken from gcc 4.2svn.
105 template<typename T> void
106 char_traits<T>::assign(char_type & c1, char_type const & c2)
112 template<typename T> bool
113 char_traits<T>::eq(char_type const & c1, char_type const & c2)
119 template<typename T> bool
120 char_traits<T>::lt(char_type const & c1, char_type const & c2)
126 template<typename T> int
127 char_traits<T>::compare(char_type const * s1, char_type const * s2, std::size_t n)
129 for (std::size_t i = 0; i < n; ++i)
130 if (lt(s1[i], s2[i]))
132 else if (lt(s2[i], s1[i]))
138 template<typename T> std::size_t
139 char_traits<T>::length(char_type const * p)
142 while (!eq(p[i], char_type()))
148 template<typename T> typename char_traits<T>::char_type const *
149 char_traits<T>::find(char_type const * s, size_t n, char_type const & a)
151 for (std::size_t i = 0; i < n; ++i)
158 template<typename T> typename char_traits<T>::char_type *
159 char_traits<T>::move(char_type * s1, char_type const * s2, std::size_t n)
161 return static_cast<T *>(std::memmove(s1, s2, n * sizeof(char_type)));
165 template<typename T> typename char_traits<T>::char_type *
166 char_traits<T>::copy(char_type * s1, char_type const * s2, std::size_t n)
168 std::copy(s2, s2 + n, s1);
173 template<typename T> typename char_traits<T>::char_type *
174 char_traits<T>::assign(char_type * s, std::size_t n, char_type a)
176 std::fill_n(s, n, a);
181 template<typename T> typename char_traits<T>::char_type
182 char_traits<T>::to_char_type(int_type const & c)
184 return static_cast<char_type>(c);
188 template<typename T> typename char_traits<T>::int_type
189 char_traits<T>::to_int_type(char_type const & c)
191 return static_cast<int_type>(c);
195 template<typename T> bool
196 char_traits<T>::eq_int_type(int_type const & c1, int_type const & c2)
202 template<typename T> typename char_traits<T>::int_type
203 char_traits<T>::eof()
205 return static_cast<int_type>(EOF);
209 template<typename T> typename char_traits<T>::int_type
210 char_traits<T>::not_eof(int_type const & c)
212 return !eq_int_type(c, eof()) ? c : to_int_type(char_type());