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