rfc:negative-string-offsets

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:negative-string-offsets [2016/01/23 15:24] francoisrfc:negative-string-offsets [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ====== PHP RFC: Generalize support of negative string offsets ====== ====== PHP RFC: Generalize support of negative string offsets ======
-  * Version: 1.0 +  * Version: 1.3 
-  * Date: 2016-01-23+  * Date: 2016-02-18
   * Author: François Laupretre <francois@php.net>   * Author: François Laupretre <francois@php.net>
-  * Status: Under discussion+  * Status: Implemented (in PHP 7.1)
   * First Published at: http://wiki.php.net/rfc/negative-string-offsets   * First Published at: http://wiki.php.net/rfc/negative-string-offsets
  
 ===== Introduction ===== ===== Introduction =====
  
-In most PHP functions, providing a negative value as string offset means 'count //n// positions from the end of the string'+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 it would make sense. +This mechanism is widely used but, unfortunately, these negative values are not supported everywhere this would make sense. 
-So, developers need to refer to the documentation to know whether a function supports negative offset values or not. +So, as PHP developers cannot easily know whether a given string functions accepts negative values or not
-If it does not, they need to insert a call to substr(), making their code less readable and slower.+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. An obvious example is strrpos() accepting negative offsets, while strpos() does not.
Line 19: Line 20:
  
 This RFC proposes to generalize support for negative string offsets everywhere it makes sense. This RFC proposes to generalize support for negative string offsets everywhere it makes sense.
-In the same spirit, it also adds support for negative lengths where it is missing. 
  
-The main objective is to impprove the overall consistency of the language.+In accordance with the existing behavior, a negative offset is a position counted backwards from the end of the string.
  
-====== Feature requests solved by this RFC ======+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'.
  
-  * [[https://bugs.php.net/bug.php?id=50649|50649]] +The reference behavior for negative offset and length is the [[http://php.net/manual/en/function.substr.php|substr()]] function.
-  * [[https://bugs.php.net/bug.php?id=36524|36524]]+
  
-====== Detailed changes ======+The main objective of this RFC is to improve the overall consistency of the language.
  
-  * Negative string offsets in read mode ($xx $str[-2] or $str{-2});+==== Feature requests solved by this RFC ====
  
-  * Negative string offsets in assignments ($str{-2} 'x');+  * https://bugs.php.net/bug.php?id=50649 
 +  * https://bugs.php.net/bug.php?id=36524
  
-  * Negative string offsets in isset()/empty() (isset($str{-5}))+===== Detailed changes =====
  
-  * strpos() : negative offset+==== In the language ====
  
-  * stripos() : negative offset+String access to individual characters using a '{}' or '[]' will be extended to negative values.
  
-  * substr_count()negative 'offset' and 'length' (same behavior as substr()).+Examples :
  
-  * grapheme_strpos() : negative offset+<code php> 
 +$str='abcdef'; 
 +var_dump($str[-2]); // => string(1) "e"
  
-  * grapheme_stripos(), grapheme_extract() : negative offset+$str{-3}='.'; 
 +var_dump($str); // => string(6"abc.ef"
  
-  * grapheme_extract() : negative offset+var_dump(isset($str{-4})); // => bool(true)
  
-  * iconv_strpos() : negative offset+var_dump(isset($str{-10})); // => bool(false) 
 +</code>
  
-  * file_get_contents(): negative offset (based on seek(SEEK_END), reserved to seekable streams)+==== In built-in functions ====
  
-  * mb_strimwidth() : negative 'start' and 'width'+^ 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 |
  
-  * mb_ereg_search_setpos(): Accept negative 'position' when search string is defined. +==== Notes ====
- +
-  * mb_strpos(): negative offset +
- +
-  * mb_stripos(): negative offset +
- +
-====== 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).   * 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).
Line 69: Line 80:
  
 This RFC extends the range of valid values. This RFC extends the range of valid values.
-In most cases, negative values raise a warning message and offset are considered as null.+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). The new behavior considers such values as valid (as long as they don't exceed the string length).
  
-I consider these BC breaks as minor because, everywhere the behavior is modified, negative values were considered as invalid. +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 replacing error cases only.+So, we are just suppressing error cases.
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
Line 94: Line 105:
 ===== Open Issues ===== ===== Open Issues =====
  
-  * Update documentation (waiting for RFC approval)+To do (waiting for RFC approval) 
 + 
 +  * Update documentation 
 +  * Update language specifications
  
 ===== Unaffected PHP Functionality ===== ===== Unaffected PHP Functionality =====
Line 102: Line 116:
 ===== Future Scope ===== ===== Future Scope =====
  
-None+==== Recommend using '{}' vs '[]' for string offsets ==== 
 + 
 +It was suggested during the discussion that, since array access and string 
 +offsets are very different operations, the official documentation should 
 +recommend using the '{}' syntax for string offsets, instead of the ambiguous 
 +'[]' syntax (and potentially deprecate '[]' for string offsets in the future). 
 + 
 +On the opposite side, it was also suggested that array access and string offsets 
 +are so closely-related concepts that we should recommend using '[]' in both 
 +cases and disable the alternate '{}' syntax for string offsets ! 
 + 
 +So, as the subject is controversial and very tangential to the subject of this RFC, 
 +it will be left for a future RFC.
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
  
 As this RFC adds support for negative string offsets in the language, it requires a 2/3 majority. As this RFC adds support for negative string offsets in the language, it requires a 2/3 majority.
 +
 +<doodle title="Generalize support of negative string offsets" auth="francois" voteType="single" closed="true">
 +   * Yes
 +   * No
 +</doodle>
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
rfc/negative-string-offsets.1453562689.txt.gz · Last modified: 2017/09/22 13:28 (external edit)