rfc:cyclic-replace

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:cyclic-replace [2015/01/09 13:54] francoisrfc:cyclic-replace [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ====== Add cyclic string replacements ====== ====== Add cyclic string replacements ======
-  * Version: 1.0 +  * Version: 1.7 
-  * Date: 2015-01-09+  * Creation date: 2015-01-05 
 +  * Last modification date : 2015-04-02
   * Author: François Laupretre, francois@tekwire.net   * Author: François Laupretre, francois@tekwire.net
-  * Status: Under discussion+  * Status: Abandoned
   * First Published at: http://wiki.php.net/rfc/cyclic-replace   * First Published at: http://wiki.php.net/rfc/cyclic-replace
  
Line 17: Line 18:
 was recently revived, refined, and improved by the internals community. was recently revived, refined, and improved by the internals community.
  
-In the current str_[i]replace() implementation, the case (string search and array replace)+In the current str_[i]replace() implementation, the case (string search array replace)
 is supported but quite useless as, in this case, the replace array is converted to the 'Array' is supported but quite useless as, in this case, the replace array is converted to the 'Array'
 string and, then, string/string conversion is performed. string and, then, string/string conversion is performed.
Line 23: Line 24:
 This RFC proposes that, in this case, the first occurence of search is replaced with This RFC proposes that, in this case, the first occurence of search is replaced with
 the first element of the replace array, the second occurence with the second the first element of the replace array, the second occurence with the second
-element, and so on. When we arrive at the end of the replace array, several +element, and so on. When we arrive at the end of the replace array, it loops 
-behaviors can be chosen (loop, repeat last element, etc... see the +to the first element, and so on.
-//options// parameter below).+
  
-This is what we name 'cyclic replace'. Purists will note that it is really 'cyclic' +Note that replacements are done in array order. Key values are ignored in replace arrays.
-only when we set the option to loop in the replace array but I didn't find a better name. +
- +
-Note that replacements are done in array order. Key values are ignored.+
  
 So, features brought by this RFC to str_[i]replace() are : So, features brought by this RFC to str_[i]replace() are :
Line 40: Line 37:
 be a string or an array. If it is a string, we have the usual string/string be a string or an array. If it is a string, we have the usual string/string
 behavior. If it is an array, cyclic replacement is performed. behavior. If it is an array, cyclic replacement is performed.
-So, the 'array search' case can be seen as an implicit loop around the+So, the 'array search' case is an implicit loop around the
 'string search' case, providing exactly the same features. 'string search' case, providing exactly the same features.
-As this is performed using recursion, search can contain any level of imbricated 
-arrays, provided replace provides the corresponding structure. 
  
-Note that, in case of multi-level arrays, replace array can be less deep than +Other feature : Arbitrarily nested subject arrays are now supportedreturning exactly the same 
-search array as, as soon as we find a non-array replace value, this value is +array structure and preserving keys. Only values are replaced.
-used as replacement string for the whole corresponding search subtree. The opposite +
-case is very different : If the replace array tree is deeper than the search tree by more +
-than 1 level, array to string conversion will occur (with E_NOTICE and the +
-wonderful 'Array' result).+
  
 Empty replace arrays are considered as unexpected. When one is provided, an Empty replace arrays are considered as unexpected. When one is provided, an
Line 58: Line 49:
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
- 
-==== C API ==== 
- 
-php_char_to_str_ex() and php_str_to_str_ex() (defined in ext/standard/php_string.h) 
-take an additional //options// 
-argument. This argument is not used at the moment but, this allows their API 
-to remain compatible with the new new php_str_to_array_ex() function. 
- 
-Note that the API for php_char_to_str() and php_str_to_str() is not modified. 
  
 ==== PHP API ==== ==== PHP API ====
Line 75: Line 57:
  
 Now, this combination of argument types causes the search string to be replaced Now, this combination of argument types causes the search string to be replaced
-with elements from the replace array. So, the behavior is different.+with elements from the replace array.
  
-The BC break is minimal here as I hope very few people ever relied on a replace +Different behavior too each time we meet an empty array as replace value. 
-array being converted to 'Array', especially with an E_NOTICE. +Previously, as seen above, an E_NOTICE
- +
-Different behavior too when replace is an empty array. Previously, as seen above, an E_NOTICE+
 was raised and the array was converted to 'Array'. Now, an E_WARNING was raised and the array was converted to 'Array'. Now, an E_WARNING
-is raised and the subject is returned unchanged. Minimal break too as giving +is raised and the subject is returned unchanged. 
-an array as replace arg, while supported (with E_NOTICE), had no real sense + 
-(even documentation states that).+The support of arbitrarily-nested subject brings the same 
 +kind of BC because previous implementations supported one array level only. 
 + 
 +All these BC breaks are similar and deal with array to string conversions in 
 +previous implementations. These can be considered as very low impact because 
 +relying on an array-to-string conversion (with E_NOTICE) when calling these 
 +functions, while theoritically supported, is very improbable.
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
Line 97: Line 83:
 ==== To Existing Extensions ==== ==== To Existing Extensions ====
  
-Extensions using one the C functions with a modified API need to be adapted (add +None
-a 0 argument to each call).+
  
 ==== To Opcache ==== ==== To Opcache ====
Line 106: Line 91:
 ==== New Constants ==== ==== New Constants ====
  
-=== C constants === +None
- +
-These new C constants are defined in ext/standard/php_strings.h : +
- +
- * PHP_STR_ARRAY_REPLACE_STOP  +
- * PHP_STR_ARRAY_REPLACE_FIRST  +
- * PHP_STR_ARRAY_REPLACE_LAST  +
- * PHP_STR_ARRAY_REPLACE_LOOP  +
- * PHP_STR_ARRAY_REPLACE_EMPTY  +
- * PHP_STR_ARRAY_REPLACE_MASK  +
- * PHP_STR_ARRAY_REPLACE_MAX  +
- * PHP_REPLACE_MASK  +
- +
-=== PHP constants === +
- +
-New constants are defined to allow controlling how the replacements are +
-done after a replace array is exhausted (when there are more occurences +
-of search in the subject than the number of elements in the replace array). These constants +
-are exclusive (they cannot be combined) : +
- +
- * STR_REPLACE_STOP : Stop replacements (up to count(replace) occurences of needle can be replaced) +
- * STR_REPLACE_FIRST : Remaining occurences are replaced with the first element of the replace array. +
- * STR_REPLACE_LAST : Remaining occurences are replaced with the last element of the replace array. +
- * STR_REPLACE_LOOP : Loop and restart replacements with the first element of the replace array. Looping occurs as many times as needed. +
- * STR_REPLACE_EMPTY : Remaining occurences are replaced with an empty string.+
  
 ==== API changes ==== ==== API changes ====
- 
-=== PHP API === 
- 
-An additional optional argument, named //options// in the documentation, is added 
-at the end of the argument list for str_replace() and str_ireplace(). 
- 
-If set, its value must be one of the STR_REPLACE_xxx constants defined above. 
- 
-When not set, the default value is STR_REPLACE_STOP. 
  
 === C API === === C API ===
Line 153: Line 105:
  
 ===== Unaffected PHP Functionality ===== ===== Unaffected PHP Functionality =====
- 
-The C API for php_char_to_str() and php_str_to_str() is left unchanged. 
  
 ===== Future Scope ===== ===== Future Scope =====
  
-Propose the addition of a similar feature to preg_replace() and preg_filter().+Proposed: 
 + 
 + * Add (search=null, replace=array) syntax. Would take search from array_keys(replace). An array of (search => replace) elements would be, IMO, more intuitive way to specify multiple replacements. 
 + 
 + * Add similar features (cyclic replacement and multi-level array recursion) to preg_replace() and preg_filter().
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
  
-Not sure of required majority for this feature. Probably 2/3.+Required majority : 50%+1.
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
  
-Pull request against current PHP7 branch : [[https://github.com/php/php-src/pull/980]]+Pull request against PHP7 branch (work in progress) : [[https://github.com/php/php-src/pull/980]]
  
-When implementation will be complete, this PR is intended to be the final patch. 
  
 ===== Implementation ===== ===== Implementation =====
Line 179: Line 132:
 ===== References ===== ===== References =====
  
-Feature request [[https://bugs.php.net/bug.php?id=38685]]+Feature Requests :
  
-A similar feature request : [[https://bugs.php.net/bug.php?id=62315]]+  * [[https://bugs.php.net/bug.php?id=38685]] 
 +  * [[https://bugs.php.net/bug.php?id=62315]] 
 +  * [[https://bugs.php.net/bug.php?id=14523]]
  
-PR : [[https://github.com/php/php-src/pull/980]]+Pull Request : 
 + 
 +  * [[https://github.com/php/php-src/pull/980]]
  
 ===== Rejected Features ===== ===== Rejected Features =====
  
-(Keep this updated with features that were discussed on the mail lists)+Support for arbitrarily-nested search/replace arrays is abandoned. 
 +Additional 'options' argument to str_replace() is abandoned. 
  
rfc/cyclic-replace.1420811690.txt.gz · Last modified: 2017/09/22 13:28 (external edit)