rfc:negative-string-offsets

This is an old revision of the document!


PHP RFC: Generalize support of negative string offsets

Introduction

In most PHP functions, providing a negative value as string offset means 'n positions counted backwards from the end of the string'. This mechanism is widely used but, unfortunately, these negative values are not supported everywhere this would make sense. So, as PHP developers cannot easily know whether a given string functions accepts negative values or not, they regularly need to refer to the documentation. If it does not, they need to insert a call to the substr() function, making their code less readable and slower.

An obvious example is strrpos() accepting negative offsets, while strpos() does not. The same with substr_count() rejecting negative offset or length, when substr() accepts them.

Proposal

This RFC proposes to generalize support for negative string offsets everywhere it makes sense.

In accordance with the existing behavior, a negative offset is a position counted backwards from the end of the string.

In the same spirit, the RFC also adds support for negative length arguments where it makes sense. A negative length (-x) means 'up to the position x counted backwards from the end of the string'.

The reference behavior for negative offset and length is the substr() function.

The main objective of this RFC is to improve the overall consistency of the language.

Feature requests solved by this RFC

Detailed changes

In the language

String access to individual characters using a '{}' or '[]' will be extended to negative values.

Examples :

$str='abcdef';
var_dump($str[-2]); // => string(1) "e"
 
$str{-3}='.';
var_dump($str);		// => string(6) "abc.ef"
 
var_dump(isset($str{-4}));	// => bool(true)
 
var_dump(isset($str{-10}));	// => bool(false)

In functions

Function Add support for
strpos Negative offset
stripos Negative offset
substr_count Negative 'offset' and 'length' (same behavior as substr())
grapheme_strpos Negative offset
grapheme_stripos Negative offset
grapheme_extract Negative offset
iconv_strpos Negative offset
file_get_contents Negative offset (based on seek(SEEK_END), reserved to seekable streams)
mb_strimwidth Negative 'start' and 'width'
mb_ereg_search_setpos Negative 'position' when search string is defined
mb_strpos Negative offset
mb_stripos Negative offset

In the documentation

As suggested during the discussion, authorizing the same '[]' syntax for strings and arrays is quite ambiguous, especially with the addition of negative string offsets, which won't be supported on arrays.

So, the documentation on www.php.net will be modified to recommend using the '{}' syntax when dealing with strings. This will open the way for a potential future deprecation of the '[]' syntax on strings.

Notes

  • Nothing done for iconv_strrpos() because function does not accept an 'offset' arg (inconsistent with every other xxx_strrpos() functions but argument cannot be added without breaking BC).
  • file_get_contents() : 'maxlen' argument cannot support negative values because of stream filters.

Backward Incompatible Changes

This RFC extends the range of valid values. In most cases, negative values raise a warning message and offset is considered as zero. The new behavior considers such values as valid (as long as they don't exceed the string length).

While not negligible, I consider these BC breaks as minor because, everywhere the behavior is modified, negative values were considered as invalid and raised an error message. So, we are just suppressing error cases.

Proposed PHP Version(s)

7.1

RFC Impact

To SAPIs

None

To Existing Extensions

None

To Opcache

None

Open Issues

  • Update documentation (waiting for RFC approval)

Unaffected PHP Functionality

mbstring functions remain compatible with their ASCII counterpart, relative to the mbstring.func_overload ini setting.

Future Scope

None

Proposed Voting Choices

As this RFC adds support for negative string offsets in the language, it requires a 2/3 majority.

Patches and Tests

Pull Request : https://github.com/php/php-src/pull/1431 (final patch)

Implementation

After the project is implemented, this section should contain

  1. the version(s) it was merged to
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature

References

None

Rejected Features

Keep this updated with features that were discussed on the mail lists.

rfc/negative-string-offsets.1454346790.txt.gz · Last modified: 2017/09/22 13:28 (external edit)