]> git.lyx.org Git - lyx.git/blob - src/support/lstrings.h
3f47c4820b4cb8b06ba8ed4739bfef0506382d09
[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 #include "Lsstream.h"
20
21 #include "LString.h"
22
23
24 ///
25 int compare_no_case(string const & s, string const & s2);
26
27 ///
28 int compare_no_case(string const & s, string const & s2, unsigned int len);
29
30 ///
31 inline
32 int compare(char const * a, char const * b)
33 {
34         return strcmp(a, b);
35 }
36
37 ///
38 inline
39 int compare(char const * a, char const * b, unsigned int len)
40 {
41         return strncmp(a, b, len);
42 }
43
44 ///
45 bool isStrInt(string const & str);
46
47 /// does the string represent an unsigned integer value ?
48 bool isStrUnsignedInt(string const & str);
49
50 ///
51 int strToInt(string const & str);
52
53 /// convert string to an unsigned integer
54 unsigned int strToUnsignedInt(string const & str);
55
56 ///
57 bool isStrDbl(string const & str);
58
59 ///
60 double strToDbl(string const & str);
61
62 /// 
63 char lowercase(char c);
64
65 /// 
66 char uppercase(char c);
67
68 ///
69 string const lowercase(string const &);
70
71 ///
72 string const uppercase(string const &);
73
74 /// convert T to string
75 template<typename T>
76 inline
77 string const tostr(T const & t) 
78 {
79         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 }
86
87
88 ///
89 template<>
90 inline
91 string const tostr(bool const & b)
92 {
93         return (b ? "true" : "false");
94 }
95
96 ///
97 template<>
98 inline
99 string const tostr(string const & s)
100 {
101         return s;
102 }
103
104 /// Does the string start with this prefix?
105 bool prefixIs(string const &, char const *);
106
107 /// Does the string start with this prefix?
108 bool prefixIs(string const &, string const &);
109
110 /// Does the string end with this char?
111 bool suffixIs(string const &, char);
112
113 /// Does the string end with this suffix?
114 bool suffixIs(string const &, char const *);
115
116 /// Does the string end with this suffix?
117 bool suffixIs(string const &, string const &);
118
119 ///
120 bool contains(char const * a, string const & b);
121
122 ///
123 bool contains(string const & a, char const * b);
124
125 ///
126 bool contains(string const & a, string const & b);
127
128 ///
129 bool contains(string const & a, char b);
130
131 ///
132 bool contains(char const * a, char const * b);
133
134 ///
135 bool containsOnly(string const &, char const *);
136
137 ///
138 bool containsOnly(string const &, string const &);
139
140 ///
141 bool containsOnly(char const *, char const *);
142
143 ///
144 bool containsOnly(char const *, string const &);
145
146 /// Counts how many of character c there is in a
147 string::size_type countChar(string const & a, char c);
148
149 /** Extracts a token from this string at the nth delim.
150   Doesn't modify the original string. Similar to strtok.
151   Example:
152   #"a;bc;d".token(';', 1) == "bc";#
153   #"a;bc;d".token(';', 2) == "d";#
154 */
155 string const token(string const & a, char delim, int n);
156
157
158 /** Search a token in this string using the delim.
159   Doesn't modify the original string. Returns -1 in case of
160   failure. 
161   Example:
162   #"a;bc;d".tokenPos(';', "bc") == 1;#
163   #"a;bc;d".token(';', "d") == 2;#
164 */
165 int tokenPos(string const & a, char delim, string const & tok);
166
167
168 /** Compares a string and a (simple) regular expression
169   The only element allowed is "*" for any string of characters
170   */
171 bool regexMatch(string const & a, string const & pattern);
172
173 /// Substitute all "oldchar"s with "newchar"
174 string const subst(string const & a, char oldchar, char newchar);
175
176 /// Substitutes all instances of oldstr with newstr
177 string const subst(string const & a,
178              char const * oldstr, string const & newstr);
179
180 /// substitutes all instances ofr oldstr with newstr
181 string const subst(string const & a,
182                    string const & oldstr, string const & newstr);
183
184 /** Strips characters off the end of a string.
185   #"abccc".strip('c') = "ab".#
186   */
187 string const strip(string const & a, char c = ' ');
188
189 /** Strips characters of the beginning of a string.
190   #"cccba".frontstrip('c') = "ba"#. */
191 string const frontStrip(string const & a, char c = ' ');
192
193 /** Strips characters off the beginning of a string.
194     #"ababcdef".frontstrip("ab") = "cdef"# .*/
195 string const frontStrip(string const & a, char const * p);
196
197 /** Splits the string by the first delim.
198   Splits the string by the first appearance of delim.
199   The leading string up to delim is returned in piece (not including
200   delim), while the original string is cut from after the delimiter.
201   Example:
202   #s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";#
203   */
204 string const split(string const & a, string & piece, char delim);
205
206 /// Same as split but does not return a piece
207 string const split(string const & a, char delim);
208
209 /// Same as split but uses the last delim.
210 string const rsplit(string const & a, string & piece, char delim);
211
212 #endif