-
Notifications
You must be signed in to change notification settings - Fork 1
/
str.c
332 lines (276 loc) · 8.04 KB
/
str.c
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_str.h"
#include "ext/standard/php_string.h"
/* True global resources - no need for thread safety here */
static int le_str;
/* {{{ str_functions[]
*
* Every user visible function must have an entry in str_functions[].
*/
const zend_function_entry str_functions[] = {
PHP_FE(str_startswith, NULL)
PHP_FE(str_endswith, NULL)
PHP_FE(str_isupper, NULL)
PHP_FE(str_islower, NULL)
PHP_FE(str_iswhitespace, NULL)
PHP_FE(str_swapcase, NULL)
PHP_FE(str_contains, NULL)
{NULL, NULL, NULL}
};
/* }}} */
/* {{{ str_module_entry
*/
zend_module_entry str_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"str",
str_functions,
PHP_MINIT(str),
PHP_MSHUTDOWN(str),
NULL,
NULL,
PHP_MINFO(str),
#if ZEND_MODULE_API_NO >= 20010901
"0.1",
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_STR
ZEND_GET_MODULE(str)
#endif
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(str)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(str)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(str)
{
php_info_print_table_start();
php_info_print_table_header(2, "str support", "enabled");
php_info_print_table_end();
}
/* }}} */
/* {{{ proto bool str_startswith(string haystack, string needle [, bool case_sensitivity])
Binary safe optionally case sensitive check if haystack starts with needle */
PHP_FUNCTION(str_startswith)
{
char *needle, *haystack;
int needle_len, haystack_len, retval;
zend_bool cs = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|b", &haystack, &haystack_len, &needle, &needle_len, &cs) == FAILURE) {
RETURN_FALSE;
}
if (needle_len > haystack_len) {
RETURN_FALSE;
}
if (cs) {
retval = zend_binary_strncmp(needle, needle_len, haystack, haystack_len, needle_len);
} else {
retval = zend_binary_strncasecmp(needle, needle_len, haystack, haystack_len, needle_len);
}
RETURN_BOOL(retval == 0);
}
/* }}} */
/* {{{ proto bool str_endswith(string haystack, string needle [, bool case_sensitivity])
Binary safe optionally case sensitive check if haystack ends with needle */
PHP_FUNCTION(str_endswith)
{
char *needle, *haystack;
int needle_len, haystack_len, retval;
zend_bool cs = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|b", &haystack, &haystack_len, &needle, &needle_len, &cs) == FAILURE) {
RETURN_FALSE;
}
if (needle_len > haystack_len) {
RETURN_FALSE;
}
if (cs) {
retval = zend_binary_strncmp(needle, needle_len, haystack + (haystack_len - needle_len), needle_len, needle_len);
} else {
retval = zend_binary_strncasecmp(needle, needle_len, haystack + (haystack_len - needle_len), needle_len, needle_len);
}
RETURN_BOOL(retval == 0);
}
/* }}} */
/* {{{ proto bool str_contains(string haystack, string needle [, bool case_sensitivity])
Binary safe optionally case sensitive check if haystack contains needle */
PHP_FUNCTION(str_contains)
{
char *needle, *haystack, *haystack_dup, *needle_dup;
char *found = NULL;
int needle_len, haystack_len;
zend_bool cs = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|b", &haystack, &haystack_len, &needle, &needle_len, &cs) == FAILURE) {
RETURN_FALSE;
}
if (needle_len > haystack_len || !haystack_len) {
RETURN_FALSE;
}
if (!needle_len) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty delimiter");
RETURN_FALSE;
}
/* TODO: mimick more of the logic from strpos, i.e. make needle
* a zval.
*/
if (cs) {
found = php_memnstr(haystack, needle, needle_len, haystack + haystack_len);
} else {
haystack_dup = estrndup(haystack, haystack_len);
php_strtolower(haystack_dup, haystack_len);
needle_dup = estrndup(needle, needle_len);
php_strtolower(needle_dup, needle_len);
found = php_memnstr(haystack_dup, needle_dup, needle_len, haystack_dup + haystack_len);
efree(haystack_dup);
efree(needle_dup);
}
RETURN_BOOL(found);
}
/* }}} */
/* {{{ proto bool str_isupper(string input)
Checks if all cased characters in the given input string are uppercase and if the
string contains at least one cased character. */
PHP_FUNCTION(str_isupper)
{
unsigned char *input, *end;
unsigned char c;
int input_len;
int has_cased_char = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &input, &input_len) == FAILURE) {
RETURN_FALSE;
}
/* A string must at least contain one cased character in order to
* yield a positive result.
*/
if (input_len == 0) {
RETURN_FALSE;
}
/* Shortcut for strings that consist of just a single character. */
if (input_len == 1) {
RETURN_BOOL(isupper(*input) != 0)
}
for (end = input + input_len; input < end; input++) {
c = *input;
if (islower(c)) {
RETURN_FALSE;
}
if (!has_cased_char && isupper(c)) {
has_cased_char = 1;
}
}
RETURN_BOOL(has_cased_char == 1);
}
/* }}} */
/* {{{ proto bool str_islower(string input)
Checks if all cased characters in the given input string are lowercase and if the
string contains at least one cased character. */
PHP_FUNCTION(str_islower)
{
unsigned char *input, *end;
unsigned char c;
int input_len;
int has_cased_char = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &input, &input_len) == FAILURE) {
RETURN_FALSE;
}
/* A string must at least contain one cased character in order to
* yield a positive result.
*/
if (input_len == 0) {
RETURN_FALSE;
}
/* Shortcut for strings that consist of just a single character. */
if (input_len == 1) {
RETURN_BOOL(islower(*input) != 0)
}
for (end = input + input_len; input < end; input++) {
c = *input;
if (isupper(c)) {
RETURN_FALSE;
}
if (!has_cased_char && islower(c)) {
has_cased_char = 1;
}
}
RETURN_BOOL(has_cased_char == 1);
}
/* }}} */
/* {{{ proto bool str_iswhitespace(string input)
Checks if the given string is empty or contains whitespace characters
only. */
PHP_FUNCTION(str_iswhitespace)
{
unsigned char *input, *end;
int input_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &input, &input_len) == FAILURE) {
RETURN_FALSE;
}
/* Empty strings yield a positive result. Need to think about this some
* more.
*/
if (input_len == 0) {
RETURN_TRUE;
}
/* Shortcut for strings that consist of just a single character. */
if (input_len == 1) {
RETURN_BOOL(isspace(*input))
}
for (end = input + input_len; input < end; input++) {
if (!isspace(*input)) {
RETURN_FALSE;
}
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool str_swapcase(string input)
Converts uppercase characters in the given string to their
lowercase representations and vice versa. */
PHP_FUNCTION(str_swapcase)
{
char *input;
unsigned char *c, *end;
int input_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &input, &input_len) == FAILURE) {
RETURN_FALSE;
}
input = estrndup(input, input_len);
c = (unsigned char *)input;
end = (unsigned char *)c + input_len;
while (c < end) {
if (isupper(*c)) {
*c = tolower(*c);
} else if (islower(*c)) {
*c = toupper(*c);
}
c++;
}
RETURN_STRINGL(input, input_len, 0)
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/