Skip to content

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

Closed
wants to merge 4 commits into from
Closed

Conversation

Easen
Copy link
Contributor

@Easen Easen commented Jun 4, 2012

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.

@@ -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)) {
Copy link

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.

Copy link
Contributor Author

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)

Copy link

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.

Choose a reason for hiding this comment

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

hey

@laruence
Copy link
Member

I think this deserve a brief rfc , could you write to internal ML?

@nikic
Copy link
Member

nikic commented Aug 17, 2012

@laruence There already is a mail about this: http://markmail.org/thread/vtinqfym4i73dpul :) It's not exactly clear what the conclusion on this was.

@laruence
Copy link
Member

oh, sorry, didn't notice that.

@lstrojny
Copy link
Contributor

@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:

  • A test is missing for invalid negative offsets: $a = array(1 =&gt; "foo"); $[-2];
  • The RFC should discuss how to access an array like that: array(-1 => "foo", -2 => "bar")

@Easen
Copy link
Contributor Author

Easen commented Aug 28, 2012

@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';

@lstrojny
Copy link
Contributor

@Easen yep, good idea. Looking forward to your RFC. Ping me, if you have troubles with the Wiki account.

@php-pulls
Copy link

Comment on behalf of lstrojny at php.net:

Closing until we have an RFC.

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.

7 participants