Skip to content

Patch add str begins and ends #2049

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
112 commits
Select commit Hold shift + click to select a range
ab3b8b1
Added str_begins and str_ends
wkhudgins92 Jul 26, 2016
f89d8ed
Added support for case insensitive searches
wkhudgins92 Jul 27, 2016
19d3d86
changed capitalization of TRUE
wkhudgins92 Jul 27, 2016
bbced23
Passes test case now
wkhudgins92 Jul 29, 2016
764cb91
Added testing for str_ends
wkhudgins92 Jul 29, 2016
50524df
Passes tests cases
wkhudgins92 Jul 31, 2016
c00d31f
Fixed build error
wkhudgins92 Aug 2, 2016
0283a08
Split functions based on case sensitivity
wkhudgins92 Aug 12, 2016
73be337
Renamed str_begins to str_begins
wkhudgins92 Aug 12, 2016
4173d3a
Updated test code
wkhudgins92 Aug 12, 2016
65f9b81
Added case insensitive test cases
wkhudgins92 Aug 12, 2016
12ef0ed
Improved argument consistency and added additional test cases
wkhudgins92 May 15, 2019
d7ec807
Merge pull request #2 from php/master
wkhudgins92 May 16, 2019
18bb703
Merge branch 'master' into patch-add-str_begins-and-ends
wkhudgins92 May 16, 2019
4d1f7b8
Updated function parameter definitions
wkhudgins92 May 16, 2019
b37545f
Corrected verb conjugation in str_begins and str_ends
wkhudgins92 May 16, 2019
8263aea
Added mb_str_begins
wkhudgins92 Jun 3, 2019
db847f5
Merge branch 'master' of https://github.com/php/php-src
wkhudgins92 Jun 3, 2019
33c3de0
Resolved merge conflicts
wkhudgins92 Jun 3, 2019
b534efa
Fixed typo
wkhudgins92 Jun 3, 2019
c7ebb7c
Added mb_str_ibegins
wkhudgins92 Jun 3, 2019
e861268
Fixed call to PHP mb_stripos API
wkhudgins92 Jun 3, 2019
ccba7f3
Added mb_str_ends
wkhudgins92 Jun 3, 2019
a3dc5eb
Fixed call to member of mbfl_string struct
wkhudgins92 Jun 3, 2019
26c5721
Added mb_str_iends
wkhudgins92 Jun 4, 2019
02bbeef
Added test for mb_str_begins
wkhudgins92 Jun 4, 2019
bfa34f4
Added string to list of functions for mbstring
wkhudgins92 Jun 4, 2019
59e0081
Added Zend Arg info
wkhudgins92 Jun 4, 2019
71f7afa
Fixed typo in test case
wkhudgins92 Jun 4, 2019
4534840
Change needle in tesT
wkhudgins92 Jun 4, 2019
21caed8
Fixed syntax error
wkhudgins92 Jun 4, 2019
4a5e9b2
Removed incorrect text in mb_str_begins test
wkhudgins92 Jun 4, 2019
aa0aa2a
Added more tests for mb_str_i?(begins|ends)
wkhudgins92 Jun 4, 2019
f33f884
Changed approach for str_ends
wkhudgins92 Jun 5, 2019
9005a5c
Output the error code when an unknown error occurs in mb_strpos
wkhudgins92 Jun 7, 2019
433e8c4
Fixed finger typo
wkhudgins92 Jun 7, 2019
fa557a0
Corrected string output, remembered this is C and not PHP
wkhudgins92 Jun 7, 2019
363e4b6
Fixed format string
wkhudgins92 Jun 7, 2019
3cc6435
Update mbstring.c
wkhudgins92 Jun 7, 2019
60c8be8
Update mbstring.c
wkhudgins92 Jun 7, 2019
9d27d12
Update mbstring.c
wkhudgins92 Jun 7, 2019
cb82c1b
Update mbstring.c
wkhudgins92 Jun 7, 2019
edc593d
Update mbstring.c
wkhudgins92 Jun 10, 2019
e614fa2
Update mbstring.c
wkhudgins92 Jun 10, 2019
0d44f2b
Update mbstring.c
wkhudgins92 Jun 17, 2019
117d677
Update mb_str_ends.phpt
wkhudgins92 Jun 17, 2019
9a4c339
Corrected mb_str_iends test
wkhudgins92 Jun 18, 2019
0dbe537
Updated mb_str_iends
wkhudgins92 Jun 18, 2019
6938799
Added explicit checks for when needle is longer than haystack
wkhudgins92 Jun 20, 2019
90684da
Fixed indentation
wkhudgins92 Jun 20, 2019
21d1862
Fixed indentation
wkhudgins92 Jun 20, 2019
7fc90b6
Fixed indentation
wkhudgins92 Jun 20, 2019
ddf1dce
Added str_begins and str_ends
wkhudgins92 Jul 26, 2016
a334293
Added support for case insensitive searches
wkhudgins92 Jul 27, 2016
f7ff725
changed capitalization of TRUE
wkhudgins92 Jul 27, 2016
5514cd0
Passes test case now
wkhudgins92 Jul 29, 2016
cbe46ec
Added testing for str_ends
wkhudgins92 Jul 29, 2016
b6ec725
Passes tests cases
wkhudgins92 Jul 31, 2016
98ec7d6
Fixed build error
wkhudgins92 Aug 2, 2016
e0c6457
Split functions based on case sensitivity
wkhudgins92 Aug 12, 2016
0d3e608
Renamed str_begins to str_begins
wkhudgins92 Aug 12, 2016
c2f769a
Updated test code
wkhudgins92 Aug 12, 2016
dd8d8df
Added case insensitive test cases
wkhudgins92 Aug 12, 2016
66f55fa
Improved argument consistency and added additional test cases
wkhudgins92 May 15, 2019
0cec5e1
Updated function parameter definitions
wkhudgins92 May 16, 2019
9ef79ed
Corrected verb conjugation in str_begins and str_ends
wkhudgins92 May 16, 2019
ee628b4
Added mb_str_begins
wkhudgins92 Jun 3, 2019
7416d62
Fixed typo
wkhudgins92 Jun 3, 2019
a057122
Added mb_str_ibegins
wkhudgins92 Jun 3, 2019
7c42c2a
Fixed call to PHP mb_stripos API
wkhudgins92 Jun 3, 2019
923433b
Added mb_str_ends
wkhudgins92 Jun 3, 2019
6c48448
Fixed call to member of mbfl_string struct
wkhudgins92 Jun 3, 2019
7af64f2
Added mb_str_iends
wkhudgins92 Jun 4, 2019
eb14215
Added test for mb_str_begins
wkhudgins92 Jun 4, 2019
a80ccf1
Added string to list of functions for mbstring
wkhudgins92 Jun 4, 2019
39076dd
Added Zend Arg info
wkhudgins92 Jun 4, 2019
7110d90
Fixed typo in test case
wkhudgins92 Jun 4, 2019
308f0b6
Change needle in tesT
wkhudgins92 Jun 4, 2019
d85e113
Fixed syntax error
wkhudgins92 Jun 4, 2019
cdb74bc
Removed incorrect text in mb_str_begins test
wkhudgins92 Jun 4, 2019
4ed887e
Added more tests for mb_str_i?(begins|ends)
wkhudgins92 Jun 4, 2019
bd3485c
Changed approach for str_ends
wkhudgins92 Jun 5, 2019
1cc0c31
Output the error code when an unknown error occurs in mb_strpos
wkhudgins92 Jun 7, 2019
1322540
Fixed finger typo
wkhudgins92 Jun 7, 2019
6cdcb23
Corrected string output, remembered this is C and not PHP
wkhudgins92 Jun 7, 2019
315a201
Fixed format string
wkhudgins92 Jun 7, 2019
4bb52bc
Update mbstring.c
wkhudgins92 Jun 7, 2019
101f29b
Update mbstring.c
wkhudgins92 Jun 7, 2019
7ecc855
Update mbstring.c
wkhudgins92 Jun 7, 2019
a44574a
Update mbstring.c
wkhudgins92 Jun 7, 2019
6ce0ac1
Update mbstring.c
wkhudgins92 Jun 10, 2019
df0e11c
Update mbstring.c
wkhudgins92 Jun 10, 2019
1a9def4
Update mbstring.c
wkhudgins92 Jun 17, 2019
cb34fe0
Update mb_str_ends.phpt
wkhudgins92 Jun 17, 2019
857ca2a
Corrected mb_str_iends test
wkhudgins92 Jun 18, 2019
7a91516
Updated mb_str_iends
wkhudgins92 Jun 18, 2019
ccbba77
Added explicit checks for when needle is longer than haystack
wkhudgins92 Jun 20, 2019
af61e14
Fixed indentation
wkhudgins92 Jun 20, 2019
a685de6
Fixed indentation
wkhudgins92 Jun 20, 2019
10205e8
Fixed indentation
wkhudgins92 Jun 20, 2019
e7b6301
Merge branch 'patch-add-str_begins-and-ends' of github.com:wkhudgins9…
wkhudgins92 Jul 3, 2019
c3c6e2d
Renamed functions
wkhudgins92 Jul 3, 2019
07f690a
Renamed test files
wkhudgins92 Jul 3, 2019
4445f67
Renamd from begins with to starts with
wkhudgins92 Jul 3, 2019
c343678
Refactored naming format
wkhudgins92 Jul 3, 2019
c448a6d
Renamed test cases
wkhudgins92 Jul 3, 2019
08f1c1c
Fixed indentation
wkhudgins92 Jul 4, 2019
9b2bff4
Fixed return macros
wkhudgins92 Jul 4, 2019
ffe96ed
Replaced starts_with loops with memcmp calls
wkhudgins92 Jul 4, 2019
756ecbf
Removed unused variable
wkhudgins92 Jul 4, 2019
7b9217f
Turned str_starts_with_ci from memcmp to loop
wkhudgins92 Jul 4, 2019
70d0f39
Readded looping variable
wkhudgins92 Jul 4, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -2227,6 +2227,26 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_strstr, 0, 0, 2)
ZEND_ARG_INFO(0, part)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_str_begin, 0, 0, 2)
ZEND_ARG_INFO(0, search_value)
ZEND_ARG_INFO(0, str)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_str_ibegin, 0, 0, 2)
ZEND_ARG_INFO(0, search_value)
ZEND_ARG_INFO(0, str)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_str_end, 0, 0, 2)
ZEND_ARG_INFO(0, search_value)
ZEND_ARG_INFO(0, str)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_str_iend, 0, 0, 2)
ZEND_ARG_INFO(0, search_value)
ZEND_ARG_INFO(0, str)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_strpos, 0, 0, 2)
ZEND_ARG_INFO(0, haystack)
ZEND_ARG_INFO(0, needle)
Expand Down Expand Up @@ -2744,6 +2764,10 @@ const zend_function_entry basic_functions[] = { /* {{{ */
PHP_FE(strtoupper, arginfo_strtoupper)
PHP_FE(strtolower, arginfo_strtolower)
PHP_FE(strpos, arginfo_strpos)
PHP_FE(str_begin, arginfo_str_begin)
PHP_FE(str_ibegin, arginfo_str_ibegin)
PHP_FE(str_end, arginfo_str_end)
PHP_FE(str_iend, arginfo_str_iend)
PHP_FE(stripos, arginfo_stripos)
PHP_FE(strrpos, arginfo_strrpos)
PHP_FE(strripos, arginfo_strripos)
Expand Down
4 changes: 4 additions & 0 deletions ext/standard/php_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ PHP_FUNCTION(basename);
PHP_FUNCTION(dirname);
PHP_FUNCTION(pathinfo);
PHP_FUNCTION(strstr);
PHP_FUNCTION(str_begin);
PHP_FUNCTION(str_ibegin);
PHP_FUNCTION(str_end);
PHP_FUNCTION(str_iend);
PHP_FUNCTION(strpos);
PHP_FUNCTION(stripos);
PHP_FUNCTION(strrpos);
Expand Down
60 changes: 60 additions & 0 deletions ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -2445,6 +2445,66 @@ PHP_FUNCTION(substr)
}
/* }}} */

/* {{{ proto boolean str_begin(string search_value, string str)
Checks if str begins with search_value */
PHP_FUNCTION(str_begin) {
zend_string *str, *search_value;
int i;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "SS", &search_value, &str) == FAILURE)
RETURN_NULL();

for (i = 0; i < search_value->len; i++)
if (str->val[i] != search_value->val[i])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the length of search_value is greater than str, maybe it will access out of bounds?

Copy link
Contributor

@blar blar Sep 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you change the the interface to bool str_begin(string $haytstack, $needle) and bool str_ends(string $haystack, string $needle) for consistency with other string function like stristr, strpos, strrchr?

RETURN_BOOL(0);
RETURN_BOOL(1);
}

/* {{{ proto boolean str_ibegin(string search_value, string str)
Performs case insensitive check to determine if str begins with search_value */
PHP_FUNCTION(str_ibegin) {
zend_string *str, *search_value;
int i;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "SS", &search_value, &str) == FAILURE)
RETURN_NULL();

for (i = 0; i < search_value->len; i++)
if (tolower(str->val[i]) != tolower(search_value->val[i]))
RETURN_BOOL(0);
RETURN_BOOL(1);
}

/* {{{ proto boolean str_end(string search_value, string str)
Checks if str ends with search_value */
PHP_FUNCTION(str_end) {
zend_string *str, *search_value;
int i, j;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "SS", &search_value, &str) == FAILURE)
RETURN_NULL();

for (i = str->len - 1, j = search_value->len - 1; j >= 0; i--, j--)
if (str->val[i] != search_value->val[j])
RETURN_BOOL(0);
RETURN_BOOL(1);
}

/* {{{ proto boolean str_iend(string search_value, string str)
Performs case insensitive check to determine if str ends with search_value */
PHP_FUNCTION(str_iend) {
zend_string *str, *search_value;
int i, j;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "SS", &search_value, &str) == FAILURE)
RETURN_NULL();

for (i = str->len - 1, j = search_value->len - 1; j >= 0; i--, j--)
if (tolower(str->val[i]) != tolower(search_value->val[j]))
RETURN_BOOL(0);
RETURN_BOOL(1);
}

/* {{{ proto mixed substr_replace(mixed str, mixed repl, mixed start [, mixed length])
Replaces part of a string with another string */
PHP_FUNCTION(substr_replace)
Expand Down
16 changes: 16 additions & 0 deletions ext/standard/tests/strings/str_begin.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
str_begin() function - unit tests for str_begin()
--FILE--
<?php
/* Prototype: boolean str_begin (string $search_value string $str);
Description: Determine if $str begins with $search_value
*/
$testStr = "beginningMiddleEnd";
var_dump(str_begin("beginning", $testStr));
var_dump(str_begin("Beginning", $testStr));
var_dump(str_begin("eginning", $testStr));
?>
--EXPECT--
bool(true)
bool(false)
bool(false)
16 changes: 16 additions & 0 deletions ext/standard/tests/strings/str_end.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
str_end() function - unit tests for str_end()
--FILE--
<?php
/* Prototype: boolean str_end (string $search_value string $str );
Description: Determine if $str ends with $search_value
*/
$testStr = "beginningMiddleEnd";
var_dump(str_end("End", $testStr));
var_dump(str_end("end", $testStr));
var_dump(str_end("en", $testStr));
?>
--EXPECT--
bool(true)
bool(false)
bool(false)
16 changes: 16 additions & 0 deletions ext/standard/tests/strings/str_ibegin.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
str_ibegin() function - unit tests for str_ibegin()
--FILE--
<?php
/* Prototype: boolean str_ibegin (string $search_value string $str);
Description: Performs case insensitive check to determine if $str begins with $search_value
*/
$testStr = "beginningMiddleEnd";
var_dump(str_ibegin("beginning", $testStr));
var_dump(str_ibegin("Beginning", $testStr));
var_dump(str_ibegin("eginning", $testStr));
?>
--EXPECT--
bool(true)
bool(true)
bool(false)
16 changes: 16 additions & 0 deletions ext/standard/tests/strings/str_iend.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
str_iend() function - unit tests for str_iend()
--FILE--
<?php
/* Prototype: boolean str_iend (string $search_value string $str );
Description: Performs case insensitive check to Determine if $str ends with $search_value
*/
$testStr = "beginningMiddleEnd";
var_dump(str_iend("End", $testStr));
var_dump(str_iend("end", $testStr));
var_dump(str_iend("en", $testStr));
?>
--EXPECT--
bool(true)
bool(true)
bool(false)