php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #54387
Patch add-str_slice-function revision 2011-03-26 09:30 UTC by birken at gmail dot com

Patch add-str_slice-function for Strings related Bug #54387

Patch version 2011-03-26 09:30 UTC

Return to Bug #54387 | Download this patch
Patch Revisions:

Developer: birken@gmail.com

Index: ext/standard/string.c
===================================================================
--- ext/standard/string.c	(revision 309705)
+++ ext/standard/string.c	(working copy)
@@ -2208,6 +2208,43 @@
 }
 /* }}} */
 
+/* {{{ proto string str_slice(string str, int start [, int end])
+   Returns a slice of a string */
+PHP_FUNCTION(str_slice)
+{
+	char *str;
+	long end = 0, start; 
+	int str_len;
+	int argc = ZEND_NUM_ARGS();
+	
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|l", &str, &str_len, &start, &end) == FAILURE) {
+		return;
+	}
+
+	if (argc == 2) {
+		end = str_len;
+	}
+
+	if (start < 0) {
+		start = MIN(MAX(str_len + start, 0), str_len);
+	} else {
+		start = MIN(start, str_len);
+	}
+
+	if (end < 0) {
+		end = MIN(MAX(str_len + end, 0), str_len);
+	} else {
+		end = MIN(end, str_len);
+	}
+
+	if (end <= start) {
+		RETURN_STRING("", 1);
+	}
+
+	RETURN_STRINGL(str + start, end - start, 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)
Index: ext/standard/basic_functions.c
===================================================================
--- ext/standard/basic_functions.c	(revision 309705)
+++ ext/standard/basic_functions.c	(working copy)
@@ -2272,6 +2272,12 @@
 	ZEND_ARG_INFO(0, length)
 ZEND_END_ARG_INFO()
 
+ZEND_BEGIN_ARG_INFO_EX(arginfo_str_slice, 0, 0, 2)
+	ZEND_ARG_INFO(0, str)
+	ZEND_ARG_INFO(0, start)
+	ZEND_ARG_INFO(0, end)
+ZEND_END_ARG_INFO()
+
 ZEND_BEGIN_ARG_INFO_EX(arginfo_substr_replace, 0, 0, 3)
 	ZEND_ARG_INFO(0, str)
 	ZEND_ARG_INFO(0, replace)
@@ -2756,6 +2762,7 @@
 #endif
 
 	PHP_FE(substr,															arginfo_substr)
+	PHP_FE(str_slice,														arginfo_str_slice)
 	PHP_FE(substr_replace,													arginfo_substr_replace)
 	PHP_FE(quotemeta,														arginfo_quotemeta)
 	PHP_FE(ucfirst,															arginfo_ucfirst)
Index: ext/standard/tests/strings/str_slice.phpt
===================================================================
--- ext/standard/tests/strings/str_slice.phpt	(revision 0)
+++ ext/standard/tests/strings/str_slice.phpt	(revision 0)
@@ -0,0 +1,175 @@
+--TEST--
+Testing str_slice() function
+--FILE--
+<?php
+
+/* Prototype: string str_slice( string str, int start[, int end] )
+ * Description: Returns the portion of string specified by the start and end parameters.
+ */
+
+$strings_array = array( NULL, "", 12345, "abcd" );
+
+
+/* Testing for error conditions */
+echo "*** Testing for error conditions ***\n";
+
+/* Zero Argument */
+var_dump( str_slice() );
+
+/* NULL as Argument */
+var_dump( str_slice(NULL) );
+
+/* Single Argument */
+var_dump( str_slice("abcde") );
+
+/* Scalar Argument */
+var_dump( str_slice(12345) );
+
+/* more than valid number of arguments ( valid are 2 or 3 ) */
+var_dump( str_slice("abcde", 2, 3, 4) );
+
+$counter = 1;
+foreach ($strings_array as $str) {
+  /* variations with two arguments */
+  /* start values >, < and = 0    */
+
+  echo ("\n--- Iteration ".$counter." ---\n");
+  echo ("\n-- Variations for two arguments --\n");
+  var_dump ( str_slice($str, 1) );
+  var_dump ( str_slice($str, 0) );
+  var_dump ( str_slice($str, -2) );
+
+  /* variations with three arguments */
+  /* start value variations with length values  */
+
+  echo ("\n-- Variations for three arguments --\n");
+  var_dump ( str_slice($str, 1, 3) );
+  var_dump ( str_slice($str, 1, 0) );
+  var_dump ( str_slice($str, 1, -3) );
+  var_dump ( str_slice($str, 0, 3) );
+  var_dump ( str_slice($str, 0, 0) );
+  var_dump ( str_slice($str, 0, -3) );
+  var_dump ( str_slice($str, -2, 3) );
+  var_dump ( str_slice($str, -2, 0 ) );
+  var_dump ( str_slice($str, -2, -3) );
+
+  $counter++;
+}
+
+/* variation of start and length to point to same element */
+echo ("\n*** Testing for variations of start and length to point to same element ***\n");
+var_dump (str_slice("abcde" , 2, -2) );
+var_dump (str_slice("abcde" , -3, -2) );
+
+/* Testing to return empty string when start denotes the position beyond the truncation (set by negative length) */
+echo ("\n*** Testing for start > truncation  ***\n");
+var_dump (str_slice("abcdef" , 4, -4) );
+
+/* start <0 && -start > length */
+echo "\n*** Start before the first char ***\n";
+var_dump (str_slice("abcd" , -8) );
+ 
+echo"\nDone";
+
+?>
+--EXPECTF--
+*** Testing for error conditions ***
+
+Warning: str_slice() expects at least 2 parameters, 0 given in %s on line %d
+NULL
+
+Warning: str_slice() expects at least 2 parameters, 1 given in %s on line %d
+NULL
+
+Warning: str_slice() expects at least 2 parameters, 1 given in %s on line %d
+NULL
+
+Warning: str_slice() expects at least 2 parameters, 1 given in %s on line %d
+NULL
+
+Warning: str_slice() expects at most 3 parameters, 4 given in %s on line %d
+NULL
+
+--- Iteration 1 ---
+
+-- Variations for two arguments --
+string(0) ""
+string(0) ""
+string(0) ""
+
+-- Variations for three arguments --
+string(0) ""
+string(0) ""
+string(0) ""
+string(0) ""
+string(0) ""
+string(0) ""
+string(0) ""
+string(0) ""
+string(0) ""
+
+--- Iteration 2 ---
+
+-- Variations for two arguments --
+string(0) ""
+string(0) ""
+string(0) ""
+
+-- Variations for three arguments --
+string(0) ""
+string(0) ""
+string(0) ""
+string(0) ""
+string(0) ""
+string(0) ""
+string(0) ""
+string(0) ""
+string(0) ""
+
+--- Iteration 3 ---
+
+-- Variations for two arguments --
+string(4) "2345"
+string(5) "12345"
+string(2) "45"
+
+-- Variations for three arguments --
+string(2) "23"
+string(0) ""
+string(1) "2"
+string(3) "123"
+string(0) ""
+string(2) "12"
+string(0) ""
+string(0) ""
+string(0) ""
+
+--- Iteration 4 ---
+
+-- Variations for two arguments --
+string(3) "bcd"
+string(4) "abcd"
+string(2) "cd"
+
+-- Variations for three arguments --
+string(2) "bc"
+string(0) ""
+string(0) ""
+string(3) "abc"
+string(0) ""
+string(1) "a"
+string(1) "c"
+string(0) ""
+string(0) ""
+
+*** Testing for variations of start and length to point to same element ***
+string(1) "c"
+string(1) "c"
+
+*** Testing for start > truncation  ***
+string(0) ""
+
+*** Start before the first char ***
+string(4) "abcd"
+
+Done
Index: ext/standard/php_string.h
===================================================================
--- ext/standard/php_string.h	(revision 309705)
+++ ext/standard/php_string.h	(working copy)
@@ -51,6 +51,7 @@
 PHP_FUNCTION(strripos);
 PHP_FUNCTION(strrchr);
 PHP_FUNCTION(substr);
+PHP_FUNCTION(str_slice);
 PHP_FUNCTION(quotemeta);
 PHP_FUNCTION(ucfirst);
 PHP_FUNCTION(lcfirst);
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 22:01:29 2024 UTC