changed bytes conversion method
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: https://github.com/symfony/symfony/pull/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)