-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Support negative string offset #100
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
Conversation
@@ -1302,7 +1302,12 @@ static void zend_fetch_dimension_address_read(temp_variable *result, zval **cont | |||
INIT_PZVAL(ptr); | |||
Z_TYPE_P(ptr) = IS_STRING; | |||
|
|||
if (Z_LVAL_P(dim) < 0 || Z_STRLEN_P(container) <= Z_LVAL_P(dim)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't you make it cache the strlen result so it does not have to compute the string length twice? I don't know how strlen is computed, but I just figured it would be good practice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Z_STRLEN_P is not a function but a macro to the string length value stored within the variable structure, so they's no unnecessary computation.
zend_operators.h:
#define Z_STRLEN(zval) (zval).value.str.len
...
#define Z_STRLEN_P(zval_p) Z_STRLEN(*zval_p)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I should have taken a greater look at the PHP source before asking that, sorry.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hey
…ng offset and updated tests
I think this deserve a brief rfc , could you write to internal ML? |
@laruence There already is a mail about this: http://markmail.org/thread/vtinqfym4i73dpul :) It's not exactly clear what the conclusion on this was. |
oh, sorry, didn't notice that. |
@Easen to gain Wiki access, just drop a mail to the PHP webmaster list (php-webmaster@lists.php.net, more on that on https://wiki.php.net/start?do=register). I would love to see this in, but a short RFC is required. Two other things:
|
@lstrojny Thanks for your response. I've submitted my request for write access, hopefully that will approved soon so I can make a start on writing a RFC for this. The points you have raised are quite interesting, IMHO this negative offset should only apply to lists and strings, not associated arrays. But, I do see how this can become confusing when debugging a complex application so this might not be the correct approach. Perhaps using ()'s could be used to highlight the negative offset in associated arrays. $a = array(-1 => "foo", -2 => "bar"); $a[(-1)] === 'bar'; |
@Easen yep, good idea. Looking forward to your RFC. Ping me, if you have troubles with the Wiki account. |
Comment on behalf of lstrojny at php.net: Closing until we have an RFC. |
Hi,
I have updated PHP to support negative string offsets, the change is very small and it's benefits are quite large (IMO).
As an alternative to:
$var = 'abc';
echo $var[strlen($var) - 1];
Can be summed up with:
echo $var[-1];
I will send a formal request to the mail list in the next few minutes.
Thanks.