rfc:cyclic-replace

Differences

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

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
rfc:cyclic-replace [2015/01/08 23:46] – created francoisrfc:cyclic-replace [2015/01/10 19:36] francois
Line 1: Line 1:
 ====== Add cyclic string replacements ====== ====== Add cyclic string replacements ======
-  * Version: 1.0 +  * Version: 1.5 
-  * Date: 2015-01-08+  * Creation date: 2015-01-05 
 +  * Last modification date : 2015-01-10
   * Author: François Laupretre, francois@tekwire.net   * Author: François Laupretre, francois@tekwire.net
   * Status: Under discussion   * Status: Under discussion
Line 30: Line 31:
 only when we set the option to loop in the replace array but I didn't find a better name. only when we set the option to loop in the replace array but I didn't find a better name.
  
-Features brought by this RFC to str_[i]replace() :+Note that replacements are done in array order. Key values are ignored in replace arrays.
  
- * When search is a string and replace an arraycyclic replace is performed.+Sofeatures brought by this RFC to str_[i]replace() are :
  
-When search and replace are arrays, each element of the replace array can now 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. So, the 'array search' case is an implicit loop whose body provides exactly the same features as when search is received as a string.+When search is a string and replace an array, cyclic replace is performed. 
 +This is the 'first-level' case. 
 + 
 +When search and replace are arrays, each element of the replace array can 
 +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. 
 +So, the 'array search' case is an implicit loop around the 
 +'string search' case, providing exactly the same features
 + 
 +Arbitrarily nested subject arrays are now supported, returning exactly the same array structure and preserving keys. Only 
 +values are replaced.
  
 Empty replace arrays are considered as unexpected. When one is provided, an Empty replace arrays are considered as unexpected. When one is provided, an
-E_WARNING error is raised and the input subject is returned as-is.+E_WARNING error is raised and the input subject is returned as-is. If search is 
 +an array, this warning can be raised more than once during a single str_[i]replace() 
 +execution, as we raise it each time we meet an empty replace array.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 57: Line 70:
  
 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 
 +was raised and the array was converted to 'Array'. Nowan E_WARNING 
 +is raised and the subject is returned unchanged. 
 + 
 +The support of multi-level arrays as search, replace, and subject brings the same 
 +kind of BC because previous implementations supported one array level only. 
 +So, if provided with multi-level arrays, array to string conversion would have 
 +been performed. 
 + 
 +All these BC breaks are similar and deal with array to string conversions in 
 +previous implementations. I think we can consider them as very low impact because 
 +relying on an array-to-string conversion (with E_NOTICE) when calling these 
 +functions, while theoritically supported, can be considered as broken and very 
 +improbable.
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
Line 73: Line 99:
 ==== To Existing Extensions ==== ==== To Existing Extensions ====
  
-Extensions using one the C functions with a modified API need to be adapted (add +Extensions using one of the C functions with a modified API (see BC changes in C API above) 
-a 0 argument to each call).+need to be adapted (adding a 0 final argument to each call). Only two occurences of such calls exist in 
 +the whole php-src tree (outside of string.c).
  
 ==== To Opcache ==== ==== To Opcache ====
Line 92: Line 119:
  * PHP_STR_ARRAY_REPLACE_EMPTY   * PHP_STR_ARRAY_REPLACE_EMPTY
  * PHP_STR_ARRAY_REPLACE_MASK   * PHP_STR_ARRAY_REPLACE_MASK
 + * PHP_STR_ARRAY_REPLACE_MAX
 + * PHP_REPLACE_MASK
  
 === PHP constants === === PHP constants ===
Line 124: Line 153:
 ===== Open Issues ===== ===== Open Issues =====
  
-None+==== Support of arbitrarily-nested search array ==== 
 + 
 +As search array support is now performed using recursion, search can contain 
 +any level of nested arrays, provided replace provides the corresponding structure. 
 + 
 +Note that, in case of multi-level arrays, replace array can be less deep than 
 +search array as, as soon as we find a non-array replace value, this value is 
 +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). 
 + 
 +Recursion also extends the behavior of 'non-cyclic' replacements as the previous 
 +implementation supported only one-level arrays for search and replace arguments. 
 + 
 + This feature is currently under discussion. The trend is towards limiting the search array 
 + to 1 level (allow array of strings only) and, as a consequence, it would limit replace to 
 + one more level than search.
  
 ===== Unaffected PHP Functionality ===== ===== Unaffected PHP Functionality =====
Line 132: Line 178:
 ===== 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. Compatible with current trend to limit search array to 1 level. 
 + 
 + * 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 : probably 2/3.
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
Line 154: Line 204:
  
 Feature request : [[https://bugs.php.net/bug.php?id=38685]] Feature request : [[https://bugs.php.net/bug.php?id=38685]]
 +
 +A similar feature request : [[https://bugs.php.net/bug.php?id=62315]]
  
 PR : [[https://github.com/php/php-src/pull/980]] PR : [[https://github.com/php/php-src/pull/980]]
rfc/cyclic-replace.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1