# [RFC]strn(case)cmp supporting negative length as third parameter

- Thread: https://externals.io/message/54226
- Original archive: https://news-web.php.net/php.internals/54226
- Started by Laruence on 2011-07-28T04:58:01+00:00
- Messages: 7
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #54226 — Laruence — 2011-07-28T04:58:01+00:00

URL: https://externals.io/message/54226#54226

Hi:

  strn(case)cmp dosen't  support a negative length as its third
paramter,  while substr dose.

 I wrote a patch to make strn(case)cmp supporting negative length,
make following script works as expect:

    <?php
   if (strncmp("prefix_num", "num", -3) === 0) {
        echo "they have same suffix\n";
   }
  ?>

 here is the rfc: https://wiki.php.net/rfc/strncmpnegativelen

 any question? plz worte me back.

 thanks

-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

## #54229 — Derick Rethans — 2011-07-28T10:59:55+00:00

URL: https://externals.io/message/54226#54229 · In reply to: #54226

> Hi:
> 
>   strn(case)cmp dosen't  support a negative length as its third
> paramter,  while substr dose.
> 
>  I wrote a patch to make strn(case)cmp supporting negative length,
> make following script works as expect:
> 
>     <?php
>    if (strncmp("prefix_num", "num", -3) === 0) {
>         echo "they have same suffix\n";
>    }
>   ?>
> 
>  here is the rfc: https://wiki.php.net/rfc/strncmpnegativelen

- You're changing binary API of some API functions, so this can't go 
  into PHP 5.3:

	-ZEND_API int zend_binary_strncmp(const char *s1, uint len1, const char *s2, uint len2, uint length);
	+ZEND_API int zend_binary_strncmp(const char *s1, uint len1, const char *s2, uint len2, int length);

cheers,
Derick

-- 
http://derickrethans.nl | http://xdebug.org
Like Xdebug? Consider a donation: http://xdebug.org/donate.php
twitter: @derickr and @xdebug

---

## #54230 — Laruence — 2011-07-28T11:32:46+00:00

URL: https://externals.io/message/54226#54230 · In reply to: #54229

Hi:

why?

I think this change would not cause any other side-effects.

and if you are worry about INT_MAX,  I think it's ok to declare with
long(in fact, I already did so).

thanks

2011/7/28 Derick Rethans <derick@php.net>:
> 
>
> > […]
>
> - You're changing binary API of some API functions, so this can't go
>  into PHP 5.3:
>
>        -ZEND_API int zend_binary_strncmp(const char *s1, uint len1, const char *s2, uint len2, uint length);
>        +ZEND_API int zend_binary_strncmp(const char *s1, uint len1, const char *s2, uint len2, int length);
>
> cheers,
> Derick
>
> --
> http://derickrethans.nl | http://xdebug.org
> Like Xdebug? Consider a donation: http://xdebug.org/donate.php
> twitter: @derickr and @xdebug
>



-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

## #54231 — Pierre Joye — 2011-07-28T11:41:05+00:00

URL: https://externals.io/message/54226#54231 · In reply to: #54230

the signature is not the same anymore, some extension may not build
anymore (depending on the compiler used and its flag).

Cheers,


> Hi:
>
> why?
>
> I think this change would not cause any other side-effects.
>
> and if you are worry about INT_MAX,  I think it's ok to declare with
> long(in fact, I already did so).
>
> thanks
>
> 2011/7/28 Derick Rethans <derick@php.net>:
> > […]
>
>
>
> --
> Laruence  Xinchen Hui
> http://www.laruence.com/
>
> --
> 
> 
>
>



-- 
Pierre

@pierrejoye | http://blog.thepimp.net | http://www.libgd.org

---

## #54232 — Laruence — 2011-07-28T12:25:07+00:00

URL: https://externals.io/message/54226#54232 · In reply to: #54231

Hi:
   As Derick and Pierre  said(in IRC),  I change the patch base from
PHP 5.3 to PHP5.4,

  and avoid changing ZEND_API declaration.

  RFC updated

  new patch : http://www.laruence.com/php-dev/php-5.4-trunk-strncmp-supproting-negative-len.patch

  thanks


2011/7/28 Pierre Joye <pierre.php@gmail.com>:
> the signature is not the same anymore, some extension may not build
> anymore (depending on the compiler used and its flag).
>
> Cheers,
>
> 
> > […]
>
>
>
> --
> Pierre
>
> @pierrejoye | http://blog.thepimp.net | http://www.libgd.org
>



-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

## #54234 — keisial@gmail.com — 2011-07-28T18:20:34+00:00

URL: https://externals.io/message/54226#54234 · In reply to: #54226

Laruence wrote:
> Hi:
>
>    strn(case)cmp dosen't  support a negative length as its third
> paramter,  while substr dose.

>   here is the rfc: https://wiki.php.net/rfc/strncmpnegativelen
>
>   any question? plz worte me back.
>
>   thanks

What do you exactly mean by "in the reverse order" in
> if the abs of the negative length is greater than any strlen of the 
> first two parameters, then strn(case)cmp will work as strncmp in the 
> reverse order with the abs value: 

Looking at
> var_dump  <http://www.php.net/var_dump>(strncmp  <http://www.php.net/strncmp>("prefix_num",  "num",  -10));
>    //outpu: int(7)
it seems to mean that it would be equivalent to
> var_dump(strncmp("mun_xiferp", "mun", 10));

However, if I'm reading the patch correctly, it would
be taking the smaller string. Ie. it would be equivalent
to

> var_dump  <http://www.php.net/var_dump>(strncmp  <http://www.php.net/strncmp>("prefix_num",  "num",  -3));

Seems an implementation bug.

---

## #54242 — Laruence — 2011-07-29T01:08:32+00:00

URL: https://externals.io/message/54226#54242 · In reply to: #54234

Hi:
  first of all, yes , you are right about the reverse order thing:

 and then, they are not equal:

 <?php
 echo "strncmp:\n";
   var_dump(strncmp("laruence", "nce", -10));
   var_dump(strncmp("ecneural", "ecn", 10));

   var_dump(strncmp("ecneural", "ecn", 3));
   var_dump(strncmp("laruence", "nce", -3));
?>

will output:
  strncmp:
  int(5)
  int(5)
  int(0)
  int(0)


thanks

2011/7/29 Ángel González <keisial@gmail.com>:
> Laruence wrote:
> > […]
>
> > […]
>
> What do you exactly mean by "in the reverse order" in
> > […]
>
> Looking at
> > […]
>
> it seems to mean that it would be equivalent to
> > […]
>
> However, if I'm reading the patch correctly, it would
> be taking the smaller string. Ie. it would be equivalent
> to
>
> > […]
>
> Seems an implementation bug.
>
>



-- 
Laruence  Xinchen Hui
http://www.laruence.com/
