summaryrefslogtreecommitdiff
path: root/plugins/SmileyAdd/bkstring.h
blob: eb164f74e21142c2dd426e0f21f299de3f2d0bd1 (plain)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
Miranda SmileyAdd Plugin
Copyright (C) 2008 - 2011 Boris Krasnovskiy All Rights Reserved

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 version 2
of the License.

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, see <http://www.gnu.org/licenses/>.
*/

#pragma once 

#include <tchar.h>
#include <string.h>

#ifndef min
#define min(A, B) ((A) < (B) ? (A) : (B)) 
#endif

class bkstring
{
public:
	typedef size_t size_type;
	typedef TCHAR  value_type;
	typedef value_type* iterator;
	typedef const value_type* const_iterator;

#if defined(_MSC_VER) && (_MSC_VER <= 1200)
	enum { npos = -1 };
#else
	static const size_type npos = size_type(-1);
#endif

private:
	value_type* buf;
	size_type sizeAlloced;
	size_type lenBuf;

	void areserve(size_type len) { reserve(lenBuf + len); }

public:

	explicit bkstring() : buf((TCHAR*)_T("")), sizeAlloced(0), lenBuf(0) 
	{}

	bkstring(const value_type* _Ptr, size_type _Count) : sizeAlloced(0), lenBuf(0)
	{ assign(_Ptr, _Count); }

	bkstring(const value_type* _Ptr) : sizeAlloced(0), lenBuf(0)
	{ assign(_Ptr); }

	bkstring(size_type _Count, value_type _Ch) : sizeAlloced(0), lenBuf(0)
	{ assign(_Count, _Ch); }

	bkstring(const bkstring& _Str) : sizeAlloced(0), lenBuf(0)
	{ assign(_Str); }

	bkstring(const bkstring& _Str, size_type _Off, size_type _Count) : sizeAlloced(0), lenBuf(0)
	{ assign(_Str, _Off, _Count); }

	~bkstring();

	size_type size(void) const { return lenBuf; }
	const value_type* c_str(void) const { return buf; }

	void clear(void) { if (lenBuf) { lenBuf = 0; buf[0] = 0; } }
	void insert(const value_type *txt);
	void reserve(size_type len);

	bkstring& assign(const value_type* _Ptr)
	{ return assign(_Ptr, _tcslen(_Ptr)); }

	bkstring& assign(const bkstring& _Str)
	{ return assign(_Str, 0, (size_type)npos); }

	bkstring& assign(const value_type* _Ptr, size_type _Count);
	bkstring& assign(const bkstring& _Str, size_type off, size_type _Count);
	bkstring& assign(size_type _Count, value_type _Ch);

	bkstring& append(const value_type* _Ptr);
	bkstring& append(const value_type* _Ptr, size_type _Count);
	bkstring& append(const bkstring& _Str, size_type _Off, size_type _Count);
	bkstring& append(const bkstring& _Str);
	bkstring& append(size_type _Count, value_type _Ch);

	int compare(const bkstring& _Str) const 
	{ return _tcscmp(buf, _Str.c_str()); }
	
	int compare(size_type _Pos1, size_type _Num1, const bkstring& _Str) const 
	{ return _tcsncmp(&buf[_Pos1], _Str.c_str(), _Num1); }
	
	int compare(size_type _Pos1, size_type _Num1, const bkstring& _Str, size_type _Off, size_type _Count) const
	{ return _tcsncmp(&buf[_Pos1], _Str.c_str()+_Off, min(_Num1, _Count)); }

	int compare(const value_type* _Ptr) const 
	{ return _tcscmp(buf, _Ptr); }
	
	int compare(size_type _Pos1, size_type _Num1, const value_type* _Ptr) const
	{ return _tcsncmp(&buf[_Pos1], _Ptr, _Num1); }
	
	int compare(size_type _Pos1, size_type _Num1, const value_type* _Ptr, size_type _Num2) const
	{ return _tcsncmp(&buf[_Pos1], _Ptr, min(_Num1, _Num2)); }

	int comparei(const bkstring& _Str) const 
	{ return _tcsicmp(buf, _Str.c_str()); }
	
	int comparei(size_type _Pos1, size_type _Num1, const bkstring& _Str) const 
	{ return _tcsnicmp(&buf[_Pos1], _Str.c_str(), _Num1); }
	
	int comparei(size_type _Pos1, size_type _Num1, const bkstring& _Str, size_type _Off, size_type _Count) const
	{ return _tcsnicmp(&buf[_Pos1], _Str.c_str()+_Off, min(_Num1, _Count)); }

	int comparei(const value_type* _Ptr) const 
	{ return _tcsicmp(buf, _Ptr); }
	
	int comparei(size_type _Pos1, size_type _Num1, const value_type* _Ptr) const
	{ return _tcsnicmp(&buf[_Pos1], _Ptr, _Num1); }
	
	int comparei(size_type _Pos1, size_type _Num1, const value_type* _Ptr, size_type _Num2) const
	{ return _tcsnicmp(&buf[_Pos1], _Ptr, min(_Num1, _Num2)); }

	bool empty(void) const { return lenBuf == 0; };
	bkstring& erase(size_type _Pos = 0, size_type _Count = npos);

	size_type find(value_type _Ch, size_type _Off = 0) const;
	size_type find(const value_type* _Ptr, size_type _Off = 0) const;
	size_type find(bkstring& _Str, size_type _Off = 0) const
	{ return find(_Str.c_str(), _Off); }

	size_type find_last_of(value_type _Ch, size_type _Off = npos) const;

	bkstring& insert(size_type _P0, const value_type* _Ptr)
	{ return insert(_P0, _Ptr, _tcslen(_Ptr)); }
	
	bkstring& insert(size_type _P0, const bkstring& _Str)
	{ return insert(_P0, _Str.c_str(), _Str.size()); };

	bkstring& insert(size_type _P0, const value_type* _Ptr, size_type _Count);
	bkstring& insert(size_type _P0, size_type _Count, value_type _Ch);

	bkstring substr(size_type _Off = 0, size_type _Count = npos) const
	{ return bkstring(*this, _Off, _Count); }

	bkstring& operator = (const bkstring& _Str)
	{ return assign(_Str); }

	bkstring& operator =(const value_type* _Ptr)
	{ return assign(_Ptr); }

	bkstring& operator = (const value_type _Ch)
	{ return assign(1, _Ch); }

	bkstring& operator +=(const bkstring& _Str)
	{ return append(_Str); }

	bkstring& operator += (const value_type* _Ptr)
	{ return append(_Ptr); }

	bkstring& operator += (const value_type _Ch)
	{ return append(1, _Ch); }

	value_type& operator[] (int ind) const
	{ return buf[ind]; }

	friend bkstring operator+ (const bkstring& _Str1, const bkstring& _Str2)
	{ bkstring s(_Str1); return s.append(_Str2); }

	friend bkstring operator+ (const bkstring& _Str1, const value_type* _Ptr2)
	{ bkstring s(_Str1); return s.append(_Ptr2); }

	friend bkstring operator+(const value_type* _Ptr1, const bkstring& _Str2)
	{ bkstring s(_Ptr1); return s.append(_Str2); }

	friend bkstring operator+ (const bkstring& _Str1, const value_type _Ch)
	{ bkstring s(_Str1); return s.append(1, _Ch); }

	friend bool operator==(const bkstring& _Str1, const bkstring& _Str2)
	{ return _Str1.compare(_Str2) == 0; }

	friend bool operator==(const bkstring& _Str1, const value_type* _Ptr2)
	{ return _Str1.compare(_Ptr2) == 0; }

	friend bool operator==(const value_type* _Ptr1, const bkstring& _Str2)
	{ return _Str2.compare(_Ptr1) == 0; }

	friend bool operator!=(const bkstring& _Str1, const bkstring& _Str2)
	{ return _Str1.compare(_Str2) != 0; }

	friend bool operator!=(const bkstring& _Str1, const value_type* _Ptr2)
	{ return _Str1.compare(_Ptr2) != 0; }

	friend bool operator!=(const value_type* _Ptr1, const bkstring& _Str2)
	{ return _Str2.compare(_Ptr1) != 0; }

	friend bool operator<(const bkstring& _Str1, const bkstring& _Str2)
	{ return _Str1.compare(_Str2) < 0; }

	friend bool operator<(const bkstring& _Str1, const value_type* _Ptr2)
	{ return _Str1.compare(_Ptr2) < 0; }

	friend bool operator<(const value_type* _Ptr1, const bkstring& _Str2)
	{ return _Str2.compare(_Ptr1) > 0; }

	friend bool operator>(const bkstring& _Str1, const bkstring& _Str2)
	{ return _Str1.compare(_Str2) > 0; }

	friend bool operator>(const bkstring& _Str1, const value_type* _Ptr2)
	{ return _Str1.compare(_Ptr2) > 0; }

	friend bool operator>(const value_type* _Ptr1, const bkstring& _Str2)
	{ return _Str2.compare(_Ptr1) < 0; }

	friend bool operator<=(const bkstring& _Str1, const bkstring& _Str2)
	{ return _Str1.compare(_Str2) <= 0; }

	friend bool operator<=(const bkstring& _Str1, const value_type* _Ptr2)
	{  return _Str1.compare(_Ptr2) <= 0; }

	friend bool  operator<=(const value_type* _Ptr1, const bkstring& _Str2)
	{ return _Str2.compare(_Ptr1) >= 0; }

	friend bool  operator>=(const bkstring& _Str1, const bkstring& _Str2)
	{ return _Str1.compare(_Str2) >= 0; }

	friend bool  operator>=(const bkstring& _Str1, const value_type* _Ptr2)
	{ return _Str1.compare(_Ptr2) >= 0; }

	friend bool  operator>=(const value_type* _Ptr1, const bkstring& _Str2)
	{ return _Str2.compare(_Ptr1) <= 0; }

	friend bool  operator==(const value_type _Ch1, const bkstring& _Str2)
	{  return (_Str2.size() == 1) && (_Str2[0] == _Ch1); }

	friend bool  operator==(const bkstring& _Str1, const value_type _Ch2)
	{ return (_Str1.size() == 1) && (_Str1[0] == _Ch2); }

	friend bool  operator!=(const value_type _Ch1, const bkstring& _Str2)
	{  return (_Str2.size() != 1) || (_Str2[0] != _Ch1); }

	friend bool  operator!=(const bkstring& _Str1, const value_type _Ch2)
	{ return (_Str1.size() != 1) || (_Str1[0] != _Ch2); }

	iterator begin(void)
	{ return buf; }
	
	const_iterator begin(void) const
	{ return buf; }
	
	iterator end(void)
	{ return buf + lenBuf; }

	const_iterator end(void) const
	{ return buf + lenBuf; }
	  
	// Custom extentions

	void appendfmt(const value_type *fmt, ...);

	size_type sizebytes(void) const { return lenBuf * sizeof(value_type); }
};

//const bkstring::size_type bkstring::npos = -1;