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