4 Website: http://www.vultaire.net/software/jben/
5 License: GNU General Public License (GPL) version 2
6 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>
24 #ifndef string_utils_h
25 #define string_utils_h
31 bool GetIndexAfterParens(const string
& s
, size_t indexParen
, size_t& indexNext
,
32 char cOpenParen
='(', char cCloseParen
=')');
33 bool GetIndexBeforeParens(const string
& s
, size_t indexParen
, size_t& indexNext
,
34 char cOpenParen
='(', char cCloseParen
=')');
35 string
ToUpper(string src
);
36 wstring
ToUpper(wstring src
);
37 string
ToLower(string src
);
38 wstring
ToLower(wstring src
);
40 wstring
Trim(wstring
);
42 /* Template functions */
44 list
< basic_string
<t
> > StrTokenize
45 (const basic_string
<t
>& src
,
47 bool emptyTokens
= false,
49 list
< basic_string
<t
> > l
;
51 size_t end
= src
.find_first_of(delimiters
, start
);
53 while(end
!=basic_string
<t
>::npos
54 && (maxTokens
==0 || l
.size()<maxTokens
-1)) {
55 if(emptyTokens
|| (start
!=end
)) {
57 src
.substr(start
, end
-start
));
60 end
= src
.find(delimiters
, start
);
63 if(start
!=basic_string
<t
>::npos
) {
64 basic_string
<t
> lastStr
= src
.substr(start
);
65 if(lastStr
.length()>0) l
.push_back(lastStr
);
70 /* Substring replace function.
71 "from" string MUST have length > 0. */
73 basic_string
<t
> TextReplace(const basic_string
<t
>& src
,
74 const basic_string
<t
>& from
,
75 const basic_string
<t
>& to
) {
76 basic_string
<t
> result
;
79 if(from
.size()<1) return src
;
81 end
= src
.find(from
, start
);
82 while(end
!=basic_string
<t
>::npos
) {
83 /* Append string particle, if present */
85 result
.append(src
.substr(start
, end
-start
));
87 /* Append "to" string */
89 /* Skip over the "from" string */
90 start
= end
+ from
.length();
92 end
= src
.find(from
, start
);
95 if(start
!=basic_string
<t
>::npos
) {
96 basic_string
<t
> lastStr
= src
.substr(start
);
97 if(lastStr
.length()>0) result
.append(lastStr
);
103 /* t must be a class based upon std::basic_string, like string or wstring. */
105 t
ListToString(const list
<t
>& l
, const t
& separator
) {
108 /* Append the first entry */
109 typename list
<t
>::const_iterator li
= l
.begin();
110 if(li
!=l
.end()) target
.append(*li
);
112 /* Append remaining entries */
113 for(li
++; li
!=l
.end(); li
++) {
114 target
.append(separator
);