Skip to content

Commit 123025d

Browse files
committed
Add ctype_tolower() and ctype_toupper()
Reintroduce the old strtolower() and strtoupper() functions with new names ctype_tolower() and ctype_toupper(). Use the old test cases.
1 parent 6314cf1 commit 123025d

File tree

5 files changed

+661
-1
lines changed

5 files changed

+661
-1
lines changed

ext/ctype/ctype.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "ctype_arginfo.h"
2525
#include "SAPI.h"
2626
#include "ext/standard/info.h"
27+
#include "ext/standard/basic_functions.h"
2728

2829
#include <ctype.h>
2930

@@ -176,4 +177,86 @@ PHP_FUNCTION(ctype_xdigit)
176177
}
177178
/* }}} */
178179

180+
/* {{{ Converts a string to lower case */
181+
PHP_FUNCTION(ctype_tolower)
182+
{
183+
zend_string *s;
184+
unsigned char *c;
185+
const unsigned char *e;
186+
187+
ZEND_PARSE_PARAMETERS_START(1, 1)
188+
Z_PARAM_STR(s)
189+
ZEND_PARSE_PARAMETERS_END();
190+
191+
if (EXPECTED(!BG(ctype_string))) {
192+
RETURN_STR(zend_string_tolower(s));
193+
} else {
194+
c = (unsigned char *)ZSTR_VAL(s);
195+
e = c + ZSTR_LEN(s);
196+
197+
while (c < e) {
198+
if (isupper(*c)) {
199+
unsigned char *r;
200+
zend_string *res = zend_string_alloc(ZSTR_LEN(s), 0);
201+
202+
if (c != (unsigned char*)ZSTR_VAL(s)) {
203+
memcpy(ZSTR_VAL(res), ZSTR_VAL(s), c - (unsigned char*)ZSTR_VAL(s));
204+
}
205+
r = c + (ZSTR_VAL(res) - ZSTR_VAL(s));
206+
while (c < e) {
207+
*r = tolower(*c);
208+
r++;
209+
c++;
210+
}
211+
*r = '\0';
212+
RETURN_STR(res);
213+
}
214+
c++;
215+
}
216+
RETURN_STR_COPY(s);
217+
}
218+
}
219+
/* }}} */
220+
221+
/* {{{ Converts a string to upper case */
222+
PHP_FUNCTION(ctype_toupper)
223+
{
224+
zend_string *s;
225+
unsigned char *c;
226+
const unsigned char *e;
227+
228+
ZEND_PARSE_PARAMETERS_START(1, 1)
229+
Z_PARAM_STR(s)
230+
ZEND_PARSE_PARAMETERS_END();
231+
232+
if (EXPECTED(!BG(ctype_string))) {
233+
RETURN_STR(zend_string_toupper(s));
234+
} else {
235+
c = (unsigned char *)ZSTR_VAL(s);
236+
e = c + ZSTR_LEN(s);
237+
238+
while (c < e) {
239+
if (islower(*c)) {
240+
unsigned char *r;
241+
zend_string *res = zend_string_alloc(ZSTR_LEN(s), 0);
242+
243+
if (c != (unsigned char*)ZSTR_VAL(s)) {
244+
memcpy(ZSTR_VAL(res), ZSTR_VAL(s), c - (unsigned char*)ZSTR_VAL(s));
245+
}
246+
r = c + (ZSTR_VAL(res) - ZSTR_VAL(s));
247+
while (c < e) {
248+
*r = toupper(*c);
249+
r++;
250+
c++;
251+
}
252+
*r = '\0';
253+
RETURN_STR(res);
254+
}
255+
c++;
256+
}
257+
RETURN_STR_COPY(s);
258+
}
259+
}
260+
/* }}} */
261+
179262
#endif /* HAVE_CTYPE */

ext/ctype/ctype.stub.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ function ctype_space(mixed $text): bool {}
2323
function ctype_upper(mixed $text): bool {}
2424

2525
function ctype_xdigit(mixed $text): bool {}
26+
27+
function ctype_tolower(string $text): string {}
28+
29+
function ctype_toupper(string $text): string {}

ext/ctype/ctype_arginfo.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 155783e1858a7f24dbc1c3e810d5cffee5468bf7 */
2+
* Stub hash: f722fd029c16401b071bd0ec8c1bca4380675d23 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ctype_alnum, 0, 1, _IS_BOOL, 0)
55
ZEND_ARG_TYPE_INFO(0, text, IS_MIXED, 0)
@@ -25,6 +25,12 @@ ZEND_END_ARG_INFO()
2525

2626
#define arginfo_ctype_xdigit arginfo_ctype_alnum
2727

28+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ctype_tolower, 0, 1, IS_STRING, 0)
29+
ZEND_ARG_TYPE_INFO(0, text, IS_STRING, 0)
30+
ZEND_END_ARG_INFO()
31+
32+
#define arginfo_ctype_toupper arginfo_ctype_tolower
33+
2834

2935
ZEND_FUNCTION(ctype_alnum);
3036
ZEND_FUNCTION(ctype_alpha);
@@ -37,6 +43,8 @@ ZEND_FUNCTION(ctype_punct);
3743
ZEND_FUNCTION(ctype_space);
3844
ZEND_FUNCTION(ctype_upper);
3945
ZEND_FUNCTION(ctype_xdigit);
46+
ZEND_FUNCTION(ctype_tolower);
47+
ZEND_FUNCTION(ctype_toupper);
4048

4149

4250
static const zend_function_entry ext_functions[] = {
@@ -51,5 +59,7 @@ static const zend_function_entry ext_functions[] = {
5159
ZEND_FE(ctype_space, arginfo_ctype_space)
5260
ZEND_FE(ctype_upper, arginfo_ctype_upper)
5361
ZEND_FE(ctype_xdigit, arginfo_ctype_xdigit)
62+
ZEND_FE(ctype_tolower, arginfo_ctype_tolower)
63+
ZEND_FE(ctype_toupper, arginfo_ctype_toupper)
5464
ZEND_FE_END
5565
};

0 commit comments

Comments
 (0)