From: Peter Cowburn Date: Thu, 23 Dec 2010 22:32:52 +0000 (+0000) Subject: Implemented FR #48767 (SplFileInfo::getExtension()) X-Git-Tag: php-5.4.0alpha1~191^2~466 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=064a254483428dad4ca1c84826e7197b70e89c88;p=php Implemented FR #48767 (SplFileInfo::getExtension()) --- diff --git a/UPGRADING b/UPGRADING index 044659ad10..da3048cf71 100755 --- a/UPGRADING +++ b/UPGRADING @@ -329,6 +329,9 @@ UPGRADE NOTES - PHP X.Y h. New methods + - DirectoryIterator + - DirectoryIterator::getExtension() + - Reflection: - ReflectionClass::isCloneable() - ReflectionClass::getTraits() @@ -339,6 +342,9 @@ UPGRADE NOTES - PHP X.Y - PDO_dblib - PDO::newRowset() + - SplFileInfo + - SplFileInfo::getExtension() + - SplFileObject - SplFileObject::fputcsv() diff --git a/ext/spl/spl_directory.c b/ext/spl/spl_directory.c index cacb11f180..543401bec1 100755 --- a/ext/spl/spl_directory.c +++ b/ext/spl/spl_directory.c @@ -844,6 +844,66 @@ SPL_METHOD(DirectoryIterator, getFilename) } /* }}} */ +/* {{{ proto string SplFileInfo::getExtension() + Returns file extension component of path */ +SPL_METHOD(SplFileInfo, getExtension) +{ + spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC); + char *fname, *p; + size_t flen; + int path_len, idx; + + if (zend_parse_parameters_none() == FAILURE) { + return; + } + + spl_filesystem_object_get_path(intern, &path_len TSRMLS_CC); + + if (path_len && path_len < intern->file_name_len) { + fname = intern->file_name + path_len + 1; + flen = intern->file_name_len - (path_len + 1); + } else { + fname = intern->file_name; + flen = intern->file_name_len; + } + + php_basename(fname, flen, NULL, 0, &fname, &flen TSRMLS_CC); + + p = zend_memrchr(fname, '.', flen); + if (p) { + idx = p - fname; + RETURN_STRINGL(fname + idx + 1, flen - idx - 1, 1); + } + + RETURN_EMPTY_STRING(); +} +/* }}}*/ + +/* {{{ proto string DirectoryIterator::getExtension() + Returns the file extension component of path */ +SPL_METHOD(DirectoryIterator, getExtension) +{ + spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC); + char *fname, *p; + size_t flen; + int idx; + + if (zend_parse_parameters_none() == FAILURE) { + return; + } + + php_basename(intern->u.dir.entry.d_name, strlen(intern->u.dir.entry.d_name), NULL, 0, &fname, &flen TSRMLS_CC); + + p = zend_memrchr(fname, '.', flen); + if (p) { + idx = p - fname; + RETURN_STRINGL(fname + idx + 1, flen - idx - 1, 1); + } + + RETURN_EMPTY_STRING(); +} +/* }}} */ + /* {{{ proto string SplFileInfo::getBasename([string $suffix]) U Returns filename component of path */ SPL_METHOD(SplFileInfo, getBasename) @@ -1778,6 +1838,7 @@ static const zend_function_entry spl_SplFileInfo_functions[] = { SPL_ME(SplFileInfo, __construct, arginfo_info___construct, ZEND_ACC_PUBLIC) SPL_ME(SplFileInfo, getPath, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC) SPL_ME(SplFileInfo, getFilename, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC) + SPL_ME(SplFileInfo, getExtension, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC) SPL_ME(SplFileInfo, getBasename, arginfo_optinalSuffix, ZEND_ACC_PUBLIC) SPL_ME(SplFileInfo, getPathname, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC) SPL_ME(SplFileInfo, getPerms, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC) @@ -1821,6 +1882,7 @@ ZEND_END_ARG_INFO(); static const zend_function_entry spl_DirectoryIterator_functions[] = { SPL_ME(DirectoryIterator, __construct, arginfo_dir___construct, ZEND_ACC_PUBLIC) SPL_ME(DirectoryIterator, getFilename, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC) + SPL_ME(DirectoryIterator, getExtension, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC) SPL_ME(DirectoryIterator, getBasename, arginfo_optinalSuffix, ZEND_ACC_PUBLIC) SPL_ME(DirectoryIterator, isDot, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC) SPL_ME(DirectoryIterator, rewind, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC) diff --git a/ext/spl/tests/DirectoryIterator_getExtension_basic.phpt b/ext/spl/tests/DirectoryIterator_getExtension_basic.phpt new file mode 100644 index 0000000000..20ef3bf36e --- /dev/null +++ b/ext/spl/tests/DirectoryIterator_getExtension_basic.phpt @@ -0,0 +1,51 @@ +--TEST-- +SPL: DirectoryIterator::getExtension() basic test +--FILE-- +getFilename(), $skip)) { + continue; + } + $dit_exts[] = $file->getExtension(); + $nfo_exts[] = pathinfo($file->getFilename(), PATHINFO_EXTENSION); +} +var_dump($dit_exts === $nfo_exts); +sort($dit_exts); +var_dump($dit_exts); +?> +--CLEAN-- + +--EXPECTF-- +bool(true) +array(5) { + [0]=> + string(0) "" + [1]=> + string(0) "" + [2]=> + string(0) "" + [3]=> + string(9) "extension" + [4]=> + string(3) "txt" +} + diff --git a/ext/spl/tests/SplFileInfo_getExtension_basic.phpt b/ext/spl/tests/SplFileInfo_getExtension_basic.phpt new file mode 100644 index 0000000000..b47b6bb4d0 --- /dev/null +++ b/ext/spl/tests/SplFileInfo_getExtension_basic.phpt @@ -0,0 +1,31 @@ +--TEST-- +SPL: SplFileInfo::getExtension() basic test +--FILE-- +getExtension(), pathinfo($file . $ext, PATHINFO_EXTENSION)); +} +?> +--CLEAN-- + +--EXPECTF-- +string(3) "txt" +string(3) "txt" +string(9) "extension" +string(9) "extension" +string(0) "" +string(0) "" +string(0) "" +string(0) "" +string(0) "" +string(0) ""