Doc #73913 [Asn->Csd]: broken strrpos with negative offset
| From: | girgias@php.net | Date: | Fri, 24 May 2019 16:43:01 +0000 |
| Subject: | Doc #73913 [Asn->Csd]: broken strrpos with negative offset | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-16680@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=73913&edit=1
ID: 73913
Updated by: girgias@php.net
Reported by: proartex at mail dot ru
Summary: broken strrpos with negative offset
-Status: Assigned
+Status: Closed
Type: Documentation Problem
Package: Strings related
Operating System: macOS Sierra 10.12.1
PHP Version: 5.6.29
Assigned To: girgias
Block user comment: N
Private report: N
Previous Comments:
------------------------------------------------------------------------
[2019-04-23 00:51:33] girgias@php.net
Automatic comment from SVN on behalf of girgias
Revision: http://svn.php.net/viewvc/?view=revision&revision=347318
Log: Fix Doc Bug #73913
A better description of how the offset parameter works (especially with negative offsets)
Added some examples to illustrate how offset affects the function.
Maybe an example that compares this to strpos should be there too?
------
Inspired by anonymous 96388 (php.florianberberich@outlook.com)
------------------------------------------------------------------------
[2018-10-01 18:18:58] cmb@php.net
> Java implementation has no negative fromIndex for "public int
> lastIndexOf(String str, int fromIndex)".
If the offset is negative, strrpos($haystack, $needle, $offset)
behaves like String.lastIndexOf(needle, -offset).
> It works the way we expect
Then you have to adjust your expectations. :) Or, your code.
For instance:
strrpos('works_like_a_charm', 'charm', -3 - strlen('charm')); //
false
------------------------------------------------------------------------
[2017-01-13 16:28:01] dima at virtuman dot com
Java implementation has no negative fromIndex for "public int lastIndexOf(String str, int
fromIndex)". Java implementation returns "-1" for any negative fromIndex.
There is an examples from python:
"
>>> 'aaaa'.rfind('a',0, -2)
1
>>> 'aaaa'.rfind('a', 0, -1)
2
>>> '/documents/show/5474'.rfind('/', 0, -20)
-1
>>> 'works_like_a_charm'.rfind('charm', 0, -3)
-1
>>> 'works_like_a_charm'.rfind('charm', 0, -5)
-1
"
It works the way we expect
------------------------------------------------------------------------
[2017-01-13 15:48:10] heiglandreas@php.net
For more infos have a look at https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#lastIndexOf(java.lang.String,%20int)
------------------------------------------------------------------------
[2017-01-13 15:46:33] heiglandreas@php.net
The current implementation is mathematically correct and is the same as f.e. the implementation in
Java and other programming languages. The docs are misleading here.
The negative offset specifies the last offset in the string the needle can start. Therefore the
results are correct.
Though the documentation of that feature needs some work to explicitly state that and make that more
clear.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=73913
--
Edit this bug report at https://bugs.php.net/bug.php?id=73913&edit=1