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/strfwd.h"
23 * String type for storing the main text in UCS4 encoding.
24 * Use std::string only in cases 7-bit ASCII is to be manipulated
25 * within the variable.
27 typedef std::basic_string<char_type> docstring;
29 /// Creates a docstring from a C string of ASCII characters
30 docstring const from_ascii(char const *);
32 /// Creates a docstring from a std::string of ASCII characters
33 docstring const from_ascii(std::string const &);
35 /// Creates a std::string of ASCII characters from a docstring
36 std::string const to_ascii(docstring const &);
38 /// Creates a docstring from a UTF8 string. This should go eventually.
39 docstring const from_utf8(std::string const &);
41 /// Creates a UTF8 string from a docstring. This should go eventually.
42 std::string const to_utf8(docstring const &);
44 /// convert \p s from the encoding of the locale to ucs4.
45 docstring const from_local8bit(std::string const & s);
48 * Convert \p s from ucs4 to the encoding of the locale.
49 * This may fail and throw an exception, the caller is expected to act
52 std::string const to_local8bit(docstring const & s);
54 /// convert \p s from the encoding of the file system to ucs4.
55 docstring const from_filesystem8bit(std::string const & s);
57 /// convert \p s from ucs4 to the encoding of the file system.
58 std::string const to_filesystem8bit(docstring const & s);
60 /// normalize \p s to precomposed form c
61 docstring const normalize_c(docstring const & s);
63 /// Compare a docstring with a C string of ASCII characters
64 bool operator==(docstring const &, char const *);
66 /// Compare a C string of ASCII characters with a docstring
67 inline bool operator==(char const * l, docstring const & r) { return r == l; }
69 /// Compare a docstring with a C string of ASCII characters
70 inline bool operator!=(docstring const & l, char const * r) { return !(l == r); }
72 /// Compare a C string of ASCII characters with a docstring
73 inline bool operator!=(char const * l, docstring const & r) { return !(r == l); }
75 /// Concatenate a docstring and a C string of ASCII characters
76 docstring operator+(docstring const &, char const *);
78 /// Concatenate a C string of ASCII characters and a docstring
79 docstring operator+(char const *, docstring const &);
81 /// Concatenate a docstring and a single ASCII character
82 docstring operator+(docstring const & l, char r);
84 /// Concatenate a single ASCII character and a docstring
85 docstring operator+(char l, docstring const & r);
87 /// Append a C string of ASCII characters to a docstring
88 docstring & operator+=(docstring &, char const *);
90 /// Append a single ASCII character to a docstring
91 docstring & operator+=(docstring & l, char r);
96 #if ! defined(USE_WCHAR_T) && defined(__GNUC__) && defined(__GNUC_MINOR__) && __GNUC__ == 3 && __GNUC_MINOR__ < 4
97 // Missing char_traits methods in gcc 3.3 and older. Taken from gcc 4.2svn.
100 template<typename T> void
101 char_traits<T>::assign(char_type & c1, char_type const & c2)
107 template<typename T> bool
108 char_traits<T>::eq(char_type const & c1, char_type const & c2)
114 template<typename T> bool
115 char_traits<T>::lt(char_type const & c1, char_type const & c2)
121 template<typename T> int
122 char_traits<T>::compare(char_type const * s1, char_type const * s2, std::size_t n)
124 for (std::size_t i = 0; i < n; ++i)
125 if (lt(s1[i], s2[i]))
127 else if (lt(s2[i], s1[i]))
133 template<typename T> std::size_t
134 char_traits<T>::length(char_type const * p)
137 while (!eq(p[i], char_type()))
143 template<typename T> typename char_traits<T>::char_type const *
144 char_traits<T>::find(char_type const * s, size_t n, char_type const & a)
146 for (std::size_t i = 0; i < n; ++i)
153 template<typename T> typename char_traits<T>::char_type *
154 char_traits<T>::move(char_type * s1, char_type const * s2, std::size_t n)
156 return static_cast<T *>(std::memmove(s1, s2, n * sizeof(char_type)));
160 template<typename T> typename char_traits<T>::char_type *
161 char_traits<T>::copy(char_type * s1, char_type const * s2, std::size_t n)
163 std::copy(s2, s2 + n, s1);
168 template<typename T> typename char_traits<T>::char_type *
169 char_traits<T>::assign(char_type * s, std::size_t n, char_type a)
171 std::fill_n(s, n, a);
176 template<typename T> typename char_traits<T>::char_type
177 char_traits<T>::to_char_type(int_type const & c)
179 return static_cast<char_type>(c);
183 template<typename T> typename char_traits<T>::int_type
184 char_traits<T>::to_int_type(char_type const & c)
186 return static_cast<int_type>(c);
190 template<typename T> bool
191 char_traits<T>::eq_int_type(int_type const & c1, int_type const & c2)
197 template<typename T> typename char_traits<T>::int_type
198 char_traits<T>::eof()
200 return static_cast<int_type>(EOF);
204 template<typename T> typename char_traits<T>::int_type
205 char_traits<T>::not_eof(int_type const & c)
207 return !eq_int_type(c, eof()) ? c : to_int_type(char_type());