]> git.lyx.org Git - lyx.git/blob - src/support/docstring.h
7b248f6ab1a65e9d4d7103f1233943bab20972e5
[lyx.git] / src / support / docstring.h
1 // -*- C++ -*-
2 /**
3  * \file docstring.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author Georg Baum
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #ifndef LYX_DOCSTRING_H
14 #define LYX_DOCSTRING_H
15
16 #include "support/types.h"
17
18 #include <string>
19
20 namespace lyx {
21
22 /// String type for storing the main text in UCS4 encoding
23 typedef std::basic_string<char_type> docstring;
24
25 /// Creates a docstring from a C string of ASCII characters
26 docstring const from_ascii(char const *);
27
28 /// Creates a docstring from a std::string of ASCII characters
29 docstring const from_ascii(std::string const &);
30
31 /// Creates a docstring from a UTF8 string. This should go eventually.
32 docstring const from_utf8(std::string const &);
33
34 /// Creates a UTF8 string from a docstring. This should go eventually.
35 std::string const to_utf8(docstring const &);
36
37 }
38
39 /// Compare a docstring with a C string of ASCII characters
40 bool operator==(lyx::docstring const &, char const *);
41
42 /// Compare a C string of ASCII characters with a docstring
43 inline bool operator==(char const * l, lyx::docstring const & r) { return r == l; }
44
45 /// Compare a docstring with a C string of ASCII characters
46 inline bool operator!=(lyx::docstring const & l, char const * r) { return !(l == r); }
47
48 /// Compare a C string of ASCII characters with a docstring
49 inline bool operator!=(char const * l, lyx::docstring const & r) { return !(r == l); }
50
51 /// Concatenate a docstring and a C string of ASCII characters
52 lyx::docstring operator+(lyx::docstring const &, char const *);
53
54 /// Concatenate a C string of ASCII characters and a docstring
55 lyx::docstring operator+(char const *, lyx::docstring const &);
56
57 /// Concatenate a docstring and a single ASCII character
58 lyx::docstring operator+(lyx::docstring const & l, char r);
59
60 /// Concatenate a single ASCII character and a docstring
61 lyx::docstring operator+(char l, lyx::docstring const & r);
62
63
64 #if SIZEOF_WCHAR_T != 4 && defined(__GNUC__) && defined(__GNUC_MINOR__) && __GNUC__ == 3 && __GNUC_MINOR__ < 4
65 // Missing char_traits methods in gcc 3.3 and older. Taken from gcc 4.2svn.
66 namespace std {
67
68 template<typename T> void
69 char_traits<T>::assign(char_type & c1, char_type const & c2)
70 {
71         c1 = c2;
72 }
73
74
75 template<typename T> bool
76 char_traits<T>::eq(char_type const & c1, char_type const & c2)
77 {
78         return c1 == c2;
79 }
80
81
82 template<typename T> bool
83 char_traits<T>::lt(char_type const & c1, char_type const & c2)
84 {
85         return c1 < c2;
86 }
87
88
89 template<typename T> int
90 char_traits<T>::compare(char_type const * s1, char_type const * s2, std::size_t n)
91 {
92         for (std::size_t i = 0; i < n; ++i)
93                 if (lt(s1[i], s2[i]))
94                         return -1;
95                 else if (lt(s2[i], s1[i]))
96                         return 1;
97         return 0;
98 }
99
100
101 template<typename T> std::size_t
102 char_traits<T>::length(char_type const * p)
103 {
104         std::size_t i = 0;
105         while (!eq(p[i], char_type()))
106                 ++i;
107         return i;
108 }
109
110
111 template<typename T> typename char_traits<T>::char_type const *
112 char_traits<T>::find(char_type const * s, size_t n, char_type const & a)
113 {
114         for (std::size_t i = 0; i < n; ++i)
115                 if (eq(s[i], a))
116                         return s + i;
117         return 0;
118 }
119
120
121 template<typename T> typename char_traits<T>::char_type *
122 char_traits<T>::move(char_type * s1, char_type const * s2, std::size_t n)
123 {
124         return static_cast<T *>(std::memmove(s1, s2, n * sizeof(char_type)));
125 }
126
127
128 template<typename T> typename char_traits<T>::char_type *
129 char_traits<T>::copy(char_type * s1, char_type const * s2, std::size_t n)
130 {
131         std::copy(s2, s2 + n, s1);
132         return s1;
133 }
134
135
136 template<typename T> typename char_traits<T>::char_type *
137 char_traits<T>::assign(char_type * s, std::size_t n, char_type a)
138 {
139         std::fill_n(s, n, a);
140         return s;
141 }
142
143
144 template<typename T> typename char_traits<T>::char_type
145 char_traits<T>::to_char_type(int_type const & c)
146 {
147         return static_cast<char_type>(c);
148 }
149
150
151 template<typename T> typename char_traits<T>::int_type
152 char_traits<T>::to_int_type(char_type const & c)
153 {
154         return static_cast<int_type>(c);
155 }
156
157
158 template<typename T> bool
159 char_traits<T>::eq_int_type(int_type const & c1, int_type const & c2)
160 {
161         return c1 == c2;
162 }
163
164
165 template<typename T> typename char_traits<T>::int_type
166 char_traits<T>::eof()
167 {
168         return static_cast<int_type>(EOF);
169 }
170
171
172 template<typename T> typename char_traits<T>::int_type
173 char_traits<T>::not_eof(int_type const & c)
174 {
175         return !eq_int_type(c, eof()) ? c : to_int_type(char_type());
176 }
177
178 }
179 #endif
180 #endif