1999-04-17 00:37:12 +00:00
/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
2004-01-08 08:18:22 +00:00
| PHP Version 5 |
1999-04-17 00:37:12 +00:00
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
2005-08-03 14:08:58 +00:00
| Copyright ( c ) 1997 - 2005 The PHP Group |
1999-04-17 00:37:12 +00:00
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
2003-06-10 20:04:29 +00:00
| This source file is subject to version 3.0 of the PHP license , |
1999-07-16 13:13:16 +00:00
| that is bundled with this package in the file LICENSE , and is |
2003-06-10 20:04:29 +00:00
| available through the world - wide - web at the following url : |
| http : //www.php.net/license/3_0.txt. |
1999-07-16 13:13:16 +00:00
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world - wide - web , please send a note to |
| license @ php . net so we can mail you a copy immediately . |
1999-04-17 00:37:12 +00:00
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
2000-05-23 20:16:14 +00:00
| Authors : Rasmus Lerdorf < rasmus @ php . net > |
2003-03-18 12:06:09 +00:00
| Stig S <EFBFBD> ther Bakken < ssb @ php . net > |
1999-07-16 13:13:16 +00:00
| Zeev Suraski < zeev @ zend . com > |
1999-04-17 00:37:12 +00:00
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
*/
/* $Id$ */
1999-04-23 20:06:01 +00:00
1999-12-18 04:01:20 +00:00
/* Synced with php 3.0 revision 1.193 1999-06-16 [ssb] */
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
1999-04-17 00:37:12 +00:00
# include <stdio.h>
# include "php.h"
1999-06-15 21:51:00 +00:00
# include "reg.h"
2002-09-26 19:18:35 +00:00
# include "php_rand.h"
1999-12-04 19:19:57 +00:00
# include "php_string.h"
2000-01-28 17:24:53 +00:00
# include "php_variables.h"
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
# ifdef HAVE_LOCALE_H
# include <locale.h>
1999-04-17 00:37:12 +00:00
# endif
2001-07-04 10:10:30 +00:00
# ifdef HAVE_LANGINFO_H
# include <langinfo.h>
# endif
2002-08-21 13:14:57 +00:00
# ifdef HAVE_MONETARY_H
# include <monetary.h>
# endif
2003-05-27 00:42:39 +00:00
# ifdef HAVE_LIBINTL
# include <libintl.h> /* For LC_MESSAGES */
# endif
2003-02-07 21:36:18 +00:00
# include <math.h>
2000-06-06 18:58:15 +00:00
# include "scanf.h"
# include "zend_API.h"
1999-06-15 21:51:00 +00:00
# include "zend_execute.h"
# include "php_globals.h"
1999-12-28 18:47:59 +00:00
# include "basic_functions.h"
2000-10-12 01:26:26 +00:00
# include "php_smart_str.h"
2001-01-14 16:36:30 +00:00
# ifdef ZTS
# include "TSRM.h"
# endif
1999-04-17 00:37:12 +00:00
2005-08-11 23:36:07 +00:00
# include "unicode/uchar.h"
2000-09-11 13:52:22 +00:00
# define STR_PAD_LEFT 0
# define STR_PAD_RIGHT 1
# define STR_PAD_BOTH 2
2002-01-22 03:35:23 +00:00
# define PHP_PATHINFO_DIRNAME 1
# define PHP_PATHINFO_BASENAME 2
# define PHP_PATHINFO_EXTENSION 4
# define PHP_PATHINFO_ALL (PHP_PATHINFO_DIRNAME | PHP_PATHINFO_BASENAME | PHP_PATHINFO_EXTENSION)
2000-07-08 20:38:23 +00:00
2002-09-25 19:06:29 +00:00
# define STR_STRSPN 0
# define STR_STRCSPN 1
2001-06-06 13:06:12 +00:00
/* {{{ register_string_constants
*/
2000-07-08 20:38:23 +00:00
void register_string_constants ( INIT_FUNC_ARGS )
{
2000-09-10 05:59:16 +00:00
REGISTER_LONG_CONSTANT ( " STR_PAD_LEFT " , STR_PAD_LEFT , CONST_CS | CONST_PERSISTENT ) ;
REGISTER_LONG_CONSTANT ( " STR_PAD_RIGHT " , STR_PAD_RIGHT , CONST_CS | CONST_PERSISTENT ) ;
REGISTER_LONG_CONSTANT ( " STR_PAD_BOTH " , STR_PAD_BOTH , CONST_CS | CONST_PERSISTENT ) ;
REGISTER_LONG_CONSTANT ( " PATHINFO_DIRNAME " , PHP_PATHINFO_DIRNAME , CONST_CS | CONST_PERSISTENT ) ;
2000-09-11 13:52:22 +00:00
REGISTER_LONG_CONSTANT ( " PATHINFO_BASENAME " , PHP_PATHINFO_BASENAME , CONST_CS | CONST_PERSISTENT ) ;
2000-09-10 05:59:16 +00:00
REGISTER_LONG_CONSTANT ( " PATHINFO_EXTENSION " , PHP_PATHINFO_EXTENSION , CONST_CS | CONST_PERSISTENT ) ;
2000-12-10 15:55:34 +00:00
2001-01-14 16:36:30 +00:00
# ifdef HAVE_LOCALECONV
/* If last members of struct lconv equal CHAR_MAX, no grouping is done */
/* This is bad, but since we are going to be hardcoding in the POSIX stuff anyway... */
# ifndef HAVE_LIMITS_H
# define CHAR_MAX 127
# endif
REGISTER_LONG_CONSTANT ( " CHAR_MAX " , CHAR_MAX , CONST_CS | CONST_PERSISTENT ) ;
# endif
2000-12-10 15:55:34 +00:00
# ifdef HAVE_LOCALE_H
REGISTER_LONG_CONSTANT ( " LC_CTYPE " , LC_CTYPE , CONST_CS | CONST_PERSISTENT ) ;
REGISTER_LONG_CONSTANT ( " LC_NUMERIC " , LC_NUMERIC , CONST_CS | CONST_PERSISTENT ) ;
REGISTER_LONG_CONSTANT ( " LC_TIME " , LC_TIME , CONST_CS | CONST_PERSISTENT ) ;
REGISTER_LONG_CONSTANT ( " LC_COLLATE " , LC_COLLATE , CONST_CS | CONST_PERSISTENT ) ;
REGISTER_LONG_CONSTANT ( " LC_MONETARY " , LC_MONETARY , CONST_CS | CONST_PERSISTENT ) ;
REGISTER_LONG_CONSTANT ( " LC_ALL " , LC_ALL , CONST_CS | CONST_PERSISTENT ) ;
2000-12-14 15:58:14 +00:00
# ifdef LC_MESSAGES
2000-12-11 16:27:48 +00:00
REGISTER_LONG_CONSTANT ( " LC_MESSAGES " , LC_MESSAGES , CONST_CS | CONST_PERSISTENT ) ;
# endif
2000-12-10 15:55:34 +00:00
# endif
2000-07-08 20:38:23 +00:00
}
2001-06-06 13:06:12 +00:00
/* }}} */
2000-07-08 20:38:23 +00:00
1999-12-05 14:16:37 +00:00
int php_tag_find ( char * tag , int len , char * set ) ;
1999-11-28 00:31:02 +00:00
/* this is read-only, so it's ok */
1999-05-16 11:12:23 +00:00
static char hexconvtab [ ] = " 0123456789abcdef " ;
2001-01-14 16:36:30 +00:00
/* localeconv mutex */
# ifdef ZTS
static MUTEX_T locale_mutex = NULL ;
# endif
2001-06-06 13:06:12 +00:00
/* {{{ php_bin2hex
*/
1999-05-16 11:32:51 +00:00
static char * php_bin2hex ( const unsigned char * old , const size_t oldlen , size_t * newlen )
1999-05-16 11:12:23 +00:00
{
2001-09-04 06:18:11 +00:00
register unsigned char * result = NULL ;
1999-08-12 10:53:29 +00:00
size_t i , j ;
1999-05-16 11:12:23 +00:00
2003-08-11 23:16:54 +00:00
result = ( char * ) safe_emalloc ( oldlen * 2 , sizeof ( char ) , 1 ) ;
1999-05-16 11:12:23 +00:00
2002-10-02 17:56:04 +00:00
for ( i = j = 0 ; i < oldlen ; i + + ) {
2000-03-26 17:06:19 +00:00
result [ j + + ] = hexconvtab [ old [ i ] > > 4 ] ;
result [ j + + ] = hexconvtab [ old [ i ] & 15 ] ;
1999-05-16 11:12:23 +00:00
}
2001-02-22 10:36:40 +00:00
result [ j ] = ' \0 ' ;
1999-05-16 11:12:23 +00:00
2001-09-04 06:18:11 +00:00
if ( newlen )
* newlen = oldlen * 2 * sizeof ( char ) ;
1999-05-16 11:12:23 +00:00
2000-03-26 17:06:19 +00:00
return result ;
1999-05-16 11:12:23 +00:00
}
2001-06-06 13:06:12 +00:00
/* }}} */
1999-05-16 11:12:23 +00:00
2001-01-14 16:36:30 +00:00
# ifdef HAVE_LOCALECONV
2001-06-05 13:12:10 +00:00
/* {{{ localeconv_r
* glibc ' s localeconv is not reentrant , so lets make it so . . . sorta */
2004-11-03 23:36:51 +00:00
PHPAPI struct lconv * localeconv_r ( struct lconv * out )
2001-01-14 16:36:30 +00:00
{
struct lconv * res ;
# ifdef ZTS
tsrm_mutex_lock ( locale_mutex ) ;
# endif
/* localeconv doesn't return an error condition */
res = localeconv ( ) ;
* out = * res ;
# ifdef ZTS
tsrm_mutex_unlock ( locale_mutex ) ;
# endif
return out ;
}
2001-06-05 13:12:10 +00:00
/* }}} */
2001-01-14 16:36:30 +00:00
# ifdef ZTS
2001-06-05 13:12:10 +00:00
/* {{{ PHP_MINIT_FUNCTION
*/
2001-01-14 16:36:30 +00:00
PHP_MINIT_FUNCTION ( localeconv )
{
locale_mutex = tsrm_mutex_alloc ( ) ;
return SUCCESS ;
}
2001-06-05 13:12:10 +00:00
/* }}} */
2001-01-14 16:36:30 +00:00
2001-06-05 13:12:10 +00:00
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
2001-01-14 16:36:30 +00:00
PHP_MSHUTDOWN_FUNCTION ( localeconv )
{
tsrm_mutex_free ( locale_mutex ) ;
locale_mutex = NULL ;
return SUCCESS ;
}
2001-06-05 13:12:10 +00:00
/* }}} */
2001-01-14 16:36:30 +00:00
# endif
# endif
2000-02-24 10:11:42 +00:00
/* {{{ proto string bin2hex(string data)
Converts the binary representation of data to hex */
1999-05-16 11:12:23 +00:00
PHP_FUNCTION ( bin2hex )
{
2005-08-17 17:39:04 +00:00
unsigned char * data ;
int data_len ;
2000-03-26 17:06:19 +00:00
char * result ;
1999-05-16 11:12:23 +00:00
size_t newlen ;
2005-08-17 17:39:04 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " y " , & data , & data_len ) = = FAILURE ) {
return ;
1999-05-16 11:12:23 +00:00
}
2005-08-17 17:39:04 +00:00
result = php_bin2hex ( data , data_len , & newlen ) ;
1999-05-16 11:12:23 +00:00
2001-09-04 06:18:11 +00:00
if ( ! result ) {
1999-05-16 11:12:23 +00:00
RETURN_FALSE ;
}
2005-08-19 17:53:03 +00:00
RETVAL_ASCII_STRINGL ( result , newlen , 0 ) ;
2005-08-17 17:39:04 +00:00
if ( UG ( unicode ) ) {
efree ( result ) ;
}
1999-05-16 11:12:23 +00:00
}
2000-02-24 10:11:42 +00:00
/* }}} */
1999-04-17 00:37:12 +00:00
2002-09-25 19:06:29 +00:00
static void php_spn_common_handler ( INTERNAL_FUNCTION_PARAMETERS , int behavior )
1999-04-17 00:37:12 +00:00
{
2005-10-19 19:10:13 +00:00
void * s1 , * s2 ;
int32_t len1 , len2 ;
zend_uchar type1 , type2 ;
long start , len ; /* For UNICODE, these are codepoint units */
1999-04-17 00:37:12 +00:00
2002-09-25 19:06:29 +00:00
start = 0 ;
len = 0 ;
2005-10-19 19:10:13 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " TT|ll " ,
& s1 , & len1 , & type1 , & s2 , & len2 , & type2 ,
& start , & len ) = = FAILURE ) {
2002-09-25 19:06:29 +00:00
return ;
}
if ( ZEND_NUM_ARGS ( ) < 4 ) {
len = len1 ;
}
/* look at substr() function for more information */
if ( start < 0 ) {
start + = len1 ;
if ( start < 0 ) {
start = 0 ;
}
} else if ( start > len1 ) {
RETURN_FALSE ;
}
if ( len < 0 ) {
len + = ( len1 - start ) ;
if ( len < 0 ) {
len = 0 ;
}
}
2004-07-11 21:15:04 +00:00
if ( ( ( unsigned ) start + ( unsigned ) len ) > len1 ) {
2002-09-25 19:06:29 +00:00
len = len1 - start ;
1999-04-17 00:37:12 +00:00
}
2001-09-04 06:18:11 +00:00
2005-10-19 19:10:13 +00:00
if ( type1 = = IS_UNICODE ) {
UChar * u_start , * u_end ;
int32_t i = 0 ;
U16_FWD_N ( ( UChar * ) s1 , i , len1 , start ) ;
u_start = ( UChar * ) s1 + i ;
U16_FWD_N ( ( UChar * ) s1 , i , len1 , len ) ;
u_end = ( UChar * ) s1 + i ;
if ( behavior = = STR_STRSPN ) {
RETURN_LONG ( php_u_strspn ( u_start /*str1_start*/ ,
( UChar * ) s2 /*str2_start*/ ,
u_end /*str1_end*/ ,
( UChar * ) s2 + len2 /*str2_end*/ ) ) ;
} else if ( behavior = = STR_STRCSPN ) {
RETURN_LONG ( php_u_strcspn ( u_start /*str1_start*/ ,
( UChar * ) s2 /*str2_start*/ ,
u_end /*str1_end*/ ,
( UChar * ) s2 + len2 /*str2_end*/ ) ) ;
}
} else {
if ( behavior = = STR_STRSPN ) {
RETURN_LONG ( php_strspn ( ( char * ) s1 + start /*str1_start*/ ,
( char * ) s2 /*str2_start*/ ,
( char * ) s1 + start + len /*str1_end*/ ,
( char * ) s2 + len2 /*str2_end*/ ) ) ;
} else if ( behavior = = STR_STRCSPN ) {
RETURN_LONG ( php_strcspn ( ( char * ) s1 + start /*str1_start*/ ,
( char * ) s2 /*str2_start*/ ,
( char * ) s1 + start + len /*str1_end*/ ,
( char * ) s2 + len2 /*str2_end*/ ) ) ;
}
2002-09-25 19:06:29 +00:00
}
2005-10-19 19:10:13 +00:00
2002-09-25 19:06:29 +00:00
}
/* {{{ proto int strspn(string str, string mask [, start [, len]])
Finds length of initial segment consisting entirely of characters found in mask . If start or / and length is provided works like strspn ( substr ( $ s , $ start , $ len ) , $ good_chars ) */
PHP_FUNCTION ( strspn )
{
php_spn_common_handler ( INTERNAL_FUNCTION_PARAM_PASSTHRU , STR_STRSPN ) ;
1999-04-17 00:37:12 +00:00
}
/* }}} */
2002-09-25 19:06:29 +00:00
/* {{{ proto int strcspn(string str, string mask [, start [, len]])
Finds length of initial segment consisting entirely of characters not found in mask . If start or / and length is provide works like strcspn ( substr ( $ s , $ start , $ len ) , $ bad_chars ) */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( strcspn )
1999-04-17 00:37:12 +00:00
{
2002-09-25 19:06:29 +00:00
php_spn_common_handler ( INTERNAL_FUNCTION_PARAM_PASSTHRU , STR_STRCSPN ) ;
1999-04-17 00:37:12 +00:00
}
/* }}} */
2001-07-05 10:49:58 +00:00
/* {{{ PHP_MINIT_FUNCTION(nl_langinfo) */
2001-07-04 10:10:30 +00:00
# if HAVE_NL_LANGINFO
PHP_MINIT_FUNCTION ( nl_langinfo )
{
# define REGISTER_NL_LANGINFO_CONSTANT(x) REGISTER_LONG_CONSTANT(#x, x, CONST_CS | CONST_PERSISTENT)
2001-08-10 18:02:59 +00:00
# ifdef ABDAY_1
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( ABDAY_1 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( ABDAY_2 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( ABDAY_3 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( ABDAY_4 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( ABDAY_5 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( ABDAY_6 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( ABDAY_7 ) ;
2001-08-10 18:02:59 +00:00
# endif
# ifdef DAY_1
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( DAY_1 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( DAY_2 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( DAY_3 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( DAY_4 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( DAY_5 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( DAY_6 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( DAY_7 ) ;
2001-08-10 18:02:59 +00:00
# endif
# ifdef ABMON_1
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( ABMON_1 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( ABMON_2 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( ABMON_3 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( ABMON_4 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( ABMON_5 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( ABMON_6 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( ABMON_7 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( ABMON_8 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( ABMON_9 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( ABMON_10 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( ABMON_11 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( ABMON_12 ) ;
2001-08-10 18:02:59 +00:00
# endif
# ifdef MON_1
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( MON_1 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( MON_2 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( MON_3 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( MON_4 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( MON_5 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( MON_6 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( MON_7 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( MON_8 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( MON_9 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( MON_10 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( MON_11 ) ;
REGISTER_NL_LANGINFO_CONSTANT ( MON_12 ) ;
2001-08-10 18:02:59 +00:00
# endif
# ifdef AM_STR
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( AM_STR ) ;
2001-08-10 18:02:59 +00:00
# endif
# ifdef PM_STR
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( PM_STR ) ;
2001-08-10 18:02:59 +00:00
# endif
# ifdef D_T_FMT
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( D_T_FMT ) ;
2001-08-10 18:02:59 +00:00
# endif
# ifdef D_FMT
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( D_FMT ) ;
2001-08-10 18:02:59 +00:00
# endif
# ifdef T_FMT
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( T_FMT ) ;
2001-08-10 18:02:59 +00:00
# endif
# ifdef T_FMT_AMPM
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( T_FMT_AMPM ) ;
2001-08-10 18:02:59 +00:00
# endif
# ifdef ERA
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( ERA ) ;
2001-08-10 18:02:59 +00:00
# endif
2001-07-05 10:49:58 +00:00
# ifdef ERA_YEAR
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( ERA_YEAR ) ;
2001-07-05 10:49:58 +00:00
# endif
2001-08-10 18:02:59 +00:00
# ifdef ERA_D_T_FMT
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( ERA_D_T_FMT ) ;
2001-08-10 18:02:59 +00:00
# endif
# ifdef ERA_D_FMT
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( ERA_D_FMT ) ;
2001-08-10 18:02:59 +00:00
# endif
# ifdef ERA_T_FMT
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( ERA_T_FMT ) ;
2001-08-10 18:02:59 +00:00
# endif
# ifdef ALT_DIGITS
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( ALT_DIGITS ) ;
2001-08-10 18:02:59 +00:00
# endif
2001-07-05 10:49:58 +00:00
# ifdef INT_CURR_SYMBOL
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( INT_CURR_SYMBOL ) ;
2001-07-05 10:49:58 +00:00
# endif
2001-07-04 10:10:30 +00:00
# ifdef CURRENCY_SYMBOL
REGISTER_NL_LANGINFO_CONSTANT ( CURRENCY_SYMBOL ) ;
# endif
# ifdef CRNCYSTR
REGISTER_NL_LANGINFO_CONSTANT ( CRNCYSTR ) ;
# endif
2001-07-05 10:49:58 +00:00
# ifdef MON_DECIMAL_POINT
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( MON_DECIMAL_POINT ) ;
2001-07-05 10:49:58 +00:00
# endif
# ifdef MON_THOUSANDS_SEP
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( MON_THOUSANDS_SEP ) ;
2001-07-05 10:49:58 +00:00
# endif
# ifdef MON_GROUPING
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( MON_GROUPING ) ;
2001-07-05 10:49:58 +00:00
# endif
# ifdef POSITIVE_SIGN
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( POSITIVE_SIGN ) ;
2001-07-05 10:49:58 +00:00
# endif
# ifdef NEGATIVE_SIGN
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( NEGATIVE_SIGN ) ;
2001-07-05 10:49:58 +00:00
# endif
# ifdef INT_FRAC_DIGITS
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( INT_FRAC_DIGITS ) ;
2001-07-05 10:49:58 +00:00
# endif
# ifdef FRAC_DIGITS
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( FRAC_DIGITS ) ;
2001-07-05 10:49:58 +00:00
# endif
# ifdef P_CS_PRECEDES
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( P_CS_PRECEDES ) ;
2001-07-05 10:49:58 +00:00
# endif
# ifdef P_SEP_BY_SPACE
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( P_SEP_BY_SPACE ) ;
2001-07-05 10:49:58 +00:00
# endif
# ifdef N_CS_PRECEDES
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( N_CS_PRECEDES ) ;
2001-07-05 10:49:58 +00:00
# endif
# ifdef N_SEP_BY_SPACE
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( N_SEP_BY_SPACE ) ;
2001-07-05 10:49:58 +00:00
# endif
# ifdef P_SIGN_POSN
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( P_SIGN_POSN ) ;
2001-07-05 10:49:58 +00:00
# endif
# ifdef N_SIGN_POSN
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( N_SIGN_POSN ) ;
2001-07-05 10:49:58 +00:00
# endif
2001-07-04 10:10:30 +00:00
# ifdef DECIMAL_POINT
REGISTER_NL_LANGINFO_CONSTANT ( DECIMAL_POINT ) ;
# endif
# ifdef RADIXCHAR
REGISTER_NL_LANGINFO_CONSTANT ( RADIXCHAR ) ;
# endif
# ifdef THOUSANDS_SEP
REGISTER_NL_LANGINFO_CONSTANT ( THOUSANDS_SEP ) ;
# endif
# ifdef THOUSEP
REGISTER_NL_LANGINFO_CONSTANT ( THOUSEP ) ;
# endif
2001-07-05 10:53:15 +00:00
# ifdef GROUPING
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( GROUPING ) ;
2001-07-05 10:53:15 +00:00
# endif
# ifdef YESEXPR
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( YESEXPR ) ;
2001-07-05 10:53:15 +00:00
# endif
# ifdef NOEXPR
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( NOEXPR ) ;
2001-07-05 10:53:15 +00:00
# endif
# ifdef YESSTR
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( YESSTR ) ;
2001-07-05 10:53:15 +00:00
# endif
# ifdef NOSTR
2001-07-04 10:10:30 +00:00
REGISTER_NL_LANGINFO_CONSTANT ( NOSTR ) ;
2001-07-05 10:53:15 +00:00
# endif
2001-07-04 10:10:30 +00:00
# ifdef CODESET
REGISTER_NL_LANGINFO_CONSTANT ( CODESET ) ;
# endif
# undef REGISTER_NL_LANGINFO_CONSTANT
return SUCCESS ;
}
2001-07-05 10:49:58 +00:00
/* }}} */
2001-12-06 19:02:27 +00:00
/* {{{ proto string nl_langinfo(int item)
Query language and locale information */
2001-07-04 10:10:30 +00:00
PHP_FUNCTION ( nl_langinfo )
{
2001-09-04 06:18:11 +00:00
zval * * item ;
char * value ;
2001-07-04 10:10:30 +00:00
2001-09-04 06:18:11 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( 1 , & item ) = = FAILURE ) {
2001-07-04 10:10:30 +00:00
WRONG_PARAM_COUNT ;
}
convert_to_long_ex ( item ) ;
2001-09-04 06:18:11 +00:00
2001-07-04 10:10:30 +00:00
value = nl_langinfo ( Z_LVAL_PP ( item ) ) ;
2002-10-02 18:13:56 +00:00
if ( value = = NULL ) {
2001-07-04 10:10:30 +00:00
RETURN_FALSE ;
2002-10-02 18:13:56 +00:00
} else {
2001-07-04 10:10:30 +00:00
RETURN_STRING ( value , 1 ) ;
}
}
# endif
2002-05-16 14:41:15 +00:00
/* }}} */
2001-07-04 10:10:30 +00:00
2001-01-17 01:10:50 +00:00
# ifdef HAVE_STRCOLL
/* {{{ proto int strcoll(string str1, string str2)
2001-09-04 10:44:28 +00:00
Compares two strings using the current locale */
2001-01-17 01:10:50 +00:00
PHP_FUNCTION ( strcoll )
{
zval * * s1 , * * s2 ;
if ( ZEND_NUM_ARGS ( ) ! = 2 | | zend_get_parameters_ex ( 2 , & s1 , & s2 ) = = FAILURE ) {
WRONG_PARAM_COUNT ;
}
convert_to_string_ex ( s1 ) ;
convert_to_string_ex ( s2 ) ;
2001-09-04 06:18:11 +00:00
RETURN_LONG ( strcoll ( ( const char * ) Z_STRVAL_PP ( s1 ) ,
( const char * ) Z_STRVAL_PP ( s2 ) ) ) ;
2001-01-17 01:10:50 +00:00
}
2001-06-05 13:12:10 +00:00
/* }}} */
2001-01-17 01:10:50 +00:00
# endif
2001-08-03 23:09:05 +00:00
/* {{{ php_charmask
* Fills a 256 - byte bytemask with input . You can specify a range like ' a . . z ' ,
* it needs to be incrementing .
2001-08-05 19:38:49 +00:00
* Returns : FAILURE / SUCCESS wether the input was correct ( i . e . no range errors )
2001-08-03 23:09:05 +00:00
*/
2002-07-13 04:13:43 +00:00
static inline int php_charmask ( unsigned char * input , int len , char * mask TSRMLS_DC )
2001-08-03 23:09:05 +00:00
{
unsigned char * end ;
unsigned char c ;
2001-08-05 19:38:49 +00:00
int result = SUCCESS ;
2001-08-03 23:09:05 +00:00
memset ( mask , 0 , 256 ) ;
2002-10-02 17:56:04 +00:00
for ( end = input + len ; input < end ; input + + ) {
2001-08-03 23:09:05 +00:00
c = * input ;
2002-10-02 17:56:04 +00:00
if ( ( input + 3 < end ) & & input [ 1 ] = = ' . ' & & input [ 2 ] = = ' . '
2001-08-05 19:38:49 +00:00
& & input [ 3 ] > = c ) {
memset ( mask + c , 1 , input [ 3 ] - c + 1 ) ;
2001-08-03 23:09:05 +00:00
input + = 3 ;
2002-10-02 17:56:04 +00:00
} else if ( ( input + 1 < end ) & & input [ 0 ] = = ' . ' & & input [ 1 ] = = ' . ' ) {
2001-08-05 19:38:49 +00:00
/* Error, try to be as helpful as possible:
( a range ending / starting with ' . ' won ' t be captured here ) */
2002-10-02 17:56:04 +00:00
if ( end - len > = input ) { /* there was no 'left' char */
2002-09-05 11:29:31 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Invalid '..'-range, no character to the left of '..'. " ) ;
2001-08-05 19:38:49 +00:00
result = FAILURE ;
continue ;
}
2002-10-02 17:56:04 +00:00
if ( input + 2 > = end ) { /* there is no 'right' char */
2002-09-05 11:29:31 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Invalid '..'-range, no character to the right of '..'. " ) ;
2001-08-05 19:38:49 +00:00
result = FAILURE ;
continue ;
}
if ( input [ - 1 ] > input [ 2 ] ) { /* wrong order */
2002-09-05 11:29:31 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Invalid '..'-range, '..'-range needs to be incrementing. " ) ;
2001-08-05 19:38:49 +00:00
result = FAILURE ;
continue ;
}
/* FIXME: better error (a..b..c is the only left possibility?) */
2002-09-05 11:29:31 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Invalid '..'-range. " ) ;
2001-08-05 19:38:49 +00:00
result = FAILURE ;
continue ;
} else {
2001-08-03 23:09:05 +00:00
mask [ c ] = 1 ;
2001-08-05 19:38:49 +00:00
}
2001-08-03 23:09:05 +00:00
}
2001-08-05 19:38:49 +00:00
return result ;
2001-08-03 23:09:05 +00:00
}
/* }}} */
2002-07-13 04:13:43 +00:00
/* {{{ php_trim()
* mode 1 : trim left
* mode 2 : trim right
* mode 3 : trim left and right
* what indicates which chars are to be trimmed . NULL - > default ( ' \ t \ n \ r \ v \ 0 ' )
2001-06-05 13:12:10 +00:00
*/
2005-08-16 06:02:56 +00:00
PHPAPI char * php_trim ( char * c , int len , char * what , int what_len , zend_uchar str_type , zval * return_value , int mode TSRMLS_DC )
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
{
register int i ;
int trimmed = 0 ;
2001-08-03 23:09:05 +00:00
char mask [ 256 ] ;
if ( what ) {
2002-07-13 04:13:43 +00:00
php_charmask ( what , what_len , mask TSRMLS_CC ) ;
2001-08-03 23:09:05 +00:00
} else {
2001-08-05 20:25:40 +00:00
php_charmask ( " \n \r \t \v \0 " , 6 , mask TSRMLS_CC ) ;
2001-08-03 23:09:05 +00:00
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
if ( mode & 1 ) {
for ( i = 0 ; i < len ; i + + ) {
2001-08-03 23:09:05 +00:00
if ( mask [ ( unsigned char ) c [ i ] ] ) {
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
trimmed + + ;
} else {
break ;
}
}
len - = trimmed ;
c + = trimmed ;
}
if ( mode & 2 ) {
for ( i = len - 1 ; i > = 0 ; i - - ) {
2001-08-03 23:09:05 +00:00
if ( mask [ ( unsigned char ) c [ i ] ] ) {
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
len - - ;
} else {
break ;
}
}
}
2002-07-13 04:13:43 +00:00
if ( return_value ) {
2005-08-16 06:02:56 +00:00
if ( str_type = = IS_BINARY ) {
RETVAL_BINARYL ( c , len , 1 ) ;
} else {
RETVAL_STRINGL ( c , len , 1 ) ;
}
2002-07-13 04:13:43 +00:00
} else {
return estrndup ( c , len ) ;
}
return " " ;
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
/* }}} */
2005-08-11 23:36:07 +00:00
/* {{{ php_expand_u_trim_range()
* Expands possible ranges of the form ' a . . b ' in input charlist ,
* where a < b in code - point order
*/
2005-08-12 05:58:02 +00:00
static int php_expand_u_trim_range ( UChar * * range , int32_t * range_len TSRMLS_DC )
2005-08-11 23:36:07 +00:00
{
UChar32 * codepts , * tmp , * input , * end , c ;
int32_t len , tmp_len , idx ;
UErrorCode err ;
int expanded = 0 ;
int result = SUCCESS ;
/* First, convert UTF-16 to UTF-32 */
len = * range_len ;
codepts = ( UChar32 * ) emalloc ( ( len + 1 ) * sizeof ( UChar32 ) ) ;
err = U_ZERO_ERROR ;
u_strToUTF32 ( ( UChar32 * ) codepts , len + 1 , & len , * range , len , & err ) ;
/* Expand ranges, if any - taken from php_charmask() */
tmp_len = len ;
tmp = ( UChar32 * ) emalloc ( ( tmp_len + 1 ) * sizeof ( UChar32 ) ) ;
input = codepts ;
for ( idx = 0 , end = input + len ; input < end ; input + + ) {
c = input [ 0 ] ;
if ( ( input + 3 < end ) & & input [ 1 ] = = ' . ' & & input [ 2 ] = = ' . ' & & input [ 3 ] > = c ) {
tmp_len + = ( input [ 3 ] - c + 1 ) ;
2005-08-17 10:26:02 +00:00
tmp = ( UChar32 * ) erealloc ( tmp , tmp_len * sizeof ( UChar32 ) ) ;
2005-08-11 23:36:07 +00:00
for ( ; c < = input [ 3 ] ; c + + ) {
if ( U_IS_UNICODE_CHAR ( c ) ) tmp [ idx + + ] = c ;
}
input + = 3 ;
expanded + + ;
} else if ( ( input + 1 < end ) & & input [ 0 ] = = ' . ' & & input [ 1 ] = = ' . ' ) {
/* Error, try to be as helpful as possible:
( a range ending / starting with ' . ' won ' t be captured here ) */
if ( end - len > = input ) { /* There is no 'left' char */
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Invalid '..'-range, no character to the left of '..' " ) ;
result = FAILURE ;
continue ;
}
if ( input + 2 > = end ) { /* There is no 'right' char */
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Invalid '..'-range, no character to the right of '..' " ) ;
result = FAILURE ;
continue ;
}
if ( input [ - 1 ] > input [ 2 ] ) { /* Wrong order */
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Invalid '..'-range, '..'-range needs to be incrementing " ) ;
result = FAILURE ;
continue ;
}
/* FIXME: Better error (a..b..c is the only left possibility?) */
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Invalid '..'-range " ) ;
result = FAILURE ;
continue ;
} else {
tmp [ idx + + ] = c ;
}
}
/* If any ranges were expanded, convert the expanded results back to UTF-16 */
if ( expanded > 0 ) {
len = tmp_len ;
* range = ( UChar * ) erealloc ( * range , ( len + 1 ) * sizeof ( UChar ) ) ;
err = U_ZERO_ERROR ;
u_strFromUTF32 ( * range , len + 1 , & len , tmp , tmp_len , & err ) ;
if ( U_FAILURE ( err ) = = U_BUFFER_OVERFLOW_ERROR ) {
err = U_ZERO_ERROR ;
* range = ( UChar * ) erealloc ( * range , ( len + 1 ) * sizeof ( UChar ) ) ;
u_strFromUTF32 ( * range , len + 1 , NULL , tmp , tmp_len , & err ) ;
if ( U_FAILURE ( err ) ) { /* Internal ICU error */
result = FAILURE ;
}
}
* range_len = len ;
}
efree ( tmp ) ;
efree ( codepts ) ;
return result ;
}
/* }}} */
/* {{{ php_u_trim()
* Unicode capable version of php_trim ( )
*/
static UChar * php_u_trim ( UChar * c , int32_t len , UChar * what , int32_t what_len , zval * return_value , int mode TSRMLS_DC )
{
int32_t i , j ;
UChar ch , wh ;
int32_t start = 0 , end = len ;
if ( what ) {
2005-08-12 05:58:02 +00:00
php_expand_u_trim_range ( & what , & what_len TSRMLS_CC ) ;
2005-08-11 23:36:07 +00:00
}
if ( mode & 1 ) {
for ( i = 0 ; i < end ; ) {
U16_NEXT ( c , i , end , ch ) ;
if ( what ) {
for ( j = 0 ; j < what_len ; ) {
U16_NEXT ( what , j , what_len , wh ) ;
if ( wh = = ch ) break ;
}
if ( wh ! = ch ) break ;
} else {
if ( u_isWhitespace ( ch ) = = FALSE ) break ;
}
}
if ( i < end ) {
U16_BACK_1 ( c , 0 , i ) ; /* U16_NEXT() post-increments 'i' */
}
start = i ;
}
if ( mode & 2 ) {
for ( i = end ; i > start ; ) {
U16_PREV ( c , 0 , i , ch ) ;
if ( what ) {
for ( j = 0 ; j < what_len ; ) {
U16_NEXT ( what , j , what_len , wh ) ;
if ( wh = = ch ) break ;
}
if ( wh ! = ch ) break ;
} else {
if ( u_isWhitespace ( ch ) = = FALSE ) break ;
}
}
end = i ;
2005-08-22 14:14:10 +00:00
} else {
- - end ;
2005-08-11 23:36:07 +00:00
}
if ( start < len ) {
if ( return_value ) {
RETVAL_UNICODEL ( c + start , end - start + 1 , 1 ) ;
2005-08-15 08:11:35 +00:00
return NULL ;
2005-08-11 23:36:07 +00:00
} else {
return eustrndup ( c + start , end - start + 1 ) ;
}
} else { /* Trimmed the whole string */
if ( return_value ) {
2005-08-12 16:46:38 +00:00
RETVAL_EMPTY_UNICODE ( ) ;
2005-08-15 08:11:35 +00:00
return NULL ;
2005-08-11 23:36:07 +00:00
} else {
return ( USTR_MAKE ( " " ) ) ;
}
}
}
/* }}} */
2002-07-13 04:13:43 +00:00
/* {{{ php_do_trim
* Base for trim ( ) , rtrim ( ) and ltrim ( ) functions .
*/
static void php_do_trim ( INTERNAL_FUNCTION_PARAMETERS , int mode )
1999-04-17 00:37:12 +00:00
{
2005-08-17 11:36:32 +00:00
zval * * str , * * what ;
int argc = ZEND_NUM_ARGS ( ) ;
if ( argc < 1 | | argc > 2 | | zend_get_parameters_ex ( argc , & str , & what ) = = FAILURE ) {
WRONG_PARAM_COUNT ;
2001-09-04 06:18:11 +00:00
}
2002-07-13 04:13:43 +00:00
2005-08-17 11:36:32 +00:00
convert_to_text_ex ( str ) ;
if ( argc > 1 ) {
if ( Z_TYPE_PP ( str ) ! = Z_TYPE_PP ( what ) ) {
zend_error ( E_WARNING , " %v() expects parameter 2 to be string (legacy, Unicode, or binary), %s given " ,
get_active_function_name ( TSRMLS_C ) ,
zend_zval_type_name ( * what ) ) ;
}
if ( Z_TYPE_PP ( str ) = = IS_UNICODE ) {
php_u_trim ( Z_USTRVAL_PP ( str ) , Z_USTRLEN_PP ( str ) , Z_USTRVAL_PP ( what ) , Z_USTRLEN_PP ( what ) , return_value , mode TSRMLS_CC ) ;
2005-08-11 23:36:07 +00:00
} else {
2005-08-17 11:36:32 +00:00
php_trim ( Z_STRVAL_PP ( str ) , Z_STRLEN_PP ( str ) , Z_STRVAL_PP ( what ) , Z_STRLEN_PP ( what ) , Z_TYPE_PP ( str ) , return_value , mode TSRMLS_CC ) ;
2005-08-11 23:36:07 +00:00
}
2002-07-13 04:13:43 +00:00
} else {
2005-08-17 11:36:32 +00:00
if ( Z_TYPE_PP ( str ) = = IS_UNICODE ) {
php_u_trim ( Z_USTRVAL_PP ( str ) , Z_USTRLEN_PP ( str ) , NULL , 0 , return_value , mode TSRMLS_CC ) ;
2005-08-11 23:36:07 +00:00
} else {
2005-08-17 11:36:32 +00:00
php_trim ( Z_STRVAL_PP ( str ) , Z_STRLEN_PP ( str ) , NULL , 0 , Z_TYPE_PP ( str ) , return_value , mode TSRMLS_CC ) ;
2005-08-11 23:36:07 +00:00
}
2001-09-04 06:18:11 +00:00
}
1999-04-17 00:37:12 +00:00
}
/* }}} */
2001-09-04 10:44:28 +00:00
/* {{{ proto string trim(string str [, string character_mask])
Strips whitespace from the beginning and end of a string */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( trim )
1999-04-17 00:37:12 +00:00
{
2002-07-13 04:13:43 +00:00
php_do_trim ( INTERNAL_FUNCTION_PARAM_PASSTHRU , 3 ) ;
}
/* }}} */
1999-04-17 00:37:12 +00:00
2002-07-13 04:13:43 +00:00
/* {{{ proto string rtrim(string str [, string character_mask])
Removes trailing whitespace */
PHP_FUNCTION ( rtrim )
{
php_do_trim ( INTERNAL_FUNCTION_PARAM_PASSTHRU , 2 ) ;
1999-04-17 00:37:12 +00:00
}
/* }}} */
2001-09-04 10:44:28 +00:00
/* {{{ proto string ltrim(string str [, string character_mask])
Strips whitespace from the beginning of a string */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( ltrim )
1999-04-17 00:37:12 +00:00
{
2002-07-13 04:13:43 +00:00
php_do_trim ( INTERNAL_FUNCTION_PARAM_PASSTHRU , 1 ) ;
1999-04-17 00:37:12 +00:00
}
/* }}} */
2003-03-09 00:05:48 +00:00
/* {{{ proto string wordwrap(string str [, int width [, string break [, boolean cut]]])
2001-09-04 10:44:28 +00:00
Wraps buffer to selected number of characters using string break char */
2000-07-22 01:12:24 +00:00
PHP_FUNCTION ( wordwrap )
{
2002-01-05 20:46:43 +00:00
const char * text , * breakchar = " \n " ;
char * newtext ;
2002-12-12 13:38:19 +00:00
int textlen , breakcharlen = 1 , newtextlen , alloced , chk ;
2002-01-05 20:46:43 +00:00
long current = 0 , laststart = 0 , lastspace = 0 ;
long linelength = 75 ;
2002-01-04 19:48:08 +00:00
zend_bool docut = 0 ;
2000-07-22 01:12:24 +00:00
2002-01-04 19:48:08 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " s|lsb " , & text , & textlen , & linelength , & breakchar , & breakcharlen , & docut ) = = FAILURE ) {
return ;
2000-07-22 01:12:24 +00:00
}
2004-07-06 22:16:42 +00:00
if ( textlen = = 0 ) {
RETURN_EMPTY_STRING ( ) ;
}
2000-07-22 01:12:24 +00:00
2002-01-04 19:48:08 +00:00
if ( linelength = = 0 & & docut ) {
2002-09-05 11:29:31 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Can't force cut when width is zero. " ) ;
2002-01-04 19:48:08 +00:00
RETURN_FALSE ;
2000-09-11 20:06:24 +00:00
}
2000-07-22 01:12:24 +00:00
/* Special case for a single-character break as it needs no
additional storage space */
2002-01-04 19:48:08 +00:00
if ( breakcharlen = = 1 & & ! docut ) {
newtext = estrndup ( text , textlen ) ;
2001-09-04 06:18:11 +00:00
2002-01-05 20:46:43 +00:00
laststart = lastspace = 0 ;
for ( current = 0 ; current < textlen ; current + + ) {
if ( text [ current ] = = breakchar [ 0 ] ) {
laststart = lastspace = current ;
2002-10-10 18:41:24 +00:00
} else if ( text [ current ] = = ' ' ) {
2002-01-05 20:46:43 +00:00
if ( current - laststart > = linelength ) {
newtext [ current ] = breakchar [ 0 ] ;
2004-05-13 17:44:23 +00:00
laststart = current + 1 ;
2000-07-22 01:12:24 +00:00
}
2002-01-05 20:46:43 +00:00
lastspace = current ;
2002-10-10 18:41:24 +00:00
} else if ( current - laststart > = linelength & & laststart ! = lastspace ) {
2002-01-05 20:46:43 +00:00
newtext [ lastspace ] = breakchar [ 0 ] ;
laststart = lastspace ;
2000-07-22 01:12:24 +00:00
}
}
2001-09-04 06:18:11 +00:00
2002-01-04 19:48:08 +00:00
RETURN_STRINGL ( newtext , textlen , 0 ) ;
2002-10-10 18:41:24 +00:00
} else {
2002-01-05 20:46:43 +00:00
/* Multiple character line break or forced cut */
if ( linelength > 0 ) {
2002-12-12 13:38:19 +00:00
chk = ( int ) ( textlen / linelength + 1 ) ;
alloced = textlen + chk * breakcharlen + 1 ;
2002-10-10 18:41:24 +00:00
} else {
2002-12-12 13:38:19 +00:00
chk = textlen ;
alloced = textlen * ( breakcharlen + 1 ) + 1 ;
2002-01-05 20:46:43 +00:00
}
2002-12-12 13:38:19 +00:00
newtext = emalloc ( alloced ) ;
2002-01-05 20:46:43 +00:00
/* now keep track of the actual new text length */
newtextlen = 0 ;
laststart = lastspace = 0 ;
for ( current = 0 ; current < textlen ; current + + ) {
2002-12-12 13:38:19 +00:00
if ( chk < = 0 ) {
alloced + = ( int ) ( ( ( textlen - current + 1 ) / linelength + 1 ) * breakcharlen ) + 1 ;
newtext = erealloc ( newtext , alloced ) ;
chk = ( int ) ( ( textlen - current ) / linelength ) + 1 ;
}
2002-01-05 20:46:43 +00:00
/* when we hit an existing break, copy to new buffer, and
* fix up laststart and lastspace */
if ( text [ current ] = = breakchar [ 0 ]
& & current + breakcharlen < textlen
& & ! strncmp ( text + current , breakchar , breakcharlen ) ) {
memcpy ( newtext + newtextlen , text + laststart , current - laststart + breakcharlen ) ;
newtextlen + = current - laststart + breakcharlen ;
current + = breakcharlen - 1 ;
laststart = lastspace = current + 1 ;
2002-12-12 13:38:19 +00:00
chk - - ;
2000-07-22 01:12:24 +00:00
}
2002-01-05 20:46:43 +00:00
/* if it is a space, check if it is at the line boundary,
* copy and insert a break , or just keep track of it */
else if ( text [ current ] = = ' ' ) {
if ( current - laststart > = linelength ) {
memcpy ( newtext + newtextlen , text + laststart , current - laststart ) ;
newtextlen + = current - laststart ;
memcpy ( newtext + newtextlen , breakchar , breakcharlen ) ;
newtextlen + = breakcharlen ;
laststart = current + 1 ;
2002-12-12 13:38:19 +00:00
chk - - ;
2000-07-22 01:12:24 +00:00
}
2002-01-05 20:46:43 +00:00
lastspace = current ;
2000-07-22 01:12:24 +00:00
}
2002-01-05 20:46:43 +00:00
/* if we are cutting, and we've accumulated enough
2002-01-05 23:49:58 +00:00
* characters , and we haven ' t see a space for this line ,
* copy and insert a break . */
else if ( current - laststart > = linelength
& & docut & & laststart > = lastspace ) {
2002-01-05 20:46:43 +00:00
memcpy ( newtext + newtextlen , text + laststart , current - laststart ) ;
newtextlen + = current - laststart ;
memcpy ( newtext + newtextlen , breakchar , breakcharlen ) ;
newtextlen + = breakcharlen ;
laststart = lastspace = current ;
2002-12-12 13:38:19 +00:00
chk - - ;
2002-01-05 20:46:43 +00:00
}
/* if the current word puts us over the linelength, copy
* back up until the last space , insert a break , and move
* up the laststart */
else if ( current - laststart > = linelength
& & laststart < lastspace ) {
memcpy ( newtext + newtextlen , text + laststart , lastspace - laststart ) ;
newtextlen + = lastspace - laststart ;
memcpy ( newtext + newtextlen , breakchar , breakcharlen ) ;
newtextlen + = breakcharlen ;
laststart = lastspace = lastspace + 1 ;
2002-12-12 13:38:19 +00:00
chk - - ;
2000-07-22 01:12:24 +00:00
}
}
2002-01-05 20:46:43 +00:00
/* copy over any stragglers */
if ( laststart ! = current ) {
memcpy ( newtext + newtextlen , text + laststart , current - laststart ) ;
newtextlen + = current - laststart ;
2000-07-22 01:12:24 +00:00
}
2002-01-05 20:46:43 +00:00
newtext [ newtextlen ] = ' \0 ' ;
2002-09-05 14:00:28 +00:00
/* free unused memory */
newtext = erealloc ( newtext , newtextlen + 1 ) ;
2002-01-05 20:46:43 +00:00
RETURN_STRINGL ( newtext , newtextlen , 0 ) ;
2000-07-22 01:12:24 +00:00
}
}
/* }}} */
2001-06-05 13:12:10 +00:00
/* {{{ php_explode
*/
2005-08-11 23:36:07 +00:00
PHPAPI void php_explode ( char * delim , uint delim_len , char * str , uint str_len , zend_uchar str_type , zval * return_value , int limit )
1999-04-17 00:37:12 +00:00
{
1999-12-06 18:29:22 +00:00
char * p1 , * p2 , * endp ;
1999-04-17 00:37:12 +00:00
2005-08-11 23:36:07 +00:00
endp = str + str_len ;
p1 = str ;
p2 = php_memnstr ( str , delim , delim_len , endp ) ;
1999-12-06 18:29:22 +00:00
2005-08-11 23:36:07 +00:00
if ( p2 = = NULL ) {
if ( str_type = = IS_BINARY ) {
add_next_index_binaryl ( return_value , p1 , str_len , 1 ) ;
} else {
add_next_index_stringl ( return_value , p1 , str_len , 1 ) ;
}
1999-12-07 08:14:00 +00:00
} else {
do {
2005-08-11 23:36:07 +00:00
if ( str_type = = IS_BINARY ) {
add_next_index_binaryl ( return_value , p1 , p2 - p1 , 1 ) ;
} else {
add_next_index_stringl ( return_value , p1 , p2 - p1 , 1 ) ;
}
p1 = p2 + delim_len ;
} while ( ( p2 = php_memnstr ( p1 , delim , delim_len , endp ) ) ! = NULL & &
( limit = = - 1 | | - - limit > 1 ) ) ;
1999-12-07 08:14:00 +00:00
2005-08-11 23:36:07 +00:00
if ( p1 < = endp ) {
if ( str_type = = IS_BINARY ) {
add_next_index_binaryl ( return_value , p1 , endp - p1 , 1 ) ;
} else {
add_next_index_stringl ( return_value , p1 , endp - p1 , 1 ) ;
}
}
1999-04-17 00:37:12 +00:00
}
}
2001-06-05 13:12:10 +00:00
/* }}} */
1999-04-17 00:37:12 +00:00
2004-07-18 11:34:28 +00:00
/* {{{ php_explode_negative_limit
*/
2005-08-11 23:36:07 +00:00
PHPAPI void php_explode_negative_limit ( char * delim , uint delim_len , char * str , uint str_len , zend_uchar str_type , zval * return_value , int limit )
2004-07-18 11:34:28 +00:00
{
# define EXPLODE_ALLOC_STEP 50
char * p1 , * p2 , * endp ;
int allocated = EXPLODE_ALLOC_STEP , found = 0 , i = 0 , to_return = 0 ;
char * * positions = safe_emalloc ( allocated , sizeof ( char * ) , 0 ) ;
2005-08-11 23:36:07 +00:00
endp = str + str_len ;
p1 = str ;
p2 = php_memnstr ( str , delim , delim_len , endp ) ;
2004-07-18 11:34:28 +00:00
2005-08-11 23:36:07 +00:00
if ( p2 = = NULL ) {
2004-07-18 11:34:28 +00:00
/*
do nothing since limit < = - 1 , thus if only one chunk - 1 + ( limit ) < = 0
by doing nothing we return empty array
*/
} else {
positions [ found + + ] = p1 ;
do {
2005-08-11 23:36:07 +00:00
if ( found > = allocated ) {
2004-07-18 11:34:28 +00:00
allocated = found + EXPLODE_ALLOC_STEP ; /* make sure we have enough memory */
positions = erealloc ( positions , allocated * sizeof ( char * ) ) ;
}
2005-08-11 23:36:07 +00:00
positions [ found + + ] = p1 = p2 + delim_len ;
} while ( ( p2 = php_memnstr ( p1 , delim , delim_len , endp ) ) ! = NULL ) ;
2004-07-18 11:34:28 +00:00
to_return = limit + found ;
/* limit is at least -1 therefore no need of bounds checking : i will be always less than found */
2005-08-11 23:36:07 +00:00
for ( i = 0 ; i < to_return ; i + + ) { /* this checks also for to_return > 0 */
if ( str_type = = IS_BINARY ) {
add_next_index_binaryl ( return_value , positions [ i ] ,
( positions [ i + 1 ] - delim_len ) - positions [ i ] , 1 ) ;
} else {
add_next_index_stringl ( return_value , positions [ i ] ,
( positions [ i + 1 ] - delim_len ) - positions [ i ] , 1 ) ;
}
2004-07-18 11:34:28 +00:00
}
}
efree ( positions ) ;
# undef EXPLODE_ALLOC_STEP
}
/* }}} */
2005-08-11 23:36:07 +00:00
/* {{{ php_u_explode
* Unicode capable version of php_explode ( )
*/
static void php_u_explode ( UChar * delim , uint delim_len , UChar * str , uint str_len , zval * return_value , int limit )
{
UChar * p1 , * p2 , * endp ;
endp = str + str_len ;
p1 = str ;
p2 = zend_u_memnstr ( str , delim , delim_len , endp ) ;
if ( p2 = = NULL ) {
add_next_index_unicodel ( return_value , p1 , str_len , 1 ) ;
} else {
do {
add_next_index_unicodel ( return_value , p1 , p2 - p1 , 1 ) ;
p1 = ( UChar * ) p2 + delim_len ;
} while ( ( p2 = zend_u_memnstr ( p1 , delim , delim_len , endp ) ) ! = NULL & &
( limit = = - 1 | | - - limit > 1 ) ) ;
if ( p1 < = endp ) {
add_next_index_unicodel ( return_value , p1 , endp - p1 , 1 ) ;
}
}
}
/* }}} */
/* {{{ php_u_explode_negative_limit
* Unicode capable version of php_explode_negative_limit ( )
*/
static void php_u_explode_negative_limit ( UChar * delim , uint delim_len , UChar * str , uint str_len , zval * return_value , int limit )
{
# define EXPLODE_ALLOC_STEP 50
UChar * p1 , * p2 , * endp ;
int allocated = EXPLODE_ALLOC_STEP , found = 0 , i = 0 , to_return = 0 ;
UChar * * positions = safe_emalloc ( allocated , sizeof ( UChar * ) , 0 ) ;
endp = str + str_len ;
p1 = str ;
p2 = zend_u_memnstr ( str , delim , delim_len , endp ) ;
if ( p2 = = NULL ) {
/*
do nothing since limit < = - 1 , thus if only one chunk - 1 + ( limit ) < = 0
by doing nothing we return empty array
*/
} else {
positions [ found + + ] = p1 ;
do {
if ( found > = allocated ) {
allocated = found + EXPLODE_ALLOC_STEP ; /* make sure we have enough memory */
positions = erealloc ( positions , allocated * sizeof ( UChar * ) ) ;
}
positions [ found + + ] = p1 = p2 + delim_len ;
} while ( ( p2 = zend_u_memnstr ( p1 , delim , delim_len , endp ) ) ! = NULL ) ;
to_return = limit + found ;
/* limit is at least -1 therefore no need of bounds checking : i will be always less than found */
for ( i = 0 ; i < to_return ; i + + ) { /* this checks also for to_return > 0 */
add_next_index_unicodel ( return_value , positions [ i ] ,
( positions [ i + 1 ] - delim_len ) - positions [ i ] , 1 ) ;
}
}
efree ( positions ) ;
# undef EXPLODE_ALLOC_STEP
}
/* }}} */
2004-07-18 11:34:28 +00:00
2000-05-23 05:04:28 +00:00
/* {{{ proto array explode(string separator, string str [, int limit])
2004-07-18 11:34:28 +00:00
Splits a string on string separator and return array of components . If limit is positive only limit number of components is returned . If limit is negative all components except the last abs ( limit ) are returned . */
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
PHP_FUNCTION ( explode )
1999-04-17 00:37:12 +00:00
{
2005-08-11 23:36:07 +00:00
void * str , * delim ;
int32_t str_len , delim_len ;
zend_uchar str_type , delim_type ;
int limit = - 1 ;
int argc = ZEND_NUM_ARGS ( ) ;
2000-05-23 05:04:28 +00:00
2005-08-11 23:36:07 +00:00
if ( argc < 2 | | argc > 3 ) {
2000-05-20 05:07:53 +00:00
WRONG_PARAM_COUNT ;
}
1999-04-17 00:37:12 +00:00
2005-08-11 23:36:07 +00:00
if ( zend_parse_parameters ( argc TSRMLS_CC , " TT|l " , & delim , & delim_len , & delim_type ,
& str , & str_len , & str_type , & limit ) = = FAILURE ) {
return ;
2001-09-04 06:18:11 +00:00
}
2005-08-11 23:36:07 +00:00
if ( delim_len = = 0 ) {
2002-09-05 11:29:31 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Empty delimiter. " ) ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
RETURN_FALSE ;
}
1999-12-06 18:29:22 +00:00
2002-12-05 22:28:02 +00:00
array_init ( return_value ) ;
1999-12-06 18:29:22 +00:00
2005-08-11 23:36:07 +00:00
if ( str_len = = 0 ) {
if ( str_type = = IS_UNICODE ) {
2005-08-15 19:12:59 +00:00
add_next_index_unicodel ( return_value , USTR_MAKE ( " " ) , sizeof ( " " ) - 1 , 0 ) ;
2005-08-11 23:36:07 +00:00
} else if ( str_type = = IS_BINARY ) {
add_next_index_binaryl ( return_value , " " , sizeof ( " " ) - 1 , 1 ) ;
} else {
add_next_index_stringl ( return_value , " " , sizeof ( " " ) - 1 , 1 ) ;
}
2004-07-18 11:34:28 +00:00
return ;
}
2001-09-04 06:18:11 +00:00
if ( limit = = 0 | | limit = = 1 ) {
2005-08-11 23:36:07 +00:00
if ( str_type = = IS_UNICODE ) {
add_index_unicodel ( return_value , 0 , ( UChar * ) str , str_len , 1 ) ;
} else if ( str_type = = IS_BINARY ) {
2005-08-12 05:58:02 +00:00
add_index_binaryl ( return_value , 0 , ( char * ) str , str_len , 1 TSRMLS_CC ) ;
2005-08-11 23:36:07 +00:00
} else {
add_index_stringl ( return_value , 0 , ( char * ) str , str_len , 1 ) ;
}
2004-07-18 11:34:28 +00:00
} else if ( limit < 0 & & argc = = 3 ) {
2005-08-11 23:36:07 +00:00
if ( str_type = = IS_UNICODE ) {
php_u_explode_negative_limit ( ( UChar * ) delim , delim_len , ( UChar * ) str , str_len , return_value , limit ) ;
} else {
php_explode_negative_limit ( ( char * ) delim , delim_len , ( char * ) str , str_len , str_type , return_value , limit ) ;
}
2000-05-23 05:04:28 +00:00
} else {
2005-08-11 23:36:07 +00:00
if ( str_type = = IS_UNICODE ) {
php_u_explode ( ( UChar * ) delim , delim_len , ( UChar * ) str , str_len , return_value , limit ) ;
} else {
php_explode ( ( char * ) delim , delim_len , ( char * ) str , str_len , str_type , return_value , limit ) ;
}
2000-05-23 05:04:28 +00:00
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
}
/* }}} */
/* {{{ proto string join(array src, string glue)
An alias for implode */
/* }}} */
2001-06-05 13:12:10 +00:00
/* {{{ php_implode
*/
2005-08-16 12:04:13 +00:00
PHPAPI void php_implode ( zval * delim , zval * arr , zval * retval )
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
{
2005-08-16 12:04:13 +00:00
zend_uchar return_type ;
2005-08-17 07:14:12 +00:00
int numelems , i = 0 ;
2005-08-16 12:04:13 +00:00
HashPosition pos ;
zval * * tmp ;
2005-08-16 20:22:33 +00:00
TSRMLS_FETCH ( ) ;
2005-08-16 12:04:13 +00:00
Z_TYPE_P ( retval ) = return_type = Z_TYPE_P ( delim ) ; /* ... to start off */
2002-10-04 20:44:19 +00:00
2005-08-16 12:04:13 +00:00
/* Setup return value */
if ( return_type = = IS_UNICODE ) {
ZVAL_EMPTY_UNICODE ( retval ) ;
} else if ( return_type = = IS_BINARY ) {
ZVAL_EMPTY_BINARY ( retval ) ;
} else {
ZVAL_EMPTY_STRING ( retval ) ;
}
1999-04-17 00:37:12 +00:00
2005-08-16 12:04:13 +00:00
numelems = zend_hash_num_elements ( Z_ARRVAL_P ( arr ) ) ;
2003-02-08 15:26:17 +00:00
if ( numelems = = 0 ) {
2005-08-16 12:04:13 +00:00
return ;
2002-10-06 11:28:11 +00:00
}
2001-09-04 06:18:11 +00:00
zend_hash_internal_pointer_reset_ex ( Z_ARRVAL_P ( arr ) , & pos ) ;
2005-08-17 07:14:12 +00:00
while ( zend_hash_get_current_data_ex ( Z_ARRVAL_P ( arr ) , ( void * * ) & tmp , & pos ) = = SUCCESS ) {
2005-08-16 12:04:13 +00:00
if ( Z_TYPE_PP ( tmp ) ! = return_type ) {
/* Convert to common type, if possible */
if ( return_type = = IS_UNICODE ) {
if ( Z_TYPE_PP ( tmp ) = = IS_BINARY ) {
/* ERROR */
2005-08-17 07:14:12 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING ,
" Cannot mix binary strings with other string types " ) ;
2005-08-16 12:04:13 +00:00
efree ( Z_USTRVAL_P ( retval ) ) ;
ZVAL_FALSE ( retval ) ;
return ;
} else {
SEPARATE_ZVAL ( tmp ) ;
convert_to_unicode_ex ( tmp ) ;
}
} else if ( return_type = = IS_BINARY ) {
if ( Z_TYPE_PP ( tmp ) = = IS_UNICODE | | Z_TYPE_PP ( tmp ) = = IS_STRING ) {
/* ERROR */
2005-08-17 07:14:12 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING ,
" Cannot mix binary strings with other string types " ) ;
2005-08-16 12:04:13 +00:00
efree ( Z_BINVAL_P ( retval ) ) ;
ZVAL_FALSE ( retval ) ;
return ;
} else {
SEPARATE_ZVAL ( tmp ) ;
convert_to_binary_ex ( tmp ) ;
}
} else {
if ( Z_TYPE_PP ( tmp ) = = IS_UNICODE ) {
/* Convert IS_STRING up to IS_UNICODE */
convert_to_unicode_ex ( & retval ) ;
convert_to_unicode_ex ( & delim ) ;
Z_TYPE_P ( retval ) = return_type = IS_UNICODE ;
} else if ( Z_TYPE_PP ( tmp ) = = IS_BINARY ) {
/* ERROR */
2005-08-17 07:14:12 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING ,
" Cannot mix binary strings with other string types " ) ;
2005-08-16 12:04:13 +00:00
efree ( Z_STRVAL_P ( retval ) ) ;
ZVAL_FALSE ( retval ) ;
return ;
} else {
SEPARATE_ZVAL ( tmp ) ;
convert_to_string_ex ( tmp ) ;
}
}
}
/* Append elem */
if ( return_type = = IS_UNICODE ) {
Z_USTRVAL_P ( retval ) = eurealloc ( Z_USTRVAL_P ( retval ) ,
2005-08-17 07:59:31 +00:00
UBYTES ( Z_USTRLEN_P ( retval ) + Z_USTRLEN_PP ( tmp ) + 1 ) ) ;
memcpy ( Z_USTRVAL_P ( retval ) + Z_USTRLEN_P ( retval ) , Z_USTRVAL_PP ( tmp ) , UBYTES ( Z_USTRLEN_PP ( tmp ) + 1 ) ) ;
2005-08-16 12:04:13 +00:00
Z_USTRLEN_P ( retval ) + = Z_USTRLEN_PP ( tmp ) ;
2005-08-17 07:14:12 +00:00
if ( + + i < numelems ) { /* Append delim */
2005-08-16 12:04:13 +00:00
Z_USTRVAL_P ( retval ) = eurealloc ( Z_USTRVAL_P ( retval ) ,
2005-08-17 07:59:31 +00:00
UBYTES ( Z_USTRLEN_P ( retval ) + Z_USTRLEN_P ( delim ) + 1 ) ) ;
memcpy ( Z_USTRVAL_P ( retval ) + Z_USTRLEN_P ( retval ) , Z_USTRVAL_P ( delim ) , UBYTES ( Z_USTRLEN_P ( delim ) + 1 ) ) ;
2005-08-16 12:04:13 +00:00
Z_USTRLEN_P ( retval ) + = Z_USTRLEN_P ( delim ) ;
}
} else {
Z_STRVAL_P ( retval ) = ( char * ) erealloc ( Z_STRVAL_P ( retval ) ,
2005-08-17 07:59:31 +00:00
Z_STRLEN_P ( retval ) + Z_STRLEN_PP ( tmp ) + 1 ) ;
memcpy ( Z_STRVAL_P ( retval ) + Z_STRLEN_P ( retval ) , Z_STRVAL_PP ( tmp ) , Z_STRLEN_PP ( tmp ) + 1 ) ;
2005-08-16 12:04:13 +00:00
Z_STRLEN_P ( retval ) + = Z_STRLEN_PP ( tmp ) ;
2005-08-17 07:14:12 +00:00
if ( + + i < numelems ) { /* Append delim */
2005-08-16 12:04:13 +00:00
Z_STRVAL_P ( retval ) = ( char * ) erealloc ( Z_STRVAL_P ( retval ) ,
2005-08-17 07:59:31 +00:00
Z_STRLEN_P ( retval ) + Z_STRLEN_P ( delim ) + 1 ) ;
memcpy ( Z_STRVAL_P ( retval ) + Z_STRLEN_P ( retval ) , Z_STRVAL_P ( delim ) , Z_STRLEN_P ( delim ) + 1 ) ;
2005-08-16 12:04:13 +00:00
Z_STRLEN_P ( retval ) + = Z_STRLEN_P ( delim ) ;
}
}
2005-08-17 07:14:12 +00:00
zend_hash_move_forward_ex ( Z_ARRVAL_P ( arr ) , & pos ) ;
1999-04-17 00:37:12 +00:00
}
2002-10-04 20:44:19 +00:00
2005-08-16 12:04:13 +00:00
return ;
1999-04-17 00:37:12 +00:00
}
2001-06-05 13:12:10 +00:00
/* }}} */
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
2002-10-04 16:53:14 +00:00
/* {{{ proto string implode([string glue,] array pieces)
2001-09-04 10:44:28 +00:00
Joins array elements placing glue string between items and return one string */
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
PHP_FUNCTION ( implode )
{
2005-08-16 12:04:13 +00:00
zval * * arg1 = NULL , * * arg2 = NULL ;
zval * delim , * arr ;
int argc = ZEND_NUM_ARGS ( ) ;
2002-10-04 16:53:14 +00:00
2005-08-16 12:04:13 +00:00
if ( argc < 1 | | argc > 2 ) {
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
WRONG_PARAM_COUNT ;
}
2005-08-16 12:04:13 +00:00
if ( zend_get_parameters_ex ( argc , & arg1 , & arg2 ) = = FAILURE ) {
return ;
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
2002-10-04 16:53:14 +00:00
if ( argc = = 1 ) {
if ( Z_TYPE_PP ( arg1 ) ! = IS_ARRAY ) {
2002-10-04 16:55:46 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Argument to implode must be an array. " ) ;
2005-08-16 12:04:13 +00:00
RETURN_FALSE ;
} else {
SEPARATE_ZVAL ( arg1 ) ;
arr = * arg1 ;
2005-08-17 07:14:12 +00:00
MAKE_STD_ZVAL ( delim ) ;
if ( UG ( unicode ) ) {
2005-08-17 07:59:31 +00:00
ZVAL_UNICODEL ( delim , EMPTY_STR , sizeof ( " " ) - 1 , 0 ) ;
2005-08-17 07:14:12 +00:00
} else {
ZVAL_STRINGL ( delim , " " , sizeof ( " " ) - 1 , 0 ) ;
}
2002-10-04 16:53:14 +00:00
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
} else {
2002-10-04 16:53:14 +00:00
if ( Z_TYPE_PP ( arg1 ) = = IS_ARRAY ) {
SEPARATE_ZVAL ( arg1 ) ;
arr = * arg1 ;
2005-08-17 07:59:31 +00:00
if ( Z_TYPE_PP ( arg2 ) ! = IS_UNICODE & & Z_TYPE_PP ( arg2 ) ! = IS_BINARY & & Z_TYPE_PP ( arg2 ) ! = IS_STRING ) {
convert_to_text_ex ( arg2 ) ;
}
2002-10-04 16:53:14 +00:00
delim = * arg2 ;
} else if ( Z_TYPE_PP ( arg2 ) = = IS_ARRAY ) {
SEPARATE_ZVAL ( arg2 ) ;
arr = * arg2 ;
2005-08-17 07:59:31 +00:00
if ( Z_TYPE_PP ( arg1 ) ! = IS_UNICODE & & Z_TYPE_PP ( arg1 ) ! = IS_BINARY & & Z_TYPE_PP ( arg1 ) ! = IS_STRING ) {
convert_to_text_ex ( arg1 ) ;
}
2002-10-04 16:53:14 +00:00
delim = * arg1 ;
} else {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Bad arguments. " ) ;
2005-08-16 12:04:13 +00:00
RETURN_FALSE ;
2002-10-04 16:53:14 +00:00
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
}
2001-09-04 06:18:11 +00:00
1999-10-16 16:35:20 +00:00
php_implode ( delim , arr , return_value ) ;
2002-11-05 13:52:11 +00:00
2003-11-27 01:08:42 +00:00
if ( argc = = 1 ) {
2002-11-05 13:52:11 +00:00
FREE_ZVAL ( delim ) ;
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
}
1999-04-17 00:37:12 +00:00
/* }}} */
2001-08-03 23:09:05 +00:00
# define STRTOK_TABLE(p) BG(strtok_table)[(unsigned char) *p]
2002-09-11 14:51:09 +00:00
/* {{{ proto string strtok([string str,] string token)
1999-04-17 00:37:12 +00:00
Tokenize a string */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( strtok )
1999-04-17 00:37:12 +00:00
{
2005-09-29 11:05:30 +00:00
void * tok , * str ;
int32_t tok_len , str_len ;
zend_uchar tok_type , str_type ;
zval * zv ;
char * token , * token_end , * p , * pe ;
UChar * u_token , * u_p , * u_pe ;
UChar32 ch , th ;
int32_t start , end , i , j , rem_len ;
int delim_found , token_present ;
2001-11-02 19:19:24 +00:00
int skipped = 0 ;
2005-09-29 11:05:30 +00:00
if ( ZEND_NUM_ARGS ( ) < 1 | | ZEND_NUM_ARGS ( ) > 2 ) {
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
2003-02-08 15:26:17 +00:00
}
2005-09-29 11:05:30 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " T|T " ,
& str , & str_len , & str_type ,
& tok , & tok_len , & tok_type ) = = FAILURE ) {
return ;
}
2003-02-08 15:26:17 +00:00
switch ( ZEND_NUM_ARGS ( ) ) {
case 1 :
2005-09-29 11:05:30 +00:00
tok = str ;
tok_len = str_len ;
tok_type = str_type ;
2003-02-08 15:26:17 +00:00
break ;
default :
case 2 :
if ( BG ( strtok_zval ) ) {
zval_ptr_dtor ( & BG ( strtok_zval ) ) ;
}
2005-09-29 11:05:30 +00:00
MAKE_STD_ZVAL ( zv ) ;
if ( str_type = = IS_UNICODE ) {
ZVAL_UNICODEL ( zv , ( UChar * ) str , str_len , 1 ) ;
} else if ( str_type = = IS_BINARY ) {
ZVAL_BINARYL ( zv , ( char * ) str , str_len , 1 ) ;
} else {
ZVAL_STRINGL ( zv , ( char * ) str , str_len , 1 ) ;
}
BG ( strtok_zval ) = zv ;
if ( str_type = = IS_UNICODE ) {
BG ( strtok_last ) = BG ( strtok_string ) = Z_USTRVAL_P ( zv ) ;
} else {
BG ( strtok_last ) = BG ( strtok_string ) = Z_STRVAL_P ( zv ) ;
}
BG ( strtok_len ) = str_len ;
2003-02-08 15:26:17 +00:00
break ;
2001-08-03 08:49:19 +00:00
}
2005-09-29 11:05:30 +00:00
if ( BG ( strtok_zval ) & & tok_type ! = Z_TYPE_P ( BG ( strtok_zval ) ) ) {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Delimiter type must match string type. " ) ;
2001-08-03 23:09:05 +00:00
RETURN_FALSE ;
2003-02-08 15:26:17 +00:00
}
2001-08-03 23:09:05 +00:00
2005-09-29 11:05:30 +00:00
if ( tok_type = = IS_UNICODE ) {
u_p = ( UChar * ) BG ( strtok_last ) ; /* Where we start to search */
u_pe = ( UChar * ) BG ( strtok_string ) + BG ( strtok_len ) ;
u_token = ( UChar * ) tok ;
if ( ! u_p | | u_p > = u_pe ) {
RETURN_FALSE ;
}
rem_len = u_pe - u_p ;
/* Skip leading delimiters */
token_present = 0 ;
for ( i = 0 ; ( u_p + i ) < u_pe ; ) {
delim_found = 0 ;
U16_NEXT ( u_p , i , rem_len , ch ) ;
for ( j = 0 ; j < tok_len ; ) {
U16_NEXT ( u_token , j , tok_len , th ) ;
if ( ch = = th ) {
delim_found = 1 ;
break ;
}
}
if ( delim_found = = 0 ) {
U16_BACK_1 ( u_p , 0 , i ) ; /* U16_NEXT() post-incrs 'i' */
start = i ;
token_present = 1 ;
break ;
}
}
if ( token_present = = 0 ) {
BG ( strtok_last ) = NULL ;
RETURN_FALSE ;
}
2001-08-03 23:09:05 +00:00
2005-09-29 11:05:30 +00:00
/* Seek to next delimiter */
delim_found = 0 ;
for ( i = start ; ( u_p + i ) < u_pe ; ) {
U16_NEXT ( u_p , i , rem_len , ch ) ;
for ( j = 0 ; j < tok_len ; ) {
U16_NEXT ( u_token , j , tok_len , th ) ;
if ( ch = = th ) {
delim_found = 1 ;
break ;
}
}
if ( delim_found ) {
U16_BACK_1 ( u_p , 0 , i ) ; /* 'i' was beyond delimiter */
break ;
}
}
end = i ;
if ( end - start ) {
BG ( strtok_last ) = u_p + end ;
RETURN_UNICODEL ( u_p + start , end - start , 1 ) ;
} else {
2001-08-03 23:09:05 +00:00
BG ( strtok_last ) = NULL ;
2005-09-29 11:05:30 +00:00
RETURN_FALSE ;
2001-08-03 22:42:02 +00:00
}
2005-09-29 11:05:30 +00:00
} else {
p = ( char * ) BG ( strtok_last ) ; /* Where we start to search */
pe = ( char * ) BG ( strtok_string ) + BG ( strtok_len ) ;
if ( ! p | | p > = pe ) {
RETURN_FALSE ;
2003-02-08 15:26:17 +00:00
}
2005-09-29 11:05:30 +00:00
token = ( char * ) tok ;
token_end = token + tok_len ;
while ( token < token_end ) {
STRTOK_TABLE ( token + + ) = 1 ;
}
/* Skip leading delimiters */
while ( STRTOK_TABLE ( p ) ) {
if ( + + p > = pe ) {
/* no other chars left */
BG ( strtok_last ) = NULL ;
RETVAL_FALSE ;
goto restore ;
}
skipped + + ;
}
/* We know at this place that *p is no delimiter, so skip it */
while ( + + p < pe ) {
if ( STRTOK_TABLE ( p ) ) {
goto return_token ;
}
}
if ( p - ( char * ) BG ( strtok_last ) ) {
2001-08-03 23:09:05 +00:00
return_token :
2005-09-29 11:05:30 +00:00
if ( tok_type = = IS_BINARY ) {
RETVAL_BINARYL ( ( char * ) BG ( strtok_last ) + skipped , ( p - ( char * ) BG ( strtok_last ) ) - skipped , 1 ) ;
} else {
RETVAL_STRINGL ( ( char * ) BG ( strtok_last ) + skipped , ( p - ( char * ) BG ( strtok_last ) ) - skipped , 1 ) ;
}
BG ( strtok_last ) = p + 1 ;
} else {
RETVAL_FALSE ;
BG ( strtok_last ) = NULL ;
}
2001-08-03 23:09:05 +00:00
2005-09-29 11:05:30 +00:00
/* Restore table -- usually faster then memset'ing the table on every invocation */
2001-08-03 23:09:05 +00:00
restore :
2005-09-29 11:05:30 +00:00
token = ( char * ) tok ;
while ( token < token_end ) {
STRTOK_TABLE ( token + + ) = 0 ;
}
2003-02-08 15:26:17 +00:00
}
1999-04-17 00:37:12 +00:00
}
/* }}} */
2001-06-05 13:12:10 +00:00
/* {{{ php_strtoupper
*/
1999-12-09 23:45:30 +00:00
PHPAPI char * php_strtoupper ( char * s , size_t len )
1999-04-17 00:37:12 +00:00
{
2002-10-25 20:09:53 +00:00
unsigned char * c , * e ;
2002-08-25 19:08:07 +00:00
1999-04-17 00:37:12 +00:00
c = s ;
2002-08-25 19:08:07 +00:00
e = c + len ;
2002-10-02 17:56:04 +00:00
while ( c < e ) {
2002-08-25 19:08:07 +00:00
* c = toupper ( * c ) ;
c + + ;
1999-04-17 00:37:12 +00:00
}
2001-09-04 06:18:11 +00:00
return s ;
1999-04-17 00:37:12 +00:00
}
2001-06-05 13:12:10 +00:00
/* }}} */
1999-04-17 00:37:12 +00:00
2005-08-11 23:36:07 +00:00
/* {{{ php_u_strtoupper
*/
PHPAPI UChar * php_u_strtoupper ( UChar * * s , int32_t * len , const char * locale )
{
UChar * dest = NULL ;
int32_t dest_len ;
UErrorCode status ;
dest_len = * len ;
while ( 1 ) {
status = U_ZERO_ERROR ;
dest = eurealloc ( dest , dest_len + 1 ) ;
dest_len = u_strToUpper ( dest , dest_len , * s , * len , locale , & status ) ;
if ( status ! = U_BUFFER_OVERFLOW_ERROR ) {
break ;
}
}
if ( U_SUCCESS ( status ) ) {
efree ( * s ) ;
dest [ dest_len ] = 0 ;
* s = dest ;
* len = dest_len ;
} else {
efree ( dest ) ;
}
return * s ;
}
/* }}} */
1999-04-17 00:37:12 +00:00
/* {{{ proto string strtoupper(string str)
2001-09-04 10:44:28 +00:00
Makes a string uppercase */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( strtoupper )
1999-04-17 00:37:12 +00:00
{
1999-12-23 14:59:01 +00:00
zval * * arg ;
1999-04-17 00:37:12 +00:00
2000-06-05 19:47:54 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( 1 , & arg ) ) {
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
}
2005-08-11 23:36:07 +00:00
if ( Z_TYPE_PP ( arg ) ! = IS_STRING & & Z_TYPE_PP ( arg ) ! = IS_UNICODE ) {
2005-08-23 09:33:46 +00:00
convert_to_text_ex ( arg ) ;
2005-08-11 23:36:07 +00:00
}
1999-04-17 00:37:12 +00:00
2005-03-10 23:42:15 +00:00
RETVAL_ZVAL ( * arg , 1 , 0 ) ;
2005-08-11 23:36:07 +00:00
if ( Z_TYPE_P ( return_value ) = = IS_UNICODE ) {
php_u_strtoupper ( & Z_USTRVAL_P ( return_value ) , & Z_USTRLEN_P ( return_value ) , UG ( default_locale ) ) ;
} else {
php_strtoupper ( Z_STRVAL_P ( return_value ) , Z_STRLEN_P ( return_value ) ) ;
}
}
/* }}} */
/* {{{ php_u_strtolower
*/
PHPAPI UChar * php_u_strtolower ( UChar * * s , int32_t * len , const char * locale )
{
UChar * dest = NULL ;
int32_t dest_len ;
UErrorCode status = U_ZERO_ERROR ;
dest_len = * len ;
while ( 1 ) {
status = U_ZERO_ERROR ;
dest = eurealloc ( dest , dest_len + 1 ) ;
dest_len = u_strToLower ( dest , dest_len , * s , * len , locale , & status ) ;
if ( status ! = U_BUFFER_OVERFLOW_ERROR ) {
break ;
}
}
if ( U_SUCCESS ( status ) ) {
efree ( * s ) ;
dest [ dest_len ] = 0 ;
* s = dest ;
* len = dest_len ;
} else {
efree ( dest ) ;
}
return * s ;
1999-04-17 00:37:12 +00:00
}
/* }}} */
2001-06-05 13:12:10 +00:00
/* {{{ php_strtolower
*/
1999-12-09 23:45:30 +00:00
PHPAPI char * php_strtolower ( char * s , size_t len )
1999-04-17 00:37:12 +00:00
{
2002-10-25 20:09:53 +00:00
unsigned char * c , * e ;
2002-08-25 19:08:07 +00:00
1999-04-17 00:37:12 +00:00
c = s ;
2002-08-25 19:08:07 +00:00
e = c + len ;
2002-10-02 17:56:04 +00:00
while ( c < e ) {
2002-08-25 19:08:07 +00:00
* c = tolower ( * c ) ;
c + + ;
1999-04-17 00:37:12 +00:00
}
2001-09-04 06:18:11 +00:00
return s ;
1999-04-17 00:37:12 +00:00
}
2001-06-05 13:12:10 +00:00
/* }}} */
1999-04-17 00:37:12 +00:00
/* {{{ proto string strtolower(string str)
2001-09-04 10:44:28 +00:00
Makes a string lowercase */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( strtolower )
1999-04-17 00:37:12 +00:00
{
1999-12-23 14:59:01 +00:00
zval * * str ;
1999-04-17 00:37:12 +00:00
2000-06-05 19:47:54 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( 1 , & str ) ) {
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
}
2005-08-11 23:36:07 +00:00
if ( Z_TYPE_PP ( str ) ! = IS_STRING & & Z_TYPE_PP ( str ) ! = IS_UNICODE ) {
2005-08-23 09:33:46 +00:00
convert_to_text_ex ( str ) ;
2005-08-11 23:36:07 +00:00
}
1999-04-17 00:37:12 +00:00
2005-03-10 23:42:15 +00:00
RETVAL_ZVAL ( * str , 1 , 0 ) ;
2005-08-11 23:36:07 +00:00
if ( Z_TYPE_P ( return_value ) = = IS_UNICODE ) {
php_u_strtolower ( & Z_USTRVAL_P ( return_value ) , & Z_USTRLEN_P ( return_value ) , UG ( default_locale ) ) ;
} else {
php_strtolower ( Z_STRVAL_P ( return_value ) , Z_STRLEN_P ( return_value ) ) ;
}
1999-04-17 00:37:12 +00:00
}
/* }}} */
2001-06-05 13:12:10 +00:00
/* {{{ php_basename
*/
2003-12-10 21:23:35 +00:00
PHPAPI void php_basename ( char * s , size_t len , char * suffix , size_t sufflen , char * * p_ret , size_t * p_len TSRMLS_DC )
1999-04-17 00:37:12 +00:00
{
2003-12-10 07:15:28 +00:00
char * ret = NULL , * c , * comp , * cend ;
size_t inc_len , cnt ;
int state ;
c = comp = cend = s ;
cnt = len ;
state = 0 ;
while ( cnt > 0 ) {
inc_len = ( * c = = ' \0 ' ? 1 : php_mblen ( c , cnt ) ) ;
switch ( inc_len ) {
case - 2 :
case - 1 :
inc_len = 1 ;
php_mblen ( NULL , 0 ) ;
break ;
case 0 :
goto quit_loop ;
case 1 :
2005-03-01 14:59:37 +00:00
# if defined(PHP_WIN32) || defined(NETWARE)
2003-12-10 07:15:28 +00:00
if ( * c = = ' / ' | | * c = = ' \\ ' ) {
# else
if ( * c = = ' / ' ) {
1999-04-17 00:37:12 +00:00
# endif
2003-12-10 07:15:28 +00:00
if ( state = = 1 ) {
state = 0 ;
cend = c ;
}
} else {
if ( state = = 0 ) {
comp = c ;
state = 1 ;
}
}
default :
break ;
}
c + = inc_len ;
cnt - = inc_len ;
2003-02-08 15:26:17 +00:00
}
2003-12-10 07:15:28 +00:00
quit_loop :
if ( state = = 1 ) {
cend = c ;
2003-02-08 15:26:17 +00:00
}
2003-12-10 07:15:28 +00:00
if ( suffix ! = NULL & & sufflen < ( cend - comp ) & &
memcmp ( cend - sufflen , suffix , sufflen ) = = 0 ) {
cend - = sufflen ;
2003-02-08 15:26:17 +00:00
}
2003-05-05 13:46:14 +00:00
2003-12-10 07:15:28 +00:00
len = cend - comp ;
ret = emalloc ( len + 1 ) ;
memcpy ( ret , comp , len ) ;
2003-05-05 13:46:14 +00:00
ret [ len ] = ' \0 ' ;
2003-12-10 07:15:28 +00:00
if ( p_ret ) {
* p_ret = ret ;
}
if ( p_len ) {
* p_len = len ;
}
2000-05-23 20:16:14 +00:00
}
2001-06-05 13:12:10 +00:00
/* }}} */
2000-05-23 20:16:14 +00:00
2001-06-23 00:51:03 +00:00
/* {{{ proto string basename(string path [, string suffix])
2001-09-04 10:44:28 +00:00
Returns the filename component of the path */
2000-05-23 20:16:14 +00:00
PHP_FUNCTION ( basename )
{
2003-05-05 13:46:14 +00:00
char * string , * suffix = NULL , * ret ;
2003-12-10 07:15:28 +00:00
int string_len , suffix_len = 0 ;
size_t ret_len ;
2000-05-23 20:16:14 +00:00
2001-12-01 15:45:55 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " s|s " , & string , & string_len , & suffix , & suffix_len ) = = FAILURE ) {
return ;
2000-05-23 20:16:14 +00:00
}
2001-06-23 00:51:03 +00:00
2003-12-10 21:23:35 +00:00
php_basename ( string , string_len , suffix , suffix_len , & ret , & ret_len TSRMLS_CC ) ;
2003-12-10 07:15:28 +00:00
RETURN_STRINGL ( ret , ( int ) ret_len , 0 ) ;
1999-04-17 00:37:12 +00:00
}
/* }}} */
2001-06-05 13:12:10 +00:00
/* {{{ php_dirname
2002-05-16 16:04:45 +00:00
Returns directory name component of path */
2003-04-12 21:53:46 +00:00
PHPAPI size_t php_dirname ( char * path , size_t len )
2000-10-03 14:43:04 +00:00
{
2000-10-03 16:47:25 +00:00
register char * end = path + len - 1 ;
2003-04-29 13:35:48 +00:00
unsigned int len_adjust = 0 ;
1999-04-17 00:37:12 +00:00
2002-05-16 16:04:45 +00:00
# ifdef PHP_WIN32
/* Note that on Win32 CWD is per drive (heritage from CP/M).
* This means dirname ( " c:foo " ) maps to " c:. " or " c: " - which means CWD on C : drive .
*/
2003-04-12 21:53:46 +00:00
if ( ( 2 < = len ) & & isalpha ( ( int ) ( ( unsigned char * ) path ) [ 0 ] ) & & ( ' : ' = = path [ 1 ] ) ) {
2002-05-16 16:04:45 +00:00
/* Skip over the drive spec (if any) so as not to change */
path + = 2 ;
2003-04-29 13:35:48 +00:00
len_adjust + = 2 ;
2002-05-16 16:04:45 +00:00
if ( 2 = = len ) {
/* Return "c:" on Win32 for dirname("c:").
* It would be more consistent to return " c:. "
* but that would require making the string * longer * .
*/
2003-04-12 21:53:46 +00:00
return len ;
2002-05-16 16:04:45 +00:00
}
}
2005-07-15 12:35:12 +00:00
# elif defined(NETWARE)
/*
* Find the first occurence of : from the left
* move the path pointer to the position just after :
* increment the len_adjust to the length of path till colon character ( inclusive )
* If there is no character beyond : simple return len
*/
char * colonpos = NULL ;
colonpos = strchr ( path , ' : ' ) ;
if ( colonpos ! = NULL ) {
len_adjust = ( ( colonpos - path ) + 1 ) ;
path + = len_adjust ;
if ( len_adjust = = len ) {
return len ;
}
}
2002-05-16 16:04:45 +00:00
# endif
2003-04-12 21:53:46 +00:00
if ( len = = 0 ) {
2000-10-03 16:47:25 +00:00
/* Illegal use of this function */
2003-04-12 21:53:46 +00:00
return 0 ;
2000-10-03 16:47:25 +00:00
}
/* Strip trailing slashes */
2002-04-25 14:43:40 +00:00
while ( end > = path & & IS_SLASH_P ( end ) ) {
2000-10-03 16:47:25 +00:00
end - - ;
}
if ( end < path ) {
/* The path only contained slashes */
2002-11-02 18:54:31 +00:00
path [ 0 ] = DEFAULT_SLASH ;
2000-10-03 16:47:25 +00:00
path [ 1 ] = ' \0 ' ;
2003-04-29 13:35:48 +00:00
return 1 + len_adjust ;
2000-10-03 16:47:25 +00:00
}
/* Strip filename */
2002-04-25 14:43:40 +00:00
while ( end > = path & & ! IS_SLASH_P ( end ) ) {
2000-10-03 16:47:25 +00:00
end - - ;
}
if ( end < path ) {
/* No slash found, therefore return '.' */
2005-07-15 12:35:12 +00:00
# ifdef NETWARE
if ( len_adjust = = 0 ) {
path [ 0 ] = ' . ' ;
path [ 1 ] = ' \0 ' ;
return 1 ; //only one character
}
else {
path [ 0 ] = ' \0 ' ;
return len_adjust ;
}
# else
2000-10-03 16:47:25 +00:00
path [ 0 ] = ' . ' ;
path [ 1 ] = ' \0 ' ;
2003-04-29 13:35:48 +00:00
return 1 + len_adjust ;
2005-07-15 12:35:12 +00:00
# endif
2000-10-03 16:47:25 +00:00
}
/* Strip slashes which came before the file name */
2002-04-25 14:43:40 +00:00
while ( end > = path & & IS_SLASH_P ( end ) ) {
2000-10-03 16:47:25 +00:00
end - - ;
}
if ( end < path ) {
2002-11-02 18:54:31 +00:00
path [ 0 ] = DEFAULT_SLASH ;
2000-10-03 16:47:25 +00:00
path [ 1 ] = ' \0 ' ;
2003-04-29 13:35:48 +00:00
return 1 + len_adjust ;
2000-10-03 16:47:25 +00:00
}
* ( end + 1 ) = ' \0 ' ;
2003-04-12 21:53:46 +00:00
2003-04-29 13:35:48 +00:00
return ( size_t ) ( end + 1 - path ) + len_adjust ;
1999-04-17 00:37:12 +00:00
}
2001-06-05 13:12:10 +00:00
/* }}} */
1999-04-17 00:37:12 +00:00
/* {{{ proto string dirname(string path)
2001-09-04 10:44:28 +00:00
Returns the directory name component of the path */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( dirname )
1999-04-17 00:37:12 +00:00
{
1999-12-23 14:59:01 +00:00
zval * * str ;
1999-04-17 00:37:12 +00:00
char * ret ;
2003-04-12 21:53:46 +00:00
size_t ret_len ;
2002-09-11 14:40:52 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( 1 , & str ) = = FAILURE ) {
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
}
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( str ) ;
2001-09-04 06:18:11 +00:00
2000-10-03 16:47:25 +00:00
ret = estrndup ( Z_STRVAL_PP ( str ) , Z_STRLEN_PP ( str ) ) ;
2003-04-12 21:53:46 +00:00
ret_len = php_dirname ( ret , Z_STRLEN_PP ( str ) ) ;
2001-09-04 06:18:11 +00:00
2003-04-12 21:53:46 +00:00
RETURN_STRINGL ( ret , ret_len , 0 ) ;
1999-04-17 00:37:12 +00:00
}
/* }}} */
2000-09-10 05:59:16 +00:00
/* {{{ proto array pathinfo(string path)
2001-09-04 10:44:28 +00:00
Returns information about a certain string */
2000-09-10 05:59:16 +00:00
PHP_FUNCTION ( pathinfo )
{
2002-01-22 03:35:23 +00:00
zval * tmp ;
2002-10-24 19:15:40 +00:00
char * path , * ret = NULL ;
2003-12-10 07:15:28 +00:00
int path_len ;
size_t ret_len ;
2003-03-06 23:07:28 +00:00
long opt = PHP_PATHINFO_ALL ;
2002-01-22 03:35:23 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " s|l " , & path , & path_len , & opt ) = = FAILURE ) {
return ;
2000-09-10 05:59:16 +00:00
}
2001-09-04 06:18:11 +00:00
2000-09-10 05:59:16 +00:00
MAKE_STD_ZVAL ( tmp ) ;
array_init ( tmp ) ;
2002-01-22 03:35:23 +00:00
if ( ( opt & PHP_PATHINFO_DIRNAME ) = = PHP_PATHINFO_DIRNAME ) {
ret = estrndup ( path , path_len ) ;
php_dirname ( ret , path_len ) ;
2003-02-08 15:26:17 +00:00
if ( * ret ) {
2005-08-23 12:53:31 +00:00
add_assoc_rt_string ( tmp , " dirname " , ret , 1 ) ;
2003-02-08 15:26:17 +00:00
}
2000-09-10 05:59:16 +00:00
efree ( ret ) ;
}
2002-01-22 03:35:23 +00:00
if ( ( opt & PHP_PATHINFO_BASENAME ) = = PHP_PATHINFO_BASENAME ) {
2003-12-10 21:23:35 +00:00
php_basename ( path , path_len , NULL , 0 , & ret , & ret_len TSRMLS_CC ) ;
2005-08-23 12:53:31 +00:00
add_assoc_rt_stringl ( tmp , " basename " , ret , ret_len , 0 ) ;
if ( UG ( unicode ) ) {
efree ( ret ) ;
}
2000-09-11 13:52:22 +00:00
}
2002-01-22 03:35:23 +00:00
if ( ( opt & PHP_PATHINFO_EXTENSION ) = = PHP_PATHINFO_EXTENSION ) {
2000-09-10 05:59:16 +00:00
char * p ;
int idx ;
2002-01-22 03:35:23 +00:00
int have_basename = ( ( opt & PHP_PATHINFO_BASENAME ) = = PHP_PATHINFO_BASENAME ) ;
/* Have we alrady looked up the basename? */
if ( ! have_basename ) {
2003-12-10 21:23:35 +00:00
php_basename ( path , path_len , NULL , 0 , & ret , & ret_len TSRMLS_CC ) ;
2002-01-22 03:35:23 +00:00
}
p = strrchr ( ret , ' . ' ) ;
2000-09-10 05:59:16 +00:00
2001-04-13 18:22:13 +00:00
if ( p ) {
2002-01-22 03:35:23 +00:00
idx = p - ret ;
2005-08-23 12:53:31 +00:00
add_assoc_rt_stringl ( tmp , " extension " , ret + idx + 1 , ret_len - idx - 1 , 1 ) ;
2002-01-22 03:35:23 +00:00
}
if ( ! have_basename ) {
efree ( ret ) ;
2000-09-10 05:59:16 +00:00
}
}
2001-09-04 06:18:11 +00:00
2002-01-22 03:35:23 +00:00
if ( opt = = PHP_PATHINFO_ALL ) {
2005-03-10 23:52:43 +00:00
RETURN_ZVAL ( tmp , 0 , 1 ) ;
2002-01-22 03:35:23 +00:00
} else {
zval * * element ;
if ( zend_hash_get_current_data ( Z_ARRVAL_P ( tmp ) , ( void * * ) & element ) = = SUCCESS ) {
2005-03-10 23:42:15 +00:00
RETVAL_ZVAL ( * element , 1 , 0 ) ;
2002-01-22 03:35:23 +00:00
} else {
ZVAL_EMPTY_STRING ( return_value ) ;
}
2000-09-10 05:59:16 +00:00
}
2005-03-10 23:42:15 +00:00
zval_ptr_dtor ( & tmp ) ;
2000-09-10 05:59:16 +00:00
}
/* }}} */
1999-04-17 00:37:12 +00:00
2005-10-20 19:25:54 +00:00
/* {{{ php_u_stristr
Unicode version of case insensitve strstr */
2005-10-22 13:36:55 +00:00
PHPAPI UChar * php_u_stristr ( UChar * s , UChar * t , int32_t s_len , int32_t t_len )
2005-10-20 19:25:54 +00:00
{
int32_t i , j , last ;
UChar32 ch1 , ch2 ;
/* Have to do this by hand since lower-casing can change lengths
by changing codepoints , and an offset within the lower - case &
2005-10-22 05:52:53 +00:00
upper - case strings might be different codepoints .
2005-10-22 13:32:51 +00:00
Find an occurrence of the first codepoint of ' t ' in ' s ' , and
starting from this point , match the rest of the codepoints of
' t ' with those in ' s ' . Comparisons are performed against
lower - case equivalents of the codepoints being matched .
2005-10-22 05:52:53 +00:00
2005-10-22 13:32:51 +00:00
' i ' & ' j ' are indices used for extracting codepoints ' ch1 ' &
2005-10-22 05:52:53 +00:00
' ch2 ' . ' last ' is offset in ' s ' where the search for ' t '
started , and indicates beginning of ' t ' in ' s ' for a successful
match .
2005-10-20 19:25:54 +00:00
*/
2005-10-22 05:52:53 +00:00
2005-10-20 19:25:54 +00:00
i = 0 ;
while ( i < = ( s_len - t_len ) ) {
last = i ;
U16_NEXT ( s , i , s_len , ch1 ) ;
2005-10-22 05:52:53 +00:00
j = 0 ;
U16_NEXT ( t , j , t_len , ch2 ) ;
2005-10-20 19:25:54 +00:00
if ( u_tolower ( ch1 ) = = u_tolower ( ch2 ) ) {
while ( j < t_len ) {
U16_NEXT ( s , i , s_len , ch1 ) ;
U16_NEXT ( t , j , t_len , ch2 ) ;
if ( u_tolower ( ch1 ) ! = u_tolower ( ch2 ) ) {
2005-10-22 05:52:53 +00:00
/* U16_NEXT() incr 'i' beyond 'ch1', re-adjust to
restart compare
*/
2005-10-20 19:25:54 +00:00
U16_BACK_1 ( s , 0 , i ) ;
break ;
}
}
if ( u_tolower ( ch1 ) = = u_tolower ( ch2 ) ) {
return s + last ;
}
}
}
return NULL ;
}
/* }}} */
2001-06-05 13:12:10 +00:00
/* {{{ php_stristr
2002-09-11 14:40:52 +00:00
case insensitve strstr */
PHPAPI char * php_stristr ( unsigned char * s , unsigned char * t , size_t s_len , size_t t_len )
1999-04-17 00:37:12 +00:00
{
1999-12-11 19:50:01 +00:00
php_strtolower ( s , s_len ) ;
php_strtolower ( t , t_len ) ;
return php_memnstr ( s , t , t_len , s + s_len ) ;
1999-04-17 00:37:12 +00:00
}
2001-06-05 13:12:10 +00:00
/* }}} */
1999-04-17 00:37:12 +00:00
2005-10-19 19:10:13 +00:00
/* {{{ php_u_strspn
*/
PHPAPI int32_t php_u_strspn ( UChar * s1 , UChar * s2 , UChar * s1_end , UChar * s2_end )
{
int32_t len1 = s1_end - s1 ;
int32_t len2 = s2_end - s2 ;
int32_t i , codepts ;
UChar32 ch ;
for ( i = 0 , codepts = 0 ; i < len1 ; codepts + + ) {
U16_NEXT ( s1 , i , len1 , ch ) ;
if ( u_memchr32 ( s2 , ch , len2 ) = = NULL ) {
break ;
}
}
return codepts ;
}
/* }}} */
2001-06-05 13:12:10 +00:00
/* {{{ php_strspn
*/
1999-12-13 23:40:36 +00:00
PHPAPI size_t php_strspn ( char * s1 , char * s2 , char * s1_end , char * s2_end )
{
register const char * p = s1 , * spanp ;
register char c = * p ;
cont :
2003-02-08 15:26:17 +00:00
for ( spanp = s2 ; p ! = s1_end & & spanp ! = s2_end ; ) {
1999-12-13 23:40:36 +00:00
if ( * spanp + + = = c ) {
c = * ( + + p ) ;
goto cont ;
}
2003-02-08 15:26:17 +00:00
}
1999-12-13 23:40:36 +00:00
return ( p - s1 ) ;
}
2001-06-05 13:12:10 +00:00
/* }}} */
1999-12-13 23:40:36 +00:00
2005-10-19 19:10:13 +00:00
/* {{{ php_u_strcspn
*/
PHPAPI int32_t php_u_strcspn ( UChar * s1 , UChar * s2 , UChar * s1_end , UChar * s2_end )
{
int32_t len1 = s1_end - s1 ;
int32_t len2 = s2_end - s2 ;
int32_t i , codepts ;
UChar32 ch ;
for ( i = 0 , codepts = 0 ; i < len1 ; codepts + + ) {
U16_NEXT ( s1 , i , len1 , ch ) ;
if ( u_memchr32 ( s2 , ch , len2 ) ) {
break ;
}
}
return codepts ;
}
/* }}} */
2001-06-05 13:12:10 +00:00
/* {{{ php_strcspn
*/
1999-12-13 23:40:36 +00:00
PHPAPI size_t php_strcspn ( char * s1 , char * s2 , char * s1_end , char * s2_end )
{
register const char * p , * spanp ;
register char c = * s1 ;
for ( p = s1 ; ; ) {
spanp = s2 ;
do {
2003-02-08 15:26:17 +00:00
if ( * spanp = = c | | p = = s1_end ) {
2001-09-04 06:18:11 +00:00
return p - s1 ;
2003-02-08 15:26:17 +00:00
}
1999-12-13 23:40:36 +00:00
} while ( spanp + + < s2_end ) ;
2001-09-04 06:18:11 +00:00
c = * + + p ;
1999-12-13 23:40:36 +00:00
}
/* NOTREACHED */
}
2001-06-05 13:12:10 +00:00
/* }}} */
1999-12-13 23:40:36 +00:00
2005-08-09 20:49:19 +00:00
/* {{{ proto string stristr(string haystack, string needle[, bool part])
2001-09-04 10:44:28 +00:00
Finds first occurrence of a string within another , case insensitive */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( stristr )
1999-04-17 00:37:12 +00:00
{
2005-10-20 19:25:54 +00:00
zval * haystack , * needle ;
2005-08-09 20:49:19 +00:00
zend_bool part = 0 ;
2005-10-20 19:25:54 +00:00
zend_uchar str_type ;
1999-12-11 19:50:01 +00:00
char needle_char [ 2 ] ;
2005-10-20 19:25:54 +00:00
UChar u_needle_char [ 3 ] ;
int32_t needle_len ;
char * haystack_copy ;
void * target ;
void * found = NULL ;
int found_offset ;
void * start , * end ;
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " zz|b " , & haystack , & needle , & part ) = = FAILURE ) {
2005-08-09 20:49:19 +00:00
return ;
1999-04-17 00:37:12 +00:00
}
2005-10-20 19:25:54 +00:00
SEPARATE_ZVAL ( & haystack ) ;
2005-08-09 20:49:19 +00:00
SEPARATE_ZVAL ( & needle ) ;
2005-10-20 19:25:54 +00:00
if ( Z_TYPE_P ( haystack ) ! = IS_UNICODE & & Z_TYPE_P ( haystack ) ! = IS_BINARY & & Z_TYPE_P ( haystack ) ! = IS_STRING ) {
convert_to_text ( haystack ) ;
}
1999-04-17 00:37:12 +00:00
2005-10-20 19:25:54 +00:00
if ( Z_TYPE_P ( needle ) = = IS_UNICODE | | Z_TYPE_P ( needle ) = = IS_BINARY | | Z_TYPE_P ( needle ) = = IS_STRING ) {
if ( ! Z_UNILEN_P ( needle ) ) {
2002-09-05 11:29:31 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Empty delimiter. " ) ;
1999-12-11 19:50:01 +00:00
RETURN_FALSE ;
}
2005-10-20 19:25:54 +00:00
if ( Z_TYPE_P ( haystack ) ! = Z_TYPE_P ( needle ) ) {
str_type = zend_get_unified_string_type ( 2 TSRMLS_CC , Z_TYPE_P ( haystack ) , Z_TYPE_P ( needle ) ) ;
if ( str_type = = ( zend_uchar ) - 1 ) {
convert_to_explicit_type ( haystack , IS_BINARY ) ;
convert_to_explicit_type ( needle , IS_BINARY ) ;
} else {
convert_to_explicit_type ( haystack , str_type ) ;
convert_to_explicit_type ( needle , str_type ) ;
}
}
target = Z_UNIVAL_P ( needle ) ;
needle_len = Z_UNILEN_P ( needle ) ;
2002-09-11 14:40:52 +00:00
} else {
2005-08-09 20:49:19 +00:00
convert_to_long_ex ( & needle ) ;
2005-10-20 19:25:54 +00:00
needle_len = 0 ;
if ( Z_TYPE_P ( haystack ) = = IS_UNICODE ) {
if ( Z_LVAL_P ( needle ) < 0 | | Z_LVAL_P ( needle ) > 0x10FFFF ) {
php_error ( E_WARNING , " Needle argument codepoint value out of range (0 - 0x10FFFF) " ) ;
RETURN_FALSE ;
}
if ( U_IS_BMP ( Z_LVAL_P ( needle ) ) ) {
u_needle_char [ needle_len + + ] = ( UChar ) Z_LVAL_P ( needle ) ;
u_needle_char [ needle_len ] = 0 ;
} else {
u_needle_char [ needle_len + + ] = ( UChar ) U16_LEAD ( Z_LVAL_P ( needle ) ) ;
u_needle_char [ needle_len + + ] = ( UChar ) U16_TRAIL ( Z_LVAL_P ( needle ) ) ;
u_needle_char [ needle_len ] = 0 ;
}
target = u_needle_char ;
} else {
needle_char [ needle_len + + ] = ( char ) Z_LVAL_P ( needle ) ;
needle_char [ needle_len ] = 0 ;
target = needle_char ;
}
}
if ( needle_len > Z_UNILEN_P ( haystack ) ) {
RETURN_FALSE ;
}
2000-03-22 08:14:00 +00:00
2005-10-20 19:25:54 +00:00
if ( Z_TYPE_P ( haystack ) = = IS_UNICODE ) {
found = php_u_stristr ( Z_USTRVAL_P ( haystack ) , ( UChar * ) target ,
Z_USTRLEN_P ( haystack ) , needle_len ) ;
} else {
haystack_copy = estrndup ( Z_STRVAL_P ( haystack ) , Z_STRLEN_P ( haystack ) ) ;
found = php_stristr ( Z_STRVAL_P ( haystack ) , ( char * ) target ,
Z_STRLEN_P ( haystack ) , needle_len ) ;
1999-04-17 00:37:12 +00:00
}
2002-09-11 14:40:52 +00:00
1999-04-17 00:37:12 +00:00
if ( found ) {
2005-10-20 19:25:54 +00:00
if ( Z_TYPE_P ( haystack ) = = IS_UNICODE ) {
start = part ? Z_USTRVAL_P ( haystack ) : found ;
end = part ? found : ( Z_USTRVAL_P ( haystack ) + Z_USTRLEN_P ( haystack ) ) ;
RETVAL_UNICODEL ( ( UChar * ) start , ( UChar * ) end - ( UChar * ) start , 1 ) ;
2005-08-09 20:49:19 +00:00
} else {
2005-10-20 19:25:54 +00:00
found_offset = ( char * ) found - Z_STRVAL_P ( haystack ) ;
start = part ? haystack_copy : haystack_copy + found_offset ;
end = part ? haystack_copy + found_offset : ( haystack_copy + Z_STRLEN_P ( haystack ) ) ;
if ( Z_TYPE_P ( haystack ) = = IS_BINARY ) {
RETVAL_BINARYL ( ( char * ) start , ( char * ) end - ( char * ) start , 1 ) ;
} else {
RETVAL_STRINGL ( ( char * ) start , ( char * ) end - ( char * ) start , 1 ) ;
}
2005-08-09 20:49:19 +00:00
}
1999-04-17 00:37:12 +00:00
} else {
2001-09-27 00:21:21 +00:00
RETVAL_FALSE ;
1999-04-17 00:37:12 +00:00
}
2001-09-04 06:18:11 +00:00
2005-10-20 19:25:54 +00:00
if ( Z_TYPE_P ( haystack ) ! = IS_UNICODE ) {
efree ( haystack_copy ) ;
}
1999-04-17 00:37:12 +00:00
}
/* }}} */
2005-08-09 20:49:19 +00:00
/* {{{ proto string strstr(string haystack, string needle[, bool part])
2001-09-04 10:44:28 +00:00
Finds first occurrence of a string within another */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( strstr )
1999-04-17 00:37:12 +00:00
{
2005-08-11 23:36:07 +00:00
void * haystack ;
int32_t haystack_len ;
zend_uchar haystack_type ;
zval * * needle ;
void * found = NULL ;
char needle_char [ 2 ] ;
UChar u_needle_char [ 3 ] ;
int32_t n_len = 0 ;
size_t found_offset ;
2005-08-09 20:49:19 +00:00
zend_bool part = 0 ;
2003-02-08 15:26:17 +00:00
2005-08-11 23:36:07 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " tZ|b " , & haystack , & haystack_len , & haystack_type , & needle , & part ) = = FAILURE ) {
2005-08-09 20:49:19 +00:00
return ;
}
1999-04-17 00:37:12 +00:00
2005-08-11 23:36:07 +00:00
if ( Z_TYPE_PP ( needle ) = = IS_STRING | | Z_TYPE_PP ( needle ) = = IS_UNICODE | | Z_TYPE_PP ( needle ) = = IS_BINARY ) {
if ( ! Z_STRLEN_PP ( needle ) ) {
2002-09-05 11:29:31 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Empty delimiter. " ) ;
1999-04-17 00:37:12 +00:00
RETURN_FALSE ;
}
2001-09-04 06:18:11 +00:00
2005-08-11 23:36:07 +00:00
/* haystack type determines the needle type */
if ( haystack_type = = IS_UNICODE ) {
convert_to_unicode_ex ( needle ) ;
found = zend_u_memnstr ( ( UChar * ) haystack ,
Z_USTRVAL_PP ( needle ) ,
Z_USTRLEN_PP ( needle ) ,
( UChar * ) haystack + haystack_len ) ;
} else {
convert_to_string_ex ( needle ) ;
found = php_memnstr ( ( char * ) haystack ,
Z_STRVAL_PP ( needle ) ,
Z_STRLEN_PP ( needle ) ,
( char * ) haystack + haystack_len ) ;
}
2002-09-11 14:40:52 +00:00
} else {
2005-08-11 23:36:07 +00:00
convert_to_long_ex ( needle ) ;
if ( haystack_type = = IS_UNICODE ) {
if ( Z_LVAL_PP ( needle ) < 0 | | Z_LVAL_PP ( needle ) > 0x10FFFF ) {
php_error ( E_WARNING , " Needle argument codepoint value out of range (0 - 0x10FFFF) " ) ;
RETURN_FALSE ;
}
/* supplementary codepoint values may require 2 UChar's */
if ( U_IS_BMP ( Z_LVAL_PP ( needle ) ) ) {
u_needle_char [ n_len + + ] = ( UChar ) Z_LVAL_PP ( needle ) ;
u_needle_char [ n_len ] = 0 ;
} else {
u_needle_char [ n_len + + ] = ( UChar ) U16_LEAD ( Z_LVAL_PP ( needle ) ) ;
u_needle_char [ n_len + + ] = ( UChar ) U16_TRAIL ( Z_LVAL_PP ( needle ) ) ;
u_needle_char [ n_len ] = 0 ;
}
1999-04-17 00:37:12 +00:00
2005-08-11 23:36:07 +00:00
found = zend_u_memnstr ( ( UChar * ) haystack ,
u_needle_char ,
n_len ,
( UChar * ) haystack + haystack_len ) ;
} else {
needle_char [ 0 ] = ( char ) Z_LVAL_PP ( needle ) ;
needle_char [ 1 ] = 0 ;
found = php_memnstr ( ( char * ) haystack ,
needle_char ,
1 ,
( char * ) haystack + haystack_len ) ;
}
2001-09-04 06:18:11 +00:00
}
1999-04-17 00:37:12 +00:00
if ( found ) {
2005-08-11 23:36:07 +00:00
switch ( haystack_type ) {
case IS_UNICODE :
found_offset = ( UChar * ) found - ( UChar * ) haystack ;
if ( part ) {
2005-08-12 16:46:38 +00:00
UChar * ret ;
2005-08-11 23:36:07 +00:00
ret = eumalloc ( found_offset + 1 ) ;
u_strncpy ( ret , haystack , found_offset ) ;
ret [ found_offset ] = ' \0 ' ;
RETURN_UNICODEL ( ret , found_offset , 0 ) ;
} else {
RETURN_UNICODEL ( found , haystack_len - found_offset , 1 ) ;
}
break ;
case IS_STRING :
found_offset = ( char * ) found - ( char * ) haystack ;
if ( part ) {
char * ret ;
ret = emalloc ( found_offset + 1 ) ;
strncpy ( ret , haystack , found_offset ) ;
ret [ found_offset ] = ' \0 ' ;
RETURN_STRINGL ( ret , found_offset , 0 ) ;
} else {
RETURN_STRINGL ( found , haystack_len - found_offset , 1 ) ;
}
break ;
case IS_BINARY :
found_offset = ( char * ) found - ( char * ) haystack ;
if ( part ) {
char * ret ;
ret = emalloc ( found_offset + 1 ) ;
strncpy ( ret , haystack , found_offset ) ;
ret [ found_offset ] = ' \0 ' ;
RETURN_BINARYL ( ret , found_offset , 0 ) ;
} else {
RETURN_BINARYL ( found , haystack_len - found_offset , 1 ) ;
}
break ;
2005-08-09 20:49:19 +00:00
}
1999-04-17 00:37:12 +00:00
} else {
2001-09-04 06:18:11 +00:00
RETURN_FALSE ;
1999-04-17 00:37:12 +00:00
}
}
/* }}} */
1999-08-09 20:52:58 +00:00
/* {{{ proto string strchr(string haystack, string needle)
An alias for strstr */
/* }}} */
2005-08-11 23:36:07 +00:00
/* {{{ proto int strpos(text haystack, mixed needle [, int offset])
2001-09-04 10:44:28 +00:00
Finds position of first occurrence of a string within another */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( strpos )
1999-04-17 00:37:12 +00:00
{
2005-08-11 23:36:07 +00:00
void * haystack ;
int32_t haystack_len ;
zend_uchar haystack_type ;
zval * * needle ;
2001-09-04 06:18:11 +00:00
int offset = 0 ;
2005-08-11 23:36:07 +00:00
void * found = NULL ;
char needle_char [ 2 ] ;
UChar u_needle_char [ 3 ] ;
int32_t n_len = 0 ;
1999-12-06 17:37:59 +00:00
2005-08-11 23:36:07 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " tZ|l " , & haystack ,
& haystack_len , & haystack_type , & needle , & offset ) = = FAILURE ) {
return ;
1999-04-17 00:37:12 +00:00
}
2005-08-11 23:36:07 +00:00
/*
* Unicode note : it ' s okay to not convert offset to codepoint offset here .
* We ' ll just do a rough check that the offset does not exceed length in
* code units , and leave the rest to zend_u_memnstr ( ) .
*/
if ( offset < 0 | | offset > haystack_len ) {
2002-09-05 11:29:31 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Offset not contained in string. " ) ;
2001-09-04 06:18:11 +00:00
RETURN_FALSE ;
}
1999-12-06 17:37:59 +00:00
2005-08-11 23:36:07 +00:00
if ( Z_TYPE_PP ( needle ) = = IS_STRING | | Z_TYPE_PP ( needle ) = = IS_UNICODE | | Z_TYPE_PP ( needle ) = = IS_BINARY ) {
2001-09-04 06:18:11 +00:00
if ( ! Z_STRLEN_PP ( needle ) ) {
2002-09-05 11:29:31 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Empty delimiter. " ) ;
1999-04-17 00:37:12 +00:00
RETURN_FALSE ;
}
1999-12-06 17:37:59 +00:00
2005-08-11 23:36:07 +00:00
/* haystack type determines the needle type */
if ( haystack_type = = IS_UNICODE ) {
int32_t cp_offset = 0 ;
convert_to_unicode_ex ( needle ) ;
/* locate the codepoint at the specified offset */
U16_FWD_N ( ( UChar * ) haystack , cp_offset , haystack_len , offset ) ;
found = zend_u_memnstr ( ( UChar * ) haystack + cp_offset ,
Z_USTRVAL_PP ( needle ) ,
Z_USTRLEN_PP ( needle ) ,
( UChar * ) haystack + haystack_len ) ;
} else {
convert_to_string_ex ( needle ) ;
found = php_memnstr ( ( char * ) haystack + offset ,
Z_STRVAL_PP ( needle ) ,
Z_STRLEN_PP ( needle ) ,
( char * ) haystack + haystack_len ) ;
}
2002-09-11 14:40:52 +00:00
} else {
1999-09-25 11:55:42 +00:00
convert_to_long_ex ( needle ) ;
2005-08-11 23:36:07 +00:00
if ( haystack_type = = IS_UNICODE ) {
int32_t cp_offset = 0 ;
if ( Z_LVAL_PP ( needle ) < 0 | | Z_LVAL_PP ( needle ) > 0x10FFFF ) {
php_error ( E_WARNING , " Needle argument codepoint value out of range (0 - 0x10FFFF) " ) ;
RETURN_FALSE ;
}
/* supplementary codepoint values may require 2 UChar's */
if ( U_IS_BMP ( Z_LVAL_PP ( needle ) ) ) {
u_needle_char [ n_len + + ] = ( UChar ) Z_LVAL_PP ( needle ) ;
u_needle_char [ n_len ] = 0 ;
} else {
u_needle_char [ n_len + + ] = ( UChar ) U16_LEAD ( Z_LVAL_PP ( needle ) ) ;
u_needle_char [ n_len + + ] = ( UChar ) U16_TRAIL ( Z_LVAL_PP ( needle ) ) ;
u_needle_char [ n_len ] = 0 ;
}
1999-12-06 17:37:59 +00:00
2005-08-11 23:36:07 +00:00
/* locate the codepoint at the specified offset */
U16_FWD_N ( ( UChar * ) haystack , cp_offset , haystack_len , offset ) ;
found = zend_u_memnstr ( ( UChar * ) haystack + cp_offset ,
u_needle_char ,
n_len ,
( UChar * ) haystack + haystack_len ) ;
} else {
needle_char [ 0 ] = ( char ) Z_LVAL_PP ( needle ) ;
needle_char [ 1 ] = 0 ;
found = php_memnstr ( ( char * ) haystack + offset ,
needle_char ,
1 ,
( char * ) haystack + haystack_len ) ;
}
1999-04-17 00:37:12 +00:00
}
if ( found ) {
2005-08-11 23:36:07 +00:00
if ( haystack_type = = IS_UNICODE ) {
/* simple subtraction will not suffice, since there may be
supplementary codepoints */
RETURN_LONG ( u_countChar32 ( haystack , ( ( char * ) found - ( char * ) haystack ) / sizeof ( UChar ) ) ) ;
} else {
RETURN_LONG ( ( char * ) found - ( char * ) haystack ) ;
}
1999-04-17 00:37:12 +00:00
} else {
2001-09-04 06:18:11 +00:00
RETURN_FALSE ;
1999-04-17 00:37:12 +00:00
}
}
/* }}} */
2003-01-11 17:52:38 +00:00
/* {{{ proto int stripos(string haystack, string needle [, int offset])
Finds position of first occurrence of a string within another , case insensitive */
PHP_FUNCTION ( stripos )
{
2005-10-17 19:50:13 +00:00
zval * haystack , * needle ;
2003-01-11 17:52:38 +00:00
long offset = 0 ;
2005-10-17 19:50:13 +00:00
int32_t haystack_len , needle_len = 0 ;
zend_uchar str_type ;
void * haystack_dup , * needle_dup = NULL ;
2003-01-11 17:52:38 +00:00
char needle_char [ 2 ] ;
2005-10-17 19:50:13 +00:00
char c ;
UChar u_needle_char [ 3 ] ;
UChar32 ch ;
void * found = NULL ;
2003-01-11 17:52:38 +00:00
2005-10-17 19:50:13 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " zz|l " , & haystack , & needle , & offset ) = = FAILURE ) {
2003-01-11 17:52:38 +00:00
return ;
}
2003-02-08 15:26:17 +00:00
2005-10-17 19:50:13 +00:00
if ( Z_TYPE_P ( haystack ) ! = IS_UNICODE & & Z_TYPE_P ( haystack ) ! = IS_BINARY & & Z_TYPE_P ( haystack ) ! = IS_STRING ) {
convert_to_text ( haystack ) ;
}
if ( offset < 0 | | offset > Z_UNILEN_P ( haystack ) ) {
2003-01-11 17:52:38 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Offset not contained in string. " ) ;
RETURN_FALSE ;
}
2005-10-28 19:29:29 +00:00
haystack_len = Z_UNILEN_P ( haystack ) ;
2005-10-17 19:50:13 +00:00
if ( Z_TYPE_P ( needle ) = = IS_UNICODE | | Z_TYPE_P ( needle ) = = IS_BINARY | | Z_TYPE_P ( needle ) = = IS_STRING ) {
if ( ! Z_UNILEN_P ( needle ) ) {
RETURN_FALSE ;
}
if ( Z_TYPE_P ( haystack ) ! = Z_TYPE_P ( needle ) ) {
str_type = zend_get_unified_string_type ( 2 TSRMLS_CC , Z_TYPE_P ( haystack ) , Z_TYPE_P ( needle ) ) ;
if ( str_type = = ( zend_uchar ) - 1 ) {
convert_to_explicit_type ( haystack , IS_BINARY ) ;
convert_to_explicit_type ( needle , IS_BINARY ) ;
} else {
convert_to_explicit_type ( haystack , str_type ) ;
convert_to_explicit_type ( needle , str_type ) ;
}
}
needle_len = Z_UNILEN_P ( needle ) ;
if ( Z_TYPE_P ( haystack ) = = IS_UNICODE ) {
haystack_dup = eustrndup ( Z_USTRVAL_P ( haystack ) , haystack_len ) ;
php_u_strtolower ( ( UChar * * ) & haystack_dup , & haystack_len , UG ( default_locale ) ) ;
needle_dup = eustrndup ( Z_STRVAL_P ( needle ) , needle_len ) ;
php_u_strtolower ( ( UChar * * ) & needle_dup , & needle_len , UG ( default_locale ) ) ;
found = zend_u_memnstr ( ( UChar * ) haystack_dup + offset ,
( UChar * ) needle_dup , needle_len ,
( UChar * ) haystack_dup + haystack_len ) ;
} else {
haystack_dup = estrndup ( Z_STRVAL_P ( haystack ) , haystack_len ) ;
php_strtolower ( ( char * ) haystack_dup , haystack_len ) ;
needle_dup = estrndup ( Z_STRVAL_P ( needle ) , needle_len ) ;
php_strtolower ( ( char * ) needle_dup , Z_STRLEN_P ( needle ) ) ;
found = php_memnstr ( ( char * ) haystack_dup + offset ,
( char * ) needle_dup , needle_len ,
( char * ) haystack_dup + haystack_len ) ;
}
2003-01-11 17:52:38 +00:00
} else {
switch ( Z_TYPE_P ( needle ) ) {
case IS_LONG :
case IS_BOOL :
2005-10-17 19:50:13 +00:00
if ( Z_TYPE_P ( haystack ) = = IS_UNICODE ) {
ch = u_tolower ( ( UChar32 ) Z_LVAL_P ( needle ) ) ;
} else {
c = tolower ( ( char ) Z_LVAL_P ( needle ) ) ;
}
2003-01-11 17:52:38 +00:00
break ;
case IS_DOUBLE :
2005-10-17 19:50:13 +00:00
if ( Z_TYPE_P ( haystack ) = = IS_UNICODE ) {
ch = u_tolower ( ( UChar32 ) Z_DVAL_P ( needle ) ) ;
} else {
c = tolower ( ( char ) Z_DVAL_P ( needle ) ) ;
}
2003-01-11 17:52:38 +00:00
break ;
default :
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " needle is not a string or an integer. " ) ;
RETURN_FALSE ;
break ;
}
2005-10-17 19:50:13 +00:00
if ( Z_TYPE_P ( haystack ) = = IS_UNICODE ) {
if ( U_IS_BMP ( ch ) ) {
u_needle_char [ needle_len + + ] = ch ;
u_needle_char [ needle_len ] = 0 ;
} else {
u_needle_char [ needle_len + + ] = U16_LEAD ( ch ) ;
u_needle_char [ needle_len + + ] = U16_TRAIL ( ch ) ;
u_needle_char [ needle_len ] = 0 ;
}
haystack_dup = eustrndup ( Z_USTRVAL_P ( haystack ) , haystack_len ) ;
php_u_strtolower ( ( UChar * * ) & haystack_dup , & haystack_len , UG ( default_locale ) ) ;
found = zend_u_memnstr ( ( UChar * ) haystack_dup + offset ,
( UChar * ) u_needle_char , needle_len ,
( UChar * ) haystack_dup + haystack_len ) ;
} else {
needle_char [ 0 ] = c ;
needle_char [ 1 ] = ' \0 ' ;
haystack_dup = estrndup ( Z_STRVAL_P ( haystack ) , haystack_len ) ;
php_strtolower ( ( char * ) haystack_dup , haystack_len ) ;
found = php_memnstr ( ( char * ) haystack_dup + offset ,
( char * ) needle_char ,
sizeof ( needle_char ) - 1 ,
( char * ) haystack_dup + haystack_len ) ;
}
2003-01-11 17:52:38 +00:00
}
efree ( haystack_dup ) ;
if ( needle_dup ) {
efree ( needle_dup ) ;
}
if ( found ) {
2005-10-17 19:50:13 +00:00
if ( Z_TYPE_P ( haystack ) = = IS_UNICODE ) {
RETURN_LONG ( ( UChar * ) found - ( UChar * ) haystack_dup ) ;
} else {
RETURN_LONG ( ( char * ) found - ( char * ) haystack_dup ) ;
}
2003-01-11 17:52:38 +00:00
} else {
RETURN_FALSE ;
}
}
/* }}} */
/* {{{ proto int strrpos(string haystack, string needle [, int offset])
2003-04-12 07:36:22 +00:00
Finds position of last occurrence of a string within another string */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( strrpos )
1999-04-17 00:37:12 +00:00
{
2005-10-17 17:07:44 +00:00
zval * zhaystack , * zneedle ;
void * haystack , * needle ;
int32_t haystack_len , needle_len = 0 ;
zend_uchar str_type ;
2004-06-15 22:38:18 +00:00
long offset = 0 ;
2003-12-03 01:11:36 +00:00
char * p , * e , ord_needle [ 2 ] ;
2005-10-17 17:07:44 +00:00
UChar * pos , * u_p , * u_e , u_ord_needle [ 3 ] ;
2003-01-11 17:52:38 +00:00
2005-10-17 17:07:44 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " zz|l " ,
& zhaystack , & zneedle , & offset ) = = FAILURE ) {
2003-04-12 07:36:22 +00:00
RETURN_FALSE ;
2003-01-11 17:52:38 +00:00
}
2005-10-17 17:07:44 +00:00
if ( Z_TYPE_P ( zhaystack ) ! = IS_UNICODE & & Z_TYPE_P ( zhaystack ) ! = IS_BINARY & & Z_TYPE_P ( zhaystack ) ! = IS_STRING ) {
convert_to_text ( zhaystack ) ;
}
if ( Z_TYPE_P ( zneedle ) = = IS_UNICODE | | Z_TYPE_P ( zneedle ) = = IS_BINARY | | Z_TYPE_P ( zneedle ) = = IS_STRING ) {
if ( Z_TYPE_P ( zneedle ) ! = Z_TYPE_P ( zhaystack ) ) {
str_type = zend_get_unified_string_type ( 2 TSRMLS_CC , Z_TYPE_P ( zhaystack ) , Z_TYPE_P ( zneedle ) ) ;
if ( str_type = = ( zend_uchar ) - 1 ) {
convert_to_explicit_type ( zhaystack , IS_BINARY ) ;
convert_to_explicit_type ( zneedle , IS_BINARY ) ;
} else {
convert_to_explicit_type ( zhaystack , str_type ) ;
convert_to_explicit_type ( zneedle , str_type ) ;
}
}
needle = Z_UNIVAL_P ( zneedle ) ;
needle_len = Z_UNILEN_P ( zneedle ) ;
2003-12-03 01:11:36 +00:00
} else {
2005-10-17 17:07:44 +00:00
if ( Z_TYPE_P ( zhaystack ) = = IS_UNICODE ) {
if ( Z_LVAL_P ( zneedle ) < 0 | | Z_LVAL_P ( zneedle ) > 0x10FFFF ) {
php_error ( E_WARNING , " Needle argument codepoint value out of range (0 - 0x10FFFF) " ) ;
RETURN_FALSE ;
}
if ( U_IS_BMP ( Z_LVAL_P ( zneedle ) ) ) {
u_ord_needle [ needle_len + + ] = ( UChar ) Z_LVAL_P ( zneedle ) ;
u_ord_needle [ needle_len ] = 0 ;
} else {
u_ord_needle [ needle_len + + ] = ( UChar ) U16_LEAD ( Z_LVAL_P ( zneedle ) ) ;
u_ord_needle [ needle_len + + ] = ( UChar ) U16_TRAIL ( Z_LVAL_P ( zneedle ) ) ;
u_ord_needle [ needle_len ] = 0 ;
}
needle = u_ord_needle ;
} else {
convert_to_long ( zneedle ) ;
ord_needle [ 0 ] = ( char ) ( Z_LVAL_P ( zneedle ) & 0xFF ) ;
ord_needle [ 1 ] = ' \0 ' ;
needle = ord_needle ;
needle_len = 1 ;
}
2003-12-03 01:11:36 +00:00
}
2005-10-17 17:07:44 +00:00
haystack = Z_UNIVAL_P ( zhaystack ) ;
haystack_len = Z_UNILEN_P ( zhaystack ) ;
2003-12-03 01:11:36 +00:00
2003-04-12 20:04:06 +00:00
if ( ( haystack_len = = 0 ) | | ( needle_len = = 0 ) ) {
RETURN_FALSE ;
}
2005-10-17 17:07:44 +00:00
if ( Z_TYPE_P ( zhaystack ) = = IS_UNICODE ) {
if ( offset > = 0 ) {
u_p = ( UChar * ) haystack + offset ;
u_e = ( UChar * ) haystack + haystack_len - needle_len ;
} else {
u_p = haystack ;
if ( - offset > haystack_len ) {
u_e = ( UChar * ) haystack - needle_len ;
} else if ( needle_len > - offset ) {
u_e = ( UChar * ) haystack + haystack_len - needle_len ;
} else {
u_e = ( UChar * ) haystack + haystack_len + offset ;
}
}
pos = u_strFindLast ( u_p , u_e - u_p + needle_len , ( UChar * ) needle , needle_len ) ;
if ( pos ) {
RETURN_LONG ( pos - ( UChar * ) haystack ) ;
} else {
RETURN_FALSE ;
}
2003-01-11 17:52:38 +00:00
} else {
2005-10-17 17:07:44 +00:00
if ( offset > = 0 ) {
p = ( char * ) haystack + offset ;
e = ( char * ) haystack + haystack_len - needle_len ;
2003-04-12 07:36:22 +00:00
} else {
2005-10-17 17:07:44 +00:00
p = haystack ;
if ( - offset > haystack_len ) {
e = ( char * ) haystack - needle_len ;
} else if ( needle_len > - offset ) {
e = ( char * ) haystack + haystack_len - needle_len ;
} else {
e = ( char * ) haystack + haystack_len + offset ;
}
}
if ( needle_len = = 1 ) {
/* Single character search can shortcut memcmps */
while ( e > = p ) {
if ( * e = = * ( char * ) needle ) {
RETURN_LONG ( e - p + ( offset > 0 ? offset : 0 ) ) ;
}
e - - ;
}
RETURN_FALSE ;
2003-04-12 07:36:22 +00:00
}
2003-01-11 17:52:38 +00:00
2003-12-03 01:31:56 +00:00
while ( e > = p ) {
2005-10-17 17:07:44 +00:00
if ( memcmp ( e , needle , needle_len ) = = 0 ) {
2003-12-03 01:31:56 +00:00
RETURN_LONG ( e - p + ( offset > 0 ? offset : 0 ) ) ;
}
e - - ;
}
2003-01-11 17:52:38 +00:00
}
2003-04-12 07:36:22 +00:00
RETURN_FALSE ;
2003-01-11 17:52:38 +00:00
}
/* }}} */
/* {{{ proto int strripos(string haystack, string needle [, int offset])
2003-04-12 07:36:22 +00:00
Finds position of last occurrence of a string within another string */
2003-01-11 17:52:38 +00:00
PHP_FUNCTION ( strripos )
{
2003-12-03 01:11:36 +00:00
zval * zneedle ;
2003-04-12 07:36:22 +00:00
char * needle , * haystack ;
2004-02-25 20:16:27 +00:00
int needle_len , haystack_len ;
long offset = 0 ;
2003-12-03 01:11:36 +00:00
char * p , * e , ord_needle [ 2 ] ;
2003-04-12 07:36:22 +00:00
char * needle_dup , * haystack_dup ;
1999-04-17 00:37:12 +00:00
2003-12-03 01:11:36 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " sz|l " , & haystack , & haystack_len , & zneedle , & offset ) = = FAILURE ) {
2003-04-12 07:36:22 +00:00
RETURN_FALSE ;
2003-01-11 17:52:38 +00:00
}
2003-12-03 01:11:36 +00:00
if ( Z_TYPE_P ( zneedle ) = = IS_STRING ) {
needle = Z_STRVAL_P ( zneedle ) ;
needle_len = Z_STRLEN_P ( zneedle ) ;
} else {
convert_to_long ( zneedle ) ;
ord_needle [ 0 ] = ( char ) ( Z_LVAL_P ( zneedle ) & 0xFF ) ;
ord_needle [ 1 ] = ' \0 ' ;
needle = ord_needle ;
needle_len = 1 ;
}
2003-04-12 20:04:06 +00:00
if ( ( haystack_len = = 0 ) | | ( needle_len = = 0 ) ) {
RETURN_FALSE ;
}
2003-12-03 01:31:56 +00:00
if ( needle_len = = 1 ) {
/* Single character search can shortcut memcmps
Can also avoid tolower emallocs */
if ( offset > = 0 ) {
p = haystack + offset ;
e = haystack + haystack_len - 1 ;
} else {
p = haystack ;
2004-05-06 16:11:50 +00:00
if ( - offset > haystack_len ) {
e = haystack + haystack_len - 1 ;
} else {
e = haystack + haystack_len + offset ;
}
2003-12-03 01:31:56 +00:00
}
/* Borrow that ord_needle buffer to avoid repeatedly tolower()ing needle */
* ord_needle = tolower ( * needle ) ;
while ( e > = p ) {
if ( tolower ( * e ) = = * ord_needle ) {
RETURN_LONG ( e - p + ( offset > 0 ? offset : 0 ) ) ;
}
e - - ;
}
RETURN_FALSE ;
}
2003-04-12 07:36:22 +00:00
needle_dup = estrndup ( needle , needle_len ) ;
php_strtolower ( needle_dup , needle_len ) ;
haystack_dup = estrndup ( haystack , haystack_len ) ;
php_strtolower ( haystack_dup , haystack_len ) ;
2003-01-11 17:52:38 +00:00
2003-04-12 07:36:22 +00:00
if ( offset > = 0 ) {
p = haystack_dup + offset ;
e = haystack_dup + haystack_len - needle_len ;
2002-10-10 18:41:24 +00:00
} else {
2003-04-12 07:36:22 +00:00
p = haystack_dup ;
2004-05-06 16:11:50 +00:00
if ( - offset > haystack_len ) {
e = haystack_dup - needle_len ;
} else if ( needle_len > - offset ) {
2003-04-12 07:36:22 +00:00
e = haystack_dup + haystack_len - needle_len ;
} else {
e = haystack_dup + haystack_len + offset ;
}
1999-04-17 00:37:12 +00:00
}
2003-04-12 07:36:22 +00:00
while ( e > = p ) {
if ( memcmp ( e , needle_dup , needle_len ) = = 0 ) {
efree ( haystack_dup ) ;
efree ( needle_dup ) ;
2003-12-03 01:31:56 +00:00
RETURN_LONG ( e - p + ( offset > 0 ? offset : 0 ) ) ;
2003-04-12 07:36:22 +00:00
}
e - - ;
}
2003-01-11 17:52:38 +00:00
efree ( haystack_dup ) ;
2003-04-12 07:36:22 +00:00
efree ( needle_dup ) ;
RETURN_FALSE ;
1999-04-17 00:37:12 +00:00
}
/* }}} */
2005-10-24 14:35:02 +00:00
/* {{{ php_u_strrchr
*/
UChar * php_u_strrchr ( UChar * s , UChar32 ch , int32_t s_len )
{
UChar32 ch1 ;
int32_t i = s_len ;
while ( i > 0 ) {
U16_PREV ( s , 0 , i , ch1 ) ;
if ( ch1 = = ch ) {
return ( s + i ) ;
}
}
return NULL ;
}
/* }}} */
1999-04-17 00:37:12 +00:00
/* {{{ proto string strrchr(string haystack, string needle)
2001-09-04 10:44:28 +00:00
Finds the last occurrence of a character in a string within another */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( strrchr )
1999-04-17 00:37:12 +00:00
{
2005-10-24 14:35:02 +00:00
zval * haystack , * needle ;
zend_uchar str_type ;
UChar32 ch ;
void * found = NULL ;
int32_t found_offset ;
if ( ZEND_NUM_ARGS ( ) ! = 2 | | zend_parse_parameters ( 2 TSRMLS_CC , " zz " , & haystack , & needle ) = = FAILURE ) {
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
}
2005-10-24 14:35:02 +00:00
if ( Z_TYPE_P ( haystack ) ! = IS_UNICODE | | Z_TYPE_P ( haystack ) ! = IS_BINARY | | Z_TYPE_P ( haystack ) ! = IS_STRING ) {
convert_to_string ( haystack ) ;
}
1999-04-17 00:37:12 +00:00
2005-10-24 14:35:02 +00:00
if ( Z_TYPE_P ( needle ) = = IS_UNICODE | | Z_TYPE_P ( needle ) = = IS_BINARY | | Z_TYPE_P ( needle ) = = IS_STRING ) {
if ( Z_TYPE_P ( needle ) ! = Z_TYPE_P ( haystack ) ) {
str_type = zend_get_unified_string_type ( 2 TSRMLS_CC , Z_TYPE_P ( haystack ) , Z_TYPE_P ( needle ) ) ;
if ( str_type = = ( zend_uchar ) - 1 ) {
convert_to_explicit_type ( haystack , IS_BINARY ) ;
convert_to_explicit_type ( needle , IS_BINARY ) ;
} else {
convert_to_explicit_type ( haystack , str_type ) ;
convert_to_explicit_type ( needle , str_type ) ;
}
}
if ( Z_TYPE_P ( haystack ) = = IS_UNICODE ) {
U16_GET ( Z_USTRVAL_P ( needle ) , 0 , 0 , Z_USTRLEN_P ( needle ) , ch ) ;
found = php_u_strrchr ( Z_USTRVAL_P ( haystack ) , ch , Z_USTRLEN_P ( haystack ) ) ;
} else {
found = strrchr ( Z_STRVAL_P ( haystack ) , * Z_STRVAL_P ( needle ) ) ;
}
2002-10-10 18:41:24 +00:00
} else {
2005-10-24 14:35:02 +00:00
convert_to_long ( needle ) ;
if ( Z_TYPE_P ( haystack ) = = IS_UNICODE ) {
if ( Z_LVAL_P ( needle ) < 0 | | Z_LVAL_P ( needle ) > 0x10FFFF ) {
php_error ( E_WARNING , " Needle argument codepoint value out of range (0 - 0x10FFFF) " ) ;
RETURN_FALSE ;
}
found = php_u_strrchr ( Z_USTRVAL_P ( haystack ) , ( UChar32 ) Z_LVAL_P ( needle ) , Z_USTRLEN_P ( haystack ) ) ;
} else {
found = strrchr ( Z_STRVAL_P ( haystack ) , ( char ) Z_LVAL_P ( needle ) ) ;
}
1999-04-17 00:37:12 +00:00
}
if ( found ) {
2005-10-24 14:35:02 +00:00
if ( Z_TYPE_P ( haystack ) = = IS_UNICODE ) {
found_offset = ( UChar * ) found - Z_USTRVAL_P ( haystack ) ;
RETURN_UNICODEL ( ( UChar * ) found , Z_USTRLEN_P ( haystack ) - found_offset , 1 ) ;
} else {
found_offset = ( char * ) found - Z_STRVAL_P ( haystack ) ;
if ( Z_TYPE_P ( haystack ) = = IS_BINARY ) {
RETURN_BINARYL ( ( char * ) found , Z_BINLEN_P ( haystack ) - found_offset , 1 ) ;
} else {
RETURN_STRINGL ( ( char * ) found , Z_STRLEN_P ( haystack ) - found_offset , 1 ) ;
}
}
1999-04-17 00:37:12 +00:00
} else {
2001-09-04 06:18:11 +00:00
RETURN_FALSE ;
1999-04-17 00:37:12 +00:00
}
}
/* }}} */
2001-06-05 13:12:10 +00:00
/* {{{ php_chunk_split
*/
2003-02-08 15:26:17 +00:00
static char * php_chunk_split ( char * src , int srclen , char * end , int endlen , int chunklen , int * destlen )
1999-04-17 00:37:12 +00:00
{
char * dest ;
char * p , * q ;
int chunks ; /* complete chunks! */
int restlen ;
chunks = srclen / chunklen ;
restlen = srclen - chunks * chunklen ; /* srclen % chunklen */
2003-08-11 23:16:54 +00:00
dest = safe_emalloc ( ( srclen + ( chunks + 1 ) * endlen + 1 ) , sizeof ( char ) , 0 ) ;
1999-04-17 00:37:12 +00:00
2002-10-02 17:56:04 +00:00
for ( p = src , q = dest ; p < ( src + srclen - chunklen + 1 ) ; ) {
1999-04-17 00:37:12 +00:00
memcpy ( q , p , chunklen ) ;
q + = chunklen ;
memcpy ( q , end , endlen ) ;
q + = endlen ;
p + = chunklen ;
}
2002-10-02 17:56:04 +00:00
if ( restlen ) {
1999-04-17 00:37:12 +00:00
memcpy ( q , p , restlen ) ;
q + = restlen ;
memcpy ( q , end , endlen ) ;
q + = endlen ;
}
* q = ' \0 ' ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
if ( destlen ) {
* destlen = q - dest ;
}
1999-04-17 00:37:12 +00:00
return ( dest ) ;
}
2001-06-05 13:12:10 +00:00
/* }}} */
1999-04-17 00:37:12 +00:00
/* {{{ proto string chunk_split(string str [, int chunklen [, string ending]])
2001-09-04 10:44:28 +00:00
Returns split line */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( chunk_split )
1999-04-17 00:37:12 +00:00
{
1999-12-23 14:59:01 +00:00
zval * * p_str , * * p_chunklen , * * p_ending ;
1999-04-17 00:37:12 +00:00
char * result ;
char * end = " \r \n " ;
int endlen = 2 ;
int chunklen = 76 ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
int result_len ;
2001-09-04 06:18:11 +00:00
int argc = ZEND_NUM_ARGS ( ) ;
1999-06-15 21:51:00 +00:00
2003-02-08 15:26:17 +00:00
if ( argc < 1 | | argc > 3 | | zend_get_parameters_ex ( argc , & p_str , & p_chunklen , & p_ending ) = = FAILURE ) {
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
}
2003-02-08 15:26:17 +00:00
2001-09-04 06:18:11 +00:00
convert_to_string_ex ( p_str ) ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
2001-09-04 06:18:11 +00:00
if ( argc > 1 ) {
2001-09-27 06:55:23 +00:00
convert_to_long_ex ( p_chunklen ) ;
2001-09-04 06:18:11 +00:00
chunklen = Z_LVAL_PP ( p_chunklen ) ;
1999-04-17 00:37:12 +00:00
}
2001-09-04 06:18:11 +00:00
if ( argc > 2 ) {
convert_to_string_ex ( p_ending ) ;
end = Z_STRVAL_PP ( p_ending ) ;
endlen = Z_STRLEN_PP ( p_ending ) ;
}
if ( chunklen < = 0 ) {
2002-09-05 11:29:31 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Chunk length should be greater than zero. " ) ;
1999-04-17 00:37:12 +00:00
RETURN_FALSE ;
}
2000-08-17 08:47:42 +00:00
2005-01-18 15:47:57 +00:00
if ( chunklen > Z_STRLEN_PP ( p_str ) ) {
2005-04-03 18:08:40 +00:00
/* to maintain BC, we must return original string + ending */
result_len = endlen + Z_STRLEN_PP ( p_str ) ;
result = emalloc ( result_len + 1 ) ;
memcpy ( result , Z_STRVAL_PP ( p_str ) , Z_STRLEN_PP ( p_str ) ) ;
memcpy ( result + Z_STRLEN_PP ( p_str ) , end , endlen ) ;
result [ result_len ] = ' \0 ' ;
RETURN_STRINGL ( result , result_len , 0 ) ;
2005-01-18 15:47:57 +00:00
}
2001-09-04 06:18:11 +00:00
if ( ! Z_STRLEN_PP ( p_str ) ) {
2000-08-17 08:47:42 +00:00
RETURN_EMPTY_STRING ( ) ;
}
2002-09-11 14:40:52 +00:00
result = php_chunk_split ( Z_STRVAL_PP ( p_str ) , Z_STRLEN_PP ( p_str ) , end , endlen , chunklen , & result_len ) ;
2003-02-08 15:26:17 +00:00
2001-09-04 06:18:11 +00:00
if ( result ) {
RETURN_STRINGL ( result , result_len , 0 ) ;
1999-04-17 00:37:12 +00:00
} else {
RETURN_FALSE ;
}
}
/* }}} */
/* {{{ proto string substr(string str, int start [, int length])
2001-09-04 10:44:28 +00:00
Returns part of a string */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( substr )
1999-04-17 00:37:12 +00:00
{
2005-08-11 23:36:07 +00:00
void * str ;
int32_t str_len , cp_len ;
zend_uchar str_type ;
int l = - 1 ;
1999-04-17 00:37:12 +00:00
int f ;
2005-08-11 23:36:07 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " tl|l " , & str , & str_len , & str_type , & f , & l ) = = FAILURE ) {
return ;
1999-04-17 00:37:12 +00:00
}
2003-02-08 15:26:17 +00:00
2005-08-11 23:36:07 +00:00
if ( str_type = = IS_UNICODE ) {
cp_len = u_countChar32 ( str , str_len ) ;
2002-10-03 18:15:18 +00:00
} else {
2005-08-11 23:36:07 +00:00
cp_len = str_len ;
2001-09-04 06:18:11 +00:00
}
1999-04-17 00:37:12 +00:00
2005-08-11 23:36:07 +00:00
if ( ZEND_NUM_ARGS ( ) = = 2 ) {
l = cp_len ;
}
1999-04-17 00:37:12 +00:00
/* if "from" position is negative, count start position from the end
* of the string
*/
if ( f < 0 ) {
2005-08-11 23:36:07 +00:00
f = cp_len + f ;
1999-04-17 00:37:12 +00:00
if ( f < 0 ) {
f = 0 ;
}
}
/* if "length" position is negative, set it to the length
* needed to stop that many chars from the end of the string
*/
if ( l < 0 ) {
2005-08-11 23:36:07 +00:00
l = ( cp_len - f ) + l ;
1999-04-17 00:37:12 +00:00
if ( l < 0 ) {
l = 0 ;
}
}
2005-08-11 23:36:07 +00:00
if ( f > = cp_len ) {
1999-04-17 00:37:12 +00:00
RETURN_FALSE ;
}
2005-08-11 23:36:07 +00:00
if ( ( ( unsigned ) f + ( unsigned ) l ) > cp_len ) {
l = cp_len - f ;
1999-04-17 00:37:12 +00:00
}
1999-06-15 21:51:00 +00:00
2005-08-11 23:36:07 +00:00
if ( str_type = = IS_UNICODE ) {
int32_t start = 0 , end = 0 ;
U16_FWD_N ( ( UChar * ) str , end , str_len , f ) ;
start = end ;
U16_FWD_N ( ( UChar * ) str , end , str_len , l ) ;
RETURN_UNICODEL ( ( UChar * ) str + start , end - start , 1 ) ;
} else {
RETURN_STRINGL ( ( char * ) str + f , l , 1 ) ;
}
1999-04-17 00:37:12 +00:00
}
/* }}} */
2003-05-02 10:02:40 +00:00
2005-09-08 05:05:36 +00:00
/* {{{ php_adjust_limits
*/
PHPAPI void php_adjust_limits ( zval * * str , int32_t * f , int32_t * l )
{
2005-09-28 14:58:31 +00:00
int32_t str_codepts ;
2005-09-08 05:05:36 +00:00
if ( Z_TYPE_PP ( str ) = = IS_UNICODE ) {
2005-09-28 14:58:31 +00:00
str_codepts = u_countChar32 ( Z_USTRVAL_PP ( str ) , Z_USTRLEN_PP ( str ) ) ;
2005-09-08 05:05:36 +00:00
} else {
str_codepts = Z_STRLEN_PP ( str ) ;
}
/* If "from" position is negative, count start position from the end
* of the string */
if ( * f < 0 ) {
* f = str_codepts + * f ;
if ( * f < 0 ) {
* f = 0 ;
}
} else if ( * f > str_codepts ) {
* f = str_codepts ;
}
/* If "length" position is negative, set it to the length
* needed to stop that many codepts / chars from the end of the string */
if ( * l < 0 ) {
* l = str_codepts - * f + * l ;
if ( * l < 0 ) {
* l = 0 ;
}
}
if ( ( ( unsigned ) ( * f ) + ( unsigned ) ( * l ) ) > str_codepts ) {
* l = str_codepts - * f ;
}
}
/* }}} */
/* {{{ php_do_substr_replace
*/
PHPAPI int32_t php_do_substr_replace ( void * * result , zval * * str , zval * * repl , int32_t f , int32_t l TSRMLS_DC )
{
void * buf ;
int32_t buf_len , idx ;
UChar ch ;
2005-09-08 12:40:01 +00:00
int repl_len ;
if ( repl ) {
repl_len = Z_UNILEN_PP ( repl ) ;
} else {
repl_len = 0 ;
}
2005-09-08 05:05:36 +00:00
if ( Z_TYPE_PP ( str ) = = IS_UNICODE ) {
2005-09-08 12:40:01 +00:00
buf = emalloc ( UBYTES ( Z_USTRLEN_PP ( str ) - l + repl_len + 1 ) ) ;
2005-09-08 05:05:36 +00:00
/* buf_len is codept count here */
buf_len = 0 ; idx = 0 ;
while ( f - - > 0 ) {
U16_NEXT ( Z_USTRVAL_PP ( str ) , idx , Z_USTRLEN_PP ( str ) , ch ) ;
buf_len + = zend_codepoint_to_uchar ( ch , ( UChar * ) buf + buf_len ) ;
}
if ( repl ! = NULL ) {
2005-09-08 12:40:01 +00:00
u_memcpy ( ( UChar * ) buf + buf_len , Z_USTRVAL_PP ( repl ) , repl_len ) ;
buf_len + = repl_len ;
2005-09-08 05:05:36 +00:00
}
U16_FWD_N ( Z_USTRVAL_PP ( str ) , idx , Z_USTRLEN_PP ( str ) , l ) ;
u_memcpy ( ( UChar * ) buf + buf_len , Z_USTRVAL_PP ( str ) + idx , Z_USTRLEN_PP ( str ) - idx ) ;
buf_len + = ( Z_USTRLEN_PP ( str ) - idx ) ;
* ( ( UChar * ) buf + buf_len ) = 0 ;
buf = erealloc ( buf , UBYTES ( buf_len + 1 ) ) ;
} else {
/* buf_len is char count here */
2005-09-08 12:40:01 +00:00
buf_len = Z_STRLEN_PP ( str ) - l + repl_len ;
2005-09-08 05:05:36 +00:00
buf = emalloc ( buf_len + 1 ) ;
memcpy ( buf , Z_STRVAL_PP ( str ) , f ) ;
2005-09-08 12:40:01 +00:00
if ( repl ! = NULL ) {
memcpy ( ( char * ) buf + f , Z_STRVAL_PP ( repl ) , repl_len ) ;
2005-09-08 05:05:36 +00:00
}
2005-09-08 12:40:01 +00:00
memcpy ( ( char * ) buf + f + repl_len , Z_STRVAL_PP ( str ) + f + l , Z_STRLEN_PP ( str ) - f - l ) ;
2005-09-08 05:05:36 +00:00
* ( ( char * ) buf + buf_len ) = ' \0 ' ;
}
* result = buf ;
return buf_len ;
}
/* }}} */
2003-05-02 10:02:40 +00:00
/* {{{ proto mixed substr_replace(mixed str, mixed repl, mixed start [, mixed length])
2001-09-04 10:44:28 +00:00
Replaces part of a string with another string */
1999-11-16 22:44:13 +00:00
PHP_FUNCTION ( substr_replace )
{
2002-09-11 14:40:52 +00:00
zval * * str ;
zval * * from ;
2003-05-02 10:02:40 +00:00
zval * * len = NULL ;
2002-09-11 14:40:52 +00:00
zval * * repl ;
2005-09-08 05:05:36 +00:00
void * result ;
int32_t result_len ;
int32_t l = 0 ;
int32_t f ;
2002-09-11 14:40:52 +00:00
int argc = ZEND_NUM_ARGS ( ) ;
2003-05-02 10:02:40 +00:00
HashPosition pos_str , pos_from , pos_repl , pos_len ;
zval * * tmp_str = NULL , * * tmp_from = NULL , * * tmp_repl = NULL , * * tmp_len = NULL ;
2005-09-28 14:58:31 +00:00
zend_uchar str_type ;
2003-05-02 10:02:40 +00:00
2002-09-11 14:40:52 +00:00
if ( argc < 3 | | argc > 4 | | zend_get_parameters_ex ( argc , & str , & repl , & from , & len ) = = FAILURE ) {
1999-11-16 22:44:13 +00:00
WRONG_PARAM_COUNT ;
}
2001-09-04 06:18:11 +00:00
2005-09-08 05:05:36 +00:00
if ( Z_TYPE_PP ( str ) ! = IS_ARRAY & & Z_TYPE_PP ( str ) ! = IS_UNICODE & &
Z_TYPE_PP ( str ) ! = IS_BINARY & & Z_TYPE_PP ( str ) ! = IS_STRING ) {
convert_to_text_ex ( str ) ;
2003-05-02 10:02:40 +00:00
}
2005-09-08 05:05:36 +00:00
if ( Z_TYPE_PP ( repl ) ! = IS_ARRAY & & Z_TYPE_PP ( repl ) ! = IS_UNICODE & &
Z_TYPE_PP ( repl ) ! = IS_BINARY & & Z_TYPE_PP ( repl ) ! = IS_STRING ) {
convert_to_text_ex ( repl ) ;
2003-05-02 10:02:40 +00:00
}
if ( Z_TYPE_PP ( from ) ! = IS_ARRAY ) {
convert_to_long_ex ( from ) ;
2005-09-08 05:05:36 +00:00
f = Z_LVAL_PP ( from ) ;
2003-05-02 10:02:40 +00:00
}
2001-09-04 06:18:11 +00:00
if ( argc > 3 ) {
2003-05-02 10:02:40 +00:00
if ( Z_TYPE_PP ( len ) ! = IS_ARRAY ) {
convert_to_long_ex ( len ) ;
l = Z_LVAL_PP ( len ) ;
}
2002-10-10 18:41:24 +00:00
} else {
2003-05-02 10:02:40 +00:00
if ( Z_TYPE_PP ( str ) ! = IS_ARRAY ) {
2005-09-08 12:40:01 +00:00
l = Z_UNILEN_PP ( str ) ;
2003-05-02 10:02:40 +00:00
}
1999-11-16 22:44:13 +00:00
}
2005-09-08 05:05:36 +00:00
if ( Z_TYPE_PP ( str ) ! = IS_ARRAY ) {
if ( ( argc = = 3 & & Z_TYPE_PP ( from ) = = IS_ARRAY ) | |
( argc = = 4 & & Z_TYPE_PP ( from ) ! = Z_TYPE_PP ( len ) ) ) {
2003-05-02 10:02:40 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " 'from' and 'len' should be of same type - numerical or array " ) ;
2005-09-08 05:05:36 +00:00
RETURN_ZVAL ( * str , 1 , 0 ) ;
2003-05-02 10:02:40 +00:00
}
if ( argc = = 4 & & Z_TYPE_PP ( from ) = = IS_ARRAY ) {
if ( zend_hash_num_elements ( Z_ARRVAL_PP ( from ) ) ! = zend_hash_num_elements ( Z_ARRVAL_PP ( len ) ) ) {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " 'from' and 'len' should have the same number of elements " ) ;
2005-09-08 05:05:36 +00:00
RETURN_ZVAL ( * str , 1 , 0 ) ;
2003-05-02 10:02:40 +00:00
}
1999-11-16 22:44:13 +00:00
}
2002-10-10 18:41:24 +00:00
}
2000-11-06 19:41:30 +00:00
2003-05-02 10:02:40 +00:00
if ( Z_TYPE_PP ( str ) ! = IS_ARRAY ) {
2005-09-08 05:05:36 +00:00
if ( Z_TYPE_PP ( from ) ! = IS_ARRAY ) {
2003-05-02 10:02:40 +00:00
if ( Z_TYPE_PP ( repl ) = = IS_ARRAY ) {
zend_hash_internal_pointer_reset_ex ( Z_ARRVAL_PP ( repl ) , & pos_repl ) ;
if ( SUCCESS = = zend_hash_get_current_data_ex ( Z_ARRVAL_PP ( repl ) , ( void * * ) & tmp_repl , & pos_repl ) ) {
2005-09-08 05:05:36 +00:00
if ( Z_TYPE_PP ( repl ) ! = IS_UNICODE & & Z_TYPE_PP ( repl ) ! = IS_BINARY & & Z_TYPE_PP ( repl ) ! = IS_STRING ) {
convert_to_text_ex ( tmp_repl ) ;
}
} else {
tmp_repl = NULL ;
2003-05-02 10:02:40 +00:00
}
} else {
2005-09-08 05:05:36 +00:00
tmp_repl = repl ;
2003-12-01 23:17:22 +00:00
}
2003-05-02 10:02:40 +00:00
2005-09-28 14:58:31 +00:00
if ( tmp_repl & & Z_TYPE_PP ( str ) ! = Z_TYPE_PP ( tmp_repl ) ) {
str_type = zend_get_unified_string_type ( 2 TSRMLS_CC , Z_TYPE_PP ( str ) , Z_TYPE_PP ( tmp_repl ) ) ;
if ( str_type = = ( zend_uchar ) - 1 ) {
2005-09-30 13:19:15 +00:00
convert_to_explicit_type_ex ( str , IS_BINARY ) ;
convert_to_explicit_type_ex ( tmp_repl , IS_BINARY ) ;
2005-09-28 14:58:31 +00:00
} else {
2005-09-30 13:19:15 +00:00
convert_to_explicit_type_ex ( str , str_type ) ;
convert_to_explicit_type_ex ( tmp_repl , str_type ) ;
2005-09-28 14:58:31 +00:00
}
}
2005-09-08 05:05:36 +00:00
php_adjust_limits ( str , & f , & l ) ;
result_len = php_do_substr_replace ( & result , str , tmp_repl , f , l TSRMLS_CC ) ;
if ( Z_TYPE_PP ( str ) = = IS_UNICODE ) {
RETURN_UNICODEL ( ( UChar * ) result , result_len , 0 ) ;
} else if ( Z_TYPE_PP ( str ) = = IS_BINARY ) {
RETURN_BINARYL ( ( char * ) result , result_len , 0 ) ;
} else {
RETURN_STRINGL ( ( char * ) result , result_len , 0 ) ;
2003-05-02 10:02:40 +00:00
}
} else {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Functionality of 'from' and 'len' as arrays is not implemented. " ) ;
2005-09-08 05:05:36 +00:00
RETURN_ZVAL ( * str , 1 , 0 ) ;
1999-11-16 22:44:13 +00:00
}
2003-05-02 10:02:40 +00:00
} else { /* str is array of strings */
array_init ( return_value ) ;
1999-11-16 22:44:13 +00:00
2003-05-02 10:02:40 +00:00
if ( Z_TYPE_PP ( from ) = = IS_ARRAY ) {
zend_hash_internal_pointer_reset_ex ( Z_ARRVAL_PP ( from ) , & pos_from ) ;
}
if ( argc > 3 & & Z_TYPE_PP ( len ) = = IS_ARRAY ) {
zend_hash_internal_pointer_reset_ex ( Z_ARRVAL_PP ( len ) , & pos_len ) ;
}
if ( Z_TYPE_PP ( repl ) = = IS_ARRAY ) {
zend_hash_internal_pointer_reset_ex ( Z_ARRVAL_PP ( repl ) , & pos_repl ) ;
}
2003-12-01 23:17:22 +00:00
2003-05-02 10:02:40 +00:00
zend_hash_internal_pointer_reset_ex ( Z_ARRVAL_PP ( str ) , & pos_str ) ;
while ( zend_hash_get_current_data_ex ( Z_ARRVAL_PP ( str ) , ( void * * ) & tmp_str , & pos_str ) = = SUCCESS ) {
2005-09-08 05:05:36 +00:00
if ( Z_TYPE_PP ( tmp_str ) ! = IS_UNICODE & & Z_TYPE_PP ( tmp_str ) ! = IS_BINARY & & Z_TYPE_PP ( tmp_str ) ! = IS_STRING ) {
convert_to_text_ex ( tmp_str ) ;
}
2003-12-01 23:17:22 +00:00
2003-05-02 10:02:40 +00:00
if ( Z_TYPE_PP ( from ) = = IS_ARRAY ) {
if ( SUCCESS = = zend_hash_get_current_data_ex ( Z_ARRVAL_PP ( from ) , ( void * * ) & tmp_from , & pos_from ) ) {
convert_to_long_ex ( tmp_from ) ;
f = Z_LVAL_PP ( tmp_from ) ;
zend_hash_move_forward_ex ( Z_ARRVAL_PP ( from ) , & pos_from ) ;
} else {
f = 0 ;
2003-12-01 23:17:22 +00:00
}
2003-05-02 10:02:40 +00:00
}
2005-09-08 05:05:36 +00:00
if ( argc > 3 & & ( Z_TYPE_PP ( len ) = = IS_ARRAY ) ) {
2003-05-02 10:02:40 +00:00
if ( SUCCESS = = zend_hash_get_current_data_ex ( Z_ARRVAL_PP ( len ) , ( void * * ) & tmp_len , & pos_len ) ) {
convert_to_long_ex ( tmp_len ) ;
l = Z_LVAL_PP ( tmp_len ) ;
zend_hash_move_forward_ex ( Z_ARRVAL_PP ( len ) , & pos_len ) ;
} else {
2005-09-08 12:40:01 +00:00
l = Z_UNILEN_PP ( tmp_str ) ;
2003-05-02 10:02:40 +00:00
}
2005-09-08 05:05:36 +00:00
} else if ( argc > 3 ) {
/* 'l' parsed & set at top of funcn */
2005-09-08 12:40:01 +00:00
l = Z_LVAL_PP ( len ) ;
2003-05-02 10:02:40 +00:00
} else {
2005-09-08 12:40:01 +00:00
l = Z_UNILEN_PP ( tmp_str ) ;
2003-12-01 23:17:22 +00:00
}
2003-05-02 10:02:40 +00:00
if ( Z_TYPE_PP ( repl ) = = IS_ARRAY ) {
if ( SUCCESS = = zend_hash_get_current_data_ex ( Z_ARRVAL_PP ( repl ) , ( void * * ) & tmp_repl , & pos_repl ) ) {
2005-09-08 05:05:36 +00:00
if ( Z_TYPE_PP ( repl ) ! = IS_UNICODE & & Z_TYPE_PP ( repl ) ! = IS_BINARY & & Z_TYPE_PP ( repl ) ! = IS_STRING ) {
convert_to_text_ex ( tmp_repl ) ;
}
2003-05-02 10:02:40 +00:00
zend_hash_move_forward_ex ( Z_ARRVAL_PP ( repl ) , & pos_repl ) ;
} else {
2005-09-08 05:05:36 +00:00
tmp_repl = NULL ;
2003-05-02 10:02:40 +00:00
}
} else {
2005-09-08 05:05:36 +00:00
tmp_repl = repl ;
}
2003-05-02 10:02:40 +00:00
2005-09-28 14:58:31 +00:00
if ( tmp_repl & & Z_TYPE_PP ( tmp_str ) ! = Z_TYPE_PP ( tmp_repl ) ) {
str_type = zend_get_unified_string_type ( 2 TSRMLS_CC , Z_TYPE_PP ( tmp_str ) , Z_TYPE_PP ( tmp_repl ) ) ;
if ( str_type = = ( zend_uchar ) - 1 ) {
2005-09-30 13:19:15 +00:00
convert_to_explicit_type_ex ( tmp_str , IS_BINARY ) ;
convert_to_explicit_type_ex ( tmp_repl , IS_BINARY ) ;
2005-09-28 14:58:31 +00:00
} else {
2005-09-30 13:19:15 +00:00
convert_to_explicit_type_ex ( tmp_str , str_type ) ;
convert_to_explicit_type_ex ( tmp_repl , str_type ) ;
2005-09-28 14:58:31 +00:00
}
}
2005-09-08 05:05:36 +00:00
php_adjust_limits ( tmp_str , & f , & l ) ;
result_len = php_do_substr_replace ( & result , tmp_str , tmp_repl , f , l TSRMLS_CC ) ;
1999-11-16 22:44:13 +00:00
2005-09-08 05:05:36 +00:00
if ( Z_TYPE_PP ( tmp_str ) = = IS_UNICODE ) {
add_next_index_unicodel ( return_value , ( UChar * ) result , result_len , 0 ) ;
} else if ( Z_TYPE_PP ( tmp_str ) = = IS_BINARY ) {
add_next_index_binaryl ( return_value , ( char * ) result , result_len , 0 ) ;
} else {
add_next_index_stringl ( return_value , ( char * ) result , result_len , 0 ) ;
2003-05-02 10:02:40 +00:00
}
zend_hash_move_forward_ex ( Z_ARRVAL_PP ( str ) , & pos_str ) ;
} /*while*/
} /* if */
1999-11-16 22:44:13 +00:00
}
/* }}} */
2003-05-02 10:02:40 +00:00
1999-04-17 00:37:12 +00:00
/* {{{ proto string quotemeta(string str)
2001-09-04 10:44:28 +00:00
Quotes meta characters */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( quotemeta )
1999-04-17 00:37:12 +00:00
{
1999-12-20 20:43:47 +00:00
zval * * arg ;
1999-04-17 00:37:12 +00:00
char * str , * old ;
1999-12-20 20:43:47 +00:00
char * old_end ;
1999-04-17 00:37:12 +00:00
char * p , * q ;
char c ;
2002-09-11 14:40:52 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( 1 , & arg ) = = FAILURE ) {
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
}
2003-02-08 15:26:17 +00:00
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( arg ) ;
1999-04-17 00:37:12 +00:00
2001-09-04 06:18:11 +00:00
old = Z_STRVAL_PP ( arg ) ;
old_end = Z_STRVAL_PP ( arg ) + Z_STRLEN_PP ( arg ) ;
1999-04-17 00:37:12 +00:00
1999-12-20 20:43:47 +00:00
if ( old = = old_end ) {
1999-04-17 00:37:12 +00:00
RETURN_FALSE ;
}
2003-08-11 23:16:54 +00:00
str = safe_emalloc ( 2 , Z_STRLEN_PP ( arg ) , 1 ) ;
1999-04-17 00:37:12 +00:00
2002-10-02 17:56:04 +00:00
for ( p = old , q = str ; p ! = old_end ; p + + ) {
1999-12-20 20:43:47 +00:00
c = * p ;
2002-10-02 17:56:04 +00:00
switch ( c ) {
1999-04-17 00:37:12 +00:00
case ' . ' :
case ' \\ ' :
case ' + ' :
case ' * ' :
case ' ? ' :
case ' [ ' :
case ' ^ ' :
case ' ] ' :
case ' $ ' :
case ' ( ' :
case ' ) ' :
* q + + = ' \\ ' ;
/* break is missing _intentionally_ */
default :
* q + + = c ;
}
}
* q = 0 ;
2001-09-04 06:18:11 +00:00
RETURN_STRINGL ( erealloc ( str , q - str + 1 ) , q - str , 0 ) ;
1999-04-17 00:37:12 +00:00
}
/* }}} */
2005-08-19 22:00:21 +00:00
/* {{{ proto int ord(text character)
Returns the codepoint value of a character */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( ord )
1999-04-17 00:37:12 +00:00
{
1999-12-23 14:59:01 +00:00
zval * * str ;
1999-04-17 00:37:12 +00:00
2003-02-08 15:26:17 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( 1 , & str ) = = FAILURE ) {
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
}
2001-09-04 06:18:11 +00:00
2005-09-08 14:07:40 +00:00
if ( Z_TYPE_PP ( str ) = = IS_BINARY ) {
RETURN_LONG ( ( unsigned char ) Z_STRVAL_PP ( str ) [ 0 ] ) ;
}
convert_to_text_ex ( str ) ;
2005-08-19 22:00:21 +00:00
if ( Z_TYPE_PP ( str ) = = IS_UNICODE ) {
RETURN_LONG ( zend_get_codepoint_at ( Z_USTRVAL_PP ( str ) , Z_USTRLEN_PP ( str ) , 0 ) ) ;
} else {
RETURN_LONG ( ( unsigned char ) Z_STRVAL_PP ( str ) [ 0 ] ) ;
}
1999-04-17 00:37:12 +00:00
}
/* }}} */
2005-08-19 22:00:21 +00:00
/* {{{ proto text chr(int codepoint)
Converts a codepoint number to a character */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( chr )
1999-04-17 00:37:12 +00:00
{
1999-12-23 14:59:01 +00:00
zval * * num ;
1999-04-17 00:37:12 +00:00
char temp [ 2 ] ;
2000-06-05 19:47:54 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( 1 , & num ) = = FAILURE ) {
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
}
1999-09-25 11:55:42 +00:00
convert_to_long_ex ( num ) ;
2001-09-04 06:18:11 +00:00
2005-08-19 22:00:21 +00:00
if ( UG ( unicode ) ) {
UChar buf [ 2 ] ;
int32_t buf_len ;
2001-09-04 06:18:11 +00:00
2005-08-19 22:00:21 +00:00
if ( Z_LVAL_PP ( num ) > UCHAR_MAX_VALUE ) {
php_error ( E_WARNING , " Codepoint value cannot be greater than %X " , UCHAR_MAX_VALUE ) ;
return ;
}
buf_len = zend_codepoint_to_uchar ( ( uint32_t ) Z_LVAL_PP ( num ) , buf ) ;
RETURN_UNICODEL ( buf , buf_len , 1 ) ;
} else {
temp [ 0 ] = ( char ) Z_LVAL_PP ( num ) ;
temp [ 1 ] = 0 ;
RETVAL_STRINGL ( temp , 1 , 1 ) ;
}
1999-04-17 00:37:12 +00:00
}
/* }}} */
2005-08-18 05:53:46 +00:00
/* {{{ php_u_ucfirst()
2005-08-17 17:33:19 +00:00
Makes an Unicode string ' s first character uppercase */
static void php_u_ucfirst ( zval * ustr , zval * return_value )
{
UChar32 lc , uc ;
UChar tmp [ 2 ] = { 0 , 0 } ; /* UChar32 will be converted to upto 2 UChar units ? */
int32_t tmp_len = 2 ;
int32_t pos = 0 ;
UErrorCode err = U_ZERO_ERROR ;
U16_NEXT ( Z_USTRVAL_P ( ustr ) , pos , Z_USTRLEN_P ( ustr ) , lc ) ;
uc = u_toupper ( lc ) ;
if ( uc = = lc ) {
ZVAL_UNICODEL ( return_value , Z_USTRVAL_P ( ustr ) , Z_USTRLEN_P ( ustr ) , 1 ) ;
return ;
}
u_strFromUTF32 ( tmp , tmp_len , & tmp_len , & uc , 1 , & err ) ;
if ( U_FAILURE ( err ) ) {
ZVAL_EMPTY_UNICODE ( return_value ) ;
return ;
}
Z_USTRVAL_P ( return_value ) = eumalloc ( tmp_len + Z_USTRLEN_P ( ustr ) - pos + 1 ) ;
Z_USTRVAL_P ( return_value ) [ 0 ] = tmp [ 0 ] ;
if ( tmp_len > 1 ) {
Z_USTRVAL_P ( return_value ) [ 1 ] = tmp [ 1 ] ;
}
memcpy ( Z_USTRVAL_P ( return_value ) + tmp_len , Z_USTRVAL_P ( ustr ) + pos , UBYTES ( Z_USTRLEN_P ( ustr ) - pos + 1 ) ) ;
Z_USTRLEN_P ( return_value ) = tmp_len + Z_USTRLEN_P ( ustr ) - pos ;
}
/* }}} */
1999-08-09 20:52:58 +00:00
/* {{{ proto string ucfirst(string str)
2001-09-04 10:44:28 +00:00
Makes a string ' s first character uppercase */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( ucfirst )
1999-04-17 00:37:12 +00:00
{
2001-09-04 06:18:11 +00:00
zval * * str ;
1999-04-17 00:37:12 +00:00
2002-09-11 14:40:52 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( 1 , & str ) = = FAILURE ) {
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
}
2005-08-17 17:33:19 +00:00
if ( Z_TYPE_PP ( str ) ! = IS_UNICODE & & Z_TYPE_PP ( str ) ! = IS_BINARY & & Z_TYPE_PP ( str ) ! = IS_STRING ) {
convert_to_text_ex ( str ) ;
}
if ( Z_TYPE_PP ( str ) = = IS_UNICODE & & ! Z_USTRLEN_PP ( str ) ) {
RETURN_EMPTY_UNICODE ( ) ;
} else if ( Z_TYPE_PP ( str ) = = IS_BINARY & & ! Z_BINLEN_PP ( str ) ) {
RETURN_EMPTY_BINARY ( ) ;
} else if ( ! Z_STRLEN_PP ( str ) ) {
2002-06-17 11:37:49 +00:00
RETURN_EMPTY_STRING ( ) ;
1999-04-17 00:37:12 +00:00
}
1999-11-14 17:20:56 +00:00
2005-08-17 17:33:19 +00:00
if ( Z_TYPE_PP ( str ) = = IS_UNICODE ) {
Z_TYPE_P ( return_value ) = IS_UNICODE ;
php_u_ucfirst ( * str , return_value ) ;
} else if ( Z_TYPE_PP ( str ) = = IS_BINARY ) {
ZVAL_BINARYL ( return_value , Z_BINVAL_PP ( str ) , Z_BINLEN_PP ( str ) , 1 ) ;
* Z_BINVAL_P ( return_value ) = toupper ( ( unsigned char ) * Z_BINVAL_P ( return_value ) ) ;
} else {
ZVAL_STRINGL ( return_value , Z_STRVAL_PP ( str ) , Z_STRLEN_PP ( str ) , 1 ) ;
* Z_STRVAL_P ( return_value ) = toupper ( ( unsigned char ) * Z_STRVAL_P ( return_value ) ) ;
}
1999-04-17 00:37:12 +00:00
}
/* }}} */
2005-08-18 05:53:46 +00:00
/* {{{ php_u_ucwords()
2005-08-17 20:29:00 +00:00
Uppercase the first character of every word in an Unicode string */
static void php_u_ucwords ( zval * ustr , zval * retval )
{
UChar32 * codepts ;
int32_t len , retval_len ;
int32_t i ;
UErrorCode err ;
len = Z_USTRLEN_P ( ustr ) ;
codepts = ( UChar32 * ) emalloc ( ( len + 1 ) * sizeof ( UChar32 ) ) ;
err = U_ZERO_ERROR ;
u_strToUTF32 ( codepts , len + 1 , & len , Z_USTRVAL_P ( ustr ) , len , & err ) ;
if ( U_FAILURE ( err ) ) {
efree ( codepts ) ;
ZVAL_EMPTY_UNICODE ( retval ) ;
return ;
}
codepts [ 0 ] = u_toupper ( codepts [ 0 ] ) ;
for ( i = 1 ; i < len ; i + + ) {
if ( u_isWhitespace ( codepts [ i - 1 ] ) = = TRUE ) {
2005-08-18 22:37:22 +00:00
codepts [ i ] = u_totitle ( codepts [ i ] ) ;
2005-08-17 20:29:00 +00:00
}
}
retval_len = len ;
Z_USTRVAL_P ( retval ) = eumalloc ( retval_len + 1 ) ;
err = U_ZERO_ERROR ;
u_strFromUTF32 ( Z_USTRVAL_P ( retval ) , retval_len + 1 , & retval_len , codepts , len , & err ) ;
if ( U_FAILURE ( err ) = = U_BUFFER_OVERFLOW_ERROR ) {
err = U_ZERO_ERROR ;
Z_USTRVAL_P ( retval ) = eurealloc ( Z_USTRVAL_P ( retval ) , retval_len + 1 ) ;
u_strFromUTF32 ( Z_USTRVAL_P ( retval ) , retval_len + 1 , NULL , codepts , len , & err ) ;
}
if ( U_SUCCESS ( err ) ) {
Z_USTRLEN_P ( retval ) = retval_len ;
} else {
efree ( Z_USTRVAL_P ( retval ) ) ;
ZVAL_EMPTY_UNICODE ( retval ) ;
}
efree ( codepts ) ;
}
/* }}} */
1999-04-17 00:37:12 +00:00
/* {{{ proto string ucwords(string str)
Uppercase the first character of every word in a string */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( ucwords )
1999-04-17 00:37:12 +00:00
{
1999-12-11 19:50:01 +00:00
zval * * str ;
2000-06-01 14:18:45 +00:00
register char * r , * r_end ;
1999-04-17 00:37:12 +00:00
2000-06-05 19:47:54 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( 1 , & str ) = = FAILURE ) {
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
}
2005-08-17 20:29:00 +00:00
if ( Z_TYPE_PP ( str ) ! = IS_UNICODE & & Z_TYPE_PP ( str ) ! = IS_BINARY & & Z_TYPE_PP ( str ) ! = IS_STRING ) {
convert_to_text_ex ( str ) ;
}
if ( Z_TYPE_PP ( str ) = = IS_UNICODE & & ! Z_USTRLEN_PP ( str ) ) {
RETURN_EMPTY_UNICODE ( ) ;
} else if ( Z_TYPE_PP ( str ) = = IS_BINARY & & ! Z_BINLEN_PP ( str ) ) {
RETURN_EMPTY_BINARY ( ) ;
} else if ( ! Z_STRLEN_PP ( str ) ) {
2002-06-17 11:37:49 +00:00
RETURN_EMPTY_STRING ( ) ;
1999-04-17 00:37:12 +00:00
}
2000-06-01 10:07:44 +00:00
2005-08-17 20:29:00 +00:00
if ( Z_TYPE_PP ( str ) = = IS_UNICODE ) {
Z_TYPE_P ( return_value ) = IS_UNICODE ;
php_u_ucwords ( * str , return_value ) ;
} else {
if ( Z_TYPE_PP ( str ) = = IS_BINARY ) {
ZVAL_BINARYL ( return_value , Z_BINVAL_PP ( str ) , Z_BINLEN_PP ( str ) , 1 ) ;
} else {
ZVAL_STRINGL ( return_value , Z_STRVAL_PP ( str ) , Z_STRLEN_PP ( str ) , 1 ) ;
}
2001-09-04 06:18:11 +00:00
2005-08-17 20:29:00 +00:00
r = Z_STRVAL_P ( return_value ) ;
* r = toupper ( ( unsigned char ) * r ) ;
for ( r_end = r + Z_STRLEN_P ( return_value ) - 1 ; r < r_end ; ) {
if ( isspace ( ( int ) * ( unsigned char * ) r + + ) ) {
* r = toupper ( ( unsigned char ) * r ) ;
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
}
1999-04-17 00:37:12 +00:00
}
}
/* }}} */
2001-06-05 13:12:10 +00:00
/* {{{ php_strtr
*/
2003-02-08 15:26:17 +00:00
PHPAPI char * php_strtr ( char * str , int len , char * str_from , char * str_to , int trlen )
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
{
int i ;
unsigned char xlat [ 256 ] ;
if ( ( trlen < 1 ) | | ( len < 1 ) ) {
2000-02-08 22:21:43 +00:00
return str ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
}
for ( i = 0 ; i < 256 ; xlat [ i ] = i , i + + ) ;
for ( i = 0 ; i < trlen ; i + + ) {
xlat [ ( unsigned char ) str_from [ i ] ] = str_to [ i ] ;
}
for ( i = 0 ; i < len ; i + + ) {
2000-02-08 22:21:43 +00:00
str [ i ] = xlat [ ( unsigned char ) str [ i ] ] ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
}
2000-02-08 22:21:43 +00:00
return str ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
}
2001-06-05 13:12:10 +00:00
/* }}} */
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
2001-06-05 13:12:10 +00:00
/* {{{ php_strtr_array
*/
2001-08-11 17:03:37 +00:00
static void php_strtr_array ( zval * return_value , char * str , int slen , HashTable * hash )
1999-11-20 15:53:18 +00:00
{
2001-09-04 06:18:11 +00:00
zval * * entry ;
char * string_key ;
uint string_key_len ;
1999-11-20 15:53:18 +00:00
zval * * trans ;
2001-09-04 06:18:11 +00:00
zval ctmp ;
1999-11-20 15:53:18 +00:00
ulong num_key ;
int minlen = 128 * 1024 ;
2000-10-12 01:26:26 +00:00
int maxlen = 0 , pos , len , found ;
char * key ;
2000-06-18 10:37:01 +00:00
HashPosition hpos ;
2000-10-12 01:26:26 +00:00
smart_str result = { 0 } ;
2004-03-18 09:48:37 +00:00
HashTable tmp_hash ;
1999-11-20 15:53:18 +00:00
2004-03-18 09:48:37 +00:00
zend_hash_init ( & tmp_hash , 0 , NULL , NULL , 0 ) ;
2000-06-18 10:37:01 +00:00
zend_hash_internal_pointer_reset_ex ( hash , & hpos ) ;
while ( zend_hash_get_current_data_ex ( hash , ( void * * ) & entry , & hpos ) = = SUCCESS ) {
2001-09-04 06:18:11 +00:00
switch ( zend_hash_get_current_key_ex ( hash , & string_key , & string_key_len , & num_key , 0 , & hpos ) ) {
2003-02-08 15:26:17 +00:00
case HASH_KEY_IS_STRING :
len = string_key_len - 1 ;
2003-09-20 16:10:49 +00:00
if ( len < 1 ) {
2004-03-18 09:48:37 +00:00
zend_hash_destroy ( & tmp_hash ) ;
2003-09-20 16:10:49 +00:00
RETURN_FALSE ;
}
2004-03-18 09:48:37 +00:00
zend_hash_add ( & tmp_hash , string_key , string_key_len , entry , sizeof ( zval * ) , NULL ) ;
2003-02-08 15:26:17 +00:00
if ( len > maxlen ) {
maxlen = len ;
}
if ( len < minlen ) {
minlen = len ;
}
break ;
1999-11-20 15:53:18 +00:00
2003-02-08 15:26:17 +00:00
case HASH_KEY_IS_LONG :
Z_TYPE ( ctmp ) = IS_LONG ;
Z_LVAL ( ctmp ) = num_key ;
1999-11-20 15:53:18 +00:00
2003-02-08 15:26:17 +00:00
convert_to_string ( & ctmp ) ;
len = Z_STRLEN ( ctmp ) ;
2004-03-18 09:48:37 +00:00
zend_hash_add ( & tmp_hash , Z_STRVAL ( ctmp ) , len + 1 , entry , sizeof ( zval * ) , NULL ) ;
2003-02-08 15:26:17 +00:00
zval_dtor ( & ctmp ) ;
1999-11-20 15:53:18 +00:00
2003-02-08 15:26:17 +00:00
if ( len > maxlen ) {
maxlen = len ;
}
if ( len < minlen ) {
minlen = len ;
}
break ;
1999-11-20 15:53:18 +00:00
}
2000-06-18 10:37:01 +00:00
zend_hash_move_forward_ex ( hash , & hpos ) ;
1999-11-20 15:53:18 +00:00
}
2000-10-12 01:26:26 +00:00
1999-11-20 15:53:18 +00:00
key = emalloc ( maxlen + 1 ) ;
2000-10-12 01:26:26 +00:00
pos = 0 ;
1999-11-20 15:53:18 +00:00
while ( pos < slen ) {
if ( ( pos + maxlen ) > slen ) {
maxlen = slen - pos ;
}
2001-09-04 06:18:11 +00:00
1999-11-20 15:53:18 +00:00
found = 0 ;
2001-08-11 17:03:37 +00:00
memcpy ( key , str + pos , maxlen ) ;
1999-11-20 15:53:18 +00:00
for ( len = maxlen ; len > = minlen ; len - - ) {
2001-09-04 06:18:11 +00:00
key [ len ] = 0 ;
1999-11-20 15:53:18 +00:00
2004-03-18 09:48:37 +00:00
if ( zend_hash_find ( & tmp_hash , key , len + 1 , ( void * * ) & trans ) = = SUCCESS ) {
1999-11-20 15:53:18 +00:00
char * tval ;
int tlen ;
zval tmp ;
2001-09-04 06:18:11 +00:00
if ( Z_TYPE_PP ( trans ) ! = IS_STRING ) {
1999-11-20 15:53:18 +00:00
tmp = * * trans ;
zval_copy_ctor ( & tmp ) ;
convert_to_string ( & tmp ) ;
2001-09-04 06:18:11 +00:00
tval = Z_STRVAL ( tmp ) ;
tlen = Z_STRLEN ( tmp ) ;
1999-11-20 15:53:18 +00:00
} else {
2001-09-04 06:18:11 +00:00
tval = Z_STRVAL_PP ( trans ) ;
tlen = Z_STRLEN_PP ( trans ) ;
1999-11-20 15:53:18 +00:00
}
2000-10-12 01:26:26 +00:00
smart_str_appendl ( & result , tval , tlen ) ;
1999-11-20 15:53:18 +00:00
pos + = len ;
found = 1 ;
2001-09-04 06:18:11 +00:00
if ( Z_TYPE_PP ( trans ) ! = IS_STRING ) {
1999-11-20 15:53:18 +00:00
zval_dtor ( & tmp ) ;
}
break ;
}
}
if ( ! found ) {
2000-10-12 01:26:26 +00:00
smart_str_appendc ( & result , str [ pos + + ] ) ;
1999-11-20 15:53:18 +00:00
}
}
efree ( key ) ;
2004-03-18 09:48:37 +00:00
zend_hash_destroy ( & tmp_hash ) ;
2000-10-12 01:26:26 +00:00
smart_str_0 ( & result ) ;
2001-08-11 17:03:37 +00:00
RETVAL_STRINGL ( result . c , result . len , 0 ) ;
1999-11-20 15:53:18 +00:00
}
2001-06-05 13:12:10 +00:00
/* }}} */
1999-11-20 15:53:18 +00:00
1999-04-17 00:37:12 +00:00
/* {{{ proto string strtr(string str, string from, string to)
2001-09-04 10:44:28 +00:00
Translates characters in str using given translation tables */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( strtr )
2003-02-08 15:26:17 +00:00
{
1999-12-23 14:59:01 +00:00
zval * * str , * * from , * * to ;
2000-06-05 19:47:54 +00:00
int ac = ZEND_NUM_ARGS ( ) ;
1999-11-20 15:53:18 +00:00
1999-12-18 22:40:35 +00:00
if ( ac < 2 | | ac > 3 | | zend_get_parameters_ex ( ac , & str , & from , & to ) = = FAILURE ) {
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
}
1999-11-20 15:53:18 +00:00
2001-09-25 21:58:48 +00:00
if ( ac = = 2 & & Z_TYPE_PP ( from ) ! = IS_ARRAY ) {
2002-09-05 11:29:31 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " The second argument is not an array. " ) ;
1999-11-20 15:53:18 +00:00
RETURN_FALSE ;
}
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( str ) ;
1999-11-20 15:53:18 +00:00
2000-12-13 10:22:36 +00:00
/* shortcut for empty string */
2002-10-02 17:56:04 +00:00
if ( Z_STRLEN_PP ( str ) = = 0 ) {
2000-12-13 10:22:36 +00:00
RETURN_EMPTY_STRING ( ) ;
}
1999-11-20 15:53:18 +00:00
if ( ac = = 2 ) {
2001-09-04 06:18:11 +00:00
php_strtr_array ( return_value , Z_STRVAL_PP ( str ) , Z_STRLEN_PP ( str ) , HASH_OF ( * from ) ) ;
1999-11-20 15:53:18 +00:00
} else {
convert_to_string_ex ( from ) ;
convert_to_string_ex ( to ) ;
2001-09-04 06:18:11 +00:00
ZVAL_STRINGL ( return_value , Z_STRVAL_PP ( str ) , Z_STRLEN_PP ( str ) , 1 ) ;
1999-11-20 15:53:18 +00:00
2001-09-04 06:18:11 +00:00
php_strtr ( Z_STRVAL_P ( return_value ) ,
Z_STRLEN_P ( return_value ) ,
Z_STRVAL_PP ( from ) ,
Z_STRVAL_PP ( to ) ,
2003-02-08 15:26:17 +00:00
MIN ( Z_STRLEN_PP ( from ) ,
Z_STRLEN_PP ( to ) ) ) ;
1999-11-20 15:53:18 +00:00
}
1999-04-17 00:37:12 +00:00
}
/* }}} */
/* {{{ proto string strrev(string str)
Reverse a string */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( strrev )
1999-04-17 00:37:12 +00:00
{
1999-12-23 14:59:01 +00:00
zval * * str ;
2002-10-09 13:37:02 +00:00
char * s , * e , * n , * p ;
2005-08-26 10:21:07 +00:00
int32_t i , x1 , x2 ;
UChar32 ch ;
UChar * u_s , * u_n , * u_p ;
1999-04-17 00:37:12 +00:00
2002-10-02 17:56:04 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( 1 , & str ) = = FAILURE ) {
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
}
2005-08-19 10:59:19 +00:00
if ( Z_TYPE_PP ( str ) ! = IS_UNICODE & & Z_TYPE_PP ( str ) ! = IS_BINARY & & Z_TYPE_PP ( str ) ! = IS_STRING ) {
convert_to_text_ex ( str ) ;
}
if ( Z_TYPE_PP ( str ) = = IS_UNICODE ) {
u_n = eumalloc ( Z_USTRLEN_PP ( str ) + 1 ) ;
u_p = u_n ;
u_s = Z_USTRVAL_PP ( str ) ;
2005-08-26 10:21:07 +00:00
i = Z_USTRLEN_PP ( str ) ;
while ( i > 0 ) {
U16_PREV ( u_s , 0 , i , ch ) ;
if ( u_getCombiningClass ( ch ) = = 0 ) {
u_p + = zend_codepoint_to_uchar ( ch , u_p ) ;
2005-08-19 10:59:19 +00:00
} else {
2005-08-26 10:21:07 +00:00
x2 = i ;
do {
U16_PREV ( u_s , 0 , i , ch ) ;
} while ( u_getCombiningClass ( ch ) ! = 0 ) ;
x1 = i ;
while ( x1 < = x2 ) {
U16_NEXT ( u_s , x1 , Z_USTRLEN_PP ( str ) , ch ) ;
u_p + = zend_codepoint_to_uchar ( ch , u_p ) ;
}
2005-08-19 10:59:19 +00:00
}
}
* u_p = 0 ;
} else {
n = emalloc ( Z_STRLEN_PP ( str ) + 1 ) ;
p = n ;
s = Z_STRVAL_PP ( str ) ;
e = s + Z_STRLEN_PP ( str ) ;
while ( - - e > = s ) {
* ( p + + ) = * e ;
}
* p = ' \0 ' ;
1999-04-17 00:37:12 +00:00
}
2002-10-09 13:37:02 +00:00
2005-08-19 10:59:19 +00:00
if ( Z_TYPE_PP ( str ) = = IS_UNICODE ) {
RETVAL_UNICODEL ( u_n , Z_USTRLEN_PP ( str ) , 0 ) ;
} else if ( Z_TYPE_PP ( str ) = = IS_BINARY ) {
RETVAL_BINARYL ( n , Z_BINLEN_PP ( str ) , 0 ) ;
} else {
RETVAL_STRINGL ( n , Z_STRLEN_PP ( str ) , 0 ) ;
}
1999-04-17 00:37:12 +00:00
}
/* }}} */
2005-10-03 06:14:10 +00:00
/* {{{ php_u_similar_str
*/
static void php_u_similar_str ( const UChar * txt1 , int32_t len1 ,
const UChar * txt2 , int32_t len2 ,
int32_t * pos1 , int32_t * end1 ,
int32_t * pos2 , int32_t * end2 , int * max )
{
int32_t i1 , i2 , j1 , j2 , l ;
UChar32 ch1 , ch2 ;
* max = 0 ;
for ( i1 = 0 ; i1 < len1 ; ) {
for ( i2 = 0 ; i2 < len2 ; ) {
l = 0 ; j1 = 0 ; j2 = 0 ;
while ( ( i1 + j1 < len1 ) & & ( i2 + j2 < len2 ) ) {
U16_NEXT ( txt1 + i1 , j1 , len1 - i1 , ch1 ) ;
U16_NEXT ( txt2 + i2 , j2 , len2 - i2 , ch2 ) ;
if ( ch1 ! = ch2 ) {
U16_BACK_1 ( txt1 + i1 , 0 , j1 ) ;
U16_BACK_1 ( txt2 + i2 , 0 , j2 ) ;
break ;
}
l + + ;
}
if ( l > * max ) {
* max = l ;
* pos1 = i1 ; * end1 = j1 ;
* pos2 = i2 ; * end2 = j2 ;
}
U16_FWD_1 ( txt2 , i2 , len2 ) ;
}
U16_FWD_1 ( txt1 , i1 , len1 ) ;
}
}
/* }}} */
2001-06-05 13:12:10 +00:00
/* {{{ php_similar_str
*/
2003-02-08 15:26:17 +00:00
static void php_similar_str ( const char * txt1 , int len1 , const char * txt2 , int len2 , int * pos1 , int * pos2 , int * max )
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
{
char * p , * q ;
char * end1 = ( char * ) txt1 + len1 ;
char * end2 = ( char * ) txt2 + len2 ;
int l ;
* max = 0 ;
for ( p = ( char * ) txt1 ; p < end1 ; p + + ) {
for ( q = ( char * ) txt2 ; q < end2 ; q + + ) {
2002-10-10 18:41:24 +00:00
for ( l = 0 ; ( p + l < end1 ) & & ( q + l < end2 ) & & ( p [ l ] = = q [ l ] ) ; l + + ) ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
if ( l > * max ) {
* max = l ;
* pos1 = p - txt1 ;
* pos2 = q - txt2 ;
}
}
}
}
2001-06-05 13:12:10 +00:00
/* }}} */
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
2005-10-04 06:33:10 +00:00
/* {{{ php_similar_char
2005-10-03 06:14:10 +00:00
*/
2005-10-04 06:33:10 +00:00
static int php_similar_char ( const char * txt1 , int len1 , const char * txt2 , int len2 )
2005-10-03 06:14:10 +00:00
{
2005-10-04 06:33:10 +00:00
int sum ;
int pos1 , pos2 , max ;
2005-10-03 06:14:10 +00:00
2005-10-04 06:33:10 +00:00
php_similar_str ( txt1 , len1 , txt2 , len2 , & pos1 , & pos2 , & max ) ;
2005-10-03 06:14:10 +00:00
if ( ( sum = max ) ) {
if ( pos1 & & pos2 ) {
2005-10-04 06:33:10 +00:00
sum + = php_similar_char ( txt1 , pos1 ,
txt2 , pos2 ) ;
2005-10-03 06:14:10 +00:00
}
2005-10-04 06:33:10 +00:00
if ( ( pos1 + max < len1 ) & & ( pos2 + max < len2 ) ) {
sum + = php_similar_char ( txt1 + pos1 + max , len1 - pos1 - max ,
txt2 + pos2 + max , len2 - pos2 - max ) ;
2005-10-03 06:14:10 +00:00
}
}
2005-10-04 06:33:10 +00:00
2005-10-03 06:14:10 +00:00
return sum ;
}
/* }}} */
2005-10-04 06:33:10 +00:00
/* {{{ php_u_similar_char
2001-06-05 13:12:10 +00:00
*/
2005-10-04 06:33:10 +00:00
static int php_u_similar_char ( const UChar * txt1 , int32_t len1 , const UChar * txt2 , int32_t len2 )
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
{
2005-10-04 06:33:10 +00:00
int sum , max ;
int32_t pos1 , pos2 , end1 , end2 ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
2005-10-04 06:33:10 +00:00
php_u_similar_str ( txt1 , len1 , txt2 , len2 , & pos1 , & end1 , & pos2 , & end2 , & max ) ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
if ( ( sum = max ) ) {
2003-02-08 15:26:17 +00:00
if ( pos1 & & pos2 ) {
2005-10-04 06:33:10 +00:00
sum + = php_u_similar_char ( txt1 , pos1 , txt2 , pos2 ) ;
2003-02-08 15:26:17 +00:00
}
2005-10-04 06:33:10 +00:00
if ( ( pos1 + end1 < len1 ) & & ( pos2 + end2 < len2 ) ) {
sum + = php_similar_char ( ( UChar * ) txt1 + pos1 + end1 , len1 - pos1 - end1 ,
( UChar * ) txt2 + pos2 + end2 , len2 - pos2 - end2 ) ;
2003-02-08 15:26:17 +00:00
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
}
return sum ;
}
2001-06-05 13:12:10 +00:00
/* }}} */
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
2001-09-21 21:59:27 +00:00
/* {{{ proto int similar_text(string str1, string str2 [, float percent])
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
Calculates the similarity between two strings */
PHP_FUNCTION ( similar_text )
{
1999-12-23 14:59:01 +00:00
zval * * t1 , * * t2 , * * percent ;
2000-06-05 19:47:54 +00:00
int ac = ZEND_NUM_ARGS ( ) ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
int sim ;
2005-10-03 06:14:10 +00:00
zend_uchar str_type ;
2002-09-11 14:40:52 +00:00
if ( ac < 2 | | ac > 3 | | zend_get_parameters_ex ( ac , & t1 , & t2 , & percent ) = = FAILURE ) {
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
WRONG_PARAM_COUNT ;
2001-09-04 06:18:11 +00:00
}
2005-10-03 06:14:10 +00:00
if ( Z_TYPE_PP ( t1 ) ! = IS_UNICODE & & Z_TYPE_PP ( t1 ) ! = IS_BINARY & & Z_TYPE_PP ( t1 ) ! = IS_STRING ) {
convert_to_text_ex ( t1 ) ;
}
if ( Z_TYPE_PP ( t2 ) ! = IS_UNICODE & & Z_TYPE_PP ( t2 ) ! = IS_BINARY & & Z_TYPE_PP ( t2 ) ! = IS_STRING ) {
convert_to_text_ex ( t2 ) ;
}
str_type = zend_get_unified_string_type ( 2 TSRMLS_CC , Z_TYPE_PP ( t1 ) , Z_TYPE_PP ( t2 ) ) ;
if ( str_type = = ( zend_uchar ) - 1 ) {
convert_to_binary_ex ( t1 ) ;
convert_to_binary_ex ( t2 ) ;
} else {
convert_to_explicit_type_ex ( t1 , str_type ) ;
convert_to_explicit_type_ex ( t2 , str_type ) ;
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
if ( ac > 2 ) {
1999-09-25 11:55:42 +00:00
convert_to_double_ex ( percent ) ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
}
2005-10-03 06:14:10 +00:00
if ( Z_UNILEN_PP ( t1 ) + Z_UNILEN_PP ( t2 ) = = 0 ) {
2002-09-11 14:40:52 +00:00
if ( ac > 2 ) {
2001-09-04 06:18:11 +00:00
Z_DVAL_PP ( percent ) = 0 ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
}
2001-09-04 06:18:11 +00:00
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
RETURN_LONG ( 0 ) ;
}
2005-10-03 06:14:10 +00:00
if ( str_type = = IS_UNICODE ) {
sim = php_u_similar_char ( Z_USTRVAL_PP ( t1 ) , Z_USTRLEN_PP ( t1 ) , Z_USTRVAL_PP ( t2 ) , Z_USTRLEN_PP ( t2 ) ) ;
} else {
sim = php_similar_char ( Z_STRVAL_PP ( t1 ) , Z_STRLEN_PP ( t1 ) , Z_STRVAL_PP ( t2 ) , Z_STRLEN_PP ( t2 ) ) ;
}
2002-09-11 14:40:52 +00:00
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
if ( ac > 2 ) {
2005-10-03 06:14:10 +00:00
if ( str_type = = IS_UNICODE ) {
Z_DVAL_PP ( percent ) = sim * 200.0 / ( Z_USTRCPLEN_PP ( t1 ) + Z_USTRCPLEN_PP ( t2 ) ) ;
} else {
Z_DVAL_PP ( percent ) = sim * 200.0 / ( Z_STRLEN_PP ( t1 ) + Z_STRLEN_PP ( t2 ) ) ;
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
}
2001-09-04 06:18:11 +00:00
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
RETURN_LONG ( sim ) ;
}
/* }}} */
1999-04-17 00:37:12 +00:00
2005-09-28 09:22:08 +00:00
/* {{{ php_u_stripslashes
*
* be careful , this edits the string in - place */
PHPAPI void php_u_stripslashes ( UChar * str , int32_t * len TSRMLS_DC )
{
int32_t tmp_len = 0 , i = 0 ;
UChar32 ch1 , ch2 ;
ch1 = - 1 ; ch2 = - 1 ;
if ( PG ( magic_quotes_sybase ) ) {
while ( i < * len ) {
U16_NEXT ( str , i , * len , ch1 ) ;
if ( ch1 = = ' \' ' ) {
tmp_len + = zend_codepoint_to_uchar ( ch1 , str + tmp_len ) ;
if ( i < * len ) {
U16_NEXT ( str , i , * len , ch2 ) ;
if ( ch2 ! = ' \' ' ) {
tmp_len + = zend_codepoint_to_uchar ( ch2 , str + tmp_len ) ;
}
}
} else if ( ch1 = = ' \\ ' ) {
if ( i < * len ) {
U16_NEXT ( str , i , * len , ch2 ) ;
if ( ch2 = = ' 0 ' ) {
tmp_len + = zend_codepoint_to_uchar ( ' \0 ' , str + tmp_len ) ;
} else {
tmp_len + = zend_codepoint_to_uchar ( ch1 , str + tmp_len ) ;
tmp_len + = zend_codepoint_to_uchar ( ch2 , str + tmp_len ) ;
}
} else {
tmp_len + = zend_codepoint_to_uchar ( ch1 , str + tmp_len ) ;
}
} else {
tmp_len + = zend_codepoint_to_uchar ( ch1 , str + tmp_len ) ;
}
}
} else {
while ( i < * len ) {
U16_NEXT ( str , i , * len , ch1 ) ;
if ( ch1 = = ' \\ ' ) {
if ( i < * len ) {
U16_NEXT ( str , i , * len , ch2 ) ;
if ( ch2 = = ' 0 ' ) {
tmp_len + = zend_codepoint_to_uchar ( ' \0 ' , str + tmp_len ) ;
} else {
tmp_len + = zend_codepoint_to_uchar ( ch2 , str + tmp_len ) ;
}
}
} else {
tmp_len + = zend_codepoint_to_uchar ( ch1 , str + tmp_len ) ;
}
}
}
* ( str + tmp_len ) = 0 ;
str = eurealloc ( str , tmp_len + 1 ) ;
* len = tmp_len ;
return ;
}
/* }}} */
2001-06-05 13:12:10 +00:00
/* {{{ php_stripslashes
*
* be careful , this edits the string in - place */
2001-08-06 03:50:52 +00:00
PHPAPI void php_stripslashes ( char * str , int * len TSRMLS_DC )
1999-04-17 00:37:12 +00:00
{
char * s , * t ;
int l ;
if ( len ! = NULL ) {
l = * len ;
} else {
2000-02-08 22:21:43 +00:00
l = strlen ( str ) ;
1999-04-17 00:37:12 +00:00
}
2000-02-08 22:21:43 +00:00
s = str ;
t = str ;
2002-03-27 20:20:57 +00:00
if ( PG ( magic_quotes_sybase ) ) {
2002-05-16 14:41:15 +00:00
while ( l > 0 ) {
2002-10-02 17:56:04 +00:00
if ( * t = = ' \' ' ) {
if ( ( l > 0 ) & & ( t [ 1 ] = = ' \' ' ) ) {
2002-03-27 20:20:57 +00:00
t + + ;
2003-02-08 15:26:17 +00:00
if ( len ! = NULL ) {
2002-03-27 20:20:57 +00:00
( * len ) - - ;
2003-02-08 15:26:17 +00:00
}
2002-03-27 20:20:57 +00:00
l - - ;
}
2002-06-18 11:51:06 +00:00
* s + + = * t + + ;
2003-04-01 12:46:01 +00:00
} else if ( * t = = ' \\ ' & & t [ 1 ] = = ' 0 ' & & l > 0 ) {
* s + + = ' \0 ' ;
t + = 2 ;
2003-03-31 18:56:41 +00:00
if ( len ! = NULL ) {
( * len ) - - ;
}
l - - ;
2002-06-18 11:51:06 +00:00
} else {
* s + + = * t + + ;
}
2002-03-27 20:20:57 +00:00
l - - ;
}
* s = ' \0 ' ;
return ;
}
1999-04-17 00:37:12 +00:00
while ( l > 0 ) {
2002-06-18 11:51:06 +00:00
if ( * t = = ' \\ ' ) {
1999-04-17 00:37:12 +00:00
t + + ; /* skip the slash */
2003-02-08 15:26:17 +00:00
if ( len ! = NULL ) {
1999-04-17 00:37:12 +00:00
( * len ) - - ;
2003-02-08 15:26:17 +00:00
}
1999-04-17 00:37:12 +00:00
l - - ;
if ( l > 0 ) {
2002-10-02 17:56:04 +00:00
if ( * t = = ' 0 ' ) {
1999-04-17 00:37:12 +00:00
* s + + = ' \0 ' ;
t + + ;
} else {
* s + + = * t + + ; /* preserve the next character */
}
l - - ;
}
} else {
2003-03-31 12:08:31 +00:00
* s + + = * t + + ;
1999-04-17 00:37:12 +00:00
l - - ;
}
}
if ( s ! = t ) {
* s = ' \0 ' ;
}
}
2001-06-05 13:12:10 +00:00
/* }}} */
1999-04-17 00:37:12 +00:00
1999-09-05 20:55:13 +00:00
/* {{{ proto string addcslashes(string str, string charlist)
2001-09-04 10:44:28 +00:00
Escapes all chars mentioned in charlist with backslash . It creates octal representations if asked to backslash characters with 8 th bit set or with ASCII < 32 ( except ' \n ' , ' \r ' , ' \t ' etc . . . ) */
1999-09-05 20:55:13 +00:00
PHP_FUNCTION ( addcslashes )
{
1999-12-23 14:59:01 +00:00
zval * * str , * * what ;
1999-09-05 20:55:13 +00:00
2002-09-11 14:40:52 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 2 | | zend_get_parameters_ex ( 2 , & str , & what ) = = FAILURE ) {
1999-09-05 20:55:13 +00:00
WRONG_PARAM_COUNT ;
}
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( str ) ;
convert_to_string_ex ( what ) ;
2000-09-17 09:15:48 +00:00
2001-08-06 03:50:52 +00:00
if ( Z_STRLEN_PP ( str ) = = 0 ) {
2000-09-17 09:15:48 +00:00
RETURN_EMPTY_STRING ( ) ;
}
2001-08-06 03:50:52 +00:00
if ( Z_STRLEN_PP ( what ) = = 0 ) {
RETURN_STRINGL ( Z_STRVAL_PP ( str ) , Z_STRLEN_PP ( str ) , 1 ) ;
2000-09-17 09:15:48 +00:00
}
2001-09-04 06:18:11 +00:00
RETURN_STRING ( php_addcslashes ( Z_STRVAL_PP ( str ) ,
Z_STRLEN_PP ( str ) ,
& Z_STRLEN_P ( return_value ) , 0 ,
Z_STRVAL_PP ( what ) ,
2002-09-11 14:40:52 +00:00
Z_STRLEN_PP ( what ) TSRMLS_CC ) , 0 ) ;
1999-09-05 20:55:13 +00:00
}
/* }}} */
1999-04-17 00:37:12 +00:00
/* {{{ proto string addslashes(string str)
2001-09-04 10:44:28 +00:00
Escapes single quote , double quotes and backslash characters in a string with backslashes */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( addslashes )
1999-04-17 00:37:12 +00:00
{
1999-12-23 14:59:01 +00:00
zval * * str ;
2005-09-28 09:22:08 +00:00
void * tmp = NULL ;
int32_t tmp_len = 0 ;
1999-04-17 00:37:12 +00:00
2002-09-11 14:40:52 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( 1 , & str ) = = FAILURE ) {
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
}
2005-09-28 09:22:08 +00:00
convert_to_text_ex ( str ) ;
2000-09-17 09:15:48 +00:00
2005-09-28 09:22:08 +00:00
if ( Z_TYPE_PP ( str ) = = IS_UNICODE & & Z_USTRLEN_PP ( str ) = = 0 ) {
RETURN_EMPTY_UNICODE ( ) ;
} else if ( Z_TYPE_PP ( str ) = = IS_BINARY & & Z_BINLEN_PP ( str ) = = 0 ) {
RETURN_EMPTY_BINARY ( ) ;
} else if ( Z_TYPE_PP ( str ) = = IS_STRING & & Z_STRLEN_PP ( str ) = = 0 ) {
2000-09-17 09:15:48 +00:00
RETURN_EMPTY_STRING ( ) ;
}
2005-09-28 09:22:08 +00:00
if ( Z_TYPE_PP ( str ) = = IS_UNICODE ) {
tmp = ( UChar * ) php_u_addslashes ( Z_USTRVAL_PP ( str ) , Z_USTRLEN_PP ( str ) ,
& tmp_len , 0 TSRMLS_CC ) ;
RETURN_UNICODEL ( ( UChar * ) tmp , tmp_len , 0 ) ;
} else {
tmp = ( char * ) php_addslashes ( Z_STRVAL_PP ( str ) , Z_STRLEN_PP ( str ) ,
& tmp_len , 0 TSRMLS_CC ) ;
if ( Z_TYPE_PP ( str ) = = IS_BINARY ) {
RETURN_BINARYL ( ( char * ) tmp , tmp_len , 0 ) ;
} else {
RETURN_STRINGL ( ( char * ) tmp , tmp_len , 0 ) ;
}
}
1999-04-17 00:37:12 +00:00
}
/* }}} */
1999-09-05 20:55:13 +00:00
/* {{{ proto string stripcslashes(string str)
2001-09-04 10:44:28 +00:00
Strips backslashes from a string . Uses C - style conventions */
1999-09-05 20:55:13 +00:00
PHP_FUNCTION ( stripcslashes )
{
1999-12-23 14:59:01 +00:00
zval * * str ;
1999-09-05 20:55:13 +00:00
2000-06-05 19:47:54 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( 1 , & str ) = = FAILURE ) {
1999-09-05 20:55:13 +00:00
WRONG_PARAM_COUNT ;
}
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( str ) ;
1999-09-05 20:55:13 +00:00
2001-09-04 06:18:11 +00:00
ZVAL_STRINGL ( return_value , Z_STRVAL_PP ( str ) , Z_STRLEN_PP ( str ) , 1 ) ;
php_stripcslashes ( Z_STRVAL_P ( return_value ) , & Z_STRLEN_P ( return_value ) ) ;
1999-09-05 20:55:13 +00:00
}
/* }}} */
1999-04-17 00:37:12 +00:00
/* {{{ proto string stripslashes(string str)
2001-09-04 10:44:28 +00:00
Strips backslashes from a string */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( stripslashes )
1999-04-17 00:37:12 +00:00
{
1999-12-23 14:59:01 +00:00
zval * * str ;
1999-04-17 00:37:12 +00:00
2000-06-05 19:47:54 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( 1 , & str ) = = FAILURE ) {
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
}
2005-09-28 09:22:08 +00:00
convert_to_text_ex ( str ) ;
1999-04-17 00:37:12 +00:00
2005-09-28 09:22:08 +00:00
if ( Z_TYPE_PP ( str ) = = IS_UNICODE ) {
ZVAL_UNICODEL ( return_value , Z_USTRVAL_PP ( str ) , Z_USTRLEN_PP ( str ) , 1 ) ;
php_u_stripslashes ( Z_USTRVAL_P ( return_value ) , & Z_USTRLEN_P ( return_value ) TSRMLS_CC ) ;
} else {
if ( Z_TYPE_PP ( str ) = = IS_BINARY ) {
ZVAL_BINARYL ( return_value , Z_BINVAL_PP ( str ) , Z_BINLEN_PP ( str ) , 1 ) ;
} else {
ZVAL_STRINGL ( return_value , Z_STRVAL_PP ( str ) , Z_STRLEN_PP ( str ) , 1 ) ;
}
php_stripslashes ( Z_STRVAL_P ( return_value ) , & Z_STRLEN_P ( return_value ) TSRMLS_CC ) ;
}
1999-04-17 00:37:12 +00:00
}
/* }}} */
# ifndef HAVE_STRERROR
2001-06-05 13:12:10 +00:00
/* {{{ php_strerror
*/
2000-02-10 19:41:21 +00:00
char * php_strerror ( int errnum )
1999-04-17 00:37:12 +00:00
{
extern int sys_nerr ;
extern char * sys_errlist [ ] ;
2001-07-28 11:36:37 +00:00
TSRMLS_FETCH ( ) ;
1999-04-17 00:37:12 +00:00
2003-02-08 15:26:17 +00:00
if ( ( unsigned int ) errnum < sys_nerr ) {
return ( sys_errlist [ errnum ] ) ;
}
( void ) sprintf ( BG ( str_ebuf ) , " Unknown error: %d " , errnum ) ;
1999-11-28 00:31:02 +00:00
return ( BG ( str_ebuf ) ) ;
1999-04-17 00:37:12 +00:00
}
2001-06-05 13:12:10 +00:00
/* }}} */
1999-04-17 00:37:12 +00:00
# endif
2001-06-05 13:12:10 +00:00
/* {{{ php_stripcslashes
*/
1999-09-05 20:55:13 +00:00
PHPAPI void php_stripcslashes ( char * str , int * len )
{
2001-08-11 17:03:37 +00:00
char * source , * target , * end ;
1999-09-05 20:55:13 +00:00
int nlen = * len , i ;
char numtmp [ 4 ] ;
2002-10-02 17:56:04 +00:00
for ( source = str , end = str + nlen , target = str ; source < end ; source + + ) {
if ( * source = = ' \\ ' & & source + 1 < end ) {
1999-09-05 20:55:13 +00:00
source + + ;
switch ( * source ) {
2003-02-08 15:26:17 +00:00
case ' n ' : * target + + = ' \n ' ; nlen - - ; break ;
case ' r ' : * target + + = ' \r ' ; nlen - - ; break ;
case ' a ' : * target + + = ' \a ' ; nlen - - ; break ;
case ' t ' : * target + + = ' \t ' ; nlen - - ; break ;
case ' v ' : * target + + = ' \v ' ; nlen - - ; break ;
case ' b ' : * target + + = ' \b ' ; nlen - - ; break ;
case ' f ' : * target + + = ' \f ' ; nlen - - ; break ;
1999-09-05 20:55:13 +00:00
case ' \\ ' : * target + + = ' \\ ' ; nlen - - ; break ;
2003-02-08 15:26:17 +00:00
case ' x ' :
if ( source + 1 < end & & isxdigit ( ( int ) ( * ( source + 1 ) ) ) ) {
1999-09-05 20:55:13 +00:00
numtmp [ 0 ] = * + + source ;
2002-10-02 17:56:04 +00:00
if ( source + 1 < end & & isxdigit ( ( int ) ( * ( source + 1 ) ) ) ) {
1999-09-05 20:55:13 +00:00
numtmp [ 1 ] = * + + source ;
numtmp [ 2 ] = ' \0 ' ;
nlen - = 3 ;
} else {
numtmp [ 1 ] = ' \0 ' ;
nlen - = 2 ;
}
1999-09-07 20:29:31 +00:00
* target + + = ( char ) strtol ( numtmp , NULL , 16 ) ;
1999-09-05 20:55:13 +00:00
break ;
}
/* break is left intentionally */
2003-02-08 15:26:17 +00:00
default :
i = 0 ;
2002-10-02 17:56:04 +00:00
while ( source < end & & * source > = ' 0 ' & & * source < = ' 7 ' & & i < 3 ) {
1999-09-05 20:55:13 +00:00
numtmp [ i + + ] = * source + + ;
}
if ( i ) {
numtmp [ i ] = ' \0 ' ;
1999-09-07 20:29:31 +00:00
* target + + = ( char ) strtol ( numtmp , NULL , 8 ) ;
1999-09-05 20:55:13 +00:00
nlen - = i ;
source - - ;
} else {
* target + + = * source ;
2000-06-09 13:58:10 +00:00
nlen - - ;
1999-09-05 20:55:13 +00:00
}
}
} else {
* target + + = * source ;
}
}
2000-07-17 10:22:30 +00:00
2002-10-02 17:56:04 +00:00
if ( nlen ! = 0 ) {
2000-07-17 10:22:30 +00:00
* target = ' \0 ' ;
}
1999-09-05 20:55:13 +00:00
* len = nlen ;
}
2001-06-05 13:12:10 +00:00
/* }}} */
1999-09-05 20:55:13 +00:00
2001-06-05 13:12:10 +00:00
/* {{{ php_addcslashes
*/
2001-08-06 03:50:52 +00:00
PHPAPI char * php_addcslashes ( char * str , int length , int * new_length , int should_free , char * what , int wlength TSRMLS_DC )
1999-09-05 00:55:48 +00:00
{
char flags [ 256 ] ;
2003-08-11 23:16:54 +00:00
char * new_str = safe_emalloc ( 4 , ( length ? length : ( length = strlen ( str ) ) ) , 1 ) ;
2001-08-11 17:03:37 +00:00
char * source , * target ;
1999-09-05 00:55:48 +00:00
char * end ;
char c ;
1999-09-05 20:55:13 +00:00
int newlen ;
1999-09-05 00:55:48 +00:00
if ( ! wlength ) {
wlength = strlen ( what ) ;
}
if ( ! length ) {
length = strlen ( str ) ;
}
2001-08-05 23:08:19 +00:00
php_charmask ( what , wlength , flags TSRMLS_CC ) ;
1999-09-05 00:55:48 +00:00
2003-02-08 15:26:17 +00:00
for ( source = str , end = source + length , target = new_str ; ( c = * source ) | | ( source < end ) ; source + + ) {
1999-09-05 20:55:13 +00:00
if ( flags [ ( unsigned char ) c ] ) {
2003-02-08 15:26:17 +00:00
if ( ( unsigned char ) c < 32 | | ( unsigned char ) c > 126 ) {
1999-09-05 00:55:48 +00:00
* target + + = ' \\ ' ;
switch ( c ) {
case ' \n ' : * target + + = ' n ' ; break ;
case ' \t ' : * target + + = ' t ' ; break ;
case ' \r ' : * target + + = ' r ' ; break ;
1999-09-05 20:55:13 +00:00
case ' \a ' : * target + + = ' a ' ; break ;
case ' \v ' : * target + + = ' v ' ; break ;
case ' \b ' : * target + + = ' b ' ; break ;
case ' \f ' : * target + + = ' f ' ; break ;
2003-02-08 15:26:17 +00:00
default : target + = sprintf ( target , " %03o " , ( unsigned char ) c ) ;
1999-09-05 00:55:48 +00:00
}
1999-09-05 20:55:13 +00:00
continue ;
}
* target + + = ' \\ ' ;
}
* target + + = c ;
1999-09-05 00:55:48 +00:00
}
* target = 0 ;
2003-02-08 15:26:17 +00:00
newlen = target - new_str ;
if ( target - new_str < length * 4 ) {
new_str = erealloc ( new_str , newlen + 1 ) ;
1999-09-05 20:55:13 +00:00
}
1999-09-05 00:55:48 +00:00
if ( new_length ) {
1999-09-05 20:55:13 +00:00
* new_length = newlen ;
1999-09-05 00:55:48 +00:00
}
if ( should_free ) {
STR_FREE ( str ) ;
}
return new_str ;
}
2001-06-05 13:12:10 +00:00
/* }}} */
1999-04-17 00:37:12 +00:00
2005-09-28 09:22:08 +00:00
/* {{{ php_u_addslashes
*/
PHPAPI UChar * php_u_addslashes ( UChar * str , int32_t length , int32_t * new_length , int should_free TSRMLS_DC )
{
return php_u_addslashes_ex ( str , length , new_length , should_free , 0 TSRMLS_CC ) ;
}
2005-10-15 12:50:20 +00:00
/* }}} */
2005-09-28 09:22:08 +00:00
/* {{{ php_u_addslashes_ex
*/
PHPAPI UChar * php_u_addslashes_ex ( UChar * str , int32_t length , int32_t * new_length , int should_free , int ignore_sybase TSRMLS_DC )
{
UChar * buf ;
int32_t buf_len = 0 , i = 0 ;
UChar32 ch ;
if ( ! new_length ) {
new_length = & buf_len ;
}
if ( ! str ) {
* new_length = 0 ;
return str ;
}
buf = eumalloc ( length * 2 ) ;
if ( ! ignore_sybase & & PG ( magic_quotes_sybase ) ) {
while ( i < length ) {
U16_NEXT ( str , i , length , ch ) ;
switch ( ch ) {
case ' \0 ' :
2005-09-29 09:33:38 +00:00
* ( buf + buf_len ) = ( UChar ) 0x5C ; buf_len + + ; /* \ */
* ( buf + buf_len ) = ( UChar ) 0x30 ; buf_len + + ; /* 0 */
2005-09-28 09:22:08 +00:00
break ;
case ' \' ' :
2005-09-29 09:33:38 +00:00
* ( buf + buf_len ) = ( UChar ) 0x27 ; buf_len + + ; /* ' */
* ( buf + buf_len ) = ( UChar ) 0x27 ; buf_len + + ; /* ' */
2005-09-28 09:22:08 +00:00
break ;
default :
buf_len + = zend_codepoint_to_uchar ( ch , buf + buf_len ) ;
break ;
}
}
} else {
while ( i < length ) {
U16_NEXT ( str , i , length , ch ) ;
switch ( ch ) {
case ' \0 ' :
2005-09-29 09:33:38 +00:00
* ( buf + buf_len ) = ( UChar ) 0x5C ; buf_len + + ; /* \ */
* ( buf + buf_len ) = ( UChar ) 0x30 ; buf_len + + ; /* 0 */
2005-09-28 09:22:08 +00:00
break ;
case ' \' ' :
case ' \" ' :
case ' \\ ' :
2005-09-29 09:33:38 +00:00
* ( buf + buf_len ) = ( UChar ) 0x5C ; buf_len + + ; /* \ */
2005-09-28 09:22:08 +00:00
/* break is missing *intentionally* */
default :
buf_len + = zend_codepoint_to_uchar ( ch , buf + buf_len ) ;
break ;
}
}
}
* ( buf + buf_len ) = 0 ;
if ( should_free ) {
STR_FREE ( str ) ;
}
buf = eurealloc ( buf , buf_len + 1 ) ;
* new_length = buf_len ;
return buf ;
}
/* }}} */
2001-06-05 13:12:10 +00:00
/* {{{ php_addslashes
*/
2001-08-06 03:50:52 +00:00
PHPAPI char * php_addslashes ( char * str , int length , int * new_length , int should_free TSRMLS_DC )
2005-05-31 12:54:56 +00:00
{
return php_addslashes_ex ( str , length , new_length , should_free , 0 TSRMLS_CC ) ;
}
/* }}} */
/* {{{ php_addslashes_ex
*/
PHPAPI char * php_addslashes_ex ( char * str , int length , int * new_length , int should_free , int ignore_sybase TSRMLS_DC )
1999-04-17 00:37:12 +00:00
{
/* maximum string length, worst case situation */
2000-02-02 21:53:27 +00:00
char * new_str ;
2001-08-11 17:03:37 +00:00
char * source , * target ;
1999-04-17 00:37:12 +00:00
char * end ;
2002-10-22 18:27:56 +00:00
int local_new_length ;
if ( ! new_length ) {
new_length = & local_new_length ;
}
2000-02-02 21:53:27 +00:00
if ( ! str ) {
* new_length = 0 ;
return str ;
}
2003-08-11 23:16:54 +00:00
new_str = ( char * ) safe_emalloc ( 2 , ( length ? length : ( length = strlen ( str ) ) ) , 1 ) ;
2002-10-09 13:37:02 +00:00
source = str ;
end = source + length ;
target = new_str ;
2005-05-31 12:54:56 +00:00
if ( ! ignore_sybase & & PG ( magic_quotes_sybase ) ) {
2002-10-09 13:50:06 +00:00
while ( source < end ) {
2002-10-09 13:37:02 +00:00
switch ( * source ) {
2002-03-12 07:05:40 +00:00
case ' \0 ' :
* target + + = ' \\ ' ;
* target + + = ' 0 ' ;
break ;
case ' \' ' :
1999-04-17 00:37:12 +00:00
* target + + = ' \' ' ;
* target + + = ' \' ' ;
break ;
2002-03-12 07:05:40 +00:00
default :
2002-10-09 13:37:02 +00:00
* target + + = * source ;
break ;
2002-03-12 07:05:40 +00:00
}
2002-10-09 13:37:02 +00:00
source + + ;
2002-03-12 07:05:40 +00:00
}
2002-10-09 13:50:06 +00:00
} else {
while ( source < end ) {
switch ( * source ) {
2002-03-12 07:05:40 +00:00
case ' \0 ' :
* target + + = ' \\ ' ;
* target + + = ' 0 ' ;
break ;
case ' \' ' :
case ' \" ' :
case ' \\ ' :
* target + + = ' \\ ' ;
/* break is missing *intentionally* */
default :
2002-10-09 13:37:02 +00:00
* target + + = * source ;
break ;
2002-03-12 07:05:40 +00:00
}
2002-10-09 13:37:02 +00:00
source + + ;
1999-04-17 00:37:12 +00:00
}
}
2002-10-09 13:37:02 +00:00
1999-04-17 00:37:12 +00:00
* target = 0 ;
2002-10-22 18:27:56 +00:00
* new_length = target - new_str ;
1999-04-17 00:37:12 +00:00
if ( should_free ) {
STR_FREE ( str ) ;
}
2003-02-08 15:26:17 +00:00
new_str = ( char * ) erealloc ( new_str , * new_length + 1 ) ;
1999-04-17 00:37:12 +00:00
return new_str ;
}
2001-06-05 13:12:10 +00:00
/* }}} */
1999-04-17 00:37:12 +00:00
# define _HEB_BLOCK_TYPE_ENG 1
# define _HEB_BLOCK_TYPE_HEB 2
2003-02-08 15:26:17 +00:00
# define isheb(c) (((((unsigned char) c) >= 224) && (((unsigned char) c) <= 250)) ? 1 : 0)
# define _isblank(c) (((((unsigned char) c) == ' ' || ((unsigned char) c) == '\t')) ? 1 : 0)
# define _isnewline(c) (((((unsigned char) c) == '\n' || ((unsigned char) c) == '\r')) ? 1 : 0)
1999-04-17 00:37:12 +00:00
2003-07-25 01:03:39 +00:00
/* {{{ php_char_to_str_ex
2001-06-05 13:12:10 +00:00
*/
2003-07-25 01:03:39 +00:00
PHPAPI int php_char_to_str_ex ( char * str , uint len , char from , char * to , int to_len , zval * result , int case_sensitivity , int * replace_count )
1999-04-17 00:37:12 +00:00
{
2001-09-09 12:55:48 +00:00
int char_count = 0 ;
int replaced = 0 ;
char * source , * target , * tmp , * source_end = str + len , * tmp_end = NULL ;
1999-04-17 00:37:12 +00:00
2002-10-02 17:56:04 +00:00
for ( source = str ; source < source_end ; source + + ) {
2005-05-20 14:23:42 +00:00
if ( ( case_sensitivity & & * source = = from ) | | ( ! case_sensitivity & & tolower ( * source ) = = tolower ( from ) ) ) {
1999-04-17 00:37:12 +00:00
char_count + + ;
}
}
2004-10-07 23:10:35 +00:00
if ( char_count = = 0 & & case_sensitivity ) {
2001-09-04 06:18:11 +00:00
ZVAL_STRINGL ( result , str , len , 1 ) ;
2001-09-09 12:55:48 +00:00
return 0 ;
1999-04-17 00:37:12 +00:00
}
2001-09-04 06:18:11 +00:00
Z_STRLEN_P ( result ) = len + ( char_count * ( to_len - 1 ) ) ;
Z_STRVAL_P ( result ) = target = emalloc ( Z_STRLEN_P ( result ) + 1 ) ;
Z_TYPE_P ( result ) = IS_STRING ;
1999-04-17 00:37:12 +00:00
2001-09-04 06:18:11 +00:00
for ( source = str ; source < source_end ; source + + ) {
2003-07-25 01:03:39 +00:00
if ( ( case_sensitivity & & * source = = from ) | | ( ! case_sensitivity & & tolower ( * source ) = = tolower ( from ) ) ) {
2001-09-09 12:55:48 +00:00
replaced = 1 ;
2003-07-25 01:03:39 +00:00
if ( replace_count ) {
* replace_count + = 1 ;
}
2001-09-04 06:18:11 +00:00
for ( tmp = to , tmp_end = tmp + to_len ; tmp < tmp_end ; tmp + + ) {
1999-04-17 00:37:12 +00:00
* target = * tmp ;
target + + ;
}
} else {
* target = * source ;
target + + ;
}
}
* target = 0 ;
2001-09-09 12:55:48 +00:00
return replaced ;
1999-04-17 00:37:12 +00:00
}
2001-06-05 13:12:10 +00:00
/* }}} */
1999-04-17 00:37:12 +00:00
2003-07-25 01:03:39 +00:00
/* {{{ php_char_to_str
*/
PHPAPI int php_char_to_str ( char * str , uint len , char from , char * to , int to_len , zval * result )
{
return php_char_to_str_ex ( str , len , from , to , to_len , result , 1 , NULL ) ;
}
/* }}} */
2003-01-30 05:00:41 +00:00
/* {{{ php_str_to_str_ex
*/
PHPAPI char * php_str_to_str_ex ( char * haystack , int length ,
2003-04-13 22:59:19 +00:00
char * needle , int needle_len , char * str , int str_len , int * _new_length , int case_sensitivity , int * replace_count )
2003-01-30 05:00:41 +00:00
{
char * new_str ;
2001-07-20 01:22:29 +00:00
2003-01-30 05:00:41 +00:00
if ( needle_len < length ) {
2004-02-23 20:34:59 +00:00
char * end , * haystack_dup = NULL , * needle_dup = NULL ;
2003-01-30 05:00:41 +00:00
char * e , * s , * p , * r ;
2001-07-20 01:22:29 +00:00
2003-01-30 05:00:41 +00:00
if ( needle_len = = str_len ) {
new_str = estrndup ( haystack , length ) ;
* _new_length = length ;
2001-07-20 01:22:29 +00:00
2003-01-30 05:00:41 +00:00
if ( case_sensitivity ) {
end = new_str + length ;
for ( p = new_str ; ( r = php_memnstr ( p , needle , needle_len , end ) ) ; p = r + needle_len ) {
memcpy ( r , str , str_len ) ;
2003-04-13 22:59:19 +00:00
if ( replace_count ) {
( * replace_count ) + + ;
}
2003-01-30 05:00:41 +00:00
}
} else {
haystack_dup = estrndup ( haystack , length ) ;
needle_dup = estrndup ( needle , needle_len ) ;
php_strtolower ( haystack_dup , length ) ;
php_strtolower ( needle_dup , needle_len ) ;
end = haystack_dup + length ;
for ( p = haystack_dup ; ( r = php_memnstr ( p , needle_dup , needle_len , end ) ) ; p = r + needle_len ) {
memcpy ( new_str + ( r - haystack_dup ) , str , str_len ) ;
2003-04-13 22:59:19 +00:00
if ( replace_count ) {
( * replace_count ) + + ;
}
2003-01-30 05:00:41 +00:00
}
efree ( haystack_dup ) ;
efree ( needle_dup ) ;
}
return new_str ;
} else {
2004-03-26 19:23:42 +00:00
if ( ! case_sensitivity ) {
haystack_dup = estrndup ( haystack , length ) ;
needle_dup = estrndup ( needle , needle_len ) ;
php_strtolower ( haystack_dup , length ) ;
php_strtolower ( needle_dup , needle_len ) ;
}
2003-01-30 05:00:41 +00:00
if ( str_len < needle_len ) {
new_str = emalloc ( length + 1 ) ;
} else {
2004-02-23 20:06:01 +00:00
int count = 0 ;
2004-02-23 20:34:59 +00:00
char * o , * n , * endp ;
2004-02-23 20:06:01 +00:00
2004-02-23 20:34:59 +00:00
if ( case_sensitivity ) {
o = haystack ;
n = needle ;
} else {
o = haystack_dup ;
n = needle_dup ;
}
endp = o + length ;
while ( ( o = php_memnstr ( o , n , needle_len , endp ) ) ) {
2004-02-23 20:06:01 +00:00
o + = needle_len ;
count + + ;
}
2004-02-23 20:13:14 +00:00
if ( count = = 0 ) {
/* Needle doesn't occur, shortcircuit the actual replacement. */
2004-02-23 20:34:59 +00:00
if ( haystack_dup ) {
efree ( haystack_dup ) ;
}
if ( needle_dup ) {
efree ( needle_dup ) ;
}
2004-02-23 20:13:14 +00:00
new_str = estrndup ( haystack , length ) ;
if ( _new_length ) {
* _new_length = length ;
}
return new_str ;
} else {
new_str = safe_emalloc ( count , str_len - needle_len , length + 1 ) ;
}
2001-07-20 01:22:29 +00:00
}
2003-01-30 05:00:41 +00:00
e = s = new_str ;
2001-07-20 01:22:29 +00:00
2003-01-30 05:00:41 +00:00
if ( case_sensitivity ) {
end = haystack + length ;
for ( p = haystack ; ( r = php_memnstr ( p , needle , needle_len , end ) ) ; p = r + needle_len ) {
memcpy ( e , p , r - p ) ;
e + = r - p ;
memcpy ( e , str , str_len ) ;
e + = str_len ;
2003-04-13 22:59:19 +00:00
if ( replace_count ) {
( * replace_count ) + + ;
}
2003-01-30 05:00:41 +00:00
}
2001-07-20 01:22:29 +00:00
2003-01-30 05:00:41 +00:00
if ( p < end ) {
memcpy ( e , p , end - p ) ;
e + = end - p ;
}
} else {
end = haystack_dup + length ;
for ( p = haystack_dup ; ( r = php_memnstr ( p , needle_dup , needle_len , end ) ) ; p = r + needle_len ) {
memcpy ( e , haystack + ( p - haystack_dup ) , r - p ) ;
e + = r - p ;
memcpy ( e , str , str_len ) ;
e + = str_len ;
2003-04-13 22:59:19 +00:00
if ( replace_count ) {
( * replace_count ) + + ;
}
2003-01-30 05:00:41 +00:00
}
2001-07-20 01:22:29 +00:00
2003-01-30 05:00:41 +00:00
if ( p < end ) {
memcpy ( e , haystack + ( p - haystack_dup ) , end - p ) ;
e + = end - p ;
}
2004-02-23 20:34:59 +00:00
}
if ( haystack_dup ) {
2003-01-30 05:00:41 +00:00
efree ( haystack_dup ) ;
2004-02-23 20:34:59 +00:00
}
if ( needle_dup ) {
2003-01-30 05:00:41 +00:00
efree ( needle_dup ) ;
}
* e = ' \0 ' ;
* _new_length = e - s ;
new_str = erealloc ( new_str , * _new_length + 1 ) ;
return new_str ;
}
} else if ( needle_len > length ) {
nothing_todo :
* _new_length = length ;
new_str = estrndup ( haystack , length ) ;
return new_str ;
} else {
if ( case_sensitivity ? strncmp ( haystack , needle , length ) : strncasecmp ( haystack , needle , length ) ) {
goto nothing_todo ;
} else {
* _new_length = str_len ;
new_str = estrndup ( str , str_len ) ;
2003-04-13 22:59:19 +00:00
if ( replace_count ) {
( * replace_count ) + + ;
}
2003-01-30 05:00:41 +00:00
return new_str ;
}
}
2001-07-20 01:22:29 +00:00
}
/* }}} */
2001-06-05 13:12:10 +00:00
/* {{{ php_str_to_str
*/
1999-10-15 20:04:31 +00:00
PHPAPI char * php_str_to_str ( char * haystack , int length ,
1999-04-17 00:37:12 +00:00
char * needle , int needle_len , char * str , int str_len , int * _new_length )
{
2003-04-13 22:59:19 +00:00
return php_str_to_str_ex ( haystack , length , needle , needle_len , str , str_len , _new_length , 1 , NULL ) ;
2003-01-30 05:00:41 +00:00
}
/* }}}
*/
1999-04-17 00:37:12 +00:00
2001-06-05 13:12:10 +00:00
/* {{{ php_str_replace_in_subject
*/
2003-04-13 22:59:19 +00:00
static void php_str_replace_in_subject ( zval * search , zval * replace , zval * * subject , zval * result , int case_sensitivity , int * replace_count )
2001-01-22 21:39:55 +00:00
{
zval * * search_entry ,
* * replace_entry = NULL ,
temp_result ;
char * replace_value = NULL ;
int replace_len = 0 ;
/* Make sure we're dealing with strings. */
convert_to_string_ex ( subject ) ;
Z_TYPE_P ( result ) = IS_STRING ;
if ( Z_STRLEN_PP ( subject ) = = 0 ) {
2004-07-19 07:19:50 +00:00
ZVAL_STRINGL ( result , " " , 0 , 1 ) ;
2001-01-22 21:39:55 +00:00
return ;
1999-04-17 00:37:12 +00:00
}
2001-01-22 21:39:55 +00:00
/* If search is an array */
if ( Z_TYPE_P ( search ) = = IS_ARRAY ) {
/* Duplicate subject string for repeated replacement */
* result = * * subject ;
zval_copy_ctor ( result ) ;
2001-11-10 23:44:04 +00:00
INIT_PZVAL ( result ) ;
2001-01-22 21:39:55 +00:00
zend_hash_internal_pointer_reset ( Z_ARRVAL_P ( search ) ) ;
1999-04-17 00:37:12 +00:00
2001-01-22 21:39:55 +00:00
if ( Z_TYPE_P ( replace ) = = IS_ARRAY ) {
zend_hash_internal_pointer_reset ( Z_ARRVAL_P ( replace ) ) ;
} else {
/* Set replacement value to the passed one */
replace_value = Z_STRVAL_P ( replace ) ;
replace_len = Z_STRLEN_P ( replace ) ;
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
2001-01-22 21:39:55 +00:00
/* For each entry in the search array, get the entry */
2001-09-04 06:18:11 +00:00
while ( zend_hash_get_current_data ( Z_ARRVAL_P ( search ) , ( void * * ) & search_entry ) = = SUCCESS ) {
2001-01-22 21:39:55 +00:00
/* Make sure we're dealing with strings. */
2003-02-14 18:59:50 +00:00
SEPARATE_ZVAL ( search_entry ) ;
convert_to_string ( * search_entry ) ;
2002-10-02 17:56:04 +00:00
if ( Z_STRLEN_PP ( search_entry ) = = 0 ) {
2001-01-22 21:39:55 +00:00
zend_hash_move_forward ( Z_ARRVAL_P ( search ) ) ;
2003-06-20 15:41:43 +00:00
if ( Z_TYPE_P ( replace ) = = IS_ARRAY ) {
zend_hash_move_forward ( Z_ARRVAL_P ( replace ) ) ;
}
2001-01-22 21:39:55 +00:00
continue ;
}
2001-09-04 06:18:11 +00:00
2001-01-22 21:39:55 +00:00
/* If replace is an array. */
if ( Z_TYPE_P ( replace ) = = IS_ARRAY ) {
/* Get current entry */
if ( zend_hash_get_current_data ( Z_ARRVAL_P ( replace ) , ( void * * ) & replace_entry ) = = SUCCESS ) {
/* Make sure we're dealing with strings. */
convert_to_string_ex ( replace_entry ) ;
/* Set replacement value to the one we got from array */
replace_value = Z_STRVAL_PP ( replace_entry ) ;
replace_len = Z_STRLEN_PP ( replace_entry ) ;
zend_hash_move_forward ( Z_ARRVAL_P ( replace ) ) ;
} else {
/* We've run out of replacement strings, so use an empty one. */
2004-07-19 07:19:50 +00:00
replace_value = " " ;
2001-01-22 21:39:55 +00:00
replace_len = 0 ;
}
}
2002-10-02 17:56:04 +00:00
if ( Z_STRLEN_PP ( search_entry ) = = 1 ) {
2003-07-25 01:03:39 +00:00
php_char_to_str_ex ( Z_STRVAL_P ( result ) ,
2001-01-22 21:39:55 +00:00
Z_STRLEN_P ( result ) ,
Z_STRVAL_PP ( search_entry ) [ 0 ] ,
replace_value ,
replace_len ,
2003-07-25 01:03:39 +00:00
& temp_result ,
case_sensitivity ,
replace_count ) ;
2001-01-22 21:39:55 +00:00
} else if ( Z_STRLEN_PP ( search_entry ) > 1 ) {
2003-01-30 05:00:41 +00:00
Z_STRVAL ( temp_result ) = php_str_to_str_ex ( Z_STRVAL_P ( result ) , Z_STRLEN_P ( result ) ,
Z_STRVAL_PP ( search_entry ) , Z_STRLEN_PP ( search_entry ) ,
2003-04-13 22:59:19 +00:00
replace_value , replace_len , & Z_STRLEN ( temp_result ) , case_sensitivity , replace_count ) ;
2001-01-22 21:39:55 +00:00
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
2001-01-22 21:39:55 +00:00
efree ( Z_STRVAL_P ( result ) ) ;
Z_STRVAL_P ( result ) = Z_STRVAL ( temp_result ) ;
Z_STRLEN_P ( result ) = Z_STRLEN ( temp_result ) ;
2001-11-10 23:44:04 +00:00
if ( Z_STRLEN_P ( result ) = = 0 ) {
return ;
}
2001-01-22 21:39:55 +00:00
zend_hash_move_forward ( Z_ARRVAL_P ( search ) ) ;
}
} else {
if ( Z_STRLEN_P ( search ) = = 1 ) {
2003-07-25 01:03:39 +00:00
php_char_to_str_ex ( Z_STRVAL_PP ( subject ) ,
2001-01-22 21:39:55 +00:00
Z_STRLEN_PP ( subject ) ,
Z_STRVAL_P ( search ) [ 0 ] ,
Z_STRVAL_P ( replace ) ,
Z_STRLEN_P ( replace ) ,
2003-07-25 01:03:39 +00:00
result ,
case_sensitivity ,
replace_count ) ;
2001-01-22 21:39:55 +00:00
} else if ( Z_STRLEN_P ( search ) > 1 ) {
2003-01-30 05:00:41 +00:00
Z_STRVAL_P ( result ) = php_str_to_str_ex ( Z_STRVAL_PP ( subject ) , Z_STRLEN_PP ( subject ) ,
Z_STRVAL_P ( search ) , Z_STRLEN_P ( search ) ,
2003-04-13 22:59:19 +00:00
Z_STRVAL_P ( replace ) , Z_STRLEN_P ( replace ) , & Z_STRLEN_P ( result ) , case_sensitivity , replace_count ) ;
2001-01-22 21:39:55 +00:00
} else {
* result = * * subject ;
zval_copy_ctor ( result ) ;
2001-11-10 23:44:04 +00:00
INIT_PZVAL ( result ) ;
2001-01-22 21:39:55 +00:00
}
1999-04-17 00:37:12 +00:00
}
2001-01-22 21:39:55 +00:00
}
2001-06-05 13:12:10 +00:00
/* }}} */
2001-01-22 21:39:55 +00:00
2003-01-30 20:09:19 +00:00
/* {{{ php_str_replace_common
*/
static void php_str_replace_common ( INTERNAL_FUNCTION_PARAMETERS , int case_sensitivity )
2001-01-22 21:39:55 +00:00
{
2003-04-13 22:59:19 +00:00
zval * * subject , * * search , * * replace , * * subject_entry , * * zcount ;
2001-01-22 21:39:55 +00:00
zval * result ;
char * string_key ;
2001-08-21 12:57:53 +00:00
uint string_key_len ;
ulong num_key ;
2003-04-13 22:59:19 +00:00
int count = 0 ;
int argc = ZEND_NUM_ARGS ( ) ;
2001-01-22 21:39:55 +00:00
2003-04-13 22:59:19 +00:00
if ( argc < 3 | | argc > 4 | |
zend_get_parameters_ex ( argc , & search , & replace , & subject , & zcount ) = = FAILURE ) {
2001-01-22 21:39:55 +00:00
WRONG_PARAM_COUNT ;
1999-05-12 15:40:15 +00:00
}
2003-01-30 05:00:41 +00:00
SEPARATE_ZVAL ( search ) ;
SEPARATE_ZVAL ( replace ) ;
SEPARATE_ZVAL ( subject ) ;
/* Make sure we're dealing with strings and do the replacement. */
if ( Z_TYPE_PP ( search ) ! = IS_ARRAY ) {
convert_to_string_ex ( search ) ;
convert_to_string_ex ( replace ) ;
} else if ( Z_TYPE_PP ( replace ) ! = IS_ARRAY ) {
convert_to_string_ex ( replace ) ;
2001-07-20 01:22:29 +00:00
}
2003-01-30 05:00:41 +00:00
/* if subject is an array */
if ( Z_TYPE_PP ( subject ) = = IS_ARRAY ) {
array_init ( return_value ) ;
zend_hash_internal_pointer_reset ( Z_ARRVAL_PP ( subject ) ) ;
/* For each subject entry, convert it to string, then perform replacement
and add the result to the return_value array . */
while ( zend_hash_get_current_data ( Z_ARRVAL_PP ( subject ) , ( void * * ) & subject_entry ) = = SUCCESS ) {
2003-05-16 23:30:08 +00:00
if ( Z_TYPE_PP ( subject_entry ) ! = IS_ARRAY & & Z_TYPE_PP ( subject_entry ) ! = IS_OBJECT ) {
MAKE_STD_ZVAL ( result ) ;
2003-05-25 21:48:57 +00:00
SEPARATE_ZVAL ( subject_entry ) ;
2003-05-16 23:30:08 +00:00
php_str_replace_in_subject ( * search , * replace , subject_entry , result , case_sensitivity , ( argc > 3 ) ? & count : NULL ) ;
} else {
2003-09-29 02:23:40 +00:00
ALLOC_ZVAL ( result ) ;
ZVAL_ADDREF ( * subject_entry ) ;
COPY_PZVAL_TO_ZVAL ( * result , * subject_entry ) ;
2003-05-16 23:30:08 +00:00
}
2003-01-30 05:00:41 +00:00
/* Add to return array */
switch ( zend_hash_get_current_key_ex ( Z_ARRVAL_PP ( subject ) , & string_key ,
& string_key_len , & num_key , 0 , NULL ) ) {
case HASH_KEY_IS_STRING :
add_assoc_zval_ex ( return_value , string_key , string_key_len , result ) ;
break ;
case HASH_KEY_IS_LONG :
add_index_zval ( return_value , num_key , result ) ;
break ;
}
zend_hash_move_forward ( Z_ARRVAL_PP ( subject ) ) ;
}
} else { /* if subject is not an array */
2003-04-13 22:59:19 +00:00
php_str_replace_in_subject ( * search , * replace , subject , return_value , case_sensitivity , ( argc > 3 ) ? & count : NULL ) ;
2003-01-30 05:00:41 +00:00
}
2003-04-13 22:59:19 +00:00
if ( argc > 3 ) {
2003-06-23 14:09:14 +00:00
zval_dtor ( * zcount ) ;
ZVAL_LONG ( * zcount , count ) ;
2003-04-13 22:59:19 +00:00
}
2003-01-30 05:00:41 +00:00
}
/* }}} */
2003-04-13 22:59:19 +00:00
/* {{{ proto mixed str_replace(mixed search, mixed replace, mixed subject [, int &replace_count])
2003-01-30 20:09:19 +00:00
Replaces all occurrences of search in haystack with replace */
PHP_FUNCTION ( str_replace )
{
php_str_replace_common ( INTERNAL_FUNCTION_PARAM_PASSTHRU , 1 ) ;
}
/* }}} */
2003-04-13 22:59:19 +00:00
/* {{{ proto mixed str_ireplace(mixed search, mixed replace, mixed subject [, int &replace_count])
2003-01-30 05:00:41 +00:00
Replaces all occurrences of search in haystack with replace / case - insensitive */
PHP_FUNCTION ( str_ireplace )
{
2003-01-30 20:09:19 +00:00
php_str_replace_common ( INTERNAL_FUNCTION_PARAM_PASSTHRU , 0 ) ;
1999-04-17 00:37:12 +00:00
}
/* }}} */
2001-06-05 13:12:10 +00:00
/* {{{ php_hebrev
*
* Converts Logical Hebrew text ( Hebrew Windows style ) to Visual text
1999-04-17 00:37:12 +00:00
* Cheers / complaints / flames - Zeev Suraski < zeev @ php . net >
*/
2001-08-11 17:03:37 +00:00
static void php_hebrev ( INTERNAL_FUNCTION_PARAMETERS , int convert_newlines )
1999-04-17 00:37:12 +00:00
{
2001-08-11 17:03:37 +00:00
zval * * str , * * max_chars_per_line ;
2002-05-04 17:38:45 +00:00
char * heb_str , * tmp , * target , * broken_str ;
1999-04-17 00:37:12 +00:00
int block_start , block_end , block_type , block_length , i ;
long max_chars = 0 ;
2001-08-11 17:03:37 +00:00
int begin , end , char_count , orig_begin ;
1999-04-17 00:37:12 +00:00
2002-10-02 17:56:04 +00:00
switch ( ZEND_NUM_ARGS ( ) ) {
1999-04-17 00:37:12 +00:00
case 1 :
2002-10-02 17:56:04 +00:00
if ( zend_get_parameters_ex ( 1 , & str ) = = FAILURE ) {
1999-04-17 00:37:12 +00:00
RETURN_FALSE ;
}
break ;
case 2 :
2002-10-02 17:56:04 +00:00
if ( zend_get_parameters_ex ( 2 , & str , & max_chars_per_line ) = = FAILURE ) {
1999-04-17 00:37:12 +00:00
RETURN_FALSE ;
}
1999-09-25 11:55:42 +00:00
convert_to_long_ex ( max_chars_per_line ) ;
2001-09-25 21:58:48 +00:00
max_chars = Z_LVAL_PP ( max_chars_per_line ) ;
1999-04-17 00:37:12 +00:00
break ;
default :
WRONG_PARAM_COUNT ;
break ;
}
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( str ) ;
1999-04-17 00:37:12 +00:00
2002-10-02 17:56:04 +00:00
if ( Z_STRLEN_PP ( str ) = = 0 ) {
1999-04-17 00:37:12 +00:00
RETURN_FALSE ;
}
2001-09-25 21:58:48 +00:00
tmp = Z_STRVAL_PP ( str ) ;
1999-04-17 00:37:12 +00:00
block_start = block_end = 0 ;
2001-09-25 21:58:48 +00:00
heb_str = ( char * ) emalloc ( Z_STRLEN_PP ( str ) + 1 ) ;
target = heb_str + Z_STRLEN_PP ( str ) ;
1999-04-17 00:37:12 +00:00
* target = 0 ;
target - - ;
block_length = 0 ;
if ( isheb ( * tmp ) ) {
block_type = _HEB_BLOCK_TYPE_HEB ;
} else {
block_type = _HEB_BLOCK_TYPE_ENG ;
}
do {
2002-10-02 17:56:04 +00:00
if ( block_type = = _HEB_BLOCK_TYPE_HEB ) {
2003-01-11 23:05:19 +00:00
while ( ( isheb ( ( int ) * ( tmp + 1 ) ) | | _isblank ( ( int ) * ( tmp + 1 ) ) | | ispunct ( ( int ) * ( tmp + 1 ) ) | | ( int ) * ( tmp + 1 ) = = ' \n ' ) & & block_end < Z_STRLEN_PP ( str ) - 1 ) {
1999-04-17 00:37:12 +00:00
tmp + + ;
block_end + + ;
block_length + + ;
}
2002-10-02 17:56:04 +00:00
for ( i = block_start ; i < = block_end ; i + + ) {
2001-09-25 21:58:48 +00:00
* target = Z_STRVAL_PP ( str ) [ i ] ;
1999-04-17 00:37:12 +00:00
switch ( * target ) {
case ' ( ' :
* target = ' ) ' ;
break ;
case ' ) ' :
* target = ' ( ' ;
break ;
2002-03-26 07:57:07 +00:00
case ' [ ' :
* target = ' ] ' ;
break ;
case ' ] ' :
* target = ' [ ' ;
break ;
case ' { ' :
* target = ' } ' ;
break ;
case ' } ' :
* target = ' { ' ;
break ;
case ' < ' :
* target = ' > ' ;
break ;
case ' > ' :
* target = ' < ' ;
break ;
case ' \\ ' :
* target = ' / ' ;
break ;
case ' / ' :
* target = ' \\ ' ;
break ;
1999-04-17 00:37:12 +00:00
default :
break ;
}
target - - ;
}
block_type = _HEB_BLOCK_TYPE_ENG ;
} else {
2003-01-11 23:05:19 +00:00
while ( ! isheb ( * ( tmp + 1 ) ) & & ( int ) * ( tmp + 1 ) ! = ' \n ' & & block_end < Z_STRLEN_PP ( str ) - 1 ) {
1999-04-17 00:37:12 +00:00
tmp + + ;
block_end + + ;
block_length + + ;
}
2002-10-02 17:56:04 +00:00
while ( ( _isblank ( ( int ) * tmp ) | | ispunct ( ( int ) * tmp ) ) & & * tmp ! = ' / ' & & * tmp ! = ' - ' & & block_end > block_start ) {
1999-04-17 00:37:12 +00:00
tmp - - ;
block_end - - ;
}
2002-10-02 17:56:04 +00:00
for ( i = block_end ; i > = block_start ; i - - ) {
2001-09-25 21:58:48 +00:00
* target = Z_STRVAL_PP ( str ) [ i ] ;
1999-04-17 00:37:12 +00:00
target - - ;
}
block_type = _HEB_BLOCK_TYPE_HEB ;
}
block_start = block_end + 1 ;
2003-01-11 23:05:19 +00:00
} while ( block_end < Z_STRLEN_PP ( str ) - 1 ) ;
1999-04-17 00:37:12 +00:00
2001-09-25 21:58:48 +00:00
broken_str = ( char * ) emalloc ( Z_STRLEN_PP ( str ) + 1 ) ;
begin = end = Z_STRLEN_PP ( str ) - 1 ;
1999-04-17 00:37:12 +00:00
target = broken_str ;
while ( 1 ) {
char_count = 0 ;
2002-10-02 17:56:04 +00:00
while ( ( ! max_chars | | char_count < max_chars ) & & begin > 0 ) {
1999-04-17 00:37:12 +00:00
char_count + + ;
begin - - ;
2002-10-02 17:56:04 +00:00
if ( begin < = 0 | | _isnewline ( heb_str [ begin ] ) ) {
while ( begin > 0 & & _isnewline ( heb_str [ begin - 1 ] ) ) {
1999-04-17 00:37:12 +00:00
begin - - ;
char_count + + ;
}
break ;
}
}
2002-10-02 17:56:04 +00:00
if ( char_count = = max_chars ) { /* try to avoid breaking words */
1999-04-17 00:37:12 +00:00
int new_char_count = char_count , new_begin = begin ;
2002-10-02 17:56:04 +00:00
while ( new_char_count > 0 ) {
1999-04-17 00:37:12 +00:00
if ( _isblank ( heb_str [ new_begin ] ) | | _isnewline ( heb_str [ new_begin ] ) ) {
break ;
}
new_begin + + ;
new_char_count - - ;
}
2002-10-02 17:56:04 +00:00
if ( new_char_count > 0 ) {
1999-04-17 00:37:12 +00:00
char_count = new_char_count ;
begin = new_begin ;
}
}
orig_begin = begin ;
if ( _isblank ( heb_str [ begin ] ) ) {
heb_str [ begin ] = ' \n ' ;
}
2002-10-02 17:56:04 +00:00
while ( begin < = end & & _isnewline ( heb_str [ begin ] ) ) { /* skip leading newlines */
1999-04-17 00:37:12 +00:00
begin + + ;
}
2002-10-02 17:56:04 +00:00
for ( i = begin ; i < = end ; i + + ) { /* copy content */
1999-04-17 00:37:12 +00:00
* target = heb_str [ i ] ;
target + + ;
}
2002-10-02 17:56:04 +00:00
for ( i = orig_begin ; i < = end & & _isnewline ( heb_str [ i ] ) ; i + + ) {
1999-04-17 00:37:12 +00:00
* target = heb_str [ i ] ;
target + + ;
}
begin = orig_begin ;
2002-10-02 17:56:04 +00:00
if ( begin < = 0 ) {
1999-04-17 00:37:12 +00:00
* target = 0 ;
break ;
}
begin - - ;
end = begin ;
}
efree ( heb_str ) ;
if ( convert_newlines ) {
2001-10-04 15:25:39 +00:00
php_char_to_str ( broken_str , Z_STRLEN_PP ( str ) , ' \n ' , " <br /> \n " , 7 , return_value ) ;
1999-04-17 00:37:12 +00:00
efree ( broken_str ) ;
} else {
2001-09-25 21:58:48 +00:00
Z_STRVAL_P ( return_value ) = broken_str ;
Z_STRLEN_P ( return_value ) = Z_STRLEN_PP ( str ) ;
Z_TYPE_P ( return_value ) = IS_STRING ;
1999-04-17 00:37:12 +00:00
}
}
2001-06-05 13:12:10 +00:00
/* }}} */
1999-04-17 00:37:12 +00:00
/* {{{ proto string hebrev(string str [, int max_chars_per_line])
2001-09-04 10:44:28 +00:00
Converts logical Hebrew text to visual text */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( hebrev )
1999-04-17 00:37:12 +00:00
{
2001-08-11 17:03:37 +00:00
php_hebrev ( INTERNAL_FUNCTION_PARAM_PASSTHRU , 0 ) ;
1999-04-17 00:37:12 +00:00
}
/* }}} */
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
/* {{{ proto string hebrevc(string str [, int max_chars_per_line])
2001-09-04 10:44:28 +00:00
Converts logical Hebrew text to visual text with newline conversion */
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
PHP_FUNCTION ( hebrevc )
1999-04-17 00:37:12 +00:00
{
2001-08-11 17:03:37 +00:00
php_hebrev ( INTERNAL_FUNCTION_PARAM_PASSTHRU , 1 ) ;
1999-04-17 00:37:12 +00:00
}
/* }}} */
2001-09-26 19:34:46 +00:00
1999-04-17 00:37:12 +00:00
/* {{{ proto string nl2br(string str)
Converts newlines to HTML line breaks */
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
PHP_FUNCTION ( nl2br )
1999-04-17 00:37:12 +00:00
{
2001-09-26 19:34:46 +00:00
/* in brief this inserts <br /> before matched regexp \n\r?|\r\n? */
2002-10-09 13:37:02 +00:00
zval * * zstr ;
char * tmp , * str ;
int new_length ;
char * end , * target ;
int repl_cnt = 0 ;
2001-09-26 19:34:46 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( 1 , & zstr ) = = FAILURE ) {
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
}
2001-09-26 19:34:46 +00:00
convert_to_string_ex ( zstr ) ;
2002-10-09 13:37:02 +00:00
str = Z_STRVAL_PP ( zstr ) ;
end = str + Z_STRLEN_PP ( zstr ) ;
2001-09-26 19:34:46 +00:00
/* it is really faster to scan twice and allocate mem once insted scanning once
and constantly reallocing */
2002-10-09 13:50:06 +00:00
while ( str < end ) {
2002-10-09 13:37:02 +00:00
if ( * str = = ' \r ' ) {
if ( * ( str + 1 ) = = ' \n ' ) {
str + + ;
}
repl_cnt + + ;
} else if ( * str = = ' \n ' ) {
if ( * ( str + 1 ) = = ' \r ' ) {
str + + ;
}
2001-09-26 19:34:46 +00:00
repl_cnt + + ;
2002-10-09 13:37:02 +00:00
}
str + + ;
2001-09-26 19:34:46 +00:00
}
2002-10-09 13:37:02 +00:00
2001-09-26 19:34:46 +00:00
if ( repl_cnt = = 0 ) {
2002-10-11 12:42:01 +00:00
RETURN_STRINGL ( Z_STRVAL_PP ( zstr ) , Z_STRLEN_PP ( zstr ) , 1 ) ;
2001-09-24 08:33:51 +00:00
}
2001-09-09 12:55:48 +00:00
2002-10-11 12:42:01 +00:00
new_length = Z_STRLEN_PP ( zstr ) + repl_cnt * ( sizeof ( " <br /> " ) - 1 ) ;
2002-10-09 13:37:02 +00:00
tmp = target = emalloc ( new_length + 1 ) ;
str = Z_STRVAL_PP ( zstr ) ;
2002-10-09 13:50:06 +00:00
while ( str < end ) {
switch ( * str ) {
2002-10-09 13:37:02 +00:00
case ' \r ' :
case ' \n ' :
* target + + = ' < ' ;
* target + + = ' b ' ;
* target + + = ' r ' ;
* target + + = ' ' ;
* target + + = ' / ' ;
* target + + = ' > ' ;
if ( ( * str = = ' \r ' & & * ( str + 1 ) = = ' \n ' ) | | ( * str = = ' \n ' & & * ( str + 1 ) = = ' \r ' ) ) {
* target + + = * str + + ;
}
/* lack of a break; is intentional */
default :
* target + + = * str ;
}
str + + ;
}
2002-10-11 12:42:01 +00:00
* target = ' \0 ' ;
2001-09-26 19:34:46 +00:00
RETURN_STRINGL ( tmp , new_length , 0 ) ;
1999-04-17 00:37:12 +00:00
}
/* }}} */
2001-09-26 19:34:46 +00:00
1999-09-20 20:05:26 +00:00
/* {{{ proto string strip_tags(string str [, string allowable_tags])
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
Strips HTML and PHP tags from a string */
PHP_FUNCTION ( strip_tags )
{
2005-10-05 12:16:02 +00:00
void * str , * allow = NULL ;
int32_t str_len , allow_len = 0 ;
2005-10-03 17:00:00 +00:00
zend_uchar str_type , allow_type ;
void * buf ;
int32_t retval_len ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
2005-10-03 17:00:00 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " T|T " , & str , & str_len , & str_type ,
& allow , & allow_len , & allow_type ) = = FAILURE ) {
return ;
}
if ( str_type = = IS_UNICODE ) {
buf = eustrndup ( str , str_len ) ;
retval_len = php_u_strip_tags ( ( UChar * ) buf , str_len , NULL , ( UChar * ) allow , allow_len TSRMLS_CC ) ;
RETURN_UNICODEL ( ( UChar * ) buf , retval_len , 0 ) ;
} else {
buf = estrndup ( str , str_len ) ;
retval_len = php_strip_tags ( ( char * ) buf , str_len , NULL , ( char * ) allow , allow_len ) ;
if ( str_type = = IS_BINARY ) {
RETURN_BINARYL ( ( char * ) buf , retval_len , 0 ) ;
} else {
RETURN_STRINGL ( ( char * ) buf , retval_len , 0 ) ;
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
}
}
/* }}} */
2002-09-18 07:55:30 +00:00
/* {{{ proto string setlocale(mixed category, string locale [, string ...])
1999-04-17 00:37:12 +00:00
Set locale information */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( setlocale )
1999-04-17 00:37:12 +00:00
{
2003-08-11 23:16:54 +00:00
pval * * * args = ( pval * * * ) safe_emalloc ( sizeof ( pval * * ) , ZEND_NUM_ARGS ( ) , 0 ) ;
1999-12-23 14:59:01 +00:00
zval * * pcategory , * * plocale ;
2002-09-18 07:55:30 +00:00
int i , cat , n_args = ZEND_NUM_ARGS ( ) ;
1999-04-17 00:37:12 +00:00
char * loc , * retval ;
2002-10-02 17:56:04 +00:00
if ( zend_get_parameters_array_ex ( n_args , args ) = = FAILURE | | n_args < 2 ) {
2002-09-18 07:55:30 +00:00
efree ( args ) ;
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
2002-09-18 07:55:30 +00:00
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
# ifdef HAVE_SETLOCALE
2002-09-18 07:55:30 +00:00
pcategory = args [ 0 ] ;
2000-12-21 13:46:50 +00:00
if ( Z_TYPE_PP ( pcategory ) = = IS_LONG ) {
convert_to_long_ex ( pcategory ) ;
cat = Z_LVAL_PP ( pcategory ) ;
} else { /* FIXME: The following behaviour should be removed. */
2002-09-18 07:55:30 +00:00
char * category ;
2002-09-05 11:29:31 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Passing locale category name as string is deprecated. Use the LC_* -constants instead. " ) ;
2000-12-21 13:46:50 +00:00
convert_to_string_ex ( pcategory ) ;
2002-09-18 07:55:30 +00:00
category = Z_STRVAL_P ( * pcategory ) ;
2000-12-21 13:46:50 +00:00
2002-09-18 07:55:30 +00:00
if ( ! strcasecmp ( " LC_ALL " , category ) )
2000-12-21 13:46:50 +00:00
cat = LC_ALL ;
2002-09-18 07:55:30 +00:00
else if ( ! strcasecmp ( " LC_COLLATE " , category ) )
2000-12-21 13:46:50 +00:00
cat = LC_COLLATE ;
2002-09-18 07:55:30 +00:00
else if ( ! strcasecmp ( " LC_CTYPE " , category ) )
2000-12-21 13:46:50 +00:00
cat = LC_CTYPE ;
2000-06-17 16:49:03 +00:00
# ifdef LC_MESSAGES
2002-09-18 07:55:30 +00:00
else if ( ! strcasecmp ( " LC_MESSAGES " , category ) )
2000-12-21 13:46:50 +00:00
cat = LC_MESSAGES ;
2000-06-17 14:48:03 +00:00
# endif
2002-09-18 07:55:30 +00:00
else if ( ! strcasecmp ( " LC_MONETARY " , category ) )
2000-12-21 13:46:50 +00:00
cat = LC_MONETARY ;
2002-09-18 07:55:30 +00:00
else if ( ! strcasecmp ( " LC_NUMERIC " , category ) )
2000-12-21 13:46:50 +00:00
cat = LC_NUMERIC ;
2002-09-18 07:55:30 +00:00
else if ( ! strcasecmp ( " LC_TIME " , category ) )
2000-12-21 13:46:50 +00:00
cat = LC_TIME ;
else {
2002-09-18 07:55:30 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Invalid locale category name %s, must be one of LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, or LC_TIME. " , category ) ;
2003-01-05 04:29:14 +00:00
efree ( args ) ;
2000-12-21 13:46:50 +00:00
RETURN_FALSE ;
}
1999-04-17 00:37:12 +00:00
}
2002-09-18 07:55:30 +00:00
if ( Z_TYPE_PP ( args [ 1 ] ) = = IS_ARRAY ) {
zend_hash_internal_pointer_reset ( Z_ARRVAL_PP ( args [ 1 ] ) ) ;
2002-10-24 19:15:40 +00:00
i = 0 ; /* not needed in this case: only kill a compiler warning */
2000-12-21 13:46:50 +00:00
} else {
2002-09-18 07:55:30 +00:00
i = 1 ;
2000-12-21 13:46:50 +00:00
}
2002-10-02 17:56:04 +00:00
while ( 1 ) {
2002-09-18 07:55:30 +00:00
if ( Z_TYPE_PP ( args [ 1 ] ) = = IS_ARRAY ) {
2003-04-03 06:21:59 +00:00
if ( ! zend_hash_num_elements ( Z_ARRVAL_PP ( args [ 1 ] ) ) ) {
break ;
}
2002-09-18 07:55:30 +00:00
zend_hash_get_current_data ( Z_ARRVAL_PP ( args [ 1 ] ) , ( void * * ) & plocale ) ;
} else {
plocale = args [ i ] ;
1999-10-08 02:10:57 +00:00
}
2002-09-18 07:55:30 +00:00
convert_to_string_ex ( plocale ) ;
if ( ! strcmp ( " 0 " , Z_STRVAL_PP ( plocale ) ) ) {
loc = NULL ;
} else {
loc = Z_STRVAL_PP ( plocale ) ;
}
retval = setlocale ( cat , loc ) ;
if ( retval ) {
/* Remember if locale was changed */
if ( loc ) {
STR_FREE ( BG ( locale_string ) ) ;
BG ( locale_string ) = estrdup ( retval ) ;
}
efree ( args ) ;
RETVAL_STRING ( retval , 1 ) ;
2002-10-10 16:29:35 +00:00
2002-09-18 07:55:30 +00:00
return ;
}
if ( Z_TYPE_PP ( args [ 1 ] ) = = IS_ARRAY ) {
2002-10-18 13:07:50 +00:00
if ( zend_hash_move_forward ( Z_ARRVAL_PP ( args [ 1 ] ) ) = = FAILURE ) break ;
2002-09-18 07:55:30 +00:00
} else {
2002-10-02 17:56:04 +00:00
if ( + + i > = n_args ) break ;
2002-09-18 07:55:30 +00:00
}
1999-04-17 00:37:12 +00:00
}
2002-09-18 07:55:30 +00:00
1999-04-17 00:37:12 +00:00
# endif
2002-09-18 07:55:30 +00:00
efree ( args ) ;
1999-04-17 00:37:12 +00:00
RETURN_FALSE ;
}
/* }}} */
2001-09-04 10:44:28 +00:00
/* {{{ proto void parse_str(string encoded_string [, array result])
2000-07-26 11:43:01 +00:00
Parses GET / POST / COOKIE data and sets global variables */
1999-07-24 22:16:54 +00:00
PHP_FUNCTION ( parse_str )
1999-04-17 00:37:12 +00:00
{
1999-12-23 14:59:01 +00:00
zval * * arg ;
2000-09-11 14:50:26 +00:00
zval * * arrayArg ;
zval * sarg ;
1999-04-17 00:37:12 +00:00
char * res = NULL ;
2000-09-11 14:50:26 +00:00
int argCount ;
1999-04-17 00:37:12 +00:00
2003-01-21 14:56:40 +00:00
argCount = ZEND_NUM_ARGS ( ) ;
2002-10-02 17:56:04 +00:00
if ( argCount < 1 | | argCount > 2 | | zend_get_parameters_ex ( argCount , & arg , & arrayArg ) = = FAILURE ) {
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
}
2000-09-11 14:50:26 +00:00
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( arg ) ;
2000-09-11 14:50:26 +00:00
sarg = * arg ;
2001-09-25 21:58:48 +00:00
if ( Z_STRVAL_P ( sarg ) & & * Z_STRVAL_P ( sarg ) ) {
res = estrndup ( Z_STRVAL_P ( sarg ) , Z_STRLEN_P ( sarg ) ) ;
2000-09-11 14:50:26 +00:00
}
2002-10-02 17:56:04 +00:00
if ( argCount = = 1 ) {
2005-09-28 22:31:29 +00:00
zval tmp ;
Z_ARRVAL ( tmp ) = EG ( active_symbol_table ) ;
sapi_module . treat_data ( PARSE_STRING , res , & tmp TSRMLS_CC ) ;
2000-09-12 10:56:25 +00:00
} else {
2000-09-11 14:50:26 +00:00
/* Clear out the array that was passed in. */
zval_dtor ( * arrayArg ) ;
array_init ( * arrayArg ) ;
2002-08-02 06:53:48 +00:00
sapi_module . treat_data ( PARSE_STRING , res , * arrayArg TSRMLS_CC ) ;
1999-04-17 00:37:12 +00:00
}
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
/* }}} */
1999-09-20 15:50:56 +00:00
# define PHP_TAG_BUF_SIZE 1023
2005-10-03 17:00:00 +00:00
/* php_u_tag_find / php_tag_find
2001-06-05 13:12:10 +00:00
*
* Check if tag is in a set of tags
1999-09-20 15:50:56 +00:00
*
* states :
2005-10-03 17:00:00 +00:00
*
1999-09-20 15:50:56 +00:00
* 0 start tag
* 1 first non - whitespace char seen
*/
2005-10-03 17:00:00 +00:00
/* {{{ php_u_tag_find
*/
int php_u_tag_find ( UChar * tag , int32_t len , UChar * set , int32_t set_len )
{
int32_t idx = 0 ;
2005-10-05 12:16:02 +00:00
UChar32 ch ;
UChar * norm , * n ;
2005-10-03 17:00:00 +00:00
int state = 0 , done = 0 ;
if ( ! len ) {
return 0 ;
}
norm = eumalloc ( len + 1 ) ;
n = norm ;
while ( ! done ) {
U16_NEXT ( tag , idx , len , ch ) ;
switch ( u_tolower ( ch ) ) {
case ' < ' :
* ( n + + ) = ch ;
break ;
case ' > ' :
done = 1 ;
break ;
default :
if ( u_isWhitespace ( ch ) = = FALSE ) {
if ( state = = 0 ) {
state = 1 ;
if ( ch ! = ' / ' )
* ( n + + ) = ch ;
} else {
* ( n + + ) = ch ;
}
} else {
if ( state = = 1 )
done = 1 ;
}
break ;
}
}
* ( n + + ) = ' > ' ;
2005-10-05 12:16:02 +00:00
* n = 0 ;
2005-10-03 17:00:00 +00:00
2005-10-05 12:16:02 +00:00
if ( u_strFindFirst ( set , set_len , norm , n - norm ) ! = NULL ) {
2005-10-03 17:00:00 +00:00
done = 1 ;
} else {
done = 0 ;
}
efree ( norm ) ;
return done ;
}
/* }}} */
/* {{{ php_tag_find
*/
1999-09-20 15:50:56 +00:00
int php_tag_find ( char * tag , int len , char * set ) {
char c , * n , * t ;
2000-02-13 15:59:32 +00:00
int state = 0 , done = 0 ;
2000-02-13 15:56:07 +00:00
char * norm = emalloc ( len + 1 ) ;
1999-09-20 15:50:56 +00:00
n = norm ;
t = tag ;
c = tolower ( * t ) ;
/*
normalize the tag removing leading and trailing whitespace
and turn any < a whatever . . . > into just < a > and any < / tag >
into < tag >
*/
2000-02-13 15:59:32 +00:00
if ( ! len ) {
return 0 ;
}
2002-10-02 17:56:04 +00:00
while ( ! done ) {
switch ( c ) {
case ' < ' :
* ( n + + ) = c ;
break ;
case ' > ' :
done = 1 ;
break ;
default :
if ( ! isspace ( ( int ) c ) ) {
if ( state = = 0 ) {
state = 1 ;
if ( c ! = ' / ' )
* ( n + + ) = c ;
} else {
* ( n + + ) = c ;
}
1999-09-20 15:50:56 +00:00
} else {
2002-10-02 17:56:04 +00:00
if ( state = = 1 )
done = 1 ;
1999-09-20 15:50:56 +00:00
}
2002-10-02 17:56:04 +00:00
break ;
1999-09-20 15:50:56 +00:00
}
c = tolower ( * ( + + t ) ) ;
}
* ( n + + ) = ' > ' ;
* n = ' \0 ' ;
2002-10-02 17:56:04 +00:00
if ( strstr ( set , norm ) ) {
2000-02-13 15:56:07 +00:00
done = 1 ;
} else {
done = 0 ;
}
1999-09-20 15:50:56 +00:00
efree ( norm ) ;
return done ;
}
2001-06-05 13:12:10 +00:00
/* }}} */
1999-09-20 15:50:56 +00:00
2005-10-03 17:00:00 +00:00
/* php_u_strip_tags / php_strip_tags
2001-06-05 13:12:10 +00:00
2002-09-11 14:40:52 +00:00
A simple little state - machine to strip out html and php tags
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
State 0 is the output state , State 1 means we are inside a
normal html tag and state 2 means we are inside a php tag .
The state variable is passed in to allow a function like fgetss
to maintain state across calls to the function .
lc holds the last significant character read and br is a bracket
counter .
1999-09-20 15:50:56 +00:00
When an allow string is passed in we keep track of the string
in state 1 and when the tag is closed check it against the
allow string to see if we should allow it .
2001-08-22 02:03:14 +00:00
swm : Added ability to strip < ? xml tags without assuming it PHP
code .
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
*/
2005-10-03 17:00:00 +00:00
/* {{{ php_u_strip_tags
*/
PHPAPI int32_t php_u_strip_tags ( UChar * rbuf , int32_t len , int * stateptr , UChar * allow , int32_t allow_len TSRMLS_DC )
{
UChar * tbuf = NULL , * tp = NULL ;
UChar * buf , * rp ;
int32_t idx = 0 , tmp , codepts ;
UChar32 ch , next , prev1 , prev2 , last , doctype [ 6 ] ;
int br = 0 , depth = 0 , state = 0 , i ;
if ( stateptr )
state = * stateptr ;
buf = eustrndup ( rbuf , len ) ;
rp = rbuf ;
2005-10-05 12:16:02 +00:00
if ( allow_len ! = 0 ) {
2005-10-12 11:00:47 +00:00
allow = eustrndup ( allow , allow_len ) ;
2005-10-03 17:00:00 +00:00
php_u_strtolower ( & allow , & allow_len , UG ( default_locale ) ) ;
tbuf = eumalloc ( PHP_TAG_BUF_SIZE + 1 ) ;
tp = tbuf ;
}
last = 0x00 ;
ch = prev1 = prev2 = next = - 1 ;
codepts = 0 ;
while ( idx < len ) {
prev2 = prev1 ; prev1 = ch ;
U16_NEXT ( buf , idx , len , ch ) ;
codepts + + ;
switch ( ch ) {
case 0x00 : /* '\0' */
break ;
case 0x3C : /* '<' */
U16_GET ( buf , 0 , idx , len , next ) ;
if ( u_isWhitespace ( next ) = = TRUE ) {
goto reg_u_char ;
}
if ( state = = 0 ) {
last = 0x3C ;
state = 1 ;
2005-10-05 12:16:02 +00:00
if ( allow_len ) {
2005-10-03 17:00:00 +00:00
tp = ( ( tp - tbuf ) > = UBYTES ( PHP_TAG_BUF_SIZE ) ? tbuf : tp ) ;
* ( tp + + ) = ch ;
}
} else if ( state = = 1 ) {
depth + + ;
}
break ;
case 0x28 : /* '(' */
if ( state = = 2 ) {
if ( last ! = 0x22 & & last ! = 0x27 ) { /* '"' & '\'' */
last = 0x28 ;
br + + ;
}
2005-10-05 12:16:02 +00:00
} else if ( allow_len & & state = = 1 ) {
2005-10-03 17:00:00 +00:00
tp = ( ( tp - tbuf ) > = UBYTES ( PHP_TAG_BUF_SIZE ) ? tbuf : tp ) ;
* ( tp + + ) = ch ;
} else if ( state = = 0 ) {
* ( rp + + ) = ch ;
}
break ;
case 0x29 : /* ')' */
if ( state = = 2 ) {
if ( last ! = 0x22 & & last ! = 0x27 ) { /* '"' & '\'' */
last = ch ;
br - - ;
}
2005-10-05 12:16:02 +00:00
} else if ( allow_len & & state = = 1 ) {
2005-10-03 17:00:00 +00:00
tp = ( ( tp - tbuf ) > = UBYTES ( PHP_TAG_BUF_SIZE ) ? tbuf : tp ) ;
* ( tp + + ) = ch ;
} else if ( state = = 0 ) {
* ( rp + + ) = ch ;
}
break ;
case 0x3E : /* '>' */
if ( depth ) {
depth - - ;
break ;
}
switch ( state ) {
case 1 : /* HTML/XML */
last = ch ;
state = 0 ;
2005-10-05 12:16:02 +00:00
if ( allow_len ) {
2005-10-03 17:00:00 +00:00
tp = ( ( tp - tbuf ) > = UBYTES ( PHP_TAG_BUF_SIZE ) ? tbuf : tp ) ;
* ( tp + + ) = ch ;
* ( tp ) = 0 ;
if ( php_u_tag_find ( tbuf , tp - tbuf , allow , allow_len ) ) {
u_memcpy ( rp , tbuf , tp - tbuf ) ;
rp + = tp - tbuf ;
}
tp = tbuf ;
}
break ;
case 2 : /* PHP */
if ( ! br & & last ! = 0x22 & & prev1 = = 0x3F ) { /* '"' & '?' */
state = 0 ;
tp = tbuf ;
}
break ;
case 3 :
state = 0 ;
tp = tbuf ;
break ;
case 4 : /* JavaScript/CSS/etc... */
if ( codepts > = 2 & & prev1 = = 0x2D & & prev2 = = 0x2D ) { /* '-' */
state = 0 ;
tp = tbuf ;
}
break ;
default :
* ( rp + + ) = ch ;
break ;
}
break ;
case 0x22 : /* '"' */
case 0x27 : /* '\'' */
if ( state = = 2 & & prev1 ! = 0x5C ) { /* '\\' */
if ( last = = ch ) {
last = 0x00 ;
} else if ( last ! = 0x5C ) {
last = ch ;
}
} else if ( state = = 0 ) {
* ( rp + + ) = ch ;
2005-10-05 12:16:02 +00:00
} else if ( allow_len & & state = = 1 ) {
2005-10-03 17:00:00 +00:00
tp = ( ( tp - tbuf ) > = UBYTES ( PHP_TAG_BUF_SIZE ) ? tbuf : tp ) ;
* ( tp + + ) = ch ;
}
break ;
case 0x21 : /* '!' */
/* JavaScript & Other HTML scripting languages */
if ( state = = 1 & & prev1 = = 0x3C ) { /* '<' */
state = 3 ;
last = ch ;
} else {
if ( state = = 0 ) {
2005-10-05 12:16:02 +00:00
* ( rp + + ) = ch ;
} else if ( allow_len & & state = = 1 ) {
tp = ( ( tp - tbuf ) > = UBYTES ( PHP_TAG_BUF_SIZE ) ? tbuf : tp ) ;
* ( tp + + ) = ch ;
2005-10-03 17:00:00 +00:00
}
}
break ;
case 0x2D : /* '-' */
if ( state = = 3 & & codepts > = 2 & & prev1 = = 0x2D & & prev2 = = 0x21 ) { /* '-' & '!' */
state = 4 ;
} else {
goto reg_u_char ;
}
break ;
case 0x3F : /* '?' */
if ( state = = 1 & & prev1 = = 0x3C ) { /* '<' */
br = 0 ;
state = 2 ;
break ;
}
case ' E ' :
case ' e ' :
/* !DOCTYPE exception */
if ( state = = 3 & & codepts > 6 ) {
tmp = idx ;
for ( i = 0 ; i < 6 ; i + + ) {
U16_PREV ( buf , 0 , tmp , doctype [ i ] ) ;
}
if ( u_tolower ( doctype [ 0 ] ) = = ' p ' & & u_tolower ( doctype [ 1 ] ) = = ' y ' & &
u_tolower ( doctype [ 2 ] ) = = ' t ' & & u_tolower ( doctype [ 3 ] ) = = ' c ' & &
u_tolower ( doctype [ 4 ] ) = = ' o ' & & u_tolower ( doctype [ 5 ] ) = = ' d ' ) {
state = 1 ;
break ;
}
}
/* fall-through */
case ' l ' :
/* swm: If we encounter '<?xml' then we shouldn't be in
* state = = 2 ( PHP ) . Switch back to HTML .
*/
if ( state = = 2 & & codepts > 2 & & prev1 = = ' m ' & & prev2 = = ' x ' ) {
state = 1 ;
break ;
}
/* fall-through */
default :
reg_u_char :
if ( state = = 0 ) {
rp + = zend_codepoint_to_uchar ( ch , rp ) ;
2005-10-05 12:16:02 +00:00
} else if ( allow_len & & state = = 1 ) {
2005-10-03 17:00:00 +00:00
tp = ( ( tp - tbuf ) > = UBYTES ( PHP_TAG_BUF_SIZE ) ? tbuf : tp ) ;
tp + = zend_codepoint_to_uchar ( ch , tp ) ;
}
break ;
}
}
* rp = 0 ;
efree ( buf ) ;
2005-10-05 12:16:02 +00:00
if ( allow_len ) {
2005-10-03 17:00:00 +00:00
efree ( tbuf ) ;
2005-10-05 12:16:02 +00:00
efree ( allow ) ;
}
2005-10-03 17:00:00 +00:00
if ( stateptr )
* stateptr = state ;
2005-10-05 12:16:02 +00:00
return ( int32_t ) ( rp - rbuf ) ;
2005-10-03 17:00:00 +00:00
}
/* }}} */
/* {{{ php_strip_tags
*/
2003-02-24 22:01:12 +00:00
PHPAPI size_t php_strip_tags ( char * rbuf , int len , int * stateptr , char * allow , int allow_len )
2000-02-08 21:29:18 +00:00
{
1999-09-20 15:50:56 +00:00
char * tbuf , * buf , * p , * tp , * rp , c , lc ;
2002-09-20 11:48:47 +00:00
int br , i = 0 , depth = 0 ;
2002-04-07 10:56:30 +00:00
int state = 0 ;
2002-03-20 14:38:13 +00:00
if ( stateptr )
state = * stateptr ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
2001-08-11 17:03:37 +00:00
buf = estrndup ( rbuf , len ) ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
c = * buf ;
lc = ' \0 ' ;
p = buf ;
rp = rbuf ;
br = 0 ;
2002-10-02 17:56:04 +00:00
if ( allow ) {
2000-02-08 21:29:18 +00:00
php_strtolower ( allow , allow_len ) ;
1999-09-20 15:50:56 +00:00
tbuf = emalloc ( PHP_TAG_BUF_SIZE + 1 ) ;
tp = tbuf ;
1999-09-24 15:34:54 +00:00
} else {
tbuf = tp = NULL ;
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
2002-10-02 17:56:04 +00:00
while ( i < len ) {
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
switch ( c ) {
2004-06-26 07:43:02 +00:00
case ' \0 ' :
break ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
case ' < ' :
2003-01-06 22:13:03 +00:00
if ( isspace ( * ( p + 1 ) ) ) {
goto reg_char ;
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
if ( state = = 0 ) {
lc = ' < ' ;
state = 1 ;
2002-10-02 17:56:04 +00:00
if ( allow ) {
2003-07-12 09:33:31 +00:00
tp = ( ( tp - tbuf ) > = PHP_TAG_BUF_SIZE ? tbuf : tp ) ;
1999-09-20 15:50:56 +00:00
* ( tp + + ) = ' < ' ;
}
2002-09-21 17:34:06 +00:00
} else if ( state = = 1 ) {
2002-09-20 11:48:47 +00:00
depth + + ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
}
break ;
case ' ( ' :
if ( state = = 2 ) {
2002-09-21 17:34:06 +00:00
if ( lc ! = ' " ' & & lc ! = ' \' ' ) {
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
lc = ' ( ' ;
br + + ;
}
2001-04-29 13:16:05 +00:00
} else if ( allow & & state = = 1 ) {
2003-07-12 09:33:31 +00:00
tp = ( ( tp - tbuf ) > = PHP_TAG_BUF_SIZE ? tbuf : tp ) ;
2001-04-28 23:07:51 +00:00
* ( tp + + ) = c ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
} else if ( state = = 0 ) {
* ( rp + + ) = c ;
}
break ;
case ' ) ' :
if ( state = = 2 ) {
2002-09-21 17:34:06 +00:00
if ( lc ! = ' " ' & & lc ! = ' \' ' ) {
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
lc = ' ) ' ;
br - - ;
}
2001-04-29 13:16:05 +00:00
} else if ( allow & & state = = 1 ) {
2003-07-12 09:33:31 +00:00
tp = ( ( tp - tbuf ) > = PHP_TAG_BUF_SIZE ? tbuf : tp ) ;
2001-04-28 23:07:51 +00:00
* ( tp + + ) = c ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
} else if ( state = = 0 ) {
* ( rp + + ) = c ;
}
break ;
case ' > ' :
2002-09-20 11:48:47 +00:00
if ( depth ) {
depth - - ;
break ;
}
2002-10-03 18:15:18 +00:00
switch ( state ) {
2002-09-21 17:34:06 +00:00
case 1 : /* HTML/XML */
lc = ' > ' ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
state = 0 ;
2002-10-02 17:56:04 +00:00
if ( allow ) {
2003-07-12 09:33:31 +00:00
tp = ( ( tp - tbuf ) > = PHP_TAG_BUF_SIZE ? tbuf : tp ) ;
2002-09-21 17:34:06 +00:00
* ( tp + + ) = ' > ' ;
* tp = ' \0 ' ;
2002-10-02 17:56:04 +00:00
if ( php_tag_find ( tbuf , tp - tbuf , allow ) ) {
2002-09-21 17:34:06 +00:00
memcpy ( rp , tbuf , tp - tbuf ) ;
rp + = tp - tbuf ;
}
tp = tbuf ;
}
break ;
case 2 : /* PHP */
2002-10-02 17:56:04 +00:00
if ( ! br & & lc ! = ' \" ' & & * ( p - 1 ) = = ' ? ' ) {
2002-09-21 17:34:06 +00:00
state = 0 ;
tp = tbuf ;
}
break ;
2003-05-04 12:44:36 +00:00
case 3 :
2003-05-04 11:45:58 +00:00
state = 0 ;
tp = tbuf ;
2002-09-21 17:34:06 +00:00
break ;
2003-05-04 12:44:36 +00:00
case 4 : /* JavaScript/CSS/etc... */
if ( p > = buf + 2 & & * ( p - 1 ) = = ' - ' & & * ( p - 2 ) = = ' - ' ) {
state = 0 ;
tp = tbuf ;
}
break ;
2002-09-21 17:34:06 +00:00
default :
* ( rp + + ) = c ;
break ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
}
break ;
2002-09-21 17:34:06 +00:00
case ' " ' :
case ' \' ' :
if ( state = = 2 & & * ( p - 1 ) ! = ' \\ ' ) {
if ( lc = = c ) {
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
lc = ' \0 ' ;
} else if ( lc ! = ' \\ ' ) {
2002-09-21 17:34:06 +00:00
lc = c ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
}
} else if ( state = = 0 ) {
* ( rp + + ) = c ;
1999-09-20 15:50:56 +00:00
} else if ( allow & & state = = 1 ) {
2003-07-12 09:33:31 +00:00
tp = ( ( tp - tbuf ) > = PHP_TAG_BUF_SIZE ? tbuf : tp ) ;
1999-09-20 15:50:56 +00:00
* ( tp + + ) = c ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
}
break ;
2002-09-21 17:34:06 +00:00
case ' ! ' :
/* JavaScript & Other HTML scripting languages */
if ( state = = 1 & & * ( p - 1 ) = = ' < ' ) {
state = 3 ;
2002-12-31 15:18:52 +00:00
lc = c ;
2002-11-27 06:20:19 +00:00
} else {
2003-01-19 11:32:54 +00:00
if ( state = = 0 ) {
* ( rp + + ) = c ;
} else if ( allow & & state = = 1 ) {
2003-07-12 09:33:31 +00:00
tp = ( ( tp - tbuf ) > = PHP_TAG_BUF_SIZE ? tbuf : tp ) ;
2003-01-19 11:32:54 +00:00
* ( tp + + ) = c ;
}
}
2002-09-21 17:34:06 +00:00
break ;
2002-12-31 15:18:52 +00:00
2003-05-04 12:44:36 +00:00
case ' - ' :
if ( state = = 3 & & p > = buf + 2 & & * ( p - 1 ) = = ' - ' & & * ( p - 2 ) = = ' ! ' ) {
state = 4 ;
2003-05-16 06:19:51 +00:00
} else {
goto reg_char ;
2003-05-04 12:44:36 +00:00
}
break ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
case ' ? ' :
2001-08-22 02:03:14 +00:00
2003-07-12 09:33:31 +00:00
if ( state = = 1 & & * ( p - 1 ) = = ' < ' ) {
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
br = 0 ;
state = 2 ;
break ;
}
2001-08-22 05:47:11 +00:00
2002-12-31 15:18:52 +00:00
case ' E ' :
case ' e ' :
/* !DOCTYPE exception */
if ( state = = 3 & & p > buf + 6
& & tolower ( * ( p - 1 ) ) = = ' p '
& & tolower ( * ( p - 2 ) ) = = ' y '
& & tolower ( * ( p - 3 ) ) = = ' t '
& & tolower ( * ( p - 4 ) ) = = ' c '
& & tolower ( * ( p - 5 ) ) = = ' o '
& & tolower ( * ( p - 6 ) ) = = ' d ' ) {
state = 1 ;
break ;
}
/* fall-through */
2001-08-22 05:47:11 +00:00
case ' l ' :
/* swm: If we encounter '<?xml' then we shouldn't be in
* state = = 2 ( PHP ) . Switch back to HTML .
*/
2002-12-31 15:18:52 +00:00
if ( state = = 2 & & p > buf + 2 & & * ( p - 1 ) = = ' m ' & & * ( p - 2 ) = = ' x ' ) {
2001-08-22 05:47:11 +00:00
state = 1 ;
break ;
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
2001-08-22 02:03:14 +00:00
/* fall-through */
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
default :
2003-01-06 22:13:03 +00:00
reg_char :
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
if ( state = = 0 ) {
* ( rp + + ) = c ;
2002-10-02 17:56:04 +00:00
} else if ( allow & & state = = 1 ) {
2003-07-12 09:33:31 +00:00
tp = ( ( tp - tbuf ) > = PHP_TAG_BUF_SIZE ? tbuf : tp ) ;
1999-09-20 15:50:56 +00:00
* ( tp + + ) = c ;
2001-08-22 02:03:14 +00:00
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
break ;
}
c = * ( + + p ) ;
1999-09-20 15:50:56 +00:00
i + + ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
}
2003-02-24 22:19:36 +00:00
if ( rp < rbuf + len ) {
* rp = ' \0 ' ;
}
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
efree ( buf ) ;
2002-10-02 17:56:04 +00:00
if ( allow )
efree ( tbuf ) ;
2002-03-20 14:38:13 +00:00
if ( stateptr )
* stateptr = state ;
2003-02-24 22:01:12 +00:00
return ( size_t ) ( rp - rbuf ) ;
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-16 17:06:53 +00:00
}
2001-06-05 13:12:10 +00:00
/* }}} */
1999-04-17 00:37:12 +00:00
1999-10-27 22:06:05 +00:00
/* {{{ proto string str_repeat(string input, int mult)
Returns the input string repeat mult times */
PHP_FUNCTION ( str_repeat )
{
2005-08-11 23:36:07 +00:00
void * input_str ; /* Input string */
int32_t input_str_len ;
int32_t input_str_chars ;
zend_uchar input_str_type ;
long mult ; /* Multiplier */
void * result ; /* Resulting string */
int32_t result_len ; /* Length of the resulting string, in bytes */
int32_t result_chars ; /* Chars/UChars in resulting string */
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " tl " , & input_str ,
& input_str_chars , & input_str_type , & mult ) = = FAILURE ) {
return ;
1999-10-27 22:06:05 +00:00
}
2005-08-11 23:36:07 +00:00
if ( mult < 0 ) {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Second argument has to be greater than or equal to 0 " ) ;
1999-10-27 22:06:05 +00:00
return ;
}
2005-08-11 23:36:07 +00:00
/* Don't waste our time if input is empty or if the multiplier is zero */
if ( input_str_chars = = 0 | | mult = = 0 ) {
if ( input_str_type = = IS_UNICODE ) {
RETURN_UNICODEL ( USTR_MAKE ( " " ) , 0 , 0 ) ;
} else if ( input_str_type = = IS_STRING ) {
RETURN_STRINGL ( " " , 0 , 1 ) ;
} else {
RETURN_BINARYL ( " " , 0 , 1 ) ;
}
}
1999-10-27 22:06:05 +00:00
/* Initialize the result string */
2005-08-11 23:36:07 +00:00
result_chars = ( input_str_chars * mult ) + 1 ;
if ( input_str_type = = IS_UNICODE ) {
input_str_len = UBYTES ( input_str_chars ) ;
result_len = UBYTES ( result_chars ) ;
if ( result_chars < 1 | | result_chars > ( 2147483647 / UBYTES ( 1 ) ) ) {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " You may not create strings longer then %ld characters " , 2147483647 / UBYTES ( 1 ) ) ;
RETURN_FALSE ;
}
} else {
input_str_len = input_str_chars ;
result_len = result_chars ;
if ( result_chars < 1 | | result_chars > 2147483647 ) {
if ( input_str_type = = IS_STRING ) {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " You may not create strings longer then 2147483647 characters " ) ;
} else {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " You may not create strings longer then 2147483647 bytes " ) ;
}
RETURN_FALSE ;
}
2003-04-02 00:25:45 +00:00
}
2005-08-11 23:36:07 +00:00
result = emalloc ( result_len ) ;
1999-10-27 22:06:05 +00:00
2002-10-04 17:10:51 +00:00
/* Heavy optimization for situations where input string is 1 byte long */
2005-08-11 23:36:07 +00:00
if ( input_str_len = = 1 ) {
memset ( result , * ( ( char * ) input_str ) , mult ) ;
2002-10-03 13:59:31 +00:00
} else {
char * s , * e , * ee ;
int l = 0 ;
2005-08-11 23:36:07 +00:00
memcpy ( result , input_str , input_str_len ) ;
2002-10-03 13:59:31 +00:00
s = result ;
2005-08-12 10:10:41 +00:00
e = ( char * ) result + input_str_len ;
ee = ( char * ) result + result_len ;
2005-08-11 23:36:07 +00:00
while ( e < ee ) {
2002-10-03 13:59:31 +00:00
l = ( e - s ) < ( ee - e ) ? ( e - s ) : ( ee - e ) ;
memmove ( e , s , l ) ;
e + = l ;
}
1999-10-27 22:06:05 +00:00
}
2005-08-11 23:36:07 +00:00
if ( input_str_type = = IS_UNICODE ) {
* ( ( ( UChar * ) result ) + result_chars - 1 ) = 0 ;
2005-08-12 13:41:15 +00:00
RETURN_UNICODEL ( ( UChar * ) result , result_chars - 1 , 0 ) ;
2005-08-11 23:36:07 +00:00
} else {
* ( ( ( char * ) result ) + result_chars - 1 ) = ' \0 ' ;
if ( input_str_type = = IS_BINARY ) {
2005-08-12 13:41:15 +00:00
RETURN_BINARYL ( ( char * ) result , result_chars - 1 , 0 ) ;
2005-08-11 23:36:07 +00:00
} else {
2005-08-12 13:41:15 +00:00
RETURN_STRINGL ( ( char * ) result , result_chars - 1 , 0 ) ;
2005-08-11 23:36:07 +00:00
}
}
1999-10-27 22:06:05 +00:00
}
/* }}} */
2000-02-24 10:11:42 +00:00
/* {{{ proto mixed count_chars(string input [, int mode])
1999-12-14 03:52:12 +00:00
Returns info about what characters are used in input */
PHP_FUNCTION ( count_chars )
{
zval * * input , * * mode ;
int chars [ 256 ] ;
2000-06-05 19:47:54 +00:00
int ac = ZEND_NUM_ARGS ( ) ;
1999-12-14 03:52:12 +00:00
int mymode = 0 ;
unsigned char * buf ;
int len , inx ;
char retstr [ 256 ] ;
int retlen = 0 ;
1999-12-18 22:40:35 +00:00
if ( ac < 1 | | ac > 2 | | zend_get_parameters_ex ( ac , & input , & mode ) = = FAILURE ) {
1999-12-14 03:52:12 +00:00
WRONG_PARAM_COUNT ;
}
convert_to_string_ex ( input ) ;
if ( ac = = 2 ) {
convert_to_long_ex ( mode ) ;
2001-09-25 21:58:48 +00:00
mymode = Z_LVAL_PP ( mode ) ;
1999-12-14 03:52:12 +00:00
if ( mymode < 0 | | mymode > 4 ) {
2002-09-05 11:29:31 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Unknown mode. " ) ;
1999-12-14 03:52:12 +00:00
RETURN_FALSE ;
}
}
2001-09-25 21:58:48 +00:00
len = Z_STRLEN_PP ( input ) ;
buf = ( unsigned char * ) Z_STRVAL_PP ( input ) ;
2001-08-11 17:03:37 +00:00
memset ( ( void * ) chars , 0 , sizeof ( chars ) ) ;
1999-12-14 03:52:12 +00:00
while ( len > 0 ) {
chars [ * buf ] + + ;
buf + + ;
len - - ;
}
if ( mymode < 3 ) {
array_init ( return_value ) ;
}
2002-10-02 17:56:04 +00:00
for ( inx = 0 ; inx < 256 ; inx + + ) {
1999-12-14 03:52:12 +00:00
switch ( mymode ) {
2002-10-03 18:15:18 +00:00
case 0 :
2001-08-11 17:03:37 +00:00
add_index_long ( return_value , inx , chars [ inx ] ) ;
2002-10-03 18:15:18 +00:00
break ;
case 1 :
if ( chars [ inx ] ! = 0 ) {
add_index_long ( return_value , inx , chars [ inx ] ) ;
}
break ;
case 2 :
if ( chars [ inx ] = = 0 ) {
add_index_long ( return_value , inx , chars [ inx ] ) ;
}
break ;
case 3 :
if ( chars [ inx ] ! = 0 ) {
retstr [ retlen + + ] = inx ;
}
break ;
case 4 :
if ( chars [ inx ] = = 0 ) {
retstr [ retlen + + ] = inx ;
}
break ;
1999-12-14 03:52:12 +00:00
}
}
if ( mymode > = 3 & & mymode < = 4 ) {
2001-08-11 17:03:37 +00:00
RETURN_STRINGL ( retstr , retlen , 1 ) ;
1999-12-14 03:52:12 +00:00
}
}
/* }}} */
2000-04-12 19:39:02 +00:00
2001-06-05 13:12:10 +00:00
/* {{{ php_strnatcmp
*/
2000-04-12 19:39:02 +00:00
static void php_strnatcmp ( INTERNAL_FUNCTION_PARAMETERS , int fold_case )
{
zval * * s1 , * * s2 ;
2000-06-05 19:47:54 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 2 | | zend_get_parameters_ex ( 2 , & s1 , & s2 ) = = FAILURE ) {
2000-04-12 19:39:02 +00:00
WRONG_PARAM_COUNT ;
}
convert_to_string_ex ( s1 ) ;
convert_to_string_ex ( s2 ) ;
2001-09-25 22:49:04 +00:00
RETURN_LONG ( strnatcmp_ex ( Z_STRVAL_PP ( s1 ) , Z_STRLEN_PP ( s1 ) ,
Z_STRVAL_PP ( s2 ) , Z_STRLEN_PP ( s2 ) ,
2000-04-12 19:39:02 +00:00
fold_case ) ) ;
}
2001-06-05 13:12:10 +00:00
/* }}} */
2000-04-26 00:29:59 +00:00
/* {{{ proto int strnatcmp(string s1, string s2)
Returns the result of string comparison using ' natural ' algorithm */
2000-04-12 19:39:02 +00:00
PHP_FUNCTION ( strnatcmp )
{
php_strnatcmp ( INTERNAL_FUNCTION_PARAM_PASSTHRU , 0 ) ;
}
2000-04-26 00:29:59 +00:00
/* }}} */
2000-04-12 19:39:02 +00:00
2001-09-04 10:44:28 +00:00
/* {{{ proto array localeconv(void)
2001-04-29 13:30:56 +00:00
Returns numeric formatting information based on the current locale */
2001-01-14 16:36:30 +00:00
PHP_FUNCTION ( localeconv )
{
zval * grouping , * mon_grouping ;
int len , i ;
/* We don't need no stinkin' parameters... */
if ( ZEND_NUM_ARGS ( ) > 0 ) {
WRONG_PARAM_COUNT ;
}
2003-01-05 04:29:14 +00:00
MAKE_STD_ZVAL ( grouping ) ;
MAKE_STD_ZVAL ( mon_grouping ) ;
2002-12-05 22:28:02 +00:00
array_init ( return_value ) ;
array_init ( grouping ) ;
array_init ( mon_grouping ) ;
2001-01-14 16:36:30 +00:00
# ifdef HAVE_LOCALECONV
{
struct lconv currlocdata ;
localeconv_r ( & currlocdata ) ;
/* Grab the grouping data out of the array */
len = strlen ( currlocdata . grouping ) ;
2002-10-02 17:56:04 +00:00
for ( i = 0 ; i < len ; i + + ) {
2001-01-14 16:36:30 +00:00
add_index_long ( grouping , i , currlocdata . grouping [ i ] ) ;
}
/* Grab the monetary grouping data out of the array */
len = strlen ( currlocdata . mon_grouping ) ;
2002-10-02 17:56:04 +00:00
for ( i = 0 ; i < len ; i + + ) {
2001-01-14 16:36:30 +00:00
add_index_long ( mon_grouping , i , currlocdata . mon_grouping [ i ] ) ;
}
add_assoc_string ( return_value , " decimal_point " , currlocdata . decimal_point , 1 ) ;
add_assoc_string ( return_value , " thousands_sep " , currlocdata . thousands_sep , 1 ) ;
add_assoc_string ( return_value , " int_curr_symbol " , currlocdata . int_curr_symbol , 1 ) ;
add_assoc_string ( return_value , " currency_symbol " , currlocdata . currency_symbol , 1 ) ;
add_assoc_string ( return_value , " mon_decimal_point " , currlocdata . mon_decimal_point , 1 ) ;
add_assoc_string ( return_value , " mon_thousands_sep " , currlocdata . mon_thousands_sep , 1 ) ;
add_assoc_string ( return_value , " positive_sign " , currlocdata . positive_sign , 1 ) ;
add_assoc_string ( return_value , " negative_sign " , currlocdata . negative_sign , 1 ) ;
add_assoc_long ( return_value , " int_frac_digits " , currlocdata . int_frac_digits ) ;
add_assoc_long ( return_value , " frac_digits " , currlocdata . frac_digits ) ;
add_assoc_long ( return_value , " p_cs_precedes " , currlocdata . p_cs_precedes ) ;
add_assoc_long ( return_value , " p_sep_by_space " , currlocdata . p_sep_by_space ) ;
add_assoc_long ( return_value , " n_cs_precedes " , currlocdata . n_cs_precedes ) ;
add_assoc_long ( return_value , " n_sep_by_space " , currlocdata . n_sep_by_space ) ;
add_assoc_long ( return_value , " p_sign_posn " , currlocdata . p_sign_posn ) ;
add_assoc_long ( return_value , " n_sign_posn " , currlocdata . n_sign_posn ) ;
}
# else
/* Ok, it doesn't look like we have locale info floating around, so I guess it
wouldn ' t hurt to just go ahead and return the POSIX locale information ? */
add_index_long ( grouping , 0 , - 1 ) ;
add_index_long ( mon_grouping , 0 , - 1 ) ;
add_assoc_string ( return_value , " decimal_point " , " \x2E " , 1 ) ;
add_assoc_string ( return_value , " thousands_sep " , " " , 1 ) ;
add_assoc_string ( return_value , " int_curr_symbol " , " " , 1 ) ;
add_assoc_string ( return_value , " currency_symbol " , " " , 1 ) ;
add_assoc_string ( return_value , " mon_decimal_point " , " \x2E " , 1 ) ;
add_assoc_string ( return_value , " mon_thousands_sep " , " " , 1 ) ;
add_assoc_string ( return_value , " positive_sign " , " " , 1 ) ;
add_assoc_string ( return_value , " negative_sign " , " " , 1 ) ;
add_assoc_long ( return_value , " int_frac_digits " , CHAR_MAX ) ;
add_assoc_long ( return_value , " frac_digits " , CHAR_MAX ) ;
add_assoc_long ( return_value , " p_cs_precedes " , CHAR_MAX ) ;
add_assoc_long ( return_value , " p_sep_by_space " , CHAR_MAX ) ;
add_assoc_long ( return_value , " n_cs_precedes " , CHAR_MAX ) ;
add_assoc_long ( return_value , " n_sep_by_space " , CHAR_MAX ) ;
add_assoc_long ( return_value , " p_sign_posn " , CHAR_MAX ) ;
add_assoc_long ( return_value , " n_sign_posn " , CHAR_MAX ) ;
# endif
2001-09-25 21:58:48 +00:00
zend_hash_update ( Z_ARRVAL_P ( return_value ) , " grouping " , 9 , & grouping , sizeof ( zval * ) , NULL ) ;
zend_hash_update ( Z_ARRVAL_P ( return_value ) , " mon_grouping " , 13 , & mon_grouping , sizeof ( zval * ) , NULL ) ;
2001-01-14 16:36:30 +00:00
}
2001-06-05 13:12:10 +00:00
/* }}} */
2000-04-26 00:29:59 +00:00
/* {{{ proto int strnatcasecmp(string s1, string s2)
Returns the result of case - insensitive string comparison using ' natural ' algorithm */
2000-04-12 19:39:02 +00:00
PHP_FUNCTION ( strnatcasecmp )
{
php_strnatcmp ( INTERNAL_FUNCTION_PARAM_PASSTHRU , 1 ) ;
}
2000-04-26 00:29:59 +00:00
/* }}} */
2005-06-18 18:23:12 +00:00
/* {{{ proto int substr_count(string haystack, string needle [, int offset [, int length]])
2000-08-06 14:36:10 +00:00
Returns the number of times a substring occurs in the string */
2000-04-26 00:29:59 +00:00
PHP_FUNCTION ( substr_count )
{
2005-08-27 19:14:05 +00:00
void * haystack , * needle ;
int32_t haystack_len , needle_len ;
zend_uchar haystack_type , needle_type ;
long offset = 0 , length = 0 ;
2005-06-18 18:23:12 +00:00
int ac = ZEND_NUM_ARGS ( ) ;
2002-10-09 13:37:02 +00:00
int count = 0 ;
2005-08-27 19:14:05 +00:00
void * p , * endp , * tmp ;
int32_t i , j ;
char cmp ;
if ( zend_parse_parameters ( ac TSRMLS_CC , " TT|ll " ,
& haystack , & haystack_len , & haystack_type ,
& needle , & needle_len , & needle_type ,
& offset , & length ) = = FAILURE ) {
return ;
2000-04-26 00:29:59 +00:00
}
2005-08-27 19:14:05 +00:00
if ( needle_len = = 0 ) {
2002-09-05 11:29:31 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Empty substring. " ) ;
2000-04-26 00:29:59 +00:00
RETURN_FALSE ;
2002-10-09 13:37:02 +00:00
}
2005-08-27 19:14:05 +00:00
if ( haystack_type = = IS_UNICODE ) {
p = ( UChar * ) haystack ;
endp = ( UChar * ) haystack + haystack_len ;
} else {
p = ( char * ) haystack ;
endp = ( char * ) haystack + haystack_len ;
}
2005-06-18 18:23:12 +00:00
if ( ac > 2 ) {
2005-08-27 19:14:05 +00:00
if ( offset < 0 ) {
2005-07-07 15:19:40 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Offset should be greater then or equal to 0. " ) ;
2005-08-27 19:14:05 +00:00
RETURN_FALSE ;
}
if ( haystack_type = = IS_UNICODE ) {
i = 0 ;
U16_FWD_N ( ( UChar * ) haystack , i , haystack_len , offset ) ;
p = ( UChar * ) haystack + i ;
} else {
p = ( char * ) haystack + offset ;
2005-07-07 15:19:40 +00:00
}
2005-06-18 18:23:12 +00:00
if ( p > endp ) {
2005-08-27 19:14:05 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Offset value %ld exceeds string length. " , offset ) ;
RETURN_FALSE ;
2005-06-18 18:23:12 +00:00
}
if ( ac = = 4 ) {
2005-08-27 19:14:05 +00:00
if ( length < 0 ) {
2005-07-07 15:19:40 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Length should be greater than 0. " ) ;
2005-08-27 19:14:05 +00:00
RETURN_FALSE ;
}
if ( haystack_type = = IS_UNICODE ) {
j = i ;
i = 0 ;
U16_FWD_N ( ( UChar * ) p , i , haystack_len - j , length ) ;
tmp = ( UChar * ) p + i ;
} else {
tmp = ( char * ) p + length ;
2005-07-07 15:19:40 +00:00
}
2005-08-27 19:14:05 +00:00
if ( tmp > endp ) {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Offset value %ld exceeds string length. " , offset ) ;
2005-06-18 18:23:12 +00:00
RETURN_FALSE ;
2005-08-27 19:14:05 +00:00
} else {
endp = tmp ;
2005-06-18 18:23:12 +00:00
}
}
}
2005-06-19 16:31:51 +00:00
2005-08-27 19:14:05 +00:00
if ( haystack_type = = IS_UNICODE ) {
while ( ( p = zend_u_memnstr ( ( UChar * ) p , ( UChar * ) needle , needle_len , ( UChar * ) endp ) ) ! = NULL ) {
2005-08-29 06:51:14 +00:00
/*(UChar *)p += needle_len; // GCC 4.0.0 cannot compile this */
2005-09-07 07:00:03 +00:00
p = ( UChar * ) p + needle_len ;
2005-06-19 16:31:51 +00:00
count + + ;
2000-04-26 00:29:59 +00:00
}
} else {
2005-08-27 19:14:05 +00:00
if ( needle_len = = 1 ) {
cmp = ( ( char * ) needle ) [ 0 ] ;
2005-09-05 16:37:45 +00:00
while ( ( p = memchr ( p , cmp , ( char * ) endp - ( char * ) p ) ) ) {
2005-08-27 19:14:05 +00:00
count + + ;
2005-09-05 16:37:45 +00:00
p = ( char * ) p + 1 ;
2005-08-27 19:14:05 +00:00
}
} else {
while ( ( p = php_memnstr ( ( char * ) p , ( char * ) needle , needle_len , ( char * ) endp ) ) ) {
2005-08-29 06:51:14 +00:00
/*(char *)p += needle_len; // GCC 4.0.0 cannot compile this */
2005-09-05 16:37:45 +00:00
p = ( char * ) p + needle_len ;
2005-08-27 19:14:05 +00:00
count + + ;
}
2000-04-26 00:29:59 +00:00
}
}
RETURN_LONG ( count ) ;
}
2000-06-06 20:42:33 +00:00
/* }}} */
2000-07-08 22:55:40 +00:00
/* {{{ proto string str_pad(string input, int pad_length [, string pad_string [, int pad_type]])
2000-06-06 20:42:33 +00:00
Returns input string padded on the left or right to specified length with pad_string */
PHP_FUNCTION ( str_pad )
{
2000-07-08 20:38:23 +00:00
/* Input arguments */
2005-09-05 10:55:35 +00:00
void * input ; /* Input string */
int32_t pad_length ; /* Length to pad to, in codepoints for Unicode */
void * padstr ; /* Padding string */
int32_t pad_type ; /* Padding type (left/right/both) */
int32_t input_len , padstr_len ; /* Lengths in code units for Unicode */
zend_uchar input_type , padstr_type ;
2000-07-08 20:38:23 +00:00
/* Helper variables */
2005-09-05 10:55:35 +00:00
int32_t input_codepts ; /* Number of codepts in Unicode input */
int32_t num_pad_chars ; /* Number of padding characters (total - input size) */
void * result = NULL ; /* Resulting string */
int32_t result_len = 0 ; /* Length of the resulting string */
int32_t i , j , left_pad = 0 , right_pad = 0 ;
UChar32 ch ;
2000-06-06 20:42:33 +00:00
2000-07-08 20:38:23 +00:00
2005-09-05 10:55:35 +00:00
if ( ZEND_NUM_ARGS ( ) < 2 | | ZEND_NUM_ARGS ( ) > 4 ) {
2000-06-06 20:42:33 +00:00
WRONG_PARAM_COUNT ;
}
2005-09-05 10:55:35 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " Tl|Tl " ,
& input , & input_len , & input_type , & pad_length ,
& padstr , & padstr_len , & padstr_type , & pad_type ) = = FAILURE ) {
return ;
}
2000-06-06 20:42:33 +00:00
2005-09-05 10:55:35 +00:00
if ( input_type = = IS_UNICODE ) {
/* For Unicode, num_pad_chars/pad_length is number of codepoints */
i = 0 ; input_codepts = 0 ;
2005-09-09 19:07:18 +00:00
input_codepts = u_countChar32 ( ( UChar * ) input , input_len ) ;
2005-09-05 10:55:35 +00:00
num_pad_chars = pad_length - input_codepts ;
} else {
num_pad_chars = pad_length - input_len ;
}
2000-06-06 20:42:33 +00:00
/* If resulting string turns out to be shorter than input string,
we simply copy the input and return . */
2000-07-08 20:38:23 +00:00
if ( num_pad_chars < 0 ) {
2005-09-05 10:55:35 +00:00
if ( input_type = = IS_UNICODE ) {
RETURN_UNICODEL ( ( UChar * ) input , input_len , 1 ) ;
} else if ( input_type = = IS_BINARY ) {
RETURN_BINARYL ( ( char * ) input , input_len , 1 ) ;
} else {
RETURN_STRINGL ( ( char * ) input , input_len , 1 ) ;
}
2000-06-06 20:42:33 +00:00
}
2005-09-05 10:55:35 +00:00
/* Setup the padding string values if NOT specified. */
2000-06-06 20:42:33 +00:00
if ( ZEND_NUM_ARGS ( ) > 2 ) {
2005-09-05 10:55:35 +00:00
if ( padstr_len = = 0 ) {
2002-09-05 11:29:31 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Padding string cannot be empty. " ) ;
2000-06-06 20:42:33 +00:00
return ;
}
2000-07-08 20:38:23 +00:00
if ( ZEND_NUM_ARGS ( ) > 3 ) {
2005-09-05 10:55:35 +00:00
if ( pad_type < STR_PAD_LEFT | | pad_type > STR_PAD_BOTH ) {
2002-09-05 11:29:31 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " Padding type has to be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH. " ) ;
2000-07-08 20:38:23 +00:00
return ;
}
2005-09-05 10:55:35 +00:00
} else {
pad_type = STR_PAD_RIGHT ;
}
} else {
if ( input_type = = IS_UNICODE ) {
padstr = USTR_MAKE ( " " ) ;
} else {
padstr = " " ;
2000-07-08 20:38:23 +00:00
}
2005-09-05 10:55:35 +00:00
padstr_len = 1 ;
pad_type = STR_PAD_RIGHT ;
2000-06-06 20:42:33 +00:00
}
2005-09-05 10:55:35 +00:00
if ( input_type = = IS_UNICODE ) {
2005-09-09 19:07:18 +00:00
result = eumalloc ( input_len + num_pad_chars * 2 + 1 ) ;
2005-09-05 10:55:35 +00:00
} else {
result = emalloc ( input_len + num_pad_chars + 1 ) ;
}
2000-06-06 20:42:33 +00:00
2000-07-08 20:38:23 +00:00
/* We need to figure out the left/right padding lengths. */
2005-09-05 10:55:35 +00:00
switch ( pad_type ) {
2000-07-08 20:38:23 +00:00
case STR_PAD_RIGHT :
left_pad = 0 ;
right_pad = num_pad_chars ;
break ;
case STR_PAD_LEFT :
left_pad = num_pad_chars ;
right_pad = 0 ;
break ;
case STR_PAD_BOTH :
left_pad = num_pad_chars / 2 ;
right_pad = num_pad_chars - left_pad ;
break ;
2000-06-06 20:42:33 +00:00
}
2005-09-05 10:55:35 +00:00
/* Pad left, copy input, pad right, terminate */
if ( input_type = = IS_UNICODE ) {
j = 0 ;
for ( i = 0 ; i < left_pad ; i + + ) {
if ( j > = padstr_len ) {
j = 0 ;
}
U16_NEXT ( ( UChar * ) padstr , j , padstr_len , ch ) ;
result_len + = zend_codepoint_to_uchar ( ch , ( UChar * ) result + result_len ) ;
}
memcpy ( ( UChar * ) result + result_len , input , UBYTES ( input_len ) ) ;
result_len + = input_len ;
j = 0 ;
for ( i = 0 ; i < right_pad ; i + + ) {
if ( j > = padstr_len ) {
j = 0 ;
}
U16_NEXT ( ( UChar * ) padstr , j , padstr_len , ch ) ;
result_len + = zend_codepoint_to_uchar ( ch , ( UChar * ) result + result_len ) ;
}
* ( ( UChar * ) result + result_len ) = 0 ;
2005-09-09 19:07:18 +00:00
result = eurealloc ( result , result_len + 1 ) ;
2005-09-05 10:55:35 +00:00
} else {
for ( i = 0 ; i < left_pad ; i + + )
* ( ( char * ) result + result_len + + ) = * ( ( char * ) padstr + ( i % padstr_len ) ) ;
2005-09-05 16:37:45 +00:00
memcpy ( ( char * ) result + result_len , input , input_len ) ;
2005-09-05 10:55:35 +00:00
result_len + = input_len ;
for ( i = 0 ; i < right_pad ; i + + )
* ( ( char * ) result + result_len + + ) = * ( ( char * ) padstr + ( i % padstr_len ) ) ;
* ( ( char * ) result + result_len ) = ' \0 ' ;
}
if ( input_type = = IS_UNICODE ) {
if ( ZEND_NUM_ARGS ( ) < 3 ) {
efree ( padstr ) ;
}
RETURN_UNICODEL ( ( UChar * ) result , result_len , 0 ) ;
} else if ( input_type = = IS_BINARY ) {
RETURN_BINARYL ( ( char * ) result , result_len , 0 ) ;
} else {
RETURN_STRINGL ( ( char * ) result , result_len , 0 ) ;
}
2000-06-06 20:42:33 +00:00
}
2000-06-06 18:58:15 +00:00
/* }}} */
2000-07-09 19:33:19 +00:00
/* {{{ proto mixed sscanf(string str, string format [, string ...])
Implements an ANSI C compatible sscanf */
2000-06-06 18:58:15 +00:00
PHP_FUNCTION ( sscanf )
{
2002-09-11 14:40:52 +00:00
zval * * * args ;
int result ;
2001-09-03 04:49:36 +00:00
int argc = ZEND_NUM_ARGS ( ) ;
if ( argc < 2 ) {
2000-06-06 18:58:15 +00:00
WRONG_PARAM_COUNT ;
}
2001-09-03 04:49:36 +00:00
2003-08-11 23:16:54 +00:00
args = ( zval * * * ) safe_emalloc ( argc , sizeof ( zval * * ) , 0 ) ;
2001-09-03 04:49:36 +00:00
if ( zend_get_parameters_array_ex ( argc , args ) = = FAILURE ) {
efree ( args ) ;
2000-06-06 18:58:15 +00:00
WRONG_PARAM_COUNT ;
}
2001-09-03 04:49:36 +00:00
convert_to_string_ex ( args [ 0 ] ) ;
convert_to_string_ex ( args [ 1 ] ) ;
2000-06-06 18:58:15 +00:00
2001-09-03 04:49:36 +00:00
result = php_sscanf_internal ( Z_STRVAL_PP ( args [ 0 ] ) ,
2001-09-03 06:10:45 +00:00
Z_STRVAL_PP ( args [ 1 ] ) ,
argc , args ,
2 , & return_value TSRMLS_CC ) ;
2000-06-06 18:58:15 +00:00
efree ( args ) ;
if ( SCAN_ERROR_WRONG_PARAM_COUNT = = result ) {
2000-06-12 16:06:27 +00:00
WRONG_PARAM_COUNT ;
2000-06-06 18:58:15 +00:00
}
}
/* }}} */
2002-08-20 20:47:47 +00:00
static char rot13_from [ ] = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ " ;
static char rot13_to [ ] = " nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM " ;
2001-12-06 21:39:01 +00:00
/* {{{ proto string str_rot13(string str)
2001-12-06 19:02:27 +00:00
Perform the rot13 transform on a string */
2001-12-06 21:39:01 +00:00
PHP_FUNCTION ( str_rot13 )
2001-12-06 19:02:27 +00:00
{
2002-07-30 10:51:33 +00:00
zval * * arg ;
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( 1 , & arg ) ) {
WRONG_PARAM_COUNT ;
2001-12-06 19:02:27 +00:00
}
2005-03-10 23:42:15 +00:00
RETVAL_ZVAL ( * arg , 1 , 0 ) ;
2001-12-06 19:02:27 +00:00
2002-09-11 14:40:52 +00:00
php_strtr ( Z_STRVAL_P ( return_value ) , Z_STRLEN_P ( return_value ) , rot13_from , rot13_to , 52 ) ;
2001-12-06 19:02:27 +00:00
}
/* }}} */
2002-10-08 09:52:59 +00:00
static void php_string_shuffle ( char * str , long len TSRMLS_DC )
2002-09-25 18:06:05 +00:00
{
2002-10-08 09:52:59 +00:00
long n_elems , rnd_idx , n_left ;
char temp ;
/* The implementation is stolen from array_data_shuffle */
/* Thus the characteristics of the randomization are the same */
n_elems = len ;
if ( n_elems < = 1 ) {
return ;
}
n_left = n_elems ;
while ( - - n_left ) {
rnd_idx = php_rand ( TSRMLS_C ) ;
RAND_RANGE ( rnd_idx , 0 , n_left , PHP_RAND_MAX ) ;
if ( rnd_idx ! = n_left ) {
temp = str [ n_left ] ;
str [ n_left ] = str [ rnd_idx ] ;
str [ rnd_idx ] = temp ;
}
}
2002-09-25 18:06:05 +00:00
}
2002-10-08 09:52:59 +00:00
/* {{{ proto void str_shuffle(string str)
2002-09-25 18:06:05 +00:00
Shuffles string . One permutation of all possible is created */
PHP_FUNCTION ( str_shuffle )
{
2002-10-09 10:33:22 +00:00
zval * * arg ;
2002-09-25 18:06:05 +00:00
2002-10-09 10:33:22 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( 1 , & arg ) ) {
WRONG_PARAM_COUNT ;
2002-09-25 18:06:05 +00:00
}
2002-10-08 09:52:59 +00:00
2002-10-09 10:33:22 +00:00
convert_to_string_ex ( arg ) ;
2005-03-10 23:42:15 +00:00
RETVAL_ZVAL ( * arg , 1 , 0 ) ;
2002-10-09 10:33:22 +00:00
if ( Z_STRLEN_P ( return_value ) > 1 ) {
2003-01-24 12:19:59 +00:00
php_string_shuffle ( Z_STRVAL_P ( return_value ) , ( long ) Z_STRLEN_P ( return_value ) TSRMLS_CC ) ;
2002-10-09 10:33:22 +00:00
}
2002-09-25 18:06:05 +00:00
}
/* }}} */
2005-03-12 14:41:20 +00:00
/* {{{ proto mixed str_word_count(string str, [int format [, string charlist]])
2002-10-17 03:27:19 +00:00
Counts the number of words inside a string . If format of 1 is specified ,
then the function will return an array containing all the words
found inside the string . If format of 2 is specified , then the function
will return an associated array where the position of the word is the key
and the word itself is the value .
For the purpose of this function , ' word ' is defined as a locale dependent
string containing alphabetic characters , which also may contain , but not start
with " ' " and " - " characters .
*/
2002-10-17 22:44:44 +00:00
PHP_FUNCTION ( str_word_count )
2002-10-17 03:27:19 +00:00
{
2005-03-12 17:51:19 +00:00
char * buf , * str , * char_list = NULL , * p , * e , * s , ch [ 256 ] ;
int str_len , char_list_len , word_count = 0 ;
long type = 0 ;
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " s|ls " , & str , & str_len , & type , & char_list , & char_list_len ) = = FAILURE ) {
2002-10-17 03:27:19 +00:00
WRONG_PARAM_COUNT ;
}
2005-03-12 17:51:19 +00:00
if ( char_list ) {
php_charmask ( char_list , char_list_len , ch TSRMLS_CC ) ;
}
p = str ;
e = str + str_len ;
2002-10-17 03:27:19 +00:00
2005-03-12 17:51:19 +00:00
if ( type = = 1 | | type = = 2 ) {
array_init ( return_value ) ;
2002-10-17 03:27:19 +00:00
}
2005-03-12 17:51:19 +00:00
2002-10-17 03:27:19 +00:00
while ( p < e ) {
2005-07-23 19:44:13 +00:00
if ( isalpha ( * p ) | | ( char_list & & ch [ ( unsigned char ) * p ] ) ) {
s = + + p - 1 ;
2005-03-12 17:51:19 +00:00
while ( isalpha ( * p ) | | * p = = ' \' ' | | ( * p = = ' - ' & & isalpha ( * ( p + 1 ) ) ) | | ( char_list & & ch [ ( unsigned char ) * p ] ) ) {
2002-10-17 03:27:19 +00:00
p + + ;
}
switch ( type )
{
case 1 :
buf = estrndup ( s , ( p - s ) ) ;
add_next_index_stringl ( return_value , buf , ( p - s ) , 1 ) ;
efree ( buf ) ;
break ;
case 2 :
buf = estrndup ( s , ( p - s ) ) ;
2005-03-12 17:51:19 +00:00
add_index_stringl ( return_value , ( s - str ) , buf , p - s , 1 ) ;
2002-10-17 03:27:19 +00:00
efree ( buf ) ;
break ;
default :
word_count + + ;
break ;
}
2005-07-23 19:44:13 +00:00
} else {
p + + ;
}
2002-10-17 03:27:19 +00:00
}
if ( ! type ) {
RETURN_LONG ( word_count ) ;
}
}
/* }}} */
2002-09-25 18:06:05 +00:00
2002-08-02 10:08:53 +00:00
# if HAVE_STRFMON
/* {{{ proto string money_format(string format , float value)
Convert monetary value ( s ) to string */
2003-02-08 15:26:17 +00:00
PHP_FUNCTION ( money_format )
{
2002-08-21 13:14:57 +00:00
int format_len = 0 , str_len ;
char * format , * str ;
2002-08-02 10:08:53 +00:00
double value ;
2003-02-08 15:26:17 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " sd " , & format , & format_len , & value ) = = FAILURE ) {
2002-08-02 10:08:53 +00:00
return ;
}
2002-08-21 13:14:57 +00:00
str_len = format_len + 1024 ;
str = emalloc ( str_len ) ;
2003-04-16 01:07:03 +00:00
if ( ( str_len = strfmon ( str , str_len , format , value ) ) < 0 ) {
efree ( str ) ;
RETURN_FALSE ;
}
2002-08-21 13:14:57 +00:00
str [ str_len ] = 0 ;
2002-08-02 10:08:53 +00:00
2002-08-21 13:14:57 +00:00
RETURN_STRINGL ( erealloc ( str , str_len + 1 ) , str_len , 0 ) ;
2002-08-02 10:08:53 +00:00
}
/* }}} */
# endif
2003-02-07 21:36:18 +00:00
/* {{{ proto array str_split(string str [, int split_length])
Convert a string to an array . If split_length is specified , break the string down into chunks each split_length characters long . */
2003-02-08 15:26:17 +00:00
PHP_FUNCTION ( str_split )
{
2003-02-07 21:36:18 +00:00
char * str ;
int str_len ;
long split_length = 1 ;
char * p ;
int n_reg_segments ;
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " s|l " , & str , & str_len , & split_length ) = = FAILURE ) {
return ;
}
if ( split_length < = 0 ) {
2004-11-21 13:35:32 +00:00
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " The length of each segment must be greater than zero. " ) ;
2003-02-07 21:36:18 +00:00
RETURN_FALSE ;
}
array_init ( return_value ) ;
2004-10-20 22:44:43 +00:00
if ( split_length > = str_len ) {
add_next_index_stringl ( return_value , str , str_len , 1 ) ;
return ;
}
2003-02-07 21:36:18 +00:00
n_reg_segments = floor ( str_len / split_length ) ;
p = str ;
while ( n_reg_segments - - > 0 ) {
add_next_index_stringl ( return_value , p , split_length , 1 ) ;
p + = split_length ;
}
if ( p ! = ( str + str_len ) ) {
add_next_index_stringl ( return_value , p , ( str + str_len - p ) , 1 ) ;
}
}
/* }}} */
2003-02-11 22:47:26 +00:00
/* {{{ proto array strpbrk(string haystack, string char_list)
Search a string for any of a set of characters */
PHP_FUNCTION ( strpbrk )
{
2005-10-22 13:25:00 +00:00
void * haystack , * char_list ;
int32_t haystack_len , char_list_len ;
zend_uchar haystack_type , char_list_type ;
void * p = NULL ;
2003-02-11 22:47:26 +00:00
2005-10-22 13:25:00 +00:00
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " TT " ,
& haystack , & haystack_len , & haystack_type ,
& char_list , & char_list_len , & char_list_type ) = = FAILURE ) {
2003-02-11 22:47:26 +00:00
RETURN_FALSE ;
}
if ( ! char_list_len ) {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " The character list cannot be empty. " ) ;
RETURN_FALSE ;
}
2005-10-22 13:25:00 +00:00
if ( haystack_type = = IS_UNICODE ) {
int32_t i , j ;
UChar32 ch1 , ch2 ;
for ( i = 0 ; i < haystack_len ; ) {
U16_NEXT ( ( UChar * ) haystack , i , haystack_len , ch1 ) ;
for ( j = 0 ; j < char_list_len ; ) {
U16_NEXT ( ( UChar * ) char_list , j , char_list_len , ch2 ) ;
if ( ch1 = = ch2 ) {
U16_BACK_1 ( ( UChar * ) haystack , 0 , i ) ;
p = ( UChar * ) haystack + i ;
break ;
}
}
if ( ch1 = = ch2 ) {
break ;
}
}
} else {
p = strpbrk ( ( char * ) haystack , ( char * ) char_list ) ;
}
if ( p ) {
if ( haystack_type = = IS_UNICODE ) {
RETURN_UNICODEL ( ( UChar * ) p , ( ( UChar * ) haystack + haystack_len - ( UChar * ) p ) , 1 ) ;
} else if ( haystack_type = = IS_BINARY ) {
RETURN_BINARYL ( ( char * ) p , ( ( char * ) haystack + haystack_len - ( char * ) p ) , 1 ) ;
} else {
RETURN_STRINGL ( ( char * ) p , ( ( char * ) haystack + haystack_len - ( char * ) p ) , 1 ) ;
}
2003-02-11 22:47:26 +00:00
} else {
RETURN_FALSE ;
}
}
/* }}} */
2003-02-07 21:36:18 +00:00
2003-10-30 00:49:33 +00:00
/* {{{ proto int substr_compare(string main_str, string str, int offset [, int length [, bool case_sensitivity]])
Binary safe optionally case insensitive comparison of 2 strings from an offset , up to length characters */
PHP_FUNCTION ( substr_compare )
{
char * s1 , * s2 ;
int s1_len , s2_len ;
long offset , len = 0 ;
zend_bool cs = 0 ;
uint cmp_len ;
if ( zend_parse_parameters ( ZEND_NUM_ARGS ( ) TSRMLS_CC , " ssl|lb " , & s1 , & s1_len , & s2 , & s2_len , & offset , & len , & cs ) = = FAILURE ) {
RETURN_FALSE ;
}
2005-09-26 23:08:10 +00:00
if ( offset < 0 ) { /* negative offset, start comparison at the end of string */
offset = s1_len + offset ;
}
2003-10-30 00:49:33 +00:00
if ( len & & offset > = s1_len ) {
php_error_docref ( NULL TSRMLS_CC , E_WARNING , " The start position cannot exceed initial string length. " ) ;
RETURN_FALSE ;
}
cmp_len = ( uint ) ( len ? len : MAX ( s2_len , ( s1_len - offset ) ) ) ;
if ( ! cs ) {
RETURN_LONG ( zend_binary_strncmp ( s1 + offset , ( s1_len - offset ) , s2 , s2_len , cmp_len ) ) ;
} else {
RETURN_LONG ( zend_binary_strncasecmp ( s1 + offset , ( s1_len - offset ) , s2 , s2_len , cmp_len ) ) ;
}
}
/* }}} */
2000-06-01 10:07:44 +00:00
/*
* Local variables :
* tab - width : 4
* c - basic - offset : 4
* End :
2001-09-09 12:55:48 +00:00
* vim600 : noet sw = 4 ts = 4 fdm = marker
* vim < 600 : noet sw = 4 ts = 4
2000-06-01 10:07:44 +00:00
*/