Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unescape optimized #38

Merged
merged 2 commits into from
Dec 21, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/Http/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ public function __toString()


/**
* Similar to rawurldecode, but preserve reserved chars encoded.
* Similar to rawurldecode, but preserves reserved chars encoded.
* @param string to decode
* @param string reserved characters
* @return string
Expand All @@ -461,14 +461,14 @@ public static function unescape($s, $reserved = '%;/?:@&=+$,')
// reserved (@see RFC 2396) = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ","
// within a path segment, the characters "/", ";", "=", "?" are reserved
// within a query component, the characters ";", "/", "?", ":", "@", "&", "=", "+", ",", "$" are reserved.
preg_match_all('#(?<=%)[a-f0-9][a-f0-9]#i', $s, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
foreach (array_reverse($matches) as $match) {
$ch = chr(hexdec($match[0][0]));
if (strpos($reserved, $ch) === FALSE) {
$s = substr_replace($s, $ch, $match[0][1] - 1, 3);
}
if ($reserved !== '') {
$s = preg_replace_callback(
'#%(' . substr(chunk_split(bin2hex($reserved), 2, '|'), 0, -1) . ')#i',
function($m) { return '%25' . strtoupper($m[1]); },
$s
);
}
return $s;
return rawurldecode($s);
}

}
21 changes: 21 additions & 0 deletions tests/Http/Url.unescape.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* Test: Nette\Http\Url unescape.
*/

use Nette\Http\Url,
Tester\Assert;


require __DIR__ . '/../bootstrap.php';


Assert::same( 'foo + bar', Url::unescape('foo + bar') );
Assert::same( 'foo + bar', Url::unescape('foo + bar', '') );
Assert::same( 'foo', Url::unescape('%66%6F%6F', '') );
Assert::same( 'f%6F%6F', Url::unescape('%66%6F%6F', 'o') );
Assert::same( '%66oo', Url::unescape('%66%6F%6F', 'f') );
Assert::same( '%66%6F%6F', Url::unescape('%66%6F%6F', 'fo') );
Assert::same( '%66%6F%6F', Url::unescape('%66%6f%6f', 'fo') );
Assert::same( "%00\x01%02", Url::unescape('%00%01%02', "\x00\x02") );