]> git.lyx.org Git - lyx.git/blob - src/support/lstrings.h
removed a warning from screen and added CFLAGS in lyx.spec.in.
[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
13 //#warning verify this please. Lgb
14 ///
15 template<class T>
16 size_t lstrlen(T const * t)
17 {
18         Assert(t); // we don't want null pointers
19         size_t count = 0;
20         T const * r = t;
21         while(*r != 0) ++r, ++count;
22         return count;
23 }
24
25
26 //#warning verify this please. Lgb
27 ///
28 template<class T>
29 T * lstrchr(T const * t, int c)
30 {
31   Assert(t); // we don't want null pointers
32   T * r = const_cast<T*>(t);
33   while(*r != 0) 
34     if (*r == c) return r; else ++r;
35   return 0;
36 }
37
38 #include <cctype>
39 #include "LString.h"
40
41 ///
42 inline int compare_no_case(string const & s, string const & s2)
43 {
44         // ANSI C
45         string::const_iterator p = s.begin();
46         string::const_iterator p2 = s2.begin();
47
48         while (p != s.end() && p2 != s2.end()) {
49                 if (tolower(*p) != tolower(*p2))
50                         return (tolower(*p) < tolower(*p2)) ? -1 : 1;
51                 ++p;
52                 ++p2;
53         }
54
55         return s.size() - s2.size();
56 }
57
58 ///
59 inline int compare_no_case(string const & s, string const & s2,
60                            unsigned int len)
61 {
62 //#warning verify this func please
63         string::const_iterator p = s.begin();
64         string::const_iterator p2 = s2.begin();
65         unsigned int i = 0;
66         while (i < len && p != s.end() && p2 != s2.end()) {
67                 if (tolower(*p) != tolower(*p2))
68                         return (tolower(*p) < tolower(*p2)) ? -1 : 1;
69                 ++i;
70                 ++p;
71                 ++p2;
72         }
73         return s.size() - s2.size();
74 }
75
76 ///
77 inline int compare(char const * a, char const * b)
78 {
79         return strcmp(a, b);
80 }
81
82 ///
83 inline int compare(char const * a, char const * b, unsigned int len)
84 {
85         return strncmp(a, b, len);
86 }
87
88 ///
89 bool isStrInt(string const & str);
90
91 ///
92 int strToInt(string const & str);
93
94 ///
95 string lowercase(string const &);
96
97 /// int to string
98 string tostr(int i);
99
100 ///
101 string tostr(unsigned int);
102
103 /// long to string
104 string tostr(long l);
105
106 ///
107 string tostr(unsigned long l); 
108
109 ///
110 string tostr(char c);
111
112 /// void* to string
113 string tostr(void* v);
114
115 /// bool to string
116 string tostr(bool b);
117
118 ///
119 string tostr(double d);
120
121 /// Does the string start with this prefix?
122 bool prefixIs(string const &, char const *);
123
124 /// Does the string end with this char?
125 bool suffixIs(string const &, char);
126
127 /// Does the string end with this suffix?
128 bool suffixIs(string const &, char const *);
129
130 ///
131 bool contains(char const * a, string const & b);
132
133 ///
134 bool contains(string const & a, char const * b);
135
136 ///
137 bool contains(string const & a, string const & b);
138
139 ///
140 bool contains(char const * a, char const * b);
141
142 /// Counts how many of character c there is in a
143 int countChar(string const & a, char const c);
144
145 /** Extracts a token from this string at the nth delim.
146   Doesn't modify the original string. Similar to strtok.
147   Example:
148   #"a;bc;d".token(';', 1) == "bc";#
149   #"a;bc;d".token(';', 2) == "d";#
150 */
151 string token(string const & a, char delim, int n);
152
153
154 /** Search a token in this string using the delim.
155   Doesn't modify the original string. Returns -1 in case of
156   failure. 
157   Example:
158   #"a;bc;d".tokenPos(';', "bc") == 1;#
159   #"a;bc;d".token(';', "d") == 2;#
160 */
161 int tokenPos(string const & a, char delim, string const & tok);
162
163
164 /** Compares a string and a (simple) regular expression
165   The only element allowed is "*" for any string of characters
166   */
167 bool regexMatch(string const & a, string const & pattern);
168
169 /// Substitute all "oldchar"s with "newchar"
170 string subst(string const & a, char oldchar, char newchar);
171
172 /// Substitutes all instances of oldstr with newstr
173 string subst(string const & a,
174               char const * oldstr, string const & newstr);
175
176 /** Strips characters off the end of a string.
177   #"abccc".strip('c') = "ab".#
178   */
179 string strip(string const & a, char const c = ' ');
180
181 /** Strips characters of the beginning of a string.
182   #"cccba".frontstrip('c') = "ba"#. */
183 string frontStrip(string const & a, char const c = ' ');
184
185 /** Strips characters off the beginning of a string.
186     #"ababcdef".frontstrip("ab") = "cdef"# .*/
187 string frontStrip(string const & a, char const * p);
188
189 /** Splits the string by the first delim.
190   Splits the string by the first appearance of delim.
191   The leading string up to delim is returned in piece (not including
192   delim), while the original string is cut from after the delimiter.
193   Example:
194   #s1=""; s2="a;bc".split(s1, ';') -> s1=="a"; s2 == "bc";#
195   */
196 string split(string const & a, string & piece, char delim);
197
198 /// Same as split but does not return a piece
199 string split(string const & a, char delim);
200
201 /// Same as split but uses the last delim.
202 string rsplit(string const & a, string & piece, char delim);
203
204 #endif