Merge branch 'MDL-40877-master' of git://github.com/FMCorz/moodle

This commit is contained in:
Damyon Wiese 2013-08-27 12:46:23 +08:00
commit 92058753ac
3 changed files with 62 additions and 15 deletions

View file

@ -238,6 +238,36 @@ class core_text {
return $result;
}
/**
* Finds the last occurrence of a character in a string within another.
* UTF-8 ONLY safe mb_strrchr().
*
* @param string $haystack The string from which to get the last occurrence of needle.
* @param string $needle The string to find in haystack.
* @param boolean $part If true, returns the portion before needle, else return the portion after (including needle).
* @return string|false False when not found.
* @since 2.4.6, 2.5.2, 2.6
*/
public static function strrchr($haystack, $needle, $part = false) {
if (function_exists('mb_strrchr')) {
return mb_strrchr($haystack, $needle, $part, 'UTF-8');
}
$pos = self::strrpos($haystack, $needle);
if ($pos === false) {
return false;
}
$length = null;
if ($part) {
$length = $pos;
$pos = 0;
}
return self::substr($haystack, $pos, $length, 'utf-8');
}
/**
* Multibyte safe strlen() function, uses mbstring or iconv for UTF-8, falls back to typo3.
*
@ -331,7 +361,7 @@ class core_text {
* @return int the numeric position of the last occurrence of needle in haystack
*/
public static function strrpos($haystack, $needle) {
if (function_exists('mb_strpos')) {
if (function_exists('mb_strrpos')) {
return mb_strrpos($haystack, $needle, null, 'UTF-8');
} else {
return iconv_strrpos($haystack, $needle, 'UTF-8');

View file

@ -360,5 +360,16 @@ class core_text_testcase extends advanced_testcase {
$this->assertSame($textlib->specialtoascii('ábc'), 'abc');
$this->assertSame($textlib->strtotitle('abc ABC'), 'Abc Abc');
}
/**
* Test strrchr.
*/
public function test_strrchr() {
$str = "Žluťoučký koníček";
$this->assertSame('koníček', core_text::strrchr($str, 'koní'));
$this->assertSame('Žluťoučký ', core_text::strrchr($str, 'koní', true));
$this->assertFalse(core_text::strrchr($str, 'A'));
$this->assertFalse(core_text::strrchr($str, 'ç', true));
}
}

View file

@ -40,6 +40,8 @@
* See: http://www.opensource.org/licenses/bsd-license.php
*/
defined('MOODLE_INTERNAL') || die();
/**
* Combine a base URL and a relative URL to produce a new
* absolute URL. The base URL is often the URL of a page,
@ -79,9 +81,9 @@ function url_to_absolute( $baseUrl, $relativeUrl )
if ( $b === FALSE || empty( $b['scheme'] ) || empty( $b['host'] ) )
return FALSE;
$r['scheme'] = $b['scheme'];
if (empty($b['path'])) {
$b['path'] = '';
}
if (empty($b['path'])) {
$b['path'] = '';
}
// If relative URL has an authority, clean path and return.
if ( isset( $r['host'] ) )
@ -110,15 +112,16 @@ function url_to_absolute( $baseUrl, $relativeUrl )
return join_url( $r );
}
// If relative URL path doesn't start with /, merge with base path
if ( $r['path'][0] != '/' )
{
$base = mb_strrchr( $b['path'], '/', TRUE, 'UTF-8' );
if ( $base === FALSE ) $base = '';
// If relative URL path doesn't start with /, merge with base path.
if ($r['path'][0] != '/') {
$base = core_text::strrchr($b['path'], '/', TRUE);
if ($base === FALSE) {
$base = '';
}
$r['path'] = $base . '/' . $r['path'];
}
$r['path'] = url_remove_dot_segments( $r['path'] );
return join_url( $r );
$r['path'] = url_remove_dot_segments($r['path']);
return join_url($r);
}
/**
@ -152,12 +155,15 @@ function url_remove_dot_segments( $path )
array_push( $outSegs, $seg );
}
$outPath = implode( '/', $outSegs );
if ( $path[0] == '/' )
if ($path[0] == '/') {
$outPath = '/' . $outPath;
// compare last multi-byte character against '/'
if ( $outPath != '/' &&
(mb_strlen($path)-1) == mb_strrpos( $path, '/', 'UTF-8' ) )
}
// Compare last multi-byte character against '/'.
if ($outPath != '/' && (core_text::strlen($path) - 1) == core_text::strrpos($path, '/', 'UTF-8')) {
$outPath .= '/';
}
return $outPath;
}