]> git.lyx.org Git - lyx.git/blob - src/support/docstring.h
convert lfun arguments to docstring
[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 <boost/cstdint.hpp>
17 #include <string>
18
19 namespace lyx {
20
21 /// String type for storing the main text in UCS4 encoding
22 typedef std::basic_string<boost::uint32_t> docstring;
23
24 /// Creates a docstring from a C string of ASCII characters
25 docstring const from_ascii(char const *);
26
27 /// Creates a docstring from a std::string of ASCII characters
28 docstring const from_ascii(std::string const &);
29
30 /// Creates a docstring from a UTF8 string. This should go eventually.
31 docstring const from_utf8(std::string const &);
32
33 /// Creates a UTF8 string from a docstring. This should go eventually.
34 std::string const to_utf8(docstring const &);
35
36 }
37
38 /// Compare a docstring with a C string of ASCII characters
39 bool operator==(lyx::docstring const &, char const *);
40
41 /// Compare a C string of ASCII characters with a docstring
42 inline bool operator==(char const * l, lyx::docstring const & r) { return r == l; }
43
44 /// Compare a docstring with a C string of ASCII characters
45 inline bool operator!=(lyx::docstring const & l, char const * r) { return !(l == r); }
46
47 /// Compare a C string of ASCII characters with a docstring
48 inline bool operator!=(char const * l, lyx::docstring const & r) { return !(r == l); }
49
50 #if defined(__GNUC__) && defined(__GNUC_MINOR__) && __GNUC__ == 3 && __GNUC_MINOR__ < 4
51 // Missing char_traits methods in gcc 3.3 and older. Taken from gcc 4.2svn.
52 namespace std {
53
54 template<typename T> void
55 char_traits<T>::assign(char_type & c1, char_type const & c2)
56 {
57         c1 = c2;
58 }
59
60
61 template<typename T> bool
62 char_traits<T>::eq(char_type const & c1, char_type const & c2)
63 {
64         return c1 == c2;
65 }
66
67
68 template<typename T> bool
69 char_traits<T>::lt(char_type const & c1, char_type const & c2)
70 {
71         return c1 < c2;
72 }
73
74
75 template<typename T> int
76 char_traits<T>::compare(char_type const * s1, char_type const * s2, std::size_t n)
77 {
78         for (std::size_t i = 0; i < n; ++i)
79                 if (lt(s1[i], s2[i]))
80                         return -1;
81                 else if (lt(s2[i], s1[i]))
82                         return 1;
83         return 0;
84 }
85
86
87 template<typename T> std::size_t
88 char_traits<T>::length(char_type const * p)
89 {
90         std::size_t i = 0;
91         while (!eq(p[i], char_type()))
92                 ++i;
93         return i;
94 }
95
96
97 template<typename T> typename char_traits<T>::char_type const *
98 char_traits<T>::find(char_type const * s, size_t n, char_type const & a)
99 {
100         for (std::size_t i = 0; i < n; ++i)
101                 if (eq(s[i], a))
102                         return s + i;
103         return 0;
104 }
105
106
107 template<typename T> typename char_traits<T>::char_type *
108 char_traits<T>::move(char_type * s1, char_type const * s2, std::size_t n)
109 {
110         return static_cast<T *>(std::memmove(s1, s2, n * sizeof(char_type)));
111 }
112
113
114 template<typename T> typename char_traits<T>::char_type *
115 char_traits<T>::copy(char_type * s1, char_type const * s2, std::size_t n)
116 {
117         std::copy(s2, s2 + n, s1);
118         return s1;
119 }
120
121
122 template<typename T> typename char_traits<T>::char_type *
123 char_traits<T>::assign(char_type * s, std::size_t n, char_type a)
124 {
125         std::fill_n(s, n, a);
126         return s;
127 }
128
129
130 template<typename T> typename char_traits<T>::char_type
131 char_traits<T>::to_char_type(int_type const & c)
132 {
133         return static_cast<char_type>(c);
134 }
135
136
137 template<typename T> typename char_traits<T>::int_type
138 char_traits<T>::to_int_type(char_type const & c)
139 {
140         return static_cast<int_type>(c);
141 }
142
143
144 template<typename T> bool
145 char_traits<T>::eq_int_type(int_type const & c1, int_type const & c2)
146 {
147         return c1 == c2;
148 }
149
150
151 template<typename T> typename char_traits<T>::int_type
152 char_traits<T>::eof()
153 {
154         return static_cast<int_type>(EOF);
155 }
156
157
158 template<typename T> typename char_traits<T>::int_type
159 char_traits<T>::not_eof(int_type const & c)
160 {
161         return !eq_int_type(c, eof()) ? c : to_int_type(char_type());
162 }
163
164 }
165 #endif
166 #endif