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