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