浏览代码

add proper method to cut the string

saeid 8 年之前
父节点
当前提交
f405fdfade
共有 2 个文件被更改,包括 14 次插入3 次删除
  1. 13 3
      src/Filesystem/File.php
  2. 1 0
      tests/TestCase/Filesystem/FileTest.php

+ 13 - 3
src/Filesystem/File.php

@@ -355,7 +355,7 @@ class File
     }
     }
 
 
     /**
     /**
-     * Returns the file basename. simulate the php basename().
+     * Returns the file basename. simulate the php basename() for multibyte (mb_basename).
      *
      *
      * @param string $path Path to file
      * @param string $path Path to file
      * @param string|null $ext The name of the extension
      * @param string|null $ext The name of the extension
@@ -363,13 +363,23 @@ class File
      */
      */
     protected static function _basename($path, $ext = null)
     protected static function _basename($path, $ext = null)
     {
     {
+        //check for multibyte string and use basename() if not found
+        if (mb_strlen($path) === strlen($path)) {
+            return ($ext===null)? basename($path) : basename($path, $ext);
+        }
+
         $splInfo = new SplFileInfo($path);
         $splInfo = new SplFileInfo($path);
         $name = ltrim($splInfo->getFilename(), DS);
         $name = ltrim($splInfo->getFilename(), DS);
-        if ($ext === null || rtrim($name, $ext) === '') {
+
+        if ($ext === null || $ext === '') {
             return $name;
             return $name;
         }
         }
+        $ext = preg_quote($ext);
+        $new = preg_replace("/({$ext})$/u", "", $name);
+
+        // basename of '/etc/.d' is '.d' not ''
+        return ($new === '')? $name : $new;
 
 
-        return rtrim($name, $ext);
     }
     }
 
 
     /**
     /**

+ 1 - 0
tests/TestCase/Filesystem/FileTest.php

@@ -176,6 +176,7 @@ class FileTest extends TestCase
             ['/نام.txt', '.txt'],
             ['/نام.txt', '.txt'],
             ['/نام فارسی.txt', '.txt'],
             ['/نام فارسی.txt', '.txt'],
             //
             //
+            ['abcde.ab', '.abe'],
             ['/etc/sudoers.d', null],
             ['/etc/sudoers.d', null],
             ['/etc/.d', '.d'],
             ['/etc/.d', '.d'],
             ['/etc/sudoers.d', '.d'],
             ['/etc/sudoers.d', '.d'],