Skip to content

Commit

Permalink
[+]: optimize "UTF8::urldecode()" -> decode as often as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
voku committed Sep 14, 2016
1 parent 3c9d67d commit 2653d6f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/voku/helper/UTF8.php
Original file line number Diff line number Diff line change
Expand Up @@ -5793,11 +5793,12 @@ public static function ucwords($str, $exceptions = array(), $charlist = '', $enc
* 'D%C3%83%C2%BCsseldorf' => 'Düsseldorf'
* 'D%25C3%2583%25C2%25BCsseldorf' => 'Düsseldorf'
*
* @param string $str <p>The input string.</p>
* @param string $str <p>The input string.</p>
* @param bool $multi_decode <p>Decode as often as possible.</p>
*
* @return string
*/
public static function urldecode($str)
public static function urldecode($str, $multi_decode = true)
{
$str = (string)$str;

Expand All @@ -5809,22 +5810,29 @@ public static function urldecode($str)

$flags = Bootup::is_php('5.4') ? ENT_QUOTES | ENT_HTML5 : ENT_QUOTES;

$str = self::fix_simple_utf8(
rawurldecode(
self::html_entity_decode(
self::to_utf8($str),
$flags
)
)
);
do {
$str_compare = $str;

$str = self::fix_simple_utf8(
rawurldecode(
self::html_entity_decode(
self::to_utf8($str),
$flags
)
)
);

} while ($multi_decode === true && $str_compare !== $str);

return (string)$str;
}

/**
* Return a array with "urlencoded"-win1252 -> UTF-8
*
* @return mixed
* @deprecated use the "UTF8::urldecode()" function to decode a string
*
* @return array
*/
public static function urldecode_fix_win1252_chars()
{
Expand Down
4 changes: 4 additions & 0 deletions tests/Utf8GlobalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3409,6 +3409,8 @@ public function testUrlDecode()
"\n" => "\n",
"\u00ed" => 'í',
'con%5cu00%366irm' => 'confirm',
'%3A%2F%2F%252567%252569%252573%252574' => '://gist',
'%253A%252F%252F%25252567%25252569%25252573%25252574' => '://gist',
"tes%20öäü%20\u00edtest" => 'tes öäü ítest',
'Düsseldorf' => 'Düsseldorf',
'Duesseldorf' => 'Duesseldorf',
Expand All @@ -3425,6 +3427,8 @@ public function testUrlDecode()
'%e7%ab%a0%e5%ad%90%e6%80%a1' => '章子怡',
'Fran%c3%a7ois Truffaut' => 'François Truffaut',
'%e1%83%a1%e1%83%90%e1%83%a5%e1%83%90%e1%83%a0%e1%83%97%e1%83%95%e1%83%94%e1%83%9a%e1%83%9d' => 'საქართველო',
'%25e1%2583%25a1%25e1%2583%2590%25e1%2583%25a5%25e1%2583%2590%25e1%2583%25a0%25e1%2583%2597%25e1%2583%2595%25e1%2583%2594%25e1%2583%259a%25e1%2583%259d' => 'საქართველო',
'%2525e1%252583%2525a1%2525e1%252583%252590%2525e1%252583%2525a5%2525e1%252583%252590%2525e1%252583%2525a0%2525e1%252583%252597%2525e1%252583%252595%2525e1%252583%252594%2525e1%252583%25259a%2525e1%252583%25259d' => 'საქართველო',
'Bj%c3%b6rk Gu%c3%b0mundsd%c3%b3ttir' => 'Björk Guðmundsdóttir',
'%e5%ae%ae%e5%b4%8e%e3%80%80%e9%a7%bf' => '宮崎 駿',
'%u7AE0%u5B50%u6021' => '章子怡',
Expand Down

0 comments on commit 2653d6f

Please sign in to comment.