Skip to content

Improve bytes conversion method #7456

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

Merged
merged 1 commit into from
Mar 25, 2013
Merged

Improve bytes conversion method #7456

merged 1 commit into from
Mar 25, 2013

Conversation

jfsimon
Copy link
Contributor

@jfsimon jfsimon commented Mar 23, 2013

This PR improves bytes conversion regex method introduced in #7413 (thanks to @vicb's comments).

  • Adds support of + prefix.
  • Adds support of blank chars between +, number and unit.
  • Adds support of octal/hexa bases.

Notice that this can not be unit tested for ServerParams and UploadedFile classes because ini_set() function does not work with post_max_size and upload_max_filesize settings.

For information, this convertion is located in 3 classes:

  • Symfony\Component\Form\Extension\Validator\Util\ServerParams
  • Symfony\Component\HttpFoundation\File\UploadedFile
  • Symfony\Component\HttpKernel\DataCollector\MemoryDataCollector
Q A
Bug fix? yes
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #7413

@vicb
Copy link
Contributor

vicb commented Mar 23, 2013

@jfsimon, you probably mis-read my comment, the code needs to be reverted.

@jfsimon
Copy link
Contributor Author

jfsimon commented Mar 23, 2013

@vicb What's wrong with this solution? I need to understand.

@vicb
Copy link
Contributor

vicb commented Mar 23, 2013

  • What was wrong with the previous (working) solution ?
  • The current look of your code.

I don't think anybody will ever use a form to upload TB of data, but anyway "t" is not supported (I think the sames goes for "b")

@jfsimon
Copy link
Contributor Author

jfsimon commented Mar 23, 2013

Okay. What about octal/hexadecimal values? Don't they need to be supported?

@vicb
Copy link
Contributor

vicb commented Mar 23, 2013

Good point. You should ask @fabpot.

@fabpot
Copy link
Member

fabpot commented Mar 23, 2013

I'm the one who asked @jfsimon to use this new way. Before the last comment of @jfsimon, I was inclined to agree with @vicb and revert the change. But @jfsimon had a good point, so I think the new way is indeed better as it covers more cases.

if (preg_match('#^\s*\+?\s*([0x]*)([1-9][0-9]*)\s*([bkmgt]?)#i', $iniMax, $match)) {
$shift = array('' => 0, 'b' => 0, 'k' => 10, 'm' => 20, 'g' => 30, 't' => 40);
$bases = array('' => 10, '0' => 8, '0x' => 16);
$iniMax = (base_convert($match[2], $bases[$match[1]], 10) * (1 << $shift[strtolower($match[3])]));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strtoloweris not needed as the value is already normalized here (upercase)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ìntval might be better than base_convert

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didnt knew intval, seems better indeed.

@fabpot
Copy link
Member

fabpot commented Mar 25, 2013

Is it ready to be merged?

@jfsimon
Copy link
Contributor Author

jfsimon commented Mar 25, 2013

@fabpot Yes.

fabpot added a commit that referenced this pull request Mar 25, 2013
This PR was merged into the master branch.

Discussion
----------

Improve bytes conversion method

This PR improves bytes conversion `regex` method introduced in #7413 (thanks to @vicb's comments).

* Adds support of `+` prefix.
* Adds support of blank chars between `+`, number and unit.
* Adds support of octal/hexa bases.

Notice that this can not be unit tested for `ServerParams` and `UploadedFile` classes because `ini_set()` function does not work with `post_max_size` and `upload_max_filesize` settings.

For information, this convertion is located in 3 classes:
* `Symfony\Component\Form\Extension\Validator\Util\ServerParams`
* `Symfony\Component\HttpFoundation\File\UploadedFile`
* `Symfony\Component\HttpKernel\DataCollector\MemoryDataCollector`

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #7413

Commits
-------

21291ca improved bytes conversion method
@fabpot fabpot merged commit 21291ca into symfony:master Mar 25, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants