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