Hi All,

I came across a situation where I had to make the first character of an
arrays keys uppercase, and found the array_change_key_case function, but
noticed it only supported CASE_UPPER and CASE_LOWER. Attached is a patch to
also add support for CASE_LCFIRST and CASE_UCFIRST.

The patch is against PHP_5_3. I wasn't sure which branch I should write it
for--let me know if it should be against another branch. This is my first
patch submission so any comments are appreciated.

Regards,
--Matthew
Index: ext/standard/array.c
===================================================================
--- ext/standard/array.c        (revision 288546)
+++ ext/standard/array.c        (working copy)
@@ -63,6 +63,8 @@
 
 #define CASE_LOWER                             0
 #define CASE_UPPER                             1
+#define CASE_LCFIRST                   2
+#define CASE_UCFIRST                   3
 
 #define COUNT_NORMAL                   0
 #define COUNT_RECURSIVE                        1
@@ -121,6 +123,8 @@
 
        REGISTER_LONG_CONSTANT("CASE_LOWER", CASE_LOWER, CONST_CS | 
CONST_PERSISTENT);
        REGISTER_LONG_CONSTANT("CASE_UPPER", CASE_UPPER, CONST_CS | 
CONST_PERSISTENT);
+       REGISTER_LONG_CONSTANT("CASE_LCFIRST", CASE_LCFIRST, CONST_CS | 
CONST_PERSISTENT);
+       REGISTER_LONG_CONSTANT("CASE_UCFIRST", CASE_UCFIRST, CONST_CS | 
CONST_PERSISTENT);
 
        REGISTER_LONG_CONSTANT("COUNT_NORMAL", COUNT_NORMAL, CONST_CS | 
CONST_PERSISTENT);
        REGISTER_LONG_CONSTANT("COUNT_RECURSIVE", COUNT_RECURSIVE, CONST_CS | 
CONST_PERSISTENT);
@@ -2711,10 +2715,20 @@
                                break;
                        case HASH_KEY_IS_STRING:
                                new_key = estrndup(string_key, str_key_len - 1);
-                               if (change_to_upper) {
-                                       php_strtoupper(new_key, str_key_len - 
1);
-                               } else {
-                                       php_strtolower(new_key, str_key_len - 
1);
+                               switch (change_to_upper) {
+                                       case CASE_UPPER:
+                                               php_strtoupper(new_key, 
str_key_len - 1);
+                                               break;
+                                       case CASE_LCFIRST:
+                                               php_lcfirst(new_key);
+                                               break;
+                                       case CASE_UCFIRST:
+                                               php_ucfirst(new_key);
+                                               break;
+                                       case CASE_LOWER:
+                                       default:
+                                               php_strtolower(new_key, 
str_key_len - 1);
+                                               break;
                                }
                                zend_hash_update(Z_ARRVAL_P(return_value), 
new_key, str_key_len, entry, sizeof(entry), NULL);
                                efree(new_key);
Index: ext/standard/string.c
===================================================================
--- ext/standard/string.c       (revision 288546)
+++ ext/standard/string.c       (working copy)
@@ -2513,11 +2513,12 @@
 
 /* {{{ php_ucfirst
    Uppercase the first character of the word in a native string */
-static void php_ucfirst(char *str) 
+PHPAPI char *php_ucfirst(char *s)
 {
-       register char *r;
-       r = str;
-       *r = toupper((unsigned char) *r);
+       unsigned char *c;
+       c = (unsigned char *)s;
+       *c = toupper(*c);
+       return s;
 }
 /* }}} */
 
@@ -2536,22 +2537,24 @@
                RETURN_EMPTY_STRING();
        }
 
-       ZVAL_STRINGL(return_value, str, str_len, 1);
-       php_ucfirst(Z_STRVAL_P(return_value));
+       str = estrndup(str, str_len);
+       php_ucfirst(str);
+       RETURN_STRINGL(str, str_len, 0);
 }
 /* }}} */
 
 /* {{{
    Lowercase the first character of the word in a native string */
-static void php_lcfirst(char *str)
+PHPAPI char *php_lcfirst(char *s)
 {
-       register char *r;
-       r = str;
-       *r = tolower((unsigned char) *r);
+       unsigned char *c;
+       c = (unsigned char *)s;
+       *c = tolower(*c);
+       return s;
 }
 /* }}} */
 
-/* {{{ proto string ucfirst(string str)
+/* {{{ proto string lcfirst(string str)
    Make a string's first character lowercase */
 PHP_FUNCTION(lcfirst)
 {
@@ -2566,8 +2569,9 @@
                RETURN_EMPTY_STRING();
        }
 
-       ZVAL_STRINGL(return_value, str, str_len, 1);
-       php_lcfirst(Z_STRVAL_P(return_value));
+       str = estrndup(str, str_len);
+       php_lcfirst(str);
+       RETURN_STRINGL(str, str_len, 0);
 }
 /* }}} */
 
-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to