This repository has been archived by the owner on Oct 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
stringtool.h
219 lines (190 loc) · 6.88 KB
/
stringtool.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// stringtool.h
#ifndef _STRINGTOOL_H
# define _STRINGTOOL_H
# include "misc.h"
//# include <tchar.h>
# include "_tchar.h"
# include <cctype>
# include <string>
# include <iosfwd>
# include <boost/regex.hpp>
# include <stdio.h> // for snprintf
# include <string.h> // for stricmp
#ifdef __CYGWIN__
namespace std
{
typedef basic_string<wchar_t> wstring; // !!
}
#endif
#if defined(__linux__) || defined(__APPLE__)
#define _tgetenv(env) getenv(env)
#endif
/// string for generic international text
typedef std::basic_string<_TCHAR> tstring;
/// istream for generic international text
typedef std::basic_istream<_TCHAR> tistream;
/// ostream for generic international text
typedef std::basic_ostream<_TCHAR> tostream;
/// streambuf for for generic international text
typedef std::basic_streambuf<_TCHAR> tstreambuf;
/// stringstream for generic international text
typedef std::basic_stringstream<_TCHAR> tstringstream;
/// ifstream for generic international text
typedef std::basic_ifstream<_TCHAR> tifstream;
/// basic_regex for generic international text
typedef boost::basic_regex<_TCHAR> tregex;
/// match_results for generic international text
typedef boost::match_results<tstring::const_iterator> tcmatch;
/// string with custom stream output
class tstringq : public tstring
{
public:
///
tstringq() { }
///
tstringq(const tstringq &i_str) : tstring(i_str) { }
///
tstringq(const tstring &i_str) : tstring(i_str) { }
///
tstringq(const _TCHAR *i_str) : tstring(i_str) { }
///
tstringq(const _TCHAR *i_str, size_t i_n) : tstring(i_str, i_n) { }
///
tstringq(const _TCHAR *i_str, size_t i_pos, size_t i_n)
: tstring(i_str, i_pos, i_n) { }
///
tstringq(size_t i_n, _TCHAR i_c) : tstring(i_n, i_c) { }
};
/// stream output
extern tostream &operator<<(tostream &i_ost, const tstringq &i_data);
/// identical to tcmatch except for str()
class tcmatch_results : public tcmatch
{
public:
/** returns match result as tstring.
match_results<const _TCHAR *>::operator[]() returns a instance of
sub_mtch<const _TCHAR *>. So, we convert sub_mtch<const _TCHAR *> to
tstring. */
tstring str(tcmatch::size_type i_n) const
{
return static_cast<tstring>(
static_cast<const tcmatch *>(this)->operator[](i_n));
}
};
/// interpret meta characters such as \n
tstring interpretMetaCharacters(const _TCHAR *i_str, size_t i_len,
const _TCHAR *i_quote = NULL,
bool i_doesUseRegexpBackReference = false);
/// add session id to i_str
tstring addSessionId(const _TCHAR *i_str);
/// copy
size_t strlcpy(char *o_dest, const char *i_src, size_t i_destSize);
/// copy
size_t mbslcpy(unsigned char *o_dest, const unsigned char *i_src,
size_t i_destSize);
/// copy
size_t wcslcpy(wchar_t *o_dest, const wchar_t *i_src, size_t i_destSize);
/// copy
inline size_t tcslcpy(char *o_dest, const char *i_src, size_t i_destSize)
{ return strlcpy(o_dest, i_src, i_destSize); }
/// copy
inline size_t tcslcpy(unsigned char *o_dest, const unsigned char *i_src,
size_t i_destSize)
{ return mbslcpy(o_dest, i_src, i_destSize); }
/// copy
inline size_t tcslcpy(wchar_t *o_dest, const wchar_t *i_src, size_t i_destSize)
{ return wcslcpy(o_dest, i_src, i_destSize); }
// escape regexp special characters in MBCS trail bytes
std::string guardRegexpFromMbcs(const char *i_str);
/// converter
std::wstring to_wstring(const std::string &i_str);
/// converter
std::string to_string(const std::wstring &i_str);
// convert wstring to UTF-8
std::string to_UTF_8(const std::wstring &i_str);
/// case insensitive string
class tstringi : public tstring
{
public:
///
tstringi() { }
///
tstringi(const tstringi &i_str) : tstring(i_str) { }
///
tstringi(const tstring &i_str) : tstring(i_str) { }
///
tstringi(const _TCHAR *i_str) : tstring(i_str) { }
///
tstringi(const _TCHAR *i_str, size_t i_n) : tstring(i_str, i_n) { }
///
tstringi(const _TCHAR *i_str, size_t i_pos, size_t i_n)
: tstring(i_str, i_pos, i_n) { }
///
tstringi(size_t i_n, _TCHAR i_c) : tstring(i_n, i_c) { }
///
int compare(const tstringi &i_str) const
{ return compare(i_str.c_str()); }
///
int compare(const tstring &i_str) const
{ return compare(i_str.c_str()); }
///
int compare(const _TCHAR *i_str) const
{ return _tcsicmp(c_str(), i_str); }
///
tstring &getString() { return *this; }
///
const tstring &getString() const { return *this; }
};
/// case insensitive string comparison
inline bool operator<(const tstringi &i_str1, const _TCHAR *i_str2)
{ return i_str1.compare(i_str2) < 0; }
/// case insensitive string comparison
inline bool operator<(const _TCHAR *i_str1, const tstringi &i_str2)
{ return 0 < i_str2.compare(i_str1); }
/// case insensitive string comparison
inline bool operator<(const tstringi &i_str1, const tstring &i_str2)
{ return i_str1.compare(i_str2) < 0; }
/// case insensitive string comparison
inline bool operator<(const tstring &i_str1, const tstringi &i_str2)
{ return 0 < i_str2.compare(i_str1); }
/// case insensitive string comparison
inline bool operator<(const tstringi &i_str1, const tstringi &i_str2)
{ return i_str1.compare(i_str2) < 0; }
/// case insensitive string comparison
inline bool operator==(const _TCHAR *i_str1, const tstringi &i_str2)
{ return i_str2.compare(i_str1) == 0; }
/// case insensitive string comparison
inline bool operator==(const tstringi &i_str1, const _TCHAR *i_str2)
{ return i_str1.compare(i_str2) == 0; }
/// case insensitive string comparison
inline bool operator==(const tstring &i_str1, const tstringi &i_str2)
{ return i_str2.compare(i_str1) == 0; }
/// case insensitive string comparison
inline bool operator==(const tstringi &i_str1, const tstring &i_str2)
{ return i_str1.compare(i_str2) == 0; }
/// case insensitive string comparison
inline bool operator==(const tstringi &i_str1, const tstringi &i_str2)
{ return i_str1.compare(i_str2) == 0; }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// workarounds for Borland C++
/// case insensitive string comparison
inline bool operator!=(const _TCHAR *i_str1, const tstringi &i_str2)
{ return i_str2.compare(i_str1) != 0; }
/// case insensitive string comparison
inline bool operator!=(const tstringi &i_str1, const _TCHAR *i_str2)
{ return i_str1.compare(i_str2) != 0; }
/// case insensitive string comparison
inline bool operator!=(const tstring &i_str1, const tstringi &i_str2)
{ return i_str2.compare(i_str1) != 0; }
/// case insensitive string comparison
inline bool operator!=(const tstringi &i_str1, const tstring &i_str2)
{ return i_str1.compare(i_str2) != 0; }
/// case insensitive string comparison
inline bool operator!=(const tstringi &i_str1, const tstringi &i_str2)
{ return i_str1.compare(i_str2) != 0; }
/// stream output
extern tostream &operator<<(tostream &i_ost, const tregex &i_data);
/// get lower string
extern tstring toLower(const tstring &i_str);
#endif // !_STRINGTOOL_H