Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4047bf2

Browse files
committed
merged branch jfsimon/preg-bytes-conversion (PR #7413)
This PR was merged into the master branch. Commits ------- 3674c22 changed bytes conversion method Discussion ---------- Changed bytes conversion method | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes **The old way:** ```php switch (strtolower(substr($memory, -1))) { case 'g': $memory *= 1024; case 'm': $memory *= 1024; case 'k': $memory *= 1024; } ``` **The new way:** ```php if (preg_match('#^(\d+)([bkmgt])#i', $memory, $match)) { $shift = array('b' => 0, 'k' => 10, 'm' => 20, 'g' => 30, 't' => 40); $memory = ($match[1] * (1 << $shift[strtolower($match[2])])); } ``` --------------------------------------------------------------------------- by bendavies at 2013-03-18T16:27:52Z pretty unreadable, no? --------------------------------------------------------------------------- by benja-M-1 at 2013-03-18T16:29:25Z I agree, I would not like to have to debug it. --------------------------------------------------------------------------- by pborreli at 2013-03-18T16:31:43Z just for my culture, what does : `1 << $var` ? --------------------------------------------------------------------------- by bendavies at 2013-03-18T16:33:23Z @pborreli it's a left shift http://php.net/manual/en/language.operators.bitwise.php --------------------------------------------------------------------------- by jfsimon at 2013-03-18T16:47:15Z @bendavies @benja-M-1 it's concise and easily recognised (if you understood it the first time). FYI I didn't find it myself... pretty clever isn't it? --------------------------------------------------------------------------- by benja-M-1 at 2013-03-18T16:50:53Z Clearly too much clever for me :) And what about moving this code in its own class to avoid the copy/paste? --------------------------------------------------------------------------- by jfsimon at 2013-03-18T16:52:51Z @benja-M-1 It would add a dependency to the components using it :( --------------------------------------------------------------------------- by bendavies at 2013-03-18T16:55:26Z @jfsimon clever indeed, but not necessarily better! --------------------------------------------------------------------------- by jfsimon at 2013-03-18T16:57:18Z @bendavies that's true. --------------------------------------------------------------------------- by Tobion at 2013-03-18T17:00:56Z There are other places where it could be used too (e.g. FileValidator). --------------------------------------------------------------------------- by bendavies at 2013-03-18T17:06:01Z on the other side of the argument, i *hate* the sneaky fall through on the switch statement. very confusing the first time you see it! --------------------------------------------------------------------------- by bendavies at 2013-03-18T17:19:42Z this method has already made it into symfony here: #7395 --------------------------------------------------------------------------- by jfsimon at 2013-03-19T08:16:19Z @Tobion I have some questions about the `FileValidator`: * Why is th `k` in lower case and the `M` in upper case? * Why is the size divided by 1000 and not 1024? --------------------------------------------------------------------------- by Tobion at 2013-03-19T08:30:23Z I was wondering the same. I guess this config (which is also displayed to users) uses the official metric prefixes (k = kilo, M = mega). So it's not about the computer terms where 1 KB = 1024 byte. --------------------------------------------------------------------------- by vicb at 2013-03-19T16:03:21Z kB =1000, kiB=1024. Imo regexps should be case insensitive and account for the "i". I am not in favor of the changes in this pr (the current way is also documented on php.net fwiw)
2 parents 84ff273 + 3674c22 commit 4047bf2

File tree

2 files changed

+7
-17
lines changed

2 files changed

+7
-17
lines changed

src/Symfony/Component/Form/Extension/Validator/Util/ServerParams.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,12 @@ public function getPostMaxSize()
2929
return null;
3030
}
3131

32-
$max = (int) $iniMax;
33-
34-
switch (substr($iniMax, -1)) {
35-
case 'G':
36-
$max *= 1024;
37-
case 'M':
38-
$max *= 1024;
39-
case 'K':
40-
$max *= 1024;
32+
if (preg_match('#^(\d+)([bkmgt])#i', $iniMax, $match)) {
33+
$shift = array('b' => 0, 'k' => 10, 'm' => 20, 'g' => 30, 't' => 40);
34+
$iniMax = ($match[1] * (1 << $shift[strtolower($match[2])]));
4135
}
4236

43-
return $max;
37+
return (int) $iniMax;
4438
}
4539

4640
/**

src/Symfony/Component/HttpFoundation/File/UploadedFile.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,9 @@ public static function getMaxFilesize()
235235
return PHP_INT_MAX;
236236
}
237237

238-
switch (strtolower(substr($max, -1))) {
239-
case 'g':
240-
$max *= 1024;
241-
case 'm':
242-
$max *= 1024;
243-
case 'k':
244-
$max *= 1024;
238+
if (preg_match('#^(\d+)([bkmgt])#i', $max, $match)) {
239+
$shift = array('b' => 0, 'k' => 10, 'm' => 20, 'g' => 30, 't' => 40);
240+
$max = ($match[1] * (1 << $shift[strtolower($match[2])]));
245241
}
246242

247243
return (integer) $max;

0 commit comments

Comments
 (0)