davidc Sat Jan 19 19:23:25 2008 UTC
Added files:
/php-src/ext/standard/tests/strings lcfirst.phpt
Modified files:
/php-src/ext/standard basic_functions.c php_string.h string.c
Log:
- Added lcfirst() function. The sister function of ucfirst()
- Test #1 for lcfirst()
http://cvs.php.net/viewvc.cgi/php-src/ext/standard/basic_functions.c?r1=1.886&r2=1.887&diff_format=u
Index: php-src/ext/standard/basic_functions.c
diff -u php-src/ext/standard/basic_functions.c:1.886
php-src/ext/standard/basic_functions.c:1.887
--- php-src/ext/standard/basic_functions.c:1.886 Mon Dec 31 07:12:15 2007
+++ php-src/ext/standard/basic_functions.c Sat Jan 19 19:23:25 2008
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: basic_functions.c,v 1.886 2007/12/31 07:12:15 sebastian Exp $ */
+/* $Id: basic_functions.c,v 1.887 2008/01/19 19:23:25 davidc Exp $ */
#include "php.h"
#include "php_streams.h"
@@ -2635,6 +2635,11 @@
ZEND_END_ARG_INFO()
static
+ZEND_BEGIN_ARG_INFO(arginfo_lcfirst, 0)
+ ZEND_ARG_INFO(0, str)
+ZEND_END_ARG_INFO()
+
+static
ZEND_BEGIN_ARG_INFO(arginfo_ucwords, 0)
ZEND_ARG_INFO(0, str)
ZEND_END_ARG_INFO()
@@ -3189,6 +3194,7 @@
PHP_FE(substr_replace,
arginfo_substr_replace)
PHP_FE(quotemeta,
arginfo_quotemeta)
PHP_FE(ucfirst,
arginfo_ucfirst)
+ PHP_FE(lcfirst,
arginfo_lcfirst)
PHP_FE(ucwords,
arginfo_ucwords)
PHP_FE(strtr,
arginfo_strtr)
PHP_FE(addslashes,
arginfo_addslashes)
http://cvs.php.net/viewvc.cgi/php-src/ext/standard/php_string.h?r1=1.109&r2=1.110&diff_format=u
Index: php-src/ext/standard/php_string.h
diff -u php-src/ext/standard/php_string.h:1.109
php-src/ext/standard/php_string.h:1.110
--- php-src/ext/standard/php_string.h:1.109 Mon Dec 31 07:12:16 2007
+++ php-src/ext/standard/php_string.h Sat Jan 19 19:23:25 2008
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: php_string.h,v 1.109 2007/12/31 07:12:16 sebastian Exp $ */
+/* $Id: php_string.h,v 1.110 2008/01/19 19:23:25 davidc Exp $ */
/* Synced with php 3.0 revision 1.43 1999-06-16 [ssb] */
@@ -54,6 +54,7 @@
PHP_FUNCTION(substr);
PHP_FUNCTION(quotemeta);
PHP_FUNCTION(ucfirst);
+PHP_FUNCTION(lcfirst);
PHP_FUNCTION(ucwords);
PHP_FUNCTION(strtr);
PHP_FUNCTION(strrev);
http://cvs.php.net/viewvc.cgi/php-src/ext/standard/string.c?r1=1.662&r2=1.663&diff_format=u
Index: php-src/ext/standard/string.c
diff -u php-src/ext/standard/string.c:1.662 php-src/ext/standard/string.c:1.663
--- php-src/ext/standard/string.c:1.662 Wed Jan 16 08:34:06 2008
+++ php-src/ext/standard/string.c Sat Jan 19 19:23:25 2008
@@ -18,7 +18,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: string.c,v 1.662 2008/01/16 08:34:06 tony2001 Exp $ */
+/* $Id: string.c,v 1.663 2008/01/19 19:23:25 davidc Exp $ */
/* Synced with php 3.0 revision 1.193 1999-06-16 [ssb] */
@@ -3856,6 +3856,69 @@
}
/* }}} */
+/* {{{ php_u_lcfirst()
+ Makes an Unicode string's first character lowercase */
+static void php_u_lcfirst(UChar *ustr, int ustr_len, zval *return_value
TSRMLS_DC)
+{
+ UChar tmp[3] = { 0, 0, 0 }; /* UChar32 will be converted to upto 2
UChar units */
+ int tmp_len = 0;
+ int pos = 0;
+ UErrorCode status = U_ZERO_ERROR;
+
+ U16_FWD_1(ustr, pos, ustr_len);
+ tmp_len = u_strToUpper(tmp, sizeof(tmp)/sizeof(UChar), ustr, pos,
UG(default_locale), &status);
+
+ Z_USTRVAL_P(return_value) = eumalloc(tmp_len + ustr_len - pos+1);
+
+ Z_USTRVAL_P(return_value)[0] = tmp[0];
+ if (tmp_len > 1) {
+ Z_USTRVAL_P(return_value)[1] = tmp[1];
+ }
+ u_memcpy(Z_USTRVAL_P(return_value)+tmp_len, ustr + pos, ustr_len -
pos+1);
+ Z_USTRLEN_P(return_value) = tmp_len + ustr_len - pos;
+}
+/* }}} */
+
+/* {{{ php_lcfirst
+ Lowercase the first character of the word in a native string */
+static void php_lcfirst(char *str)
+{
+ register char *r;
+ r = str;
+ *r = tolower((unsigned char) *r);
+}
+/* }}} */
+
+/* {{{ proto string ucfirst(string str) U
+ Makes a string's first character uppercase */
+PHP_FUNCTION(lcfirst)
+{
+ zstr str;
+ int str_len;
+ zend_uchar str_type;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "t", &str,
&str_len, &str_type) == FAILURE) {
+ return;
+ }
+
+ if (str_len == 0) {
+ if (str_type == IS_UNICODE) {
+ RETURN_EMPTY_UNICODE();
+ } else {
+ RETURN_EMPTY_STRING();
+ }
+ }
+
+ if (str_type == IS_UNICODE) {
+ Z_TYPE_P(return_value) = IS_UNICODE;
+ php_u_lcfirst(str.u, str_len, return_value TSRMLS_CC);
+ } else {
+ ZVAL_STRINGL(return_value, str.s, str_len, ZSTR_DUPLICATE);
+ php_lcfirst(Z_STRVAL_P(return_value));
+ }
+}
+/* }}} */
+
/* {{{ php_u_ucfirst()
Makes an Unicode string's first character uppercase */
static void php_u_ucfirst(UChar *ustr, int ustr_len, zval *return_value
TSRMLS_DC)
http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/strings/lcfirst.phpt?view=markup&rev=1.1
Index: php-src/ext/standard/tests/strings/lcfirst.phpt
+++ php-src/ext/standard/tests/strings/lcfirst.phpt
--TEST--
"lcfirst()" function
--INI--
precision=14
--FILE--
<?php
/* Make a string's first character uppercase */
echo "#### Basic and Various operations ####\n";
$str_array = array(
"TesTing lcfirst.",
"1.testing lcfirst",
"HELLO wORLD",
'HELLO wORLD',
"\0", // Null
"\x00", // Hex Null
"\x000",
"abcd", // double quoted string
'xyz', // single quoted string
string, // without quotes
"-3",
-3,
'-3.344',
-3.344,
NULL,
"NULL",
"0",
0,
TRUE, // bool type
"TRUE",
"1",
1,
1.234444,
FALSE,
"FALSE",
" ",
" ",
'b', // single char
'\t', // escape sequences
"\t",
"12",
"12twelve", // int + string
);
/* loop to test working of lcfirst with different values */
foreach ($str_array as $string) {
var_dump( lcfirst($string) );
}
echo "\n#### Testing Miscelleneous inputs ####\n";
echo "--- Testing arrays ---";
$str_arr = array("Hello", "?world", "!$%**()%**[][[[&@#~!", array());
var_dump( lcfirst($str_arr) );
echo "\n--- Testing lowercamelcase action call example ---\n";
class Setter {
protected $vars = array('partnerName' => false);
public function __call($m, $v) {
if (stristr($m, 'set')) {
$action = lcfirst(substr($m, 3));
$this->$action = $v[0];
}
}
public function __set($key, $value) {
if (array_key_exists($key, $this->vars)) {
$this->vars[$key] = $value;
}
}
public function __get($key) {
if (array_key_exists($key, $this->vars)) {
return $this->vars[$key];
}
}
}
$class = new Setter();
$class->setPartnerName('partnerName');
var_dump($class->partnerName);
echo "\n--- Testing objects ---\n";
/* we get "Catchable fatal error: saying Object of class could not be converted
to string" by default when an object is passed instead of string:
The error can be avoided by chosing the __toString magix method as follows: */
class string {
function __toString() {
return "Hello world";
}
}
$obj_string = new string;
var_dump(lcfirst("$obj_string"));
echo "\n--- Testing Resources ---\n";
$filename1 = "dummy.txt";
$file1 = fopen($filename1, "w"); // creating new file
/* getting resource type for file handle */
$string1 = get_resource_type($file1);
$string2 = (int)get_resource_type($file1); // converting stream type to int
/* $string1 is of "stream" type */
var_dump(lcfirst($string1));
/* $string2 holds a value of "int(0)" */
var_dump(lcfirst($string2));
fclose($file1); // closing the file "dummy.txt"
unlink("$filename1"); // deletes "dummy.txt"
echo "\n--- Testing a longer and heredoc string ---\n";
$string = <<<EOD
Abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
@#$%^&**&[EMAIL PROTECTED]:())))((((&&&**%$###@@@[EMAIL PROTECTED]&*
abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
EOD;
var_dump(lcfirst($string));
echo "\n--- Testing a heredoc null string ---\n";
$str = <<<EOD
EOD;
var_dump(lcfirst($str));
echo "\n--- Testing simple and complex syntax strings ---\n";
$str = 'world';
/* Simple syntax */
var_dump(lcfirst("$str"));
var_dump(lcfirst("$str'S"));
var_dump(lcfirst("$strS"));
/* String with curly braces, complex syntax */
var_dump(lcfirst("${str}S"));
var_dump(lcfirst("{$str}S"));
echo "\n--- Nested lcfirst() ---\n";
var_dump(lcfirst(lcfirst("hello")));
echo "\n#### error conditions ####";
/* Zero arguments */
lcfirst();
/* More than expected no. of args */
lcfirst($str_array[0], $str_array[1]);
lcfirst((int)10, (int)20);
echo "Done\n";
?>
--EXPECTF--
#### Basic and Various operations ####
Notice: Use of undefined constant string - assumed 'string' in %s on line %d
string(16) "tesTing lcfirst."
string(17) "1.testing lcfirst"
string(11) "hELLO wORLD"
string(11) "hELLO wORLD"
string(1) "
string(1) "
string(2) "
string(4) "abcd"
string(3) "xyz"
string(6) "string"
string(2) "-3"
string(2) "-3"
string(6) "-3.344"
string(6) "-3.344"
string(0) ""
string(4) "nULL"
string(1) "0"
string(1) "0"
string(1) "1"
string(4) "tRUE"
string(1) "1"
string(1) "1"
string(8) "1.234444"
string(0) ""
string(5) "fALSE"
string(1) " "
string(5) " "
string(1) "b"
string(2) "\t"
string(1) " "
string(2) "12"
string(8) "12twelve"
#### Testing Miscelleneous inputs ####
--- Testing arrays ---
Warning: lcfirst() expects parameter 1 to be string (Unicode or binary), array
given in %s on line %d
NULL
--- Testing lowercamelcase action call example ---
string(%d) "partnerName"
--- Testing objects ---
string(11) "hello world"
--- Testing Resources ---
string(6) "stream"
string(1) "0"
--- Testing a longer and heredoc string ---
string(639)
"abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
@#$%^&**&[EMAIL PROTECTED]:())))((((&&&**%$###@@@[EMAIL PROTECTED]&*
abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
--- Testing a heredoc null string ---
string(0) ""
--- Testing simple and complex syntax strings ---
string(5) "world"
string(7) "world'S"
Notice: Undefined variable: strS in %s on line %d
string(0) ""
string(6) "worldS"
string(6) "worldS"
--- Nested lcfirst() ---
string(5) "hello"
#### error conditions ####
Warning: lcfirst() expects exactly 1 parameter, 0 given in %s on line %d
Warning: lcfirst() expects exactly 1 parameter, 2 given in %s on line %d
Warning: lcfirst() expects exactly 1 parameter, 2 given in %s on line %d
Done
--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php