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