Skip to content

Commit e9dd988

Browse files
committed
Merge pull request #33 from JanTvrdik/request_factory_nonchars_performance
RequestFactory: optimized UTF-8 validation performance
2 parents 88784b1 + f29070c commit e9dd988

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

src/Http/RequestFactory.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
class RequestFactory extends Nette\Object
2020
{
2121
/** @internal */
22-
const NONCHARS = '#[^\x09\x0A\x0D\x20-\x7E\xA0-\x{10FFFF}]#u';
22+
const CHARS = '#^[\x09\x0A\x0D\x20-\x7E\xA0-\x{10FFFF}]*+\z#u';
2323

2424
/** @var array */
2525
public $urlFilters = array(
@@ -147,7 +147,7 @@ public function createHttpRequest()
147147
$k = stripslashes($k);
148148
}
149149

150-
if (!$this->binary && is_string($k) && (preg_match(self::NONCHARS, $k) || preg_last_error())) {
150+
if (!$this->binary && is_string($k) && (!preg_match(self::CHARS, $k) || preg_last_error())) {
151151
// invalid key -> ignore
152152

153153
} elseif (is_array($v)) {
@@ -158,7 +158,7 @@ public function createHttpRequest()
158158
if ($gpc && !$useFilter) {
159159
$v = stripSlashes($v);
160160
}
161-
if (!$this->binary && (preg_match(self::NONCHARS, $v) || preg_last_error())) {
161+
if (!$this->binary && (!preg_match(self::CHARS, $v) || preg_last_error())) {
162162
$v = '';
163163
}
164164
$list[$key][$k] = $v;
@@ -174,7 +174,7 @@ public function createHttpRequest()
174174
$list = array();
175175
if (!empty($_FILES)) {
176176
foreach ($_FILES as $k => $v) {
177-
if (!$this->binary && is_string($k) && (preg_match(self::NONCHARS, $k) || preg_last_error())) {
177+
if (!$this->binary && is_string($k) && (!preg_match(self::CHARS, $k) || preg_last_error())) {
178178
continue;
179179
}
180180
$v['@'] = & $files[$k];
@@ -190,7 +190,7 @@ public function createHttpRequest()
190190
if ($gpc) {
191191
$v['name'] = stripSlashes($v['name']);
192192
}
193-
if (!$this->binary && (preg_match(self::NONCHARS, $v['name']) || preg_last_error())) {
193+
if (!$this->binary && (!preg_match(self::CHARS, $v['name']) || preg_last_error())) {
194194
$v['name'] = '';
195195
}
196196
if ($v['error'] !== UPLOAD_ERR_NO_FILE) {
@@ -200,7 +200,7 @@ public function createHttpRequest()
200200
}
201201

202202
foreach ($v['name'] as $k => $foo) {
203-
if (!$this->binary && is_string($k) && (preg_match(self::NONCHARS, $k) || preg_last_error())) {
203+
if (!$this->binary && is_string($k) && (!preg_match(self::CHARS, $k) || preg_last_error())) {
204204
continue;
205205
}
206206
$list[] = array(

0 commit comments

Comments
 (0)