]> git.lyx.org Git - lyx.git/blob - src/support/lstrings.h
noncopyable + read ChangeLog
[lyx.git] / src / support / lstrings.h
1 // -*- C++ -*-
2
3 /** This is a collection of string helper functions that works
4     together with string (and later also with STL String. Some of these
5     would certainly benefit from a rewrite/optimization.
6 */
7
8 #ifndef LSTRINGS_H
9 #define LSTRINGS_H
10
11 #ifdef __GNUG__
12 #pragma interface
13 #endif
14
15 #include <cstring>
16 #include <cctype>
17
18 #ifdef HAVE_SSTREAM
19 #include <sstream>
20 #else
21 #include <strstream>
22 #endif
23
24 #include "LString.h"
25
26
27 ///
28 int compare_no_case(string const & s, string const & s2);
29
30 ///
31 int compare_no_case(string const & s, string const & s2, unsigned int len);
32
33
34 inline
35 int compare(char const * a, char const * b)
36 {
37         return strcmp(a, b);
38 }
39
40
41
42 inline
43 int compare(char const * a, char const * b, unsigned int len)
44 {
45         return strncmp(a, b, len);
46 }
47
48
49 ///
50 bool isStrInt(string const & str);
51
52 ///
53 int strToInt(string const & str);
54
55 ///
56 bool isStrDbl(string const & str);
57
58 ///
59 double strToDbl(string const & str);
60
61 /// 
62 char lowercase(char c);
63
64 /// 
65 char uppercase(char c);
66
67 ///
68 string lowercase(string const &);
69
70 ///
71 string uppercase(string const &);
72
73 // convert T to string
74 template<typename T>
75 inline
76 string tostr(T const & t) 
77 {
78 #ifdef HAVE_SSTREAM
79         std::ostringstream ostr;
80         ostr << t;
81         return ostr.str().c_str();
82         // We need to use the .c_str since we sometimes are using
83         // our own string class and that is not compatible with
84         // basic_string<char>. (of course we don't want this later)
85 #else
86         // The buf is probably a bit large, but if we want to be safer
87         // we should leave it this big. As compiler/libs gets updated
88         // this part of the code will cease to be used and we loose
89         // nothing.
90         char buf[2048]; // a bit too large perhaps?
91         ostrstream ostr(buf, sizeof(buf));
92         ostr << t << '\0';
93         return buf;
94 #endif
95 }
96
97 inline
98 string tostr(bool b)
99 {
100         return (b ? "true" : "false");
101 }
102         
103 /// Does the string start with this prefix?
104 bool prefixIs(string const &, char const *);
105
106 /// Does the string end with this char?
107 bool suffixIs(string const &, char);
108
109 /// Does the string end with this suffix?
110 bool suffixIs(string const &, char const *);
111
112 ///
113 bool contains(char const * a, string const & b);
114
115 ///
116 bool contains(string const & a, char const * b);
117
118 ///
119 bool contains(string const & a, string const & b);
120
121 ///
122 bool contains(char const * a, char const * b);
123
124 ///
125 bool containsOnly(string const &, char const *);
126
127 ///
128 bool containsOnly(string const &, string const &);
129
130 ///
131 bool containsOnly(char const *, char const *);
132
133 ///
134 bool containsOnly(char const *, string const &);
135
136 /// Counts how many of character c there is in a
137 unsigned int countChar(string const & a, char const c);
138
139 /** Extracts a token from this string at the nth delim.
140   Doesn't modify the original string. Similar to strtok.
141   Example:
142   #"a;bc;d".token(';', 1) == "bc";#
143   #"a;bc;d".token(';', 2) == "d";#
144 */
145 string token(string const & a, char delim, int n);
146
147
148 /** Search a token in this string using the delim.
149   Doesn't modify the original string. Returns -1 in case of
150   failure. 
151   Example:
152   #"a;bc;d".tokenPos(';', "bc") == 1;#
153   #"a;bc;d".token(';', "d") == 2;#
154 */
155 int tokenPos(string const & a, char delim, string const & tok);
156
157
158 /** Compares a string and a (simple) regular expression
159   The only element allowed is "*" for any string of characters
160   */
161 bool regexMatch(string const & a, string const & pattern);
162
163 /// Substitute all "oldchar"s with "newchar"
164 string subst(string const & a, char oldchar, char newchar);
165
166 /// Substitutes all instances of oldstr with newstr
167 string subst(string const & a,
168              char const * oldstr, string const & newstr);
169
170 /** Strips characters off the end of a string.
171   #"abccc".strip('c') = "ab".#
172   */
173 string strip(string const & a, char const c = ' ');
174
175 /** Strips characters of the beginning of a string.
176   #"cccba".frontstrip('c') = "ba"#. */
177 string frontStrip(string const & a, char const c = ' ');
178
179 /** Strips characters off the beginning of a string.
180     #"ababcdef".frontstrip("ab") = "cdef"# .*/
181 string frontStrip(string const & a, char const * p);
182
183 /** Splits the string by the first delim.
184   Splits the string by the first appearance of delim.
185   The leading string up to delim is returned in piece (not including
186   delim), while the original string is cut from after the delimiter.
187   Example:
188   #s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";#
189   */
190 string split(string const & a, string & piece, char delim);
191
192 /// Same as split but does not return a piece
193 string split(string const & a, char delim);
194
195 /// Same as split but uses the last delim.
196 string rsplit(string const & a, string & piece, char delim);
197
198 #endif