]> git.lyx.org Git - features.git/blob - src/support/lstrings.h
The big KDE reorg + fi l10n update
[features.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(char const * a, char const * b);
130
131 ///
132 bool containsOnly(string const &, char const *);
133
134 ///
135 bool containsOnly(string const &, string const &);
136
137 ///
138 bool containsOnly(char const *, char const *);
139
140 ///
141 bool containsOnly(char const *, string const &);
142
143 /// Counts how many of character c there is in a
144 string::size_type countChar(string const & a, char c);
145
146 /** Extracts a token from this string at the nth delim.
147   Doesn't modify the original string. Similar to strtok.
148   Example:
149   #"a;bc;d".token(';', 1) == "bc";#
150   #"a;bc;d".token(';', 2) == "d";#
151 */
152 string const token(string const & a, char delim, int n);
153
154
155 /** Search a token in this string using the delim.
156   Doesn't modify the original string. Returns -1 in case of
157   failure. 
158   Example:
159   #"a;bc;d".tokenPos(';', "bc") == 1;#
160   #"a;bc;d".token(';', "d") == 2;#
161 */
162 int tokenPos(string const & a, char delim, string const & tok);
163
164
165 /** Compares a string and a (simple) regular expression
166   The only element allowed is "*" for any string of characters
167   */
168 bool regexMatch(string const & a, string const & pattern);
169
170 /// Substitute all "oldchar"s with "newchar"
171 string const subst(string const & a, char oldchar, char newchar);
172
173 /// Substitutes all instances of oldstr with newstr
174 string const subst(string const & a,
175              char const * oldstr, string const & newstr);
176
177 /// substitutes all instances ofr oldstr with newstr
178 string const subst(string const & a,
179                    string const & oldstr, string const & newstr);
180
181 /** Strips characters off the end of a string.
182   #"abccc".strip('c') = "ab".#
183   */
184 string const strip(string const & a, char c = ' ');
185
186 /** Strips characters of the beginning of a string.
187   #"cccba".frontstrip('c') = "ba"#. */
188 string const frontStrip(string const & a, char c = ' ');
189
190 /** Strips characters off the beginning of a string.
191     #"ababcdef".frontstrip("ab") = "cdef"# .*/
192 string const frontStrip(string const & a, char const * p);
193
194 /** Splits the string by the first delim.
195   Splits the string by the first appearance of delim.
196   The leading string up to delim is returned in piece (not including
197   delim), while the original string is cut from after the delimiter.
198   Example:
199   #s1= ""; s2= "a;bc".split(s1, ';') -> s1 == "a"; s2 == "bc";#
200   */
201 string const split(string const & a, string & piece, char delim);
202
203 /// Same as split but does not return a piece
204 string const split(string const & a, char delim);
205
206 /// Same as split but uses the last delim.
207 string const rsplit(string const & a, string & piece, char delim);
208
209 #endif