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