]> git.lyx.org Git - lyx.git/blob - src/support/lstrings.h
fix a couple of hard crashes, constify local variables, whitespace changes, some...
[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 ///
48 int strToInt(string const & str);
49
50 ///
51 bool isStrDbl(string const & str);
52
53 ///
54 double strToDbl(string const & str);
55
56 /// 
57 char lowercase(char c);
58
59 /// 
60 char uppercase(char c);
61
62 ///
63 string const lowercase(string const &);
64
65 ///
66 string const uppercase(string const &);
67
68 /// convert T to string
69 template<typename T>
70 inline
71 string const tostr(T const & t) 
72 {
73         ostringstream ostr;
74         ostr << t;
75         return ostr.str().c_str();
76         // We need to use the .c_str since we sometimes are using
77         // our own string class and that is not compatible with
78         // basic_string<char>. (of course we don't want this later)
79 }
80
81
82 ///
83 template<>
84 inline
85 string const tostr(bool const & b)
86 {
87         return (b ? "true" : "false");
88 }
89
90 ///
91 template<>
92 inline
93 string const tostr(string const & s)
94 {
95         return s;
96 }
97
98 /// Does the string start with this prefix?
99 bool prefixIs(string const &, char const *);
100
101 /// Does the string start with this prefix?
102 bool prefixIs(string const &, string const &);
103
104 /// Does the string end with this char?
105 bool suffixIs(string const &, char);
106
107 /// Does the string end with this suffix?
108 bool suffixIs(string const &, char const *);
109
110 /// Does the string end with this suffix?
111 bool suffixIs(string const &, string const &);
112
113 ///
114 bool contains(char const * a, string const & b);
115
116 ///
117 bool contains(string const & a, char const * b);
118
119 ///
120 bool contains(string const & a, string const & b);
121
122 ///
123 bool contains(char const * a, char const * b);
124
125 ///
126 bool containsOnly(string const &, char const *);
127
128 ///
129 bool containsOnly(string const &, string const &);
130
131 ///
132 bool containsOnly(char const *, char const *);
133
134 ///
135 bool containsOnly(char const *, string const &);
136
137 /// Counts how many of character c there is in a
138 string::size_type countChar(string const & a, char c);
139
140 /** Extracts a token from this string at the nth delim.
141   Doesn't modify the original string. Similar to strtok.
142   Example:
143   #"a;bc;d".token(';', 1) == "bc";#
144   #"a;bc;d".token(';', 2) == "d";#
145 */
146 string const token(string const & a, char delim, int n);
147
148
149 /** Search a token in this string using the delim.
150   Doesn't modify the original string. Returns -1 in case of
151   failure. 
152   Example:
153   #"a;bc;d".tokenPos(';', "bc") == 1;#
154   #"a;bc;d".token(';', "d") == 2;#
155 */
156 int tokenPos(string const & a, char delim, string const & tok);
157
158
159 /** Compares a string and a (simple) regular expression
160   The only element allowed is "*" for any string of characters
161   */
162 bool regexMatch(string const & a, string const & pattern);
163
164 /// Substitute all "oldchar"s with "newchar"
165 string const subst(string const & a, char oldchar, char newchar);
166
167 /// Substitutes all instances of oldstr with newstr
168 string const subst(string const & a,
169              char const * oldstr, string const & newstr);
170
171 /// substitutes all instances ofr oldstr with newstr
172 string const subst(string const & a,
173                    string 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