]> granicus.if.org Git - php/commitdiff
Add php_string_tolower/toupper which is locale depened
authorXinchen Hui <laruence@php.net>
Thu, 25 Dec 2014 03:28:54 +0000 (11:28 +0800)
committerXinchen Hui <laruence@php.net>
Thu, 25 Dec 2014 03:29:11 +0000 (11:29 +0800)
ext/standard/array.c
ext/standard/php_string.h
ext/standard/string.c

index e226b62ad2b0c12a0ff37a23416509fbe07dca73..9d55ff6da4fa0640aa7e5a73ece5e0d31eebdad6 100644 (file)
@@ -2848,6 +2848,7 @@ zend_bool array_column_param_helper(zval *param,
                        return 0;
        }
 }
+/* }}} */
 
 /* {{{ proto array array_column(array input, mixed column_key[, mixed index_key])
    Return the values from a single column in the input array, identified by the
@@ -3075,11 +3076,10 @@ PHP_FUNCTION(array_change_key_case)
                if (!string_key) {
                        zend_hash_index_update(Z_ARRVAL_P(return_value), num_key, entry);
                } else {
-                       new_key = zend_string_init(string_key->val, string_key->len, 0);
                        if (change_to_upper) {
-                               php_strtoupper(new_key->val, new_key->len);
+                               new_key = php_string_toupper(string_key);
                        } else {
-                               php_strtolower(new_key->val, new_key->len);
+                               new_key = php_string_tolower(string_key);
                        }
                        zend_hash_update(Z_ARRVAL_P(return_value), new_key, entry);
                        zend_string_release(new_key);
index 8e0ad5c97a02c4f4208bbc54392f1ee171a0aab5..cbcd39e3ff33acad82176737097489844220b5f5 100644 (file)
@@ -120,6 +120,8 @@ PHPAPI struct lconv *localeconv_r(struct lconv *out);
 
 PHPAPI char *php_strtoupper(char *s, size_t len);
 PHPAPI char *php_strtolower(char *s, size_t len);
+PHPAPI zend_string *php_string_toupper(zend_string *s);
+PHPAPI zend_string *php_string_tolower(zend_string *s);
 PHPAPI char *php_strtr(char *str, size_t len, char *str_from, char *str_to, size_t trlen);
 PHPAPI zend_string *php_addslashes(char *str, size_t length, int should_free);
 PHPAPI zend_string *php_addcslashes(const char *str, size_t length, int freeit, char *what, size_t wlength);
index e275e6456ce6159e9b42e368cc8f49cbae9ea39c..477032d05f59c741e3e1c23bc5b1c5f40ccdafcc 100644 (file)
@@ -1368,20 +1368,49 @@ PHPAPI char *php_strtoupper(char *s, size_t len)
 }
 /* }}} */
 
+/* {{{ php_string_toupper
+ */
+PHPAPI zend_string *php_string_toupper(zend_string *s)
+{
+       unsigned char *c, *e;
+
+       c = (unsigned char *)s->val;
+       e = c + s->len;
+
+       while (c < e) {
+               if (!isupper(*c)) {
+                       register unsigned char *r;
+                       zend_string *res = zend_string_alloc(s->len, 0);
+
+                       if (c != (unsigned char*)s->val) {
+                               memcpy(res->val, s->val, c - (unsigned char*)s->val);
+                       }
+                       r = c + (res->val - s->val);
+                       while (c < e) {
+                               *r = toupper(*c);
+                               r++;
+                               c++;
+                       }
+                       *r = '\0';
+                       return res;
+               }
+               c++;
+       }
+       return zend_string_copy(s);
+}
+/* }}} */
+
 /* {{{ proto string strtoupper(string str)
    Makes a string uppercase */
 PHP_FUNCTION(strtoupper)
 {
        zend_string *arg;
-       zend_string *result;
 
        if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &arg) == FAILURE) {
                return;
        }
 
-       result = zend_string_init(arg->val, arg->len, 0);
-       php_strtoupper(result->val, result->len);
-       RETURN_NEW_STR(result);
+       RETURN_STR(php_string_toupper(arg));
 }
 /* }}} */
 
@@ -1402,12 +1431,43 @@ PHPAPI char *php_strtolower(char *s, size_t len)
 }
 /* }}} */
 
+/* {{{ php_string_tolower
+ */
+PHPAPI zend_string *php_string_tolower(zend_string *s)
+{
+       unsigned char *c, *e;
+
+       c = (unsigned char *)s->val;
+       e = c + s->len;
+
+       while (c < e) {
+               if (!islower(*c)) {
+                       register unsigned char *r;
+                       zend_string *res = zend_string_alloc(s->len, 0);
+
+                       if (c != (unsigned char*)s->val) {
+                               memcpy(res->val, s->val, c - (unsigned char*)s->val);
+                       }
+                       r = c + (res->val - s->val);
+                       while (c < e) {
+                               *r = tolower(*c);
+                               r++;
+                               c++;
+                       }
+                       *r = '\0';
+                       return res;
+               }
+               c++;
+       }
+       return zend_string_copy(s);
+}
+/* }}} */
+
 /* {{{ proto string strtolower(string str)
    Makes a string lowercase */
 PHP_FUNCTION(strtolower)
 {
        zend_string *str;
-       zend_string *result;
 
 #ifndef FAST_ZPP
        if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &str) == FAILURE) {
@@ -1419,9 +1479,7 @@ PHP_FUNCTION(strtolower)
        ZEND_PARSE_PARAMETERS_END();
 #endif
 
-       result = zend_string_init(str->val, str->len, 0);
-       php_strtolower(result->val, result->len);
-       RETURN_NEW_STR(result);
+       RETURN_STR(php_string_tolower(str));
 }
 /* }}} */