Kiln » TortoiseHg » TortoiseHg
Clone URL:  
StringUtils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// TortoiseCVS - a Windows shell extension for easy version control // Copyright (C) 2002 - Francis Irving // <francis@flourish.org> - May 2002 // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #ifndef _STRING_UTILS_H #define _STRING_UTILS_H #include <string> //#include <wx/string.h> #include <vector> #include <map> #include <windows.h> //#include "FixWinDefs.h" #ifdef _MSC_VER // Fancier GUI Visual C++ asserts #include <crtdbg.h> #else // Standard C assert #include <assert.h> #ifndef _ASSERT #define _ASSERT assert #endif // This seems needed by Borland and GCC. // Who else? What standard is it? #include <errno.h> #ifndef ENOMEM #define ENOMEM 12 #endif #endif #ifndef ASSERT #define ASSERT _ASSERT #endif // From: http://www.kbcafe.com/articles/cplusplus.tricks.html template<class T> void FindAndReplace(T& source, const T& find, const T& replace) { size_t j; for (j = 0; (j = source.find(find, j)) != T::npos;) { source.replace(j, find.length(), replace); j += replace.length(); } } // Return the length of the longest string in the vector. int MaxStringLength(const std::vector<std::string>& stringvec); void MakeLowerCase(std::string& s); void MakeUpperCase(std::string& s); // Remove leading whitespaces from a string std::string TrimLeft(const std::string& str); // Remove trailing whitespaces from a string std::string TrimRight(const std::string& str); // Remove leading and trailing whitespaces from a string std::string Trim(const std::string& str); // Test if string starts with substr bool StartsWith(const std::string& str, const std::string& substr); // Quotes a string std::string Quote(const std::string& str); // Cuts the first token off a delimited list std::string CutFirstToken(std::string& sList, const std::string sDelimiter); // Printf returning a std::string //wxString Printf(const wxChar* format, ...); std::string PrintfA(const char* format, ...); // Convert Unicode string to multibyte string std::string WideToMultibyte(const std::wstring& wide, UINT CodePage = CP_ACP); // Convert multibyte string to Unicode string std::wstring MultibyteToWide(const std::string& multibyte, UINT CodePage = CP_ACP); #if wxUSE_UNICODE #define wxText(xxx) MultibyteToWide(xxx) #define wxTextCStr(xxx) MultibyteToWide(xxx).c_str() #define wxAscii(xxx) WideToMultibyte(xxx) #else #define wxText(xxx) xxx #define wxTextCStr(xxx) (xxx).c_str() #define wxAscii(xxx) xxx #endif // Serialize a vector of strings std::string SerializeStringVector(const std::vector<std::string>& vStrings, const std::string& sDelimiter); // Expand environment strings std::string ExpandEnvStrings(const std::string& str); #if wxUSE_UNICODE wxString ExpandEnvStrings(const wxString& str); #endif // Replace parameters (%something) std::string ReplaceParams(const std::string& str, const std::map<std::string, std::string> params); // comparison function object class less_nocase { public: bool operator()(const std::string& x, const std::string& y) const { std::string::const_iterator p = x.begin(); std::string::const_iterator q = y.begin(); while (p != x.end() && q != y.end() && toupper(*p) == toupper(*q)) ++p, ++q; if (p == x.end()) // Reached end of x: Return true if y is longer than x return q != y.end(); if (q == y.end()) // Reached end of y, but not x, so x is longer than y return false; return toupper(*p) < toupper(*q); } }; #endif