php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #48767
Patch getextension-trunk revision 2010-11-25 13:09 UTC by salathe@php.net
Patch splfileinfo-getextension-trunk revision 2010-11-17 21:34 UTC by salathe@php.net

Patch splfileinfo-getextension-trunk for SPL related Bug #48767

Patch version 2010-11-17 21:34 UTC

Return to Bug #48767 | Download this patch
This patch is obsolete

Obsoleted by patches:

Patch Revisions:

Developer: salathe@php.net

Index: ext/spl/tests/SplFileInfo_getExtension_basic.phpt
===================================================================
--- ext/spl/tests/SplFileInfo_getExtension_basic.phpt	(revision 0)
+++ ext/spl/tests/SplFileInfo_getExtension_basic.phpt	(revision 0)
@@ -0,0 +1,31 @@
+--TEST--
+SPL: SplFileInfo::getExtension() basic test
+--FILE--
+<?php
+$file = md5('SplFileInfo::getExtension');
+$exts = array('.txt', '.extension', '..', '.', '');
+foreach ($exts as $ext) {
+    touch($file . $ext);
+    $info = new SplFileInfo($file . $ext);
+    var_dump($info->getExtension(), pathinfo($file . $ext, PATHINFO_EXTENSION));
+}
+?>
+--CLEAN--
+<?php
+$file = md5('SplFileInfo::getExtension');
+$exts = array('.txt', '.extension', '..', '.', '');
+foreach ($exts as $ext) {
+    unlink($file . $ext);
+}
+?>
+--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) ""
Index: ext/spl/spl_directory.c
===================================================================
--- ext/spl/spl_directory.c	(revision 305471)
+++ ext/spl/spl_directory.c	(working copy)
@@ -844,6 +844,39 @@
 }
 /* }}} */
 
+/* {{{ 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;
+	}
+
+	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 +1811,7 @@
 	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)
Index: UPGRADING
===================================================================
--- UPGRADING	(revision 305471)
+++ UPGRADING	(working copy)
@@ -313,6 +313,9 @@
        - PDO_dblib
          - PDO::newRowset()
 
+       - SplFileInfo
+         - SplFileInfo::getExtension()
+
        - SplFileObject
          - SplFileObject::fputcsv()
 
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Sun Apr 20 19:01:51 2014 UTC