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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
/*
*
* Copyright (c) 1998-9
* Dr John Maddock
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Dr John Maddock makes no representations
* about the suitability of this software for any purpose.
* It is provided "as is" without express or implied warranty.
*
*/
/*
* FILE re_str.h
* VERSION 2.12
* This is an internal header file, do not include directly.
* String support and helper functions, for regular
* expression library.
*/
#ifndef RE_STR_H
#define RE_STR_H
#ifndef JM_CFG_H
#include <jm/jm_cfg.h>
#endif
#include <string.h>
JM_NAMESPACE(__JM)
//
// start by defining some template function aliases for C API functions:
//
template <class charT>
size_t RE_CALL re_strlen(const charT *s)
{
size_t len = 0;
while(*s)
{
++s;
++len;
}
return len;
}
template <class charT>
int RE_CALL re_strcmp(const charT *s1, const charT *s2)
{
while(*s1 && *s2)
{
if(*s1 != *s2)
return *s1 - *s2;
++s1;
++s2;
}
return *s1 - *s2;
}
template <class charT>
charT* RE_CALL re_strcpy(charT *s1, const charT *s2)
{
charT* base = s1;
while(*s2)
{
*s1 = *s2;
++s1;
++s2;
}
*s1 = *s2;
return base;
}
template <class charT>
unsigned int RE_CALL re_strwiden(charT *s1, unsigned int len, const char *s2)
{
unsigned int result = 1 + re_strlen(s2);
if(result > len)
return result;
while(*s2)
{
*s1 = (unsigned char)*s2;
++s2;
++s1;
}
*s1 = (unsigned char)*s2;
return result;
}
template <class charT>
unsigned int RE_CALL re_strnarrow(char *s1, unsigned int len, const charT *s2)
{
unsigned int result = 1 + re_strlen(s2);
if(result > len)
return result;
while(*s2)
{
*s1 = (char)(unsigned char)*s2;
++s2;
++s1;
}
*s1 = (char)(unsigned char)*s2;
return result;
}
inline size_t RE_CALL re_strlen(const char *s)
{
return strlen(s);
}
inline int RE_CALL re_strcmp(const char *s1, const char *s2)
{
return strcmp(s1, s2);
}
inline char* RE_CALL re_strcpy(char *s1, const char *s2)
{
return strcpy(s1, s2);
}
#ifndef JM_NO_WCSTRING
inline size_t RE_CALL re_strlen(const wchar_t *s)
{
return wcslen(s);
}
inline int RE_CALL re_strcmp(const wchar_t *s1, const wchar_t *s2)
{
return wcscmp(s1, s2);
}
inline wchar_t* RE_CALL re_strcpy(wchar_t *s1, const wchar_t *s2)
{
return wcscpy(s1, s2);
}
#endif
#if !defined(JM_NO_WCSTRING) || defined(JM_PLATFORM_W32)
JM_IX_DECL unsigned int RE_CALL _re_strnarrow(char *s1, unsigned int len, const wchar_t *s2);
JM_IX_DECL unsigned int RE_CALL _re_strwiden(wchar_t *s1, unsigned int len, const char *s2);
inline unsigned int RE_CALL re_strnarrow(char *s1, unsigned int len, const wchar_t *s2)
{
return _re_strnarrow(s1, len, s2);
}
inline unsigned int RE_CALL re_strwiden(wchar_t *s1, unsigned int len, const char *s2)
{
return _re_strwiden(s1, len, s2);
}
#endif
template <class charT>
charT* RE_CALL re_strdup(const charT* p)
{
charT* buf = new charT[re_strlen(p) + 1];
re_strcpy(buf, p);
return buf;
}
template <class charT>
charT* RE_CALL re_strdup(const charT* p1, const charT* p2)
{
unsigned int len = p2 - p1 + 1;
charT* buf = new charT[len];
memcpy(buf, p1, (len - 1) * sizeof(charT));
*(buf + len - 1) = 0;
return buf;
}
template <class charT>
inline void RE_CALL re_strfree(charT* p)
{
delete[] p;
}
template <class charT>
class re_str
{
charT* buf;
public:
re_str()
{
charT c = 0;
buf = re_strdup(&c);
}
~re_str();
re_str(const re_str& other);
re_str(const charT* p1);
re_str(const charT* p1, const charT* p2);
re_str(charT c);
re_str& RE_CALL operator=(const re_str& other)
{
re_strfree(buf);
buf = re_strdup(other.buf);
return *this;
}
re_str& RE_CALL operator=(const charT* p)
{
re_strfree(buf);
buf = re_strdup(p);
return *this;
}
re_str& RE_CALL operator=(charT c)
{
re_strfree(buf);
buf = re_strdup(&c, &c+1);
return *this;
}
const charT* RE_CALL c_str()const { return buf; }
RE_CALL operator const charT*()const { return buf; }
unsigned int RE_CALL size()const { return re_strlen(buf); }
charT& RE_CALL operator[](unsigned int i) { return buf[i]; }
charT RE_CALL operator[](unsigned int i)const { return buf[i]; }
bool RE_CALL operator==(const re_str& other)const { return re_strcmp(buf, other.buf) == 0; }
bool RE_CALL operator==(const charT* p)const { return re_strcmp(buf, p) == 0; }
bool RE_CALL operator==(const charT c)const
{
if((*buf) && (*buf == c) && (*(buf+1) == 0))
return true;
return false;
}
bool RE_CALL operator!=(const re_str& other)const { return re_strcmp(buf, other.buf) != 0; }
bool RE_CALL operator!=(const charT* p)const { return re_strcmp(buf, p) != 0; }
bool RE_CALL operator!=(const charT c)const { return !(*this == c); }
bool RE_CALL operator<(const re_str& other)const { return re_strcmp(buf, other.buf) < 0; }
bool RE_CALL operator<=(const re_str& other)const { return re_strcmp(buf, other.buf) <= 0; }
bool RE_CALL operator>(const re_str& other)const { return re_strcmp(buf, other.buf) > 0; }
bool RE_CALL operator>=(const re_str& other)const { return re_strcmp(buf, other.buf) >= 0; }
bool RE_CALL operator<(const charT* p)const { return re_strcmp(buf, p) < 0; }
bool RE_CALL operator<=(const charT* p)const { return re_strcmp(buf, p) <= 0; }
bool RE_CALL operator>(const charT* p)const { return re_strcmp(buf, p) > 0; }
bool RE_CALL operator>=(const charT* p)const { return re_strcmp(buf, p) >= 0; }
};
template <class charT>
CONSTRUCTOR_INLINE re_str<charT>::~re_str() { re_strfree(buf); }
template <class charT>
CONSTRUCTOR_INLINE re_str<charT>::re_str(const re_str<charT>& other) { buf = re_strdup(other.buf); }
template <class charT>
CONSTRUCTOR_INLINE re_str<charT>::re_str(const charT* p1) { buf = re_strdup(p1); }
template <class charT>
CONSTRUCTOR_INLINE re_str<charT>::re_str(const charT* p1, const charT* p2) { buf = re_strdup(p1, p2); }
template <class charT>
CONSTRUCTOR_INLINE re_str<charT>::re_str(charT c) { buf = re_strdup(&c, &c+1); }
#ifndef JM_NO_WCSTRING
JM_IX_DECL void RE_CALL re_transform(re_str<wchar_t>& out, const re_str<wchar_t>& in);
#endif
JM_IX_DECL void RE_CALL re_transform(re_str<char>& out, const re_str<char>& in);
template <class charT>
void RE_CALL re_trunc_primary(re_str<charT>& s)
{
for(unsigned int i = 0; i < s.size(); ++i)
{
if(s[i] <= 1)
{
s[i] = 0;
break;
}
}
}
#ifdef RE_LOCALE_C
#define TRANSFORM_ERROR (size_t)-1
#else
#define TRANSFORM_ERROR 0
#endif
JM_END_NAMESPACE
#endif
|