1999-04-17 00:37:12 +00:00
/*
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
1999-07-16 13:13:16 +00:00
| PHP version 4.0 |
1999-04-17 00:37:12 +00:00
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
2001-02-26 06:11:02 +00:00
| Copyright ( c ) 1997 - 2001 The PHP Group |
1999-04-17 00:37:12 +00:00
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
2000-05-18 15:34:45 +00:00
| This source file is subject to version 2.02 of the PHP license , |
1999-07-16 13:13:16 +00:00
| that is bundled with this package in the file LICENSE , and is |
| available at through the world - wide - web at |
2000-05-18 15:34:45 +00:00
| http : //www.php.net/license/2_02.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 > |
* 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
| Stig S <EFBFBD> ther Bakken < ssb @ fast . no > |
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"
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
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
2000-09-11 13:52:22 +00:00
# define STR_PAD_LEFT 0
# define STR_PAD_RIGHT 1
# define STR_PAD_BOTH 2
# define PHP_PATHINFO_DIRNAME 0
# define PHP_PATHINFO_BASENAME 1
# define PHP_PATHINFO_EXTENSION 2
2000-07-08 20:38:23 +00:00
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
{
2000-03-26 17:06:19 +00:00
unsigned char * result = NULL ;
1999-08-12 10:53:29 +00:00
size_t i , j ;
1999-05-16 11:12:23 +00:00
2001-02-22 10:36:40 +00:00
result = ( char * ) emalloc ( oldlen * 2 * sizeof ( char ) + 1 ) ;
2000-03-26 17:06:19 +00:00
if ( ! result ) {
return result ;
1999-05-16 11:12:23 +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
if ( newlen ) * newlen = oldlen * 2 * sizeof ( char ) ;
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 */
2001-01-14 16:36:30 +00:00
struct lconv * localeconv_r ( struct lconv * out )
{
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 )
{
1999-12-23 14:59:01 +00:00
zval * * data ;
2000-03-26 17:06:19 +00:00
char * result ;
1999-05-16 11:12:23 +00:00
size_t newlen ;
2000-06-05 19:47:54 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( 1 , & data ) = = FAILURE ) {
1999-05-16 11:12:23 +00:00
WRONG_PARAM_COUNT ;
}
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( data ) ;
1999-05-16 11:12:23 +00:00
2000-03-26 17:06:19 +00:00
result = php_bin2hex ( ( * data ) - > value . str . val , ( * data ) - > value . str . len , & newlen ) ;
1999-05-16 11:12:23 +00:00
2000-03-26 17:06:19 +00:00
if ( ! result ) {
1999-05-16 11:12:23 +00:00
RETURN_FALSE ;
}
2000-03-26 17:06:19 +00:00
RETURN_STRINGL ( result , newlen , 0 ) ;
1999-05-16 11:12:23 +00:00
}
2000-02-24 10:11:42 +00:00
/* }}} */
1999-04-17 00:37:12 +00:00
/* {{{ proto int strspn(string str, string mask)
Find length of initial segment consisting entirely of characters found in mask */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( strspn )
1999-04-17 00:37:12 +00:00
{
2001-08-11 17:03:37 +00:00
zval * * s1 , * * s2 ;
1999-04-17 00:37:12 +00:00
2000-06-05 19:47:54 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 2 | | zend_get_parameters_ex ( 2 , & s1 , & s2 ) = = FAILURE ) {
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
}
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( s1 ) ;
convert_to_string_ex ( s2 ) ;
1999-12-13 23:40:36 +00:00
RETURN_LONG ( php_strspn ( ( * s1 ) - > value . str . val , ( * s2 ) - > value . str . val ,
( * s1 ) - > value . str . val + ( * s1 ) - > value . str . len ,
( * s2 ) - > value . str . val + ( * s2 ) - > value . str . len ) ) ;
1999-04-17 00:37:12 +00:00
}
/* }}} */
/* {{{ proto int strcspn(string str, string mask)
Find length of initial segment consisting entirely of characters not found in mask */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( strcspn )
1999-04-17 00:37:12 +00:00
{
2001-08-11 17:03:37 +00:00
zval * * s1 , * * s2 ;
1999-04-17 00:37:12 +00:00
2000-06-05 19:47:54 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 2 | | zend_get_parameters_ex ( 2 , & s1 , & s2 ) = = FAILURE ) {
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
}
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( s1 ) ;
convert_to_string_ex ( s2 ) ;
1999-12-13 23:40:36 +00:00
RETURN_LONG ( php_strcspn ( ( * s1 ) - > value . str . val , ( * s2 ) - > value . str . val ,
( * s1 ) - > value . str . val + ( * s1 ) - > value . str . len ,
( * s2 ) - > value . str . val + ( * s2 ) - > value . str . len ) ) ;
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-07-04 10:10:30 +00:00
PHP_FUNCTION ( nl_langinfo )
{
zval * * item ;
char * value ;
if ( ZEND_NUM_ARGS ( ) ! = 1 | | zend_get_parameters_ex ( 1 , & item ) = = FAILURE ) {
WRONG_PARAM_COUNT ;
}
convert_to_long_ex ( item ) ;
value = nl_langinfo ( Z_LVAL_PP ( item ) ) ;
if ( value = = NULL ) {
RETURN_FALSE ;
}
else {
RETURN_STRING ( value , 1 ) ;
}
}
# endif
2001-01-17 01:10:50 +00:00
# ifdef HAVE_STRCOLL
/* {{{ proto int strcoll(string str1, string str2)
Compare two strings using the current locale */
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 ) ;
RETURN_LONG ( strcoll ( ( const char * ) ( * s1 ) - > value . str . val , ( const char * ) ( * s2 ) - > value . str . val ) ) ;
}
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
*/
2001-08-05 20:25:40 +00:00
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 ) ;
for ( end = input + len ; input < end ; input + + ) {
c = * input ;
2001-08-05 19:38:49 +00:00
if ( input + 3 < end & & input [ 1 ] = = ' . ' & & input [ 2 ] = = ' . '
& & input [ 3 ] > = c ) {
memset ( mask + c , 1 , input [ 3 ] - c + 1 ) ;
2001-08-03 23:09:05 +00:00
input + = 3 ;
2001-08-05 19:38:49 +00:00
} 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 was no 'left' char */
2001-08-11 17:03:37 +00:00
php_error ( E_WARNING , " Invalid '..'-range passed to %s(), no character to the left of '..' " , get_active_function_name ( TSRMLS_C ) ) ;
2001-08-05 19:38:49 +00:00
result = FAILURE ;
continue ;
}
if ( input + 2 > = end ) { /* there is no 'right' char */
2001-08-11 17:03:37 +00:00
php_error ( E_WARNING , " Invalid '..'-range passed to %s(), no character to the right of '..' " , get_active_function_name ( TSRMLS_C ) ) ;
2001-08-05 19:38:49 +00:00
result = FAILURE ;
continue ;
}
if ( input [ - 1 ] > input [ 2 ] ) { /* wrong order */
2001-08-11 17:03:37 +00:00
php_error ( E_WARNING , " Invalid '..'-range passed to %s(), '..'-range needs to be incrementing " , get_active_function_name ( TSRMLS_C ) ) ;
2001-08-05 19:38:49 +00:00
result = FAILURE ;
continue ;
}
/* FIXME: better error (a..b..c is the only left possibility?) */
2001-08-11 17:03:37 +00:00
php_error ( E_WARNING , " Invalid '..'-range passed to %s() " , get_active_function_name ( TSRMLS_C ) ) ;
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
}
/* }}} */
2001-06-05 13:12:10 +00:00
/* {{{ php_trim
2001-08-03 23:09:05 +00:00
Compatibility function , ports old - API to new one . ( DEPRECATED )
*/
2001-08-06 03:50:52 +00:00
void php_trim ( zval * str , zval * return_value , int mode TSRMLS_DC )
2001-08-03 23:09:05 +00:00
{
2001-08-06 03:50:52 +00:00
php_trim2 ( str , NULL , return_value , mode TSRMLS_CC ) ;
2001-08-03 23:09:05 +00:00
}
/* }}} */
/* {{{ php_trim2
2001-06-05 13:12:10 +00:00
*/
2001-08-06 03:50:52 +00:00
PHPAPI void php_trim2 ( zval * str , zval * what , 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
/* mode 1 : trim left
mode 2 : trim right
mode 3 : trim left and right
2001-08-03 23:09:05 +00:00
what indicates which chars are to be trimmed . NULL - > default ( ' \ t \ n \ r \ v \ 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
*/
{
register int i ;
int len = str - > value . str . len ;
int trimmed = 0 ;
char * c = str - > value . str . val ;
2001-08-03 23:09:05 +00:00
char mask [ 256 ] ;
if ( what ) {
2001-08-05 20:25:40 +00:00
php_charmask ( what - > value . str . val , what - > value . str . 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 ;
}
}
}
RETVAL_STRINGL ( c , len , 1 ) ;
}
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
/* {{{ proto string rtrim(string str)
An alias for chop */
/* }}} */
1999-04-17 00:37:12 +00:00
/* {{{ proto string chop(string str)
Remove trailing whitespace */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( chop )
1999-04-17 00:37:12 +00:00
{
2001-08-03 23:09:05 +00:00
zval * * str , * * what ;
1999-04-17 00:37:12 +00:00
2001-08-03 23:09:05 +00:00
if ( ZEND_NUM_ARGS ( ) < 1 | | ZEND_NUM_ARGS ( ) > 2 )
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
2001-08-03 23:10:04 +00:00
zend_get_parameters_ex ( ZEND_NUM_ARGS ( ) , & str , & what ) ;
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( str ) ;
2001-08-03 23:09:05 +00:00
if ( ZEND_NUM_ARGS ( ) = = 2 )
convert_to_string_ex ( str ) ;
2001-08-03 22:12:46 +00:00
2001-08-03 23:09:05 +00:00
/* convert_to_string_ex never fails (last line: op->type = IS_STRING),
so , not checking for that . */
2001-08-06 03:50:52 +00:00
php_trim2 ( * str , ZEND_NUM_ARGS ( ) = = 2 ? * what : NULL , return_value , 2 TSRMLS_CC ) ;
1999-04-17 00:37:12 +00:00
}
/* }}} */
/* {{{ proto string trim(string str)
Strip 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
{
2001-08-03 23:09:05 +00:00
zval * * str , * * what ;
1999-04-17 00:37:12 +00:00
2001-08-03 23:09:05 +00:00
if ( ZEND_NUM_ARGS ( ) < 1 | | ZEND_NUM_ARGS ( ) > 2 )
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
2001-08-03 23:10:04 +00:00
zend_get_parameters_ex ( ZEND_NUM_ARGS ( ) , & str , & what ) ;
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( str ) ;
2001-08-03 23:09:05 +00:00
if ( ZEND_NUM_ARGS ( ) = = 2 )
convert_to_string_ex ( str ) ;
1999-04-17 00:37:12 +00:00
2001-08-06 03:50:52 +00:00
php_trim2 ( * str , ZEND_NUM_ARGS ( ) = = 2 ? * what : NULL , return_value , 3 TSRMLS_CC ) ;
1999-04-17 00:37:12 +00:00
}
/* }}} */
/* {{{ proto string ltrim(string str)
Strip 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
{
2001-08-03 23:09:05 +00:00
zval * * str , * * what ;
1999-04-17 00:37:12 +00:00
2001-08-03 23:09:05 +00:00
if ( ZEND_NUM_ARGS ( ) < 1 | | ZEND_NUM_ARGS ( ) > 2 )
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
2001-08-03 23:10:04 +00:00
zend_get_parameters_ex ( ZEND_NUM_ARGS ( ) , & str , & what ) ;
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( str ) ;
2001-08-03 23:09:05 +00:00
if ( ZEND_NUM_ARGS ( ) = = 2 )
convert_to_string_ex ( str ) ;
2001-08-06 03:50:52 +00:00
php_trim2 ( * str , ZEND_NUM_ARGS ( ) = = 2 ? * what : NULL , return_value , 1 TSRMLS_CC ) ;
1999-04-17 00:37:12 +00:00
}
/* }}} */
2000-09-11 20:06:24 +00:00
/* {{{ proto string wordwrap(string str [, int width [, string break [, int cut]]])
2000-08-06 14:36:10 +00:00
Wrap buffer to selected number of characters using string break char */
2000-07-22 01:12:24 +00:00
PHP_FUNCTION ( wordwrap )
{
2000-09-11 20:06:24 +00:00
pval * * ptext , * * plinelength , * * pbreakchar , * * cut ;
long i = 0 , l = 0 , pgr = 0 , linelength = 0 , last = 0 , breakcharlen , docut = 0 ;
2000-07-22 01:12:24 +00:00
char * text , * breakchar , * newtext ;
2000-09-11 20:06:24 +00:00
if ( ZEND_NUM_ARGS ( ) < 1 | | ZEND_NUM_ARGS ( ) > 4 | | zend_get_parameters_ex ( ZEND_NUM_ARGS ( ) , & ptext , & plinelength , & pbreakchar , & cut ) = = FAILURE ) {
2000-07-22 01:12:24 +00:00
WRONG_PARAM_COUNT ;
}
convert_to_string_ex ( ptext ) ;
2000-12-17 02:09:07 +00:00
if ( Z_STRVAL_PP ( ptext ) = = 0 )
RETVAL_FALSE ;
2000-07-22 01:12:24 +00:00
text = ( * ptext ) - > value . str . val ;
if ( ZEND_NUM_ARGS ( ) > 1 ) {
convert_to_long_ex ( plinelength ) ;
linelength = ( * plinelength ) - > value . lval ;
}
else {
linelength = 75 ;
}
if ( ZEND_NUM_ARGS ( ) > 2 ) {
convert_to_string_ex ( pbreakchar ) ;
breakchar = ( * pbreakchar ) - > value . str . val ;
breakcharlen = ( * pbreakchar ) - > value . str . len ;
}
else {
breakchar = " \n " ;
breakcharlen = 1 ;
}
2000-09-11 20:06:24 +00:00
if ( ZEND_NUM_ARGS ( ) > 3 ) {
convert_to_long_ex ( cut ) ;
docut = ( * cut ) - > value . lval ;
}
2000-07-22 01:12:24 +00:00
/* Special case for a single-character break as it needs no
additional storage space */
2000-09-11 20:06:24 +00:00
if ( breakcharlen = = 1 & & docut = = 0 ) {
newtext = estrdup ( text ) ;
while ( newtext [ i ] ! = ' \0 ' ) {
2000-07-22 01:12:24 +00:00
/* prescan line to see if it is greater than linelength */
l = 0 ;
2000-09-11 20:06:24 +00:00
while ( newtext [ i + l ] ! = breakchar [ 0 ] ) {
if ( newtext [ i + l ] = = ' \0 ' ) {
2001-02-26 15:49:38 +00:00
l - - ;
2000-07-22 01:12:24 +00:00
break ;
}
l + + ;
}
2000-12-01 01:26:42 +00:00
if ( l > = linelength ) {
2000-07-22 01:12:24 +00:00
pgr = l ;
l = linelength ;
/* needs breaking; work backwards to find previous word */
while ( l > = 0 ) {
2000-09-11 20:06:24 +00:00
if ( newtext [ i + l ] = = ' ' ) {
newtext [ i + l ] = breakchar [ 0 ] ;
2000-07-22 01:12:24 +00:00
break ;
}
2001-02-26 15:49:38 +00:00
l - - ;
2000-07-22 01:12:24 +00:00
}
if ( l = = - 1 ) {
/* couldn't break is backwards, try looking forwards */
l = linelength ;
while ( l < = pgr ) {
2000-09-11 20:06:24 +00:00
if ( newtext [ i + l ] = = ' ' ) {
newtext [ i + l ] = breakchar [ 0 ] ;
2000-07-22 01:12:24 +00:00
break ;
}
2001-02-26 15:49:38 +00:00
l + + ;
2000-07-22 01:12:24 +00:00
}
}
}
i + = l + 1 ;
}
2000-09-11 20:06:24 +00:00
RETVAL_STRINGL ( newtext , strlen ( newtext ) , 1 ) ;
efree ( newtext ) ;
2000-07-22 01:12:24 +00:00
}
else {
/* Multiple character line break */
2001-02-18 18:31:31 +00:00
newtext = emalloc ( ( * ptext ) - > value . str . len * ( breakcharlen + 1 ) + 1 ) ;
2000-07-22 01:12:24 +00:00
newtext [ 0 ] = ' \0 ' ;
i = 0 ;
while ( text [ i ] ! = ' \0 ' ) {
/* prescan line to see if it is greater than linelength */
l = 0 ;
while ( text [ i + l ] ! = ' \0 ' ) {
if ( text [ i + l ] = = breakchar [ 0 ] ) {
if ( breakcharlen = = 1 | | strncmp ( text + i + l , breakchar , breakcharlen ) = = 0 )
break ;
}
2001-02-26 15:49:38 +00:00
l + + ;
2000-07-22 01:12:24 +00:00
}
2000-12-01 01:55:31 +00:00
if ( l > = linelength ) {
2000-07-22 01:12:24 +00:00
pgr = l ;
l = linelength ;
/* needs breaking; work backwards to find previous word */
while ( l > = 0 ) {
if ( text [ i + l ] = = ' ' ) {
strncat ( newtext , text + last , i + l - last ) ;
strcat ( newtext , breakchar ) ;
last = i + l + 1 ;
break ;
}
2001-02-26 15:49:38 +00:00
l - - ;
2000-07-22 01:12:24 +00:00
}
if ( l = = - 1 ) {
/* couldn't break it backwards, try looking forwards */
2001-01-18 21:46:24 +00:00
l = linelength - 1 ;
2000-07-22 01:12:24 +00:00
while ( l < = pgr ) {
2000-09-11 20:06:24 +00:00
if ( docut = = 0 )
{
if ( text [ i + l ] = = ' ' ) {
strncat ( newtext , text + last , i + l - last ) ;
strcat ( newtext , breakchar ) ;
last = i + l + 1 ;
break ;
}
}
if ( docut = = 1 )
{
if ( text [ i + l ] = = ' ' | | l > i - last ) {
2001-01-18 21:46:24 +00:00
strncat ( newtext , text + last , i + l - last + 1 ) ;
2000-09-11 20:06:24 +00:00
strcat ( newtext , breakchar ) ;
2001-01-18 21:46:24 +00:00
last = i + l + 1 ;
2000-09-11 20:06:24 +00:00
break ;
}
2000-07-22 01:12:24 +00:00
}
2001-02-26 15:49:38 +00:00
l + + ;
2000-07-22 01:12:24 +00:00
}
}
i + = l + 1 ;
}
else {
i + = ( l ? l : 1 ) ;
}
}
if ( i + l > last ) {
strcat ( newtext , text + last ) ;
}
RETVAL_STRINGL ( newtext , strlen ( newtext ) , 1 ) ;
efree ( newtext ) ;
}
}
/* }}} */
2001-06-05 13:12:10 +00:00
/* {{{ php_explode
*/
2000-05-23 05:04:28 +00:00
PHPAPI void php_explode ( zval * delim , zval * str , 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
1999-12-06 18:29:22 +00:00
endp = str - > value . str . val + str - > value . str . len ;
p1 = str - > value . str . val ;
p2 = php_memnstr ( str - > value . str . val , delim - > value . str . val , delim - > value . str . len , endp ) ;
1999-04-17 00:37:12 +00:00
if ( p2 = = NULL ) {
2000-05-23 12:38:12 +00:00
add_next_index_stringl ( return_value , p1 , str - > value . str . len , 1 ) ;
1999-12-07 08:14:00 +00:00
} else {
do {
2000-05-23 12:38:12 +00:00
add_next_index_stringl ( return_value , p1 , p2 - p1 , 1 ) ;
1999-12-07 08:14:00 +00:00
p1 = p2 + delim - > value . str . len ;
2000-05-23 12:38:12 +00:00
} while ( ( p2 = php_memnstr ( p1 , delim - > value . str . val , delim - > value . str . len , endp ) ) ! = NULL & &
( limit = = - 1 | | - - limit > 1 ) ) ;
1999-12-07 08:14:00 +00:00
2000-05-23 12:38:12 +00:00
if ( p1 < = endp )
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
2000-05-23 05:04:28 +00:00
/* {{{ proto array explode(string separator, string str [, int limit])
* 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
Split a string on string separator and return array of components */
PHP_FUNCTION ( explode )
1999-04-17 00:37:12 +00:00
{
2000-05-23 05:04:28 +00:00
zval * * str , * * delim , * * zlimit = NULL ;
int limit ;
2000-06-05 19:47:54 +00:00
switch ( ZEND_NUM_ARGS ( ) ) {
2000-05-23 05:04:28 +00:00
case 2 :
if ( zend_get_parameters_ex ( 2 , & delim , & str ) = = FAILURE )
WRONG_PARAM_COUNT ;
limit = - 1 ;
break ;
case 3 :
if ( zend_get_parameters_ex ( 3 , & delim , & str , & zlimit ) = = FAILURE )
2000-05-20 05:07:53 +00:00
WRONG_PARAM_COUNT ;
2000-05-23 05:04:28 +00:00
convert_to_long_ex ( zlimit ) ;
limit = ( * zlimit ) - > value . lval ;
break ;
default :
WRONG_PARAM_COUNT ;
2000-05-20 05:07:53 +00:00
}
1999-12-06 18:29:22 +00:00
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( str ) ;
convert_to_string_ex ( delim ) ;
1999-04-17 00:37:12 +00:00
1999-12-06 18:29:22 +00:00
if ( ! ( * delim ) - > value . str . len ) {
2001-08-11 17:03:37 +00:00
php_error ( 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
* 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 ( array_init ( return_value ) = = FAILURE ) {
1999-12-06 18:29:22 +00:00
RETURN_FALSE ;
1999-04-17 00:37:12 +00:00
}
1999-12-06 18:29:22 +00:00
2000-05-23 12:38:12 +00:00
if ( limit = = 0 | | limit = = 1 ) {
2000-05-23 05:04:28 +00:00
add_index_stringl ( return_value , 0 , ( * str ) - > value . str . val , ( * str ) - > value . str . len , 1 ) ;
} else {
php_explode ( * delim , * str , return_value , limit ) ;
}
* 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
*/
1999-12-23 14:59:01 +00:00
PHPAPI void php_implode ( zval * delim , zval * arr , zval * return_value )
* 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-12-23 14:59:01 +00:00
zval * * tmp ;
1999-12-11 20:00:40 +00:00
int len = 0 , count = 0 , target = 0 ;
2000-06-18 10:37:01 +00:00
HashPosition pos ;
1999-04-17 00:37:12 +00:00
/* convert everything to strings, and calculate length */
2000-06-18 10:37:01 +00:00
zend_hash_internal_pointer_reset_ex ( arr - > value . ht , & pos ) ;
while ( zend_hash_get_current_data_ex ( arr - > value . ht , ( void * * ) & tmp , & pos ) = = SUCCESS ) {
1999-10-15 14:45:54 +00:00
convert_to_string_ex ( tmp ) ;
1999-12-11 20:00:40 +00:00
len + = ( * tmp ) - > value . str . len ;
if ( count > 0 ) {
len + = delim - > value . str . len ;
1999-04-17 00:37:12 +00:00
}
1999-12-11 20:00:40 +00:00
count + + ;
2000-06-18 10:37:01 +00:00
zend_hash_move_forward_ex ( arr - > value . ht , & pos ) ;
1999-04-17 00:37:12 +00:00
}
/* do it */
return_value - > value . str . val = ( char * ) emalloc ( len + 1 ) ;
return_value - > value . str . val [ 0 ] = ' \0 ' ;
return_value - > value . str . val [ len ] = ' \0 ' ;
2000-06-18 10:37:01 +00:00
zend_hash_internal_pointer_reset_ex ( arr - > value . ht , & pos ) ;
while ( zend_hash_get_current_data_ex ( arr - > value . ht , ( void * * ) & tmp , & pos ) = = SUCCESS ) {
1999-12-11 20:00:40 +00:00
count - - ;
memcpy ( return_value - > value . str . val + target , ( * tmp ) - > value . str . val ,
( * tmp ) - > value . str . len ) ;
target + = ( * tmp ) - > value . str . len ;
if ( count > 0 ) {
memcpy ( return_value - > value . str . val + target , delim - > value . str . val ,
delim - > value . str . len ) ;
target + = delim - > value . str . len ;
1999-04-17 00:37:12 +00:00
}
2000-06-18 10:37:01 +00:00
zend_hash_move_forward_ex ( arr - > value . ht , & pos ) ;
1999-04-17 00:37:12 +00:00
}
return_value - > type = IS_STRING ;
return_value - > value . str . len = len ;
}
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
/* {{{ proto string implode(array src, string glue)
Join array elements placing glue string between items and return one string */
PHP_FUNCTION ( implode )
{
1999-12-23 14:59:01 +00:00
zval * * arg1 , * * arg2 , * delim , * arr ;
* 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-06-05 19:47:54 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 2 | | zend_get_parameters_ex ( 2 , & arg1 , & arg2 ) = = 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 ;
}
2000-08-18 13:43:22 +00:00
if ( ( * arg1 ) - > type = = IS_ARRAY ) {
1999-10-15 14:45:54 +00:00
SEPARATE_ZVAL ( arg1 ) ;
1999-09-25 11:55:42 +00:00
arr = * arg1 ;
2000-08-18 13:22:09 +00:00
convert_to_string_ex ( arg2 ) ;
1999-09-25 11:55:42 +00:00
delim = * arg2 ;
} else if ( ( * arg2 ) - > type = = IS_ARRAY ) {
1999-10-15 14:53:56 +00:00
SEPARATE_ZVAL ( arg2 )
1999-09-25 11:55:42 +00:00
arr = * arg2 ;
1999-10-15 14:53:56 +00:00
convert_to_string_ex ( arg1 ) ;
1999-09-25 11:55:42 +00:00
delim = * arg1 ;
* 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 {
1999-08-02 19:17:14 +00:00
php_error ( E_WARNING , " Bad arguments to %s() " ,
2001-07-30 08:24:42 +00:00
get_active_function_name ( TSRMLS_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
return ;
}
1999-10-16 16:35:20 +00:00
php_implode ( delim , arr , return_value ) ;
* 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]
2001-08-11 17:03:37 +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
{
2001-08-03 23:09:05 +00:00
zval * * args [ 2 ] ;
zval * * tok , * * str ;
char * token ;
char * token_end ;
char * p ;
char * pe ;
1999-04-17 00:37:12 +00:00
2001-08-03 23:09:05 +00:00
if ( ZEND_NUM_ARGS ( ) < 1 | | ZEND_NUM_ARGS ( ) > 2 | |
zend_get_parameters_array_ex ( ZEND_NUM_ARGS ( ) , args ) = = FAILURE )
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
2001-08-03 23:09:05 +00:00
switch ( ZEND_NUM_ARGS ( ) ) {
case 1 :
tok = args [ 0 ] ;
break ;
case 2 :
str = args [ 0 ] ;
tok = args [ 1 ] ;
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( str ) ;
1999-04-17 00:37:12 +00:00
2001-08-03 23:09:05 +00:00
zval_add_ref ( str ) ;
if ( BG ( strtok_zval ) )
2001-08-04 16:53:57 +00:00
zval_ptr_dtor ( & BG ( strtok_zval ) ) ;
BG ( strtok_zval ) = * str ;
2001-08-03 23:09:05 +00:00
BG ( strtok_last ) = BG ( strtok_string ) = Z_STRVAL_PP ( str ) ;
BG ( strtok_len ) = Z_STRLEN_PP ( str ) ;
break ;
2001-08-03 08:49:19 +00:00
}
2001-08-03 23:09:05 +00:00
p = BG ( strtok_last ) ; /* Where we start to search */
pe = BG ( strtok_string ) + BG ( strtok_len ) ;
2001-08-03 08:49:19 +00:00
2001-08-03 23:09:05 +00:00
if ( ! p | | p > = pe )
RETURN_FALSE ;
convert_to_string_ex ( tok ) ;
token = Z_STRVAL_PP ( tok ) ;
token_end = token + Z_STRLEN_PP ( tok ) ;
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 ;
2001-08-03 22:42:02 +00:00
}
2001-08-03 23:09:05 +00:00
/* 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 - BG ( strtok_last ) ) {
return_token :
RETVAL_STRINGL ( BG ( strtok_last ) , p - BG ( strtok_last ) , 1 ) ;
BG ( strtok_last ) = p + 1 ;
1999-04-17 00:37:12 +00:00
} else {
RETVAL_FALSE ;
2001-08-03 23:09:05 +00:00
BG ( strtok_last ) = NULL ;
1999-04-17 00:37:12 +00:00
}
2001-08-03 23:09:05 +00:00
/* Restore table -- usually faster then memset'ing the table
on every invocation */
restore :
token = Z_STRVAL_PP ( tok ) ;
while ( token < token_end )
STRTOK_TABLE ( token + + ) = 0 ;
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
{
char * c ;
int ch ;
1999-12-25 09:06:55 +00:00
size_t i ;
1999-04-17 00:37:12 +00:00
c = s ;
1999-12-09 23:45:30 +00:00
for ( i = 0 ; i < len ; i + + ) {
1999-04-17 00:37:12 +00:00
ch = toupper ( ( unsigned char ) * c ) ;
* c + + = ch ;
}
return ( s ) ;
}
2001-06-05 13:12:10 +00:00
/* }}} */
1999-04-17 00:37:12 +00:00
/* {{{ proto string strtoupper(string str)
Make 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 ;
}
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( arg ) ;
1999-04-17 00:37:12 +00:00
1999-09-27 18:00:30 +00:00
* return_value = * * arg ;
zval_copy_ctor ( return_value ) ;
1999-12-09 23:45:30 +00:00
php_strtoupper ( return_value - > value . str . val , return_value - > value . str . len ) ;
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
{
register int ch ;
char * c ;
1999-12-25 09:06:55 +00:00
size_t i ;
1999-04-17 00:37:12 +00:00
c = s ;
1999-12-09 23:45:30 +00:00
for ( i = 0 ; i < len ; i + + ) {
1999-04-17 00:37:12 +00:00
ch = tolower ( ( unsigned char ) * c ) ;
* c + + = ch ;
}
return ( s ) ;
}
2001-06-05 13:12:10 +00:00
/* }}} */
1999-04-17 00:37:12 +00:00
/* {{{ proto string strtolower(string str)
Make 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
char * ret ;
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 ;
}
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( str ) ;
1999-04-17 00:37:12 +00:00
1999-09-27 18:00:30 +00:00
* return_value = * * str ;
zval_copy_ctor ( return_value ) ;
1999-12-09 23:45:30 +00:00
ret = php_strtolower ( return_value - > value . str . val , return_value - > value . str . len ) ;
1999-04-17 00:37:12 +00:00
}
/* }}} */
2001-06-05 13:12:10 +00:00
/* {{{ php_basename
*/
2001-06-23 00:51:03 +00:00
PHPAPI char * php_basename ( char * s , size_t len , char * suffix , size_t sufflen )
1999-04-17 00:37:12 +00:00
{
2000-05-23 20:16:14 +00:00
char * ret = NULL , * c , * p = NULL , buf = ' \0 ' ;
c = s + len - 1 ;
2001-06-23 00:51:03 +00:00
/* do suffix removal as the unix command does */
if ( suffix & & ( len > sufflen ) ) {
2001-08-11 17:03:37 +00:00
if ( ! strncmp ( suffix , c - sufflen + 1 , sufflen ) ) {
2001-06-23 00:51:03 +00:00
c - = sufflen ;
* ( c + 1 ) = ' \0 ' ;
}
}
2000-05-23 20:16:14 +00:00
/* strip trailing slashes */
1999-04-17 00:37:12 +00:00
while ( * c = = ' / '
2000-02-11 15:59:30 +00:00
# ifdef PHP_WIN32
1999-04-17 00:37:12 +00:00
| | * c = = ' \\ '
# endif
)
c - - ;
2000-05-23 20:16:14 +00:00
if ( c < s + len - 1 ) {
buf = * ( c + 1 ) ; /* Save overwritten char */
* ( c + 1 ) = ' \0 ' ; /* overwrite char */
p = c + 1 ; /* Save pointer to overwritten char */
}
if ( ( c = strrchr ( s , ' / ' ) )
2000-02-11 15:59:30 +00:00
# ifdef PHP_WIN32
2000-05-23 20:16:14 +00:00
| | ( c = strrchr ( s , ' \\ ' ) )
1999-04-17 00:37:12 +00:00
# endif
) {
2000-05-23 20:16:14 +00:00
ret = estrdup ( c + 1 ) ;
1999-04-17 00:37:12 +00:00
} else {
2000-05-23 20:16:14 +00:00
ret = estrdup ( s ) ;
1999-04-17 00:37:12 +00:00
}
2000-05-23 20:16:14 +00:00
if ( buf ) * p = buf ;
return ( ret ) ;
}
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])
2000-05-23 20:16:14 +00:00
Return the filename component of the path */
PHP_FUNCTION ( basename )
{
2001-06-23 00:51:03 +00:00
zval * * str , * * suffix ;
2000-05-23 20:16:14 +00:00
char * ret ;
2001-06-23 00:51:03 +00:00
switch ( ZEND_NUM_ARGS ( ) ) {
case 2 :
if ( zend_get_parameters_ex ( 2 , & str , & suffix ) ) WRONG_PARAM_COUNT ;
convert_to_string_ex ( str ) ;
convert_to_string_ex ( suffix ) ;
break ;
case 1 :
if ( zend_get_parameters_ex ( 1 , & str ) ) WRONG_PARAM_COUNT ;
convert_to_string_ex ( str ) ;
suffix = NULL ;
break ;
default : WRONG_PARAM_COUNT ;
2000-05-23 20:16:14 +00:00
}
2001-06-23 00:51:03 +00:00
ret = php_basename ( Z_STRVAL_PP ( str )
, Z_STRLEN_PP ( str )
, ( suffix ) ? Z_STRVAL_PP ( suffix ) : NULL
, ( suffix ) ? Z_STRLEN_PP ( suffix ) : 0
) ;
2000-10-03 16:47:25 +00:00
RETVAL_STRING ( ret , 0 )
1999-04-17 00:37:12 +00:00
}
/* }}} */
2001-06-05 13:12:10 +00:00
/* {{{ php_dirname
*
* This function doesn ' t work with absolute paths in Win32 such as C : \ foo
2000-10-03 16:47:25 +00:00
* ( and it didn ' t before either ) . This needs to be fixed
*/
PHPAPI void php_dirname ( char * path , int len )
2000-10-03 14:43:04 +00:00
{
2000-10-03 16:47:25 +00:00
register char * end = path + len - 1 ;
1999-04-17 00:37:12 +00:00
2000-10-03 16:47:25 +00:00
if ( len < = 0 ) {
/* Illegal use of this function */
return ;
}
/* Strip trailing slashes */
while ( end > = path & & IS_SLASH ( * end ) ) {
end - - ;
}
if ( end < path ) {
/* The path only contained slashes */
path [ 0 ] = DEFAULT_SLASH ;
path [ 1 ] = ' \0 ' ;
return ;
}
/* Strip filename */
while ( end > = path & & ! IS_SLASH ( * end ) ) {
end - - ;
}
if ( end < path ) {
/* No slash found, therefore return '.' */
path [ 0 ] = ' . ' ;
path [ 1 ] = ' \0 ' ;
return ;
}
/* Strip slashes which came before the file name */
while ( end > = path & & IS_SLASH ( * end ) ) {
end - - ;
}
if ( end < path ) {
path [ 0 ] = DEFAULT_SLASH ;
path [ 1 ] = ' \0 ' ;
return ;
}
* ( end + 1 ) = ' \0 ' ;
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)
Return 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 ;
2000-09-10 05:59:16 +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 ) ;
2000-10-03 16:47:25 +00:00
ret = estrndup ( Z_STRVAL_PP ( str ) , Z_STRLEN_PP ( str ) ) ;
php_dirname ( ret , Z_STRLEN_PP ( str ) ) ;
RETVAL_STRING ( ret , 0 ) ;
1999-04-17 00:37:12 +00:00
}
/* }}} */
2000-09-10 05:59:16 +00:00
/* {{{ proto array pathinfo(string path)
Return information about a certain string */
PHP_FUNCTION ( pathinfo )
{
2000-09-11 13:52:22 +00:00
zval * * path , * * uopt , * tmp ;
2000-09-10 05:59:16 +00:00
char * ret ;
int argc = ZEND_NUM_ARGS ( ) , opt , len ;
if ( argc < 1 | | argc > 2 | |
2000-09-11 13:52:22 +00:00
zend_get_parameters_ex ( argc , & path , & uopt ) = = FAILURE ) {
2000-09-10 05:59:16 +00:00
WRONG_PARAM_COUNT ;
}
2000-09-11 13:52:22 +00:00
convert_to_string_ex ( path ) ;
len = Z_STRLEN_PP ( path ) ;
2000-09-10 05:59:16 +00:00
if ( argc > 1 ) {
convert_to_long_ex ( uopt ) ;
opt = Z_LVAL_PP ( uopt ) ;
2000-09-11 13:54:59 +00:00
if ( opt < PHP_PATHINFO_DIRNAME | | opt > PHP_PATHINFO_EXTENSION ) {
2000-09-11 13:52:22 +00:00
php_error ( E_WARNING , " Invalid option in call to %s() " ,
2001-07-30 08:24:42 +00:00
get_active_function_name ( TSRMLS_C ) ) ;
2000-09-11 13:52:22 +00:00
return ;
}
2000-09-10 05:59:16 +00:00
}
MAKE_STD_ZVAL ( tmp ) ;
array_init ( tmp ) ;
2000-11-22 04:15:27 +00:00
if ( argc < 2 | | opt = = PHP_PATHINFO_DIRNAME ) {
2000-09-11 13:52:22 +00:00
ret = estrndup ( Z_STRVAL_PP ( path ) , len ) ;
2000-09-10 05:59:16 +00:00
php_dirname ( ret , len ) ;
if ( * ret )
2000-09-11 13:52:22 +00:00
add_assoc_string ( tmp , " dirname " , ret , 1 ) ;
2000-09-10 05:59:16 +00:00
efree ( ret ) ;
}
2000-11-22 04:16:25 +00:00
if ( argc < 2 | | opt = = PHP_PATHINFO_BASENAME ) {
2001-06-23 00:51:03 +00:00
ret = php_basename ( Z_STRVAL_PP ( path ) , len , NULL , 0 ) ;
2000-09-11 13:52:22 +00:00
add_assoc_string ( tmp , " basename " , ret , 0 ) ;
}
2000-11-22 04:15:27 +00:00
if ( argc < 2 | | opt = = PHP_PATHINFO_EXTENSION ) {
2000-09-10 05:59:16 +00:00
char * p ;
int idx ;
2000-09-11 13:52:22 +00:00
p = strrchr ( Z_STRVAL_PP ( path ) , ' . ' ) ;
2001-04-13 18:22:13 +00:00
if ( p ) {
2000-09-11 13:52:22 +00:00
idx = p - Z_STRVAL_PP ( path ) ;
add_assoc_stringl ( tmp , " extension " , Z_STRVAL_PP ( path ) + idx + 1 , len - idx - 1 , 1 ) ;
2000-09-10 05:59:16 +00:00
}
}
2000-09-11 13:52:22 +00:00
if ( argc = = 2 ) {
2000-09-10 05:59:16 +00:00
zval * * element ;
zend_hash_get_current_data ( Z_ARRVAL_P ( tmp ) , ( void * * ) & element ) ;
* return_value = * * element ;
} else {
* return_value = * tmp ;
}
zval_copy_ctor ( return_value ) ;
2000-09-11 13:52:22 +00:00
zval_dtor ( tmp ) ;
efree ( tmp ) ;
2000-09-10 05:59:16 +00:00
}
/* }}} */
1999-04-17 00:37:12 +00:00
2001-06-05 13:12:10 +00:00
/* {{{ php_stristr
* case insensitve strstr */
1999-12-11 19:50:01 +00:00
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
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 :
for ( spanp = s2 ; p ! = s1_end & & spanp ! = s2_end ; )
if ( * spanp + + = = c ) {
c = * ( + + p ) ;
goto cont ;
}
return ( p - s1 ) ;
}
2001-06-05 13:12:10 +00:00
/* }}} */
1999-12-13 23:40:36 +00:00
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 {
if ( * spanp = = c | | p = = s1_end )
return ( p - s1 ) ;
} while ( spanp + + < s2_end ) ;
c = * ( + + p ) ;
}
/* NOTREACHED */
}
2001-06-05 13:12:10 +00:00
/* }}} */
1999-12-13 23:40:36 +00:00
1999-08-09 20:52:58 +00:00
/* {{{ proto string stristr(string haystack, string needle)
1999-04-17 00:37:12 +00:00
Find 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
{
1999-12-11 19:50:01 +00:00
zval * * haystack , * * needle ;
1999-04-17 00:37:12 +00:00
char * found = NULL ;
2000-03-30 14:34:46 +00:00
int found_offset ;
char * haystack_orig ;
1999-12-11 19:50:01 +00:00
char needle_char [ 2 ] ;
1999-04-17 00:37:12 +00:00
2000-06-05 19:47:54 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 2 | | zend_get_parameters_ex ( 2 , & haystack , & needle ) = =
1999-04-17 00:37:12 +00:00
FAILURE ) {
WRONG_PARAM_COUNT ;
}
2000-03-22 08:14:00 +00:00
SEPARATE_ZVAL ( haystack ) ;
SEPARATE_ZVAL ( needle ) ;
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( haystack ) ;
2000-03-30 14:34:46 +00:00
haystack_orig = estrndup ( ( * haystack ) - > value . str . val ,
( * haystack ) - > value . str . len ) ;
1999-04-17 00:37:12 +00:00
1999-12-11 19:50:01 +00:00
if ( ( * needle ) - > type = = IS_STRING ) {
if ( ( * needle ) - > value . str . len = = 0 ) {
2001-08-11 17:03:37 +00:00
php_error ( E_WARNING , " Empty delimiter " ) ;
2000-03-30 14:34:46 +00:00
efree ( haystack_orig ) ;
1999-12-11 19:50:01 +00:00
RETURN_FALSE ;
}
2000-03-22 08:14:00 +00:00
1999-12-11 19:50:01 +00:00
found = php_stristr ( ( * haystack ) - > value . str . val , ( * needle ) - > value . str . val ,
( * haystack ) - > value . str . len , ( * needle ) - > value . str . len ) ;
} else {
convert_to_long_ex ( needle ) ;
needle_char [ 0 ] = tolower ( ( char ) ( * needle ) - > value . lval ) ;
needle_char [ 1 ] = ' \0 ' ;
2000-03-22 08:14:00 +00:00
1999-12-11 19:50:01 +00:00
found = php_stristr ( ( * haystack ) - > value . str . val , needle_char ,
( * haystack ) - > value . str . len , 1 ) ;
1999-04-17 00:37:12 +00:00
}
if ( found ) {
2000-03-30 14:34:46 +00:00
found_offset = found - ( * haystack ) - > value . str . val ;
RETVAL_STRINGL ( haystack_orig + found_offset ,
( * haystack ) - > value . str . len - found_offset , 1 ) ;
1999-04-17 00:37:12 +00:00
} else {
RETVAL_FALSE ;
}
2000-03-30 14:34:46 +00:00
efree ( haystack_orig ) ;
1999-04-17 00:37:12 +00:00
}
/* }}} */
/* {{{ proto string strstr(string haystack, string needle)
Find 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
{
1999-12-11 19:50:01 +00:00
zval * * haystack , * * needle ;
char * haystack_end ;
1999-04-17 00:37:12 +00:00
char * found = NULL ;
1999-12-11 19:50:01 +00:00
char needle_char [ 2 ] ;
1999-04-17 00:37:12 +00:00
2000-06-05 19:47:54 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 2 | | zend_get_parameters_ex ( 2 , & haystack , & needle ) = =
1999-04-17 00:37:12 +00:00
FAILURE ) {
WRONG_PARAM_COUNT ;
}
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( haystack ) ;
1999-12-11 19:50:01 +00:00
haystack_end = ( * haystack ) - > value . str . val + ( * haystack ) - > value . str . len ;
1999-04-17 00:37:12 +00:00
1999-09-25 11:55:42 +00:00
if ( ( * needle ) - > type = = IS_STRING ) {
1999-12-11 19:50:01 +00:00
if ( ( * needle ) - > value . str . len = = 0 ) {
2001-08-11 17:03:37 +00:00
php_error ( E_WARNING , " Empty delimiter " ) ;
1999-04-17 00:37:12 +00:00
RETURN_FALSE ;
}
1999-12-11 19:50:01 +00:00
found = php_memnstr ( ( * haystack ) - > value . str . val , ( * needle ) - > value . str . val ,
( * needle ) - > value . str . len , haystack_end ) ;
1999-04-17 00:37:12 +00:00
} else {
1999-09-25 11:55:42 +00:00
convert_to_long_ex ( needle ) ;
1999-12-11 19:50:01 +00:00
needle_char [ 0 ] = ( char ) ( * needle ) - > value . lval ;
needle_char [ 1 ] = ' \0 ' ;
found = php_memnstr ( ( * haystack ) - > value . str . val , needle_char , 1 , haystack_end ) ;
1999-04-17 00:37:12 +00:00
}
if ( found ) {
1999-12-11 19:50:01 +00:00
RETVAL_STRING ( found , 1 ) ;
1999-04-17 00:37:12 +00:00
} else {
RETVAL_FALSE ;
}
}
/* }}} */
1999-08-09 20:52:58 +00:00
/* {{{ proto string strchr(string haystack, string needle)
An alias for strstr */
/* }}} */
* 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 int strpos(string haystack, string needle [, int offset])
1999-04-17 00:37:12 +00:00
Find 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
{
1999-12-23 14:59:01 +00:00
zval * * haystack , * * needle , * * OFFSET ;
1999-04-17 00:37:12 +00:00
int offset = 0 ;
char * found = NULL ;
1999-12-06 17:37:59 +00:00
char * endp ;
char * startp ;
1999-04-17 00:37:12 +00:00
2000-06-05 19:47:54 +00:00
switch ( ZEND_NUM_ARGS ( ) ) {
1999-04-17 00:37:12 +00:00
case 2 :
1999-12-18 22:40:35 +00:00
if ( zend_get_parameters_ex ( 2 , & haystack , & needle ) = = FAILURE ) {
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
}
break ;
case 3 :
1999-12-18 22:40:35 +00:00
if ( zend_get_parameters_ex ( 3 , & haystack , & needle , & OFFSET ) = = FAILURE ) {
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
}
1999-09-25 11:55:42 +00:00
convert_to_long_ex ( OFFSET ) ;
offset = ( * OFFSET ) - > value . lval ;
1999-12-06 17:37:59 +00:00
if ( offset < 0 ) {
2001-08-11 17:03:37 +00:00
php_error ( E_WARNING , " offset not contained in string " ) ;
1999-12-06 17:37:59 +00:00
RETURN_FALSE ;
}
1999-04-17 00:37:12 +00:00
break ;
default :
WRONG_PARAM_COUNT ;
}
1999-12-06 17:37:59 +00:00
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( haystack ) ;
1999-12-06 17:37:59 +00:00
1999-09-25 11:55:42 +00:00
if ( offset > ( * haystack ) - > value . str . len ) {
2001-08-11 17:03:37 +00:00
php_error ( E_WARNING , " offset not contained in string " ) ;
1999-04-17 00:37:12 +00:00
RETURN_FALSE ;
}
1999-12-06 17:37:59 +00:00
startp = ( * haystack ) - > value . str . val ;
startp + = offset ;
endp = ( * haystack ) - > value . str . val ;
endp + = ( * haystack ) - > value . str . len ;
1999-09-25 11:55:42 +00:00
if ( ( * needle ) - > type = = IS_STRING ) {
if ( ( * needle ) - > value . str . len = = 0 ) {
2001-08-11 17:03:37 +00:00
php_error ( E_WARNING , " Empty delimiter " ) ;
1999-04-17 00:37:12 +00:00
RETURN_FALSE ;
}
1999-12-06 17:37:59 +00:00
found = php_memnstr ( startp , ( * needle ) - > value . str . val , ( * needle ) - > value . str . len , endp ) ;
1999-04-17 00:37:12 +00:00
} else {
1999-12-06 17:37:59 +00:00
char buf ;
1999-09-25 11:55:42 +00:00
convert_to_long_ex ( needle ) ;
1999-12-06 17:37:59 +00:00
buf = ( char ) ( * needle ) - > value . lval ;
found = php_memnstr ( startp , & buf , 1 , endp ) ;
1999-04-17 00:37:12 +00:00
}
if ( found ) {
1999-09-25 11:55:42 +00:00
RETVAL_LONG ( found - ( * haystack ) - > value . str . val ) ;
1999-04-17 00:37:12 +00:00
} else {
RETVAL_FALSE ;
}
}
/* }}} */
/* {{{ proto int strrpos(string haystack, string needle)
2000-07-09 19:33:19 +00:00
Find position of last occurrence of a character in a string within another */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( strrpos )
1999-04-17 00:37:12 +00:00
{
1999-12-23 14:59:01 +00:00
zval * * haystack , * * needle ;
1999-04-17 00:37:12 +00:00
char * found = NULL ;
2000-06-05 19:47:54 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 2 | | zend_get_parameters_ex ( 2 , & haystack , & needle ) = = FAILURE ) {
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
}
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( haystack ) ;
1999-04-17 00:37:12 +00:00
1999-09-25 11:55:42 +00:00
if ( ( * needle ) - > type = = IS_STRING ) {
found = strrchr ( ( * haystack ) - > value . str . val , * ( * needle ) - > value . str . val ) ;
1999-04-17 00:37:12 +00:00
} else {
1999-09-25 11:55:42 +00:00
convert_to_long_ex ( needle ) ;
found = strrchr ( ( * haystack ) - > value . str . val , ( char ) ( * needle ) - > value . lval ) ;
1999-04-17 00:37:12 +00:00
}
if ( found ) {
1999-09-25 11:55:42 +00:00
RETVAL_LONG ( ( * haystack ) - > value . str . len - strlen ( found ) ) ;
1999-04-17 00:37:12 +00:00
} else {
RETVAL_FALSE ;
}
}
/* }}} */
/* {{{ proto string strrchr(string haystack, string needle)
Find 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
{
1999-12-23 14:59:01 +00:00
zval * * haystack , * * needle ;
1999-04-17 00:37:12 +00:00
char * found = NULL ;
2000-06-05 19:47:54 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 2 | | zend_get_parameters_ex ( 2 , & haystack , & needle ) = =
1999-04-17 00:37:12 +00:00
FAILURE ) {
WRONG_PARAM_COUNT ;
}
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( haystack ) ;
1999-04-17 00:37:12 +00:00
1999-09-25 11:55:42 +00:00
if ( ( * needle ) - > type = = IS_STRING ) {
found = strrchr ( ( * haystack ) - > value . str . val , * ( * needle ) - > value . str . val ) ;
1999-04-17 00:37:12 +00:00
} else {
1999-09-25 11:55:42 +00:00
convert_to_long_ex ( needle ) ;
found = strrchr ( ( * haystack ) - > value . str . val , ( * needle ) - > value . lval ) ;
1999-04-17 00:37:12 +00:00
}
if ( found ) {
2001-08-11 17:03:37 +00:00
RETVAL_STRING ( found , 1 ) ;
1999-04-17 00:37:12 +00:00
} else {
RETVAL_FALSE ;
}
}
/* }}} */
2001-06-05 13:12:10 +00:00
/* {{{ php_chunk_split
*/
1999-10-16 16:35:20 +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 */
dest = emalloc ( ( srclen + ( chunks + 1 ) * endlen + 1 ) * sizeof ( char ) ) ;
for ( p = src , q = dest ; p < ( src + srclen - chunklen + 1 ) ; ) {
memcpy ( q , p , chunklen ) ;
q + = chunklen ;
memcpy ( q , end , endlen ) ;
q + = endlen ;
p + = chunklen ;
}
if ( restlen ) {
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]])
Return 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
int argc ;
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 ;
1999-04-17 00:37:12 +00:00
2000-06-05 19:47:54 +00:00
argc = ZEND_NUM_ARGS ( ) ;
1999-06-15 21:51:00 +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 ( argc < 1 | | argc > 3 | |
1999-12-18 22:40:35 +00:00
zend_get_parameters_ex ( argc , & p_str , & p_chunklen , & p_ending ) = = FAILURE ) {
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
}
* 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
switch ( argc ) {
case 3 :
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( p_ending ) ;
end = ( * p_ending ) - > value . str . val ;
endlen = ( * p_ending ) - > value . str . len ;
1999-04-17 00:37:12 +00:00
case 2 :
1999-09-25 11:55:42 +00:00
convert_to_long_ex ( p_chunklen ) ;
chunklen = ( * p_chunklen ) - > value . lval ;
1999-04-17 00:37:12 +00:00
case 1 :
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( p_str ) ;
1999-04-17 00:37:12 +00:00
}
2000-08-17 08:47:42 +00:00
if ( chunklen < = 0 ) {
php_error ( 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
if ( ( * p_str ) - > value . str . len = = 0 ) {
RETURN_EMPTY_STRING ( ) ;
}
1999-10-16 16:35:20 +00:00
result = php_chunk_split ( ( * p_str ) - > value . str . val , ( * p_str ) - > value . str . len ,
end , endlen , chunklen , & result_len ) ;
1999-04-17 00:37:12 +00:00
if ( result ) {
* 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
RETVAL_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])
Return part of a string */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( substr )
1999-04-17 00:37:12 +00:00
{
2000-02-08 22:21:43 +00:00
zval * * str , * * from , * * len ;
1999-04-17 00:37:12 +00:00
int argc , l ;
int f ;
2000-06-05 19:47:54 +00:00
argc = ZEND_NUM_ARGS ( ) ;
1999-04-17 00:37:12 +00:00
2000-02-08 22:21:43 +00:00
if ( ( argc = = 2 & & zend_get_parameters_ex ( 2 , & str , & from ) = = FAILURE ) | |
( argc = = 3 & & zend_get_parameters_ex ( 3 , & str , & from , & len ) = = FAILURE ) | |
1999-04-17 00:37:12 +00:00
argc < 2 | | argc > 3 ) {
WRONG_PARAM_COUNT ;
}
2000-02-08 22:21:43 +00:00
convert_to_string_ex ( str ) ;
1999-09-25 11:55:42 +00:00
convert_to_long_ex ( from ) ;
f = ( * from ) - > value . lval ;
1999-04-17 00:37:12 +00:00
if ( argc = = 2 ) {
2000-02-08 22:21:43 +00:00
l = ( * str ) - > value . str . len ;
1999-04-17 00:37:12 +00:00
} else {
1999-09-25 11:55:42 +00:00
convert_to_long_ex ( len ) ;
l = ( * len ) - > value . lval ;
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 ) {
2000-02-08 22:21:43 +00:00
f = ( * str ) - > value . str . 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 ) {
2000-02-08 22:21:43 +00:00
l = ( ( * str ) - > value . str . len - f ) + l ;
1999-04-17 00:37:12 +00:00
if ( l < 0 ) {
l = 0 ;
}
}
2000-02-08 22:21:43 +00:00
if ( f > = ( int ) ( * str ) - > value . str . len ) {
1999-04-17 00:37:12 +00:00
RETURN_FALSE ;
}
2000-02-08 22:21:43 +00:00
if ( ( f + l ) > ( int ) ( * str ) - > value . str . len ) {
l = ( int ) ( * str ) - > value . str . len - f ;
1999-04-17 00:37:12 +00:00
}
1999-06-15 21:51:00 +00:00
2000-02-08 22:21:43 +00:00
RETVAL_STRINGL ( ( * str ) - > value . str . val + f , l , 1 ) ;
1999-04-17 00:37:12 +00:00
}
/* }}} */
1999-11-20 22:09:27 +00:00
/* {{{ proto string substr_replace(string str, string repl, int start [, int length])
1999-11-16 22:44:13 +00:00
Replace part of a string with another string */
PHP_FUNCTION ( substr_replace )
{
2000-02-08 22:21:43 +00:00
zval * * str ;
1999-11-16 22:44:13 +00:00
zval * * from ;
zval * * len ;
zval * * repl ;
char * result ;
int result_len ;
int argc ;
int l ;
int f ;
2000-06-05 19:47:54 +00:00
argc = ZEND_NUM_ARGS ( ) ;
1999-11-16 22:44:13 +00:00
2000-02-08 22:21:43 +00:00
if ( ( argc = = 3 & & zend_get_parameters_ex ( 3 , & str , & repl , & from ) = = FAILURE ) | |
( argc = = 4 & & zend_get_parameters_ex ( 4 , & str , & repl , & from , & len ) = = FAILURE ) | |
1999-11-20 22:09:27 +00:00
argc < 3 | | argc > 4 ) {
1999-11-16 22:44:13 +00:00
WRONG_PARAM_COUNT ;
}
2000-02-08 22:21:43 +00:00
convert_to_string_ex ( str ) ;
1999-11-20 22:09:27 +00:00
convert_to_string_ex ( repl ) ;
1999-11-16 22:44:13 +00:00
convert_to_long_ex ( from ) ;
f = ( * from ) - > value . lval ;
1999-11-20 22:09:27 +00:00
if ( argc = = 3 ) {
2000-02-08 22:21:43 +00:00
l = ( * str ) - > value . str . len ;
1999-11-16 22:44:13 +00:00
} else {
convert_to_long_ex ( len ) ;
l = ( * len ) - > value . lval ;
}
/* if "from" position is negative, count start position from the end
* of the string
*/
if ( f < 0 ) {
2000-02-08 22:21:43 +00:00
f = ( * str ) - > value . str . len + f ;
1999-11-16 22:44:13 +00:00
if ( f < 0 ) {
f = 0 ;
}
2000-11-06 19:41:30 +00:00
} else if ( f > ( int ) ( * str ) - > value . str . len )
f = ( int ) ( * str ) - > value . str . len ;
1999-11-16 22:44:13 +00:00
/* 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 ) {
2000-02-08 22:21:43 +00:00
l = ( ( * str ) - > value . str . len - f ) + l ;
1999-11-16 22:44:13 +00:00
if ( l < 0 ) {
l = 0 ;
}
}
2000-02-08 22:21:43 +00:00
if ( ( f + l ) > ( int ) ( * str ) - > value . str . len ) {
l = ( int ) ( * str ) - > value . str . len - f ;
1999-11-16 22:44:13 +00:00
}
2000-02-08 22:21:43 +00:00
result_len = ( * str ) - > value . str . len - l + ( * repl ) - > value . str . len ;
1999-11-16 22:44:13 +00:00
result = ( char * ) ecalloc ( result_len + 1 , sizeof ( char * ) ) ;
2000-02-08 22:21:43 +00:00
memcpy ( result , ( * str ) - > value . str . val , f ) ;
1999-12-09 23:45:30 +00:00
memcpy ( & result [ f ] , ( * repl ) - > value . str . val , ( * repl ) - > value . str . len ) ;
2000-02-08 22:21:43 +00:00
memcpy ( & result [ f + ( * repl ) - > value . str . len ] , ( * str ) - > value . str . val + f + l ,
( * str ) - > value . str . len - f - l ) ;
1999-11-16 22:44:13 +00:00
1999-12-09 23:45:30 +00:00
RETVAL_STRINGL ( result , result_len , 0 ) ;
1999-11-16 22:44:13 +00:00
}
/* }}} */
1999-04-17 00:37:12 +00:00
/* {{{ proto string quotemeta(string str)
Quote 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 ;
2000-06-05 19:47:54 +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 ;
}
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( arg ) ;
1999-04-17 00:37:12 +00:00
1999-09-25 11:55:42 +00:00
old = ( * arg ) - > value . str . val ;
1999-12-20 20:43:47 +00:00
old_end = ( * arg ) - > value . str . val + ( * arg ) - > value . str . len ;
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 ;
}
1999-09-25 11:55:42 +00:00
str = emalloc ( 2 * ( * arg ) - > value . str . len + 1 ) ;
1999-04-17 00:37:12 +00:00
1999-12-20 20:43:47 +00:00
for ( p = old , q = str ; p ! = old_end ; p + + ) {
c = * p ;
1999-04-17 00:37:12 +00:00
switch ( c ) {
case ' . ' :
case ' \\ ' :
case ' + ' :
case ' * ' :
case ' ? ' :
case ' [ ' :
case ' ^ ' :
case ' ] ' :
case ' $ ' :
case ' ( ' :
case ' ) ' :
* q + + = ' \\ ' ;
/* break is missing _intentionally_ */
default :
* q + + = c ;
}
}
* q = 0 ;
1999-12-20 20:43:47 +00:00
RETVAL_STRINGL ( erealloc ( str , q - str + 1 ) , q - str , 0 ) ;
1999-04-17 00:37:12 +00:00
}
/* }}} */
/* {{{ proto int ord(string character)
Return ASCII value of 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
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 ;
}
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( str ) ;
RETVAL_LONG ( ( unsigned char ) ( * str ) - > value . str . val [ 0 ] ) ;
1999-04-17 00:37:12 +00:00
}
/* }}} */
/* {{{ proto string chr(int ascii)
Convert ASCII code 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 ) ;
temp [ 0 ] = ( char ) ( * num ) - > value . lval ;
1999-04-17 00:37:12 +00:00
temp [ 1 ] = ' \0 ' ;
2001-08-11 17:03:37 +00:00
RETVAL_STRINGL ( temp , 1 , 1 ) ;
1999-04-17 00:37:12 +00:00
}
/* }}} */
1999-08-09 20:52:58 +00:00
/* {{{ proto string ucfirst(string str)
1999-04-17 00:37:12 +00:00
Make 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
{
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 ) = = FAILURE ) {
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
}
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( arg ) ;
1999-04-17 00:37:12 +00:00
1999-09-25 11:55:42 +00:00
if ( ! * ( * arg ) - > value . str . val ) {
1999-04-17 00:37:12 +00:00
RETURN_FALSE ;
}
1999-11-14 17:20:56 +00:00
* return_value = * * arg ;
zval_copy_ctor ( return_value ) ;
* return_value - > value . str . val = toupper ( ( unsigned char ) * return_value - > value . str . val ) ;
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 ;
}
1999-12-11 19:50:01 +00:00
convert_to_string_ex ( str ) ;
1999-04-17 00:37:12 +00:00
2000-06-01 14:18:45 +00:00
if ( ! Z_STRLEN_PP ( str ) ) {
1999-04-17 00:37:12 +00:00
RETURN_FALSE ;
}
1999-12-11 19:50:01 +00:00
* return_value = * * str ;
1999-11-14 17:20:56 +00:00
zval_copy_ctor ( return_value ) ;
2000-06-01 10:07:44 +00:00
2000-06-01 11:47:48 +00:00
r = return_value - > value . str . val ;
* r = toupper ( ( unsigned char ) * r ) ;
2000-06-01 14:18:45 +00:00
for ( r_end = r + return_value - > value . str . len - 1 ; r < r_end ; ) {
2000-06-01 14:56:28 +00:00
if ( isspace ( ( int ) * r + + ) ) {
2000-06-01 14:40:41 +00:00
* 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
*/
2000-02-08 22:21:43 +00:00
PHPAPI char * php_strtr ( char * str , int len , char * str_from ,
1999-10-16 16:35:20 +00:00
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
{
zval * entry ;
char * string_key ;
zval * * trans ;
zval ctmp ;
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 } ;
1999-11-20 15:53:18 +00:00
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 ) {
2000-12-22 12:57:09 +00:00
switch ( zend_hash_get_current_key_ex ( hash , & string_key , NULL , & num_key , 0 , & hpos ) ) {
1999-11-20 15:53:18 +00:00
case HASH_KEY_IS_STRING :
len = strlen ( string_key ) ;
if ( len > maxlen ) maxlen = len ;
if ( len < minlen ) minlen = len ;
break ;
case HASH_KEY_IS_LONG :
ctmp . type = IS_LONG ;
ctmp . value . lval = num_key ;
convert_to_string ( & ctmp ) ;
len = ctmp . value . str . len ;
zval_dtor ( & ctmp ) ;
if ( len > maxlen ) maxlen = len ;
if ( len < minlen ) minlen = len ;
break ;
}
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 ;
}
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 - - ) {
key [ len ] = 0 ;
2001-08-11 17:03:37 +00:00
if ( zend_hash_find ( hash , key , len + 1 , ( void * * ) & trans ) = = SUCCESS ) {
1999-11-20 15:53:18 +00:00
char * tval ;
int tlen ;
zval tmp ;
if ( ( * trans ) - > type ! = IS_STRING ) {
tmp = * * trans ;
zval_copy_ctor ( & tmp ) ;
convert_to_string ( & tmp ) ;
tval = tmp . value . str . val ;
tlen = tmp . value . str . len ;
} else {
tval = ( * trans ) - > value . str . val ;
tlen = ( * trans ) - > value . str . len ;
}
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 ;
if ( ( * trans ) - > type ! = IS_STRING ) {
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 ) ;
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)
Translate characters in str using given translation tables */
1999-06-15 21:51:00 +00:00
PHP_FUNCTION ( strtr )
2001-08-11 17:03:37 +00:00
{ /* strtr(STRING, FROM, TO) */
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
if ( ac = = 2 & & ( * from ) - > type ! = IS_ARRAY ) {
2001-08-11 17:03:37 +00:00
php_error ( E_WARNING , " arg2 must be passed 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 */
if ( Z_STRLEN_PP ( str ) = = 0 ) {
RETURN_EMPTY_STRING ( ) ;
}
1999-11-20 15:53:18 +00:00
if ( ac = = 2 ) {
2001-08-11 17:03:37 +00:00
php_strtr_array ( return_value , ( * str ) - > value . str . val , ( * str ) - > value . str . len , HASH_OF ( * from ) ) ;
1999-11-20 15:53:18 +00:00
} else {
convert_to_string_ex ( from ) ;
convert_to_string_ex ( to ) ;
* return_value = * * str ;
zval_copy_ctor ( return_value ) ;
php_strtr ( return_value - > value . str . val ,
return_value - > value . str . len ,
( * from ) - > value . str . val ,
( * to ) - > value . str . val ,
2001-08-11 17:03:37 +00:00
MIN ( ( * from ) - > value . str . len , ( * to ) - > value . str . len ) ) ;
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 ;
2001-08-11 17:03:37 +00:00
int i , len ;
1999-04-17 00:37:12 +00:00
char c ;
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 ;
}
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( str ) ;
1999-04-17 00:37:12 +00:00
1999-12-23 14:55:50 +00:00
* return_value = * * str ;
1999-12-23 14:59:01 +00:00
zval_copy_ctor ( return_value ) ;
1999-12-23 14:55:50 +00:00
len = return_value - > value . str . len ;
1999-04-17 00:37:12 +00:00
for ( i = 0 ; i < len - 1 - i ; i + + ) {
1999-12-23 14:55:50 +00:00
c = return_value - > value . str . val [ i ] ;
return_value - > value . str . val [ i ] = return_value - > value . str . val [ len - 1 - i ] ;
return_value - > value . str . val [ len - 1 - i ] = c ;
1999-04-17 00:37:12 +00:00
}
}
/* }}} */
2001-06-05 13:12:10 +00:00
/* {{{ php_similar_str
*/
1999-10-16 16:35:20 +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 + + ) {
for ( l = 0 ; ( p + l < end1 ) & & ( q + l < end2 ) & & ( p [ l ] = = q [ l ] ) ;
l + + ) ;
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
2001-06-05 13:12:10 +00:00
/* {{{ php_similar_char
*/
1999-10-16 16:35:20 +00:00
static int php_similar_char ( const char * txt1 , int len1 ,
const char * txt2 , int 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
{
int sum ;
int pos1 , pos2 , max ;
1999-10-16 16:35:20 +00:00
php_similar_str ( txt1 , len1 , txt2 , len2 , & pos1 , & pos2 , & 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 ) ) {
if ( pos1 & & pos2 )
1999-10-16 16:35:20 +00:00
sum + = php_similar_char ( txt1 , pos1 , txt2 , pos2 ) ;
* 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 ( ( pos1 + max < len1 ) & & ( pos2 + max < len2 ) )
1999-10-16 16:35:20 +00:00
sum + = php_similar_char ( txt1 + pos1 + max , len1 - pos1 - max ,
txt2 + pos2 + max , len2 - pos2 - 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
}
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
/* {{{ proto int similar_text(string str1, string str2 [, double percent])
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 ;
if ( ac < 2 | | ac > 3 | |
1999-12-18 22:40:35 +00:00
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 ;
}
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( t1 ) ;
convert_to_string_ex ( 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
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
}
1999-09-25 11:55:42 +00:00
if ( ( ( * t1 ) - > value . str . len + ( * t2 ) - > value . str . 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
if ( ac > 2 ) {
1999-09-25 11:55:42 +00:00
( * percent ) - > value . dval = 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
}
RETURN_LONG ( 0 ) ;
}
1999-10-16 16:35:20 +00:00
sim = php_similar_char ( ( * t1 ) - > value . str . val , ( * t1 ) - > value . str . len ,
1999-09-25 11:55:42 +00:00
( * t2 ) - > value . str . val , ( * t2 ) - > value . str . 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
if ( ac > 2 ) {
1999-09-25 11:55:42 +00:00
( * percent ) - > value . dval = sim * 200.0 / ( ( * t1 ) - > value . str . len + ( * t2 ) - > value . str . 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
}
RETURN_LONG ( sim ) ;
}
/* }}} */
1999-04-17 00:37:12 +00:00
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 ;
char escape_char = ' \\ ' ;
if ( PG ( magic_quotes_sybase ) ) {
escape_char = ' \' ' ;
}
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 ;
1999-04-17 00:37:12 +00:00
while ( l > 0 ) {
if ( * t = = escape_char ) {
t + + ; /* skip the slash */
if ( len ! = NULL )
( * len ) - - ;
l - - ;
if ( l > 0 ) {
if ( * t = = ' 0 ' ) {
* s + + = ' \0 ' ;
t + + ;
} else {
* s + + = * t + + ; /* preserve the next character */
}
l - - ;
}
} else {
if ( s ! = t )
* s + + = * t + + ;
else {
s + + ;
t + + ;
}
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)
2000-08-06 14:36:10 +00:00
Escape 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
2000-06-05 19:47:54 +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-08-06 03:50:52 +00:00
Z_STRVAL_P ( return_value ) = php_addcslashes ( Z_STRVAL_PP ( str ) ,
Z_STRLEN_PP ( str ) , & Z_STRLEN_P ( return_value ) , 0 , Z_STRVAL_PP ( what ) ,
Z_STRLEN_PP ( what ) TSRMLS_CC ) ;
1999-09-05 20:55:13 +00:00
return_value - > type = IS_STRING ;
}
/* }}} */
1999-04-17 00:37:12 +00:00
/* {{{ proto string addslashes(string str)
Escape 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 ;
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 ;
}
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( str ) ;
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
Z_STRVAL_P ( return_value ) = php_addslashes ( Z_STRVAL_PP ( str ) ,
Z_STRLEN_PP ( str ) , & Z_STRLEN_P ( return_value ) , 0 TSRMLS_CC ) ;
Z_TYPE_P ( return_value ) = IS_STRING ;
1999-04-17 00:37:12 +00:00
}
/* }}} */
1999-09-05 20:55:13 +00:00
/* {{{ proto string stripcslashes(string str)
2000-02-24 10:11:42 +00:00
Strip 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
1999-09-27 18:00:30 +00:00
* return_value = * * str ;
zval_copy_ctor ( return_value ) ;
2001-08-11 17:03:37 +00:00
php_stripcslashes ( return_value - > value . str . val , & return_value - > value . str . len ) ;
1999-09-05 20:55:13 +00:00
}
/* }}} */
1999-04-17 00:37:12 +00:00
/* {{{ proto string stripslashes(string str)
Strip 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 ;
}
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( str ) ;
1999-04-17 00:37:12 +00:00
1999-09-27 18:00:30 +00:00
* return_value = * * str ;
zval_copy_ctor ( return_value ) ;
2001-08-06 03:50:52 +00:00
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
if ( ( unsigned int ) errnum < sys_nerr ) return ( sys_errlist [ errnum ] ) ;
1999-11-28 00:31:02 +00:00
( void ) sprintf ( BG ( str_ebuf ) , " Unknown error: %d " , errnum ) ;
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 ] ;
2001-08-11 17:03:37 +00:00
for ( source = str , end = str + nlen , target = str ; source < end ; source + + ) {
1999-09-05 20:55:13 +00:00
if ( * source = = ' \\ ' & & source + 1 < end ) {
source + + ;
switch ( * source ) {
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 ;
case ' \\ ' : * target + + = ' \\ ' ; nlen - - ; break ;
1999-09-07 16:09:18 +00:00
case ' x ' : if ( source + 1 < end & & isxdigit ( ( int ) ( * ( source + 1 ) ) ) ) {
1999-09-05 20:55:13 +00:00
numtmp [ 0 ] = * + + source ;
1999-09-07 16:09:18 +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 */
default : i = 0 ;
while ( source < end & & * source > = ' 0 ' & & * source < = ' 7 ' & & i < 3 ) {
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
if ( nlen ! = 0 ) {
* 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 ] ;
1999-09-05 20:55:13 +00:00
char * new_str = emalloc ( ( length ? length : ( length = strlen ( str ) ) ) * 4 + 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
2001-08-11 17:03:37 +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 ] ) {
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 ;
1999-12-19 14:39:41 +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 ;
1999-09-05 20:55:13 +00:00
newlen = target - new_str ;
if ( target - new_str < length * 4 ) {
new_str = erealloc ( new_str , newlen + 1 ) ;
}
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
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 )
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 ;
char c ;
2000-02-02 21:53:27 +00:00
if ( ! str ) {
* new_length = 0 ;
return str ;
}
new_str = ( char * ) emalloc ( ( length ? length : ( length = strlen ( str ) ) ) * 2 + 1 ) ;
2001-08-11 17:03:37 +00:00
for ( source = str , end = source + length , target = new_str ; source < end ; source + + ) {
2000-07-21 15:29:59 +00:00
c = * source ;
1999-04-17 00:37:12 +00:00
switch ( c ) {
case ' \0 ' :
1999-09-05 20:55:13 +00:00
* target + + = ' \\ ' ;
1999-04-17 00:37:12 +00:00
* target + + = ' 0 ' ;
break ;
case ' \' ' :
if ( PG ( magic_quotes_sybase ) ) {
* target + + = ' \' ' ;
* target + + = ' \' ' ;
break ;
}
/* break is missing *intentionally* */
case ' \" ' :
case ' \\ ' :
if ( ! PG ( magic_quotes_sybase ) ) {
* target + + = ' \\ ' ;
}
/* break is missing *intentionally* */
default :
* target + + = c ;
break ;
}
}
* target = 0 ;
1999-06-15 21:51:00 +00:00
if ( new_length ) {
* new_length = target - new_str ;
}
1999-04-17 00:37:12 +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
# define _HEB_BLOCK_TYPE_ENG 1
# define _HEB_BLOCK_TYPE_HEB 2
# 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)
2001-06-05 13:12:10 +00:00
/* {{{ php_char_to_str
*/
2001-08-11 17:03:37 +00:00
PHPAPI void php_char_to_str ( char * str , uint len , char from , char * to , int to_len , zval * result )
1999-04-17 00:37:12 +00:00
{
int char_count = 0 ;
2001-08-11 17:03:37 +00:00
char * source , * target , * tmp , * source_end = str + len , * tmp_end = NULL ;
1999-04-17 00:37:12 +00:00
for ( source = str ; source < source_end ; source + + ) {
if ( * source = = from ) {
char_count + + ;
}
}
result - > type = IS_STRING ;
if ( char_count = = 0 ) {
2001-08-11 17:03:37 +00:00
result - > value . str . val = estrndup ( str , len ) ;
1999-04-17 00:37:12 +00:00
result - > value . str . len = len ;
return ;
}
result - > value . str . len = len + char_count * ( to_len - 1 ) ;
result - > value . str . val = target = ( char * ) emalloc ( result - > value . str . len + 1 ) ;
for ( source = str ; source < source_end ; source + + ) {
if ( * source = = from ) {
2001-08-11 17:03:37 +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-06-05 13:12:10 +00:00
/* }}} */
1999-04-17 00:37:12 +00:00
2001-07-20 01:22:29 +00:00
/* {{{ boyer_str_to_str */
static char * boyer_str_to_str ( char * haystack , int length ,
char * needle , int needle_len , char * str ,
int str_len , int * new_length )
{
char * p , * pe , * cursor , * end , * r ;
int off ;
char jump_table [ 256 ] ;
smart_str result = { 0 } ;
/*
* We implement only the first half of the Boyer - Moore algorithm ,
* because the second half is too expensive to compute during run - time .
* TODO : Split matching into compile - / match - stage .
*/
/* Prepare the jump_table which contains the skip offsets */
memset ( jump_table , needle_len , 256 ) ;
off = needle_len - 1 ;
/* Calculate the default start where each comparison starts */
pe = needle + off ;
/* Assign skip offsets based on the pattern */
for ( p = needle ; p < = pe ; p + + )
jump_table [ ( unsigned char ) * p ] = off - - ;
/* Start to look at the first possible position for the pattern */
cursor = haystack + needle_len - 1 ;
/* The cursor must not cross this limit */
end = haystack + length ;
/* Start to copy at haystack */
r = haystack ;
nextiter :
while ( cursor < end ) {
p = pe ; /* Compare from right to left */
while ( * p = = * cursor ) {
if ( - - p < needle ) { /* Found the pattern */
/* Append whatever was not matched */
smart_str_appendl ( & result , r , cursor - r ) ;
/* Append replacement string */
smart_str_appendl ( & result , str , str_len ) ;
/* Update copy pointer */
r = cursor + needle_len ;
/* needle_len was substracted from cursor for
* this comparison , add it back . Also add
* needle_len - 1 which is the default search
* offset .
*/
cursor + = ( needle_len < < 1 ) - 1 ;
/* Next iteration */
goto nextiter ;
}
cursor - - ;
}
cursor + = jump_table [ ( unsigned char ) * cursor ] ;
}
if ( r < end ) /* Copy the remaining data */
smart_str_appendl ( & result , r , end - r ) ;
smart_str_0 ( & result ) ; /* NUL-ify result */
if ( new_length )
* new_length = result . len ;
return result . c ;
}
/* }}} */
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 )
{
2000-10-12 00:50:28 +00:00
char * p ;
char * r ;
1999-04-17 00:37:12 +00:00
char * end = haystack + length ;
2000-10-12 00:50:28 +00:00
smart_str result = { 0 } ;
2001-07-20 01:22:29 +00:00
2000-10-12 00:50:28 +00:00
for ( p = haystack ;
( r = php_memnstr ( p , needle , needle_len , end ) ) ;
p = r + needle_len ) {
smart_str_appendl ( & result , p , r - p ) ;
smart_str_appendl ( & result , str , str_len ) ;
1999-04-17 00:37:12 +00:00
}
2000-10-12 00:50:28 +00:00
if ( p < end )
smart_str_appendl ( & result , p , end - p ) ;
smart_str_0 ( & result ) ;
if ( _new_length ) * _new_length = result . len ;
return result . c ;
1999-04-17 00:37:12 +00:00
}
2001-06-05 13:12:10 +00:00
/* }}} */
1999-04-17 00:37:12 +00:00
2001-06-05 13:12:10 +00:00
/* {{{ php_str_replace_in_subject
*/
2001-07-20 01:22:29 +00:00
static void php_str_replace_in_subject ( zval * search , zval * replace , zval * * subject , zval * result , int boyer )
2001-01-22 21:39:55 +00:00
{
zval * * search_entry ,
* * replace_entry = NULL ,
temp_result ;
char * replace_value = NULL ;
int replace_len = 0 ;
2001-07-20 01:22:29 +00:00
char * ( * str_to_str ) ( char * , int , char * , int , char * , int , int * ) ;
2001-01-22 21:39:55 +00:00
2001-07-20 01:22:29 +00:00
str_to_str = boyer ? boyer_str_to_str : php_str_to_str ;
2001-01-22 21:39:55 +00:00
/* Make sure we're dealing with strings. */
convert_to_string_ex ( subject ) ;
Z_TYPE_P ( result ) = IS_STRING ;
if ( Z_STRLEN_PP ( subject ) = = 0 ) {
ZVAL_STRINGL ( result , empty_string , 0 , 1 ) ;
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 ) ;
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 */
while ( zend_hash_get_current_data ( Z_ARRVAL_P ( search ) , ( void * * ) & search_entry ) = = SUCCESS ) {
/* Make sure we're dealing with strings. */
convert_to_string_ex ( search_entry ) ;
if ( Z_STRLEN_PP ( search_entry ) = = 0 ) {
zend_hash_move_forward ( Z_ARRVAL_P ( search ) ) ;
continue ;
}
/* 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. */
replace_value = empty_string ;
replace_len = 0 ;
}
}
if ( Z_STRLEN_PP ( search_entry ) = = 1 ) {
php_char_to_str ( Z_STRVAL_P ( result ) ,
Z_STRLEN_P ( result ) ,
Z_STRVAL_PP ( search_entry ) [ 0 ] ,
replace_value ,
replace_len ,
& temp_result ) ;
} else if ( Z_STRLEN_PP ( search_entry ) > 1 ) {
2001-07-20 01:22:29 +00:00
Z_STRVAL ( temp_result ) = str_to_str ( Z_STRVAL_P ( result ) , Z_STRLEN_P ( result ) ,
2001-01-22 21:39:55 +00:00
Z_STRVAL_PP ( search_entry ) , Z_STRLEN_PP ( search_entry ) ,
replace_value , replace_len , & Z_STRLEN ( temp_result ) ) ;
}
* 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 ) ;
zend_hash_move_forward ( Z_ARRVAL_P ( search ) ) ;
}
} else {
if ( Z_STRLEN_P ( search ) = = 1 ) {
php_char_to_str ( Z_STRVAL_PP ( subject ) ,
Z_STRLEN_PP ( subject ) ,
Z_STRVAL_P ( search ) [ 0 ] ,
Z_STRVAL_P ( replace ) ,
Z_STRLEN_P ( replace ) ,
result ) ;
} else if ( Z_STRLEN_P ( search ) > 1 ) {
2001-07-20 01:22:29 +00:00
Z_STRVAL_P ( result ) = str_to_str ( Z_STRVAL_PP ( subject ) , Z_STRLEN_PP ( subject ) ,
2001-01-22 21:39:55 +00:00
Z_STRVAL_P ( search ) , Z_STRLEN_P ( search ) ,
Z_STRVAL_P ( replace ) , Z_STRLEN_P ( replace ) , & Z_STRLEN_P ( result ) ) ;
} else {
* result = * * subject ;
zval_copy_ctor ( result ) ;
}
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
2001-07-20 01:22:29 +00:00
/* {{{ proto mixed str_replace(mixed search, mixed replace, mixed subject[, bool boyer])
2001-01-22 21:39:55 +00:00
Replace all occurrences of search in haystack with replace */
PHP_FUNCTION ( str_replace )
{
2001-07-20 01:22:29 +00:00
zval * * subject , * * search , * * replace , * * subject_entry , * * pboyer ;
2001-01-22 21:39:55 +00:00
zval * result ;
char * string_key ;
ulong string_key_len , num_key ;
2001-07-20 01:22:29 +00:00
int boyer = 0 ;
2001-01-22 21:39:55 +00:00
2001-07-20 01:22:29 +00:00
if ( ZEND_NUM_ARGS ( ) < 3 | |
ZEND_NUM_ARGS ( ) > 4 | |
zend_get_parameters_ex ( ZEND_NUM_ARGS ( ) , & search ,
& replace , & subject , & pboyer ) = = FAILURE ) {
2001-01-22 21:39:55 +00:00
WRONG_PARAM_COUNT ;
1999-05-12 15:40:15 +00:00
}
2001-07-20 01:22:29 +00:00
switch ( ZEND_NUM_ARGS ( ) ) {
case 4 :
convert_to_boolean_ex ( pboyer ) ;
if ( Z_BVAL_PP ( pboyer ) )
boyer = 1 ;
break ;
}
2001-01-22 21:39:55 +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 ) ;
/* 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 ) {
MAKE_STD_ZVAL ( result ) ;
2001-07-20 01:22:29 +00:00
php_str_replace_in_subject ( * search , * replace , subject_entry , result , boyer ) ;
2001-01-22 21:39:55 +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 */
2001-07-20 01:22:29 +00:00
php_str_replace_in_subject ( * search , * replace , subject , return_value , boyer ) ;
2001-01-22 21:39:55 +00:00
}
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 ;
char * heb_str , * tmp , * target , * opposite_target , * broken_str ;
1999-04-17 00:37:12 +00:00
int block_start , block_end , block_type , block_length , i ;
int block_ended ;
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
2000-06-05 19:47:54 +00:00
switch ( ZEND_NUM_ARGS ( ) ) {
1999-04-17 00:37:12 +00:00
case 1 :
1999-12-18 22:40:35 +00:00
if ( zend_get_parameters_ex ( 1 , & str ) = = FAILURE ) {
1999-04-17 00:37:12 +00:00
RETURN_FALSE ;
}
break ;
case 2 :
1999-12-18 22:40:35 +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 ) ;
max_chars = ( * max_chars_per_line ) - > value . lval ;
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
1999-09-25 11:55:42 +00:00
if ( ( * str ) - > value . str . len = = 0 ) {
1999-04-17 00:37:12 +00:00
RETURN_FALSE ;
}
1999-09-25 11:55:42 +00:00
tmp = ( * str ) - > value . str . val ;
1999-04-17 00:37:12 +00:00
block_start = block_end = 0 ;
block_ended = 0 ;
1999-09-25 11:55:42 +00:00
heb_str = ( char * ) emalloc ( ( * str ) - > value . str . len + 1 ) ;
target = heb_str + ( * str ) - > value . str . len ;
1999-04-17 00:37:12 +00:00
opposite_target = heb_str ;
* target = 0 ;
target - - ;
block_length = 0 ;
if ( isheb ( * tmp ) ) {
block_type = _HEB_BLOCK_TYPE_HEB ;
} else {
block_type = _HEB_BLOCK_TYPE_ENG ;
}
do {
if ( block_type = = _HEB_BLOCK_TYPE_HEB ) {
1999-09-25 11:55:42 +00:00
while ( ( isheb ( ( int ) * ( tmp + 1 ) ) | | _isblank ( ( int ) * ( tmp + 1 ) ) | | ispunct ( ( int ) * ( tmp + 1 ) ) | | ( int ) * ( tmp + 1 ) = = ' \n ' ) & & block_end < ( * str ) - > value . str . len - 1 ) {
1999-04-17 00:37:12 +00:00
tmp + + ;
block_end + + ;
block_length + + ;
}
for ( i = block_start ; i < = block_end ; i + + ) {
1999-09-25 11:55:42 +00:00
* target = ( * str ) - > value . str . val [ i ] ;
1999-04-17 00:37:12 +00:00
switch ( * target ) {
case ' ( ' :
* target = ' ) ' ;
break ;
case ' ) ' :
* target = ' ( ' ;
break ;
default :
break ;
}
target - - ;
}
block_type = _HEB_BLOCK_TYPE_ENG ;
} else {
1999-09-25 11:55:42 +00:00
while ( ! isheb ( * ( tmp + 1 ) ) & & ( int ) * ( tmp + 1 ) ! = ' \n ' & & block_end < ( * str ) - > value . str . len - 1 ) {
1999-04-17 00:37:12 +00:00
tmp + + ;
block_end + + ;
block_length + + ;
}
while ( ( _isblank ( ( int ) * tmp ) | | ispunct ( ( int ) * tmp ) ) & & * tmp ! = ' / ' & & * tmp ! = ' - ' & & block_end > block_start ) {
tmp - - ;
block_end - - ;
}
for ( i = block_end ; i > = block_start ; i - - ) {
1999-09-25 11:55:42 +00:00
* target = ( * str ) - > value . str . val [ i ] ;
1999-04-17 00:37:12 +00:00
target - - ;
}
block_type = _HEB_BLOCK_TYPE_HEB ;
}
block_start = block_end + 1 ;
1999-09-25 11:55:42 +00:00
} while ( block_end < ( * str ) - > value . str . len - 1 ) ;
1999-04-17 00:37:12 +00:00
1999-09-25 11:55:42 +00:00
broken_str = ( char * ) emalloc ( ( * str ) - > value . str . len + 1 ) ;
begin = end = ( * str ) - > value . str . len - 1 ;
1999-04-17 00:37:12 +00:00
target = broken_str ;
while ( 1 ) {
char_count = 0 ;
while ( ( ! max_chars | | char_count < max_chars ) & & begin > 0 ) {
char_count + + ;
begin - - ;
if ( begin < = 0 | | _isnewline ( heb_str [ begin ] ) ) {
while ( begin > 0 & & _isnewline ( heb_str [ begin - 1 ] ) ) {
begin - - ;
char_count + + ;
}
break ;
}
}
if ( char_count = = max_chars ) { /* try to avoid breaking words */
int new_char_count = char_count , new_begin = begin ;
while ( new_char_count > 0 ) {
if ( _isblank ( heb_str [ new_begin ] ) | | _isnewline ( heb_str [ new_begin ] ) ) {
break ;
}
new_begin + + ;
new_char_count - - ;
}
if ( new_char_count > 0 ) {
char_count = new_char_count ;
begin = new_begin ;
}
}
orig_begin = begin ;
if ( _isblank ( heb_str [ begin ] ) ) {
heb_str [ begin ] = ' \n ' ;
}
while ( begin < = end & & _isnewline ( heb_str [ begin ] ) ) { /* skip leading newlines */
begin + + ;
}
for ( i = begin ; i < = end ; i + + ) { /* copy content */
* target = heb_str [ i ] ;
target + + ;
}
for ( i = orig_begin ; i < = end & & _isnewline ( heb_str [ i ] ) ; i + + ) {
* target = heb_str [ i ] ;
target + + ;
}
begin = orig_begin ;
if ( begin < = 0 ) {
* target = 0 ;
break ;
}
begin - - ;
end = begin ;
}
efree ( heb_str ) ;
if ( convert_newlines ) {
2001-08-11 17:03:37 +00:00
php_char_to_str ( broken_str , ( * str ) - > value . str . len , ' \n ' , " <br> \n " , 5 , return_value ) ;
1999-04-17 00:37:12 +00:00
efree ( broken_str ) ;
} else {
return_value - > value . str . val = broken_str ;
1999-09-25 11:55:42 +00:00
return_value - > value . str . len = ( * str ) - > value . str . len ;
1999-04-17 00:37:12 +00:00
return_value - > type = IS_STRING ;
}
}
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])
Convert 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])
1999-04-17 00:37:12 +00:00
Convert 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
}
/* }}} */
/* {{{ 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
{
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 ;
}
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( str ) ;
1999-04-17 00:37:12 +00:00
2001-08-11 17:03:37 +00:00
php_char_to_str ( ( * str ) - > value . str . val , ( * str ) - > value . str . len , ' \n ' , " <br /> \n " , 7 , return_value ) ;
1999-04-17 00:37:12 +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 )
{
char * buf ;
1999-12-23 14:59:01 +00:00
zval * * str , * * allow = NULL ;
2000-02-08 21:29:18 +00:00
char * allowed_tags = NULL ;
int allowed_tags_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
2000-06-05 19:47:54 +00:00
switch ( ZEND_NUM_ARGS ( ) ) {
1999-09-20 15:50:56 +00:00
case 1 :
1999-12-18 22:40:35 +00:00
if ( zend_get_parameters_ex ( 1 , & str ) = = FAILURE ) {
1999-09-20 15:50:56 +00:00
RETURN_FALSE ;
}
break ;
case 2 :
1999-12-18 22:40:35 +00:00
if ( zend_get_parameters_ex ( 2 , & str , & allow ) = = FAILURE ) {
1999-09-20 15:50:56 +00:00
RETURN_FALSE ;
}
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( allow ) ;
2000-02-08 21:29:18 +00:00
allowed_tags = ( * allow ) - > value . str . val ;
allowed_tags_len = ( * allow ) - > value . str . len ;
1999-09-20 15:50:56 +00:00
break ;
default :
WRONG_PARAM_COUNT ;
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
}
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( str ) ;
2001-08-11 17:03:37 +00:00
buf = estrndup ( ( * str ) - > value . str . val , ( * str ) - > value . str . len ) ;
2000-02-08 21:29:18 +00:00
php_strip_tags ( buf , ( * str ) - > value . str . len , 0 , allowed_tags , allowed_tags_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
RETURN_STRING ( buf , 0 ) ;
}
/* }}} */
2000-12-21 13:46:50 +00:00
/* {{{ proto string setlocale(mixed category, string locale)
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
{
1999-12-23 14:59:01 +00:00
zval * * pcategory , * * plocale ;
zval * category , * locale ;
1999-04-17 00:37:12 +00:00
int cat ;
char * loc , * retval ;
2000-06-05 19:47:54 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 2 | | zend_get_parameters_ex ( 2 , & pcategory , & plocale ) = = FAILURE )
1999-04-17 00:37:12 +00:00
WRONG_PARAM_COUNT ;
* 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
1999-09-25 11:55:42 +00:00
convert_to_string_ex ( plocale ) ;
locale = * plocale ;
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. */
2001-08-11 17:03:37 +00:00
php_error ( E_NOTICE , " 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 ) ;
category = * pcategory ;
if ( ! strcasecmp ( " LC_ALL " , category - > value . str . val ) )
cat = LC_ALL ;
else if ( ! strcasecmp ( " LC_COLLATE " , category - > value . str . val ) )
cat = LC_COLLATE ;
else if ( ! strcasecmp ( " LC_CTYPE " , category - > value . str . val ) )
cat = LC_CTYPE ;
2000-06-17 16:49:03 +00:00
# ifdef LC_MESSAGES
2000-12-21 13:46:50 +00:00
else if ( ! strcasecmp ( " LC_MESSAGES " , category - > value . str . val ) )
cat = LC_MESSAGES ;
2000-06-17 14:48:03 +00:00
# endif
2000-12-21 13:46:50 +00:00
else if ( ! strcasecmp ( " LC_MONETARY " , category - > value . str . val ) )
cat = LC_MONETARY ;
else if ( ! strcasecmp ( " LC_NUMERIC " , category - > value . str . val ) )
cat = LC_NUMERIC ;
else if ( ! strcasecmp ( " LC_TIME " , category - > value . str . val ) )
cat = LC_TIME ;
else {
2001-08-11 17:03:37 +00:00
php_error ( 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 - > value . str . val ) ;
2000-12-21 13:46:50 +00:00
RETURN_FALSE ;
}
1999-04-17 00:37:12 +00:00
}
2000-12-21 13:46:50 +00:00
if ( ! strcmp ( " 0 " , locale - > value . str . val ) ) {
1999-04-17 00:37:12 +00:00
loc = NULL ;
2000-12-21 13:46:50 +00:00
} else {
1999-04-17 00:37:12 +00:00
loc = locale - > value . str . val ;
2000-12-21 13:46:50 +00:00
}
1999-04-17 00:37:12 +00:00
retval = setlocale ( cat , loc ) ;
if ( retval ) {
1999-10-08 02:10:57 +00:00
/* Remember if locale was changed */
if ( loc ) {
1999-11-28 00:31:02 +00:00
STR_FREE ( BG ( locale_string ) ) ;
2000-01-05 12:26:41 +00:00
BG ( locale_string ) = estrdup ( retval ) ;
1999-10-08 02:10:57 +00:00
}
2001-08-11 17:03:37 +00:00
RETVAL_STRING ( retval , 1 ) ;
1999-04-17 00:37:12 +00:00
return ;
}
# endif
RETURN_FALSE ;
}
/* }}} */
2000-09-11 14:50:26 +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 ;
2000-09-12 10:56:25 +00:00
int old_rg ;
1999-04-17 00:37:12 +00:00
2000-09-11 14:50:26 +00:00
argCount = ARG_COUNT ( ht ) ;
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 ;
if ( sarg - > value . str . val & & * sarg - > value . str . val ) {
res = estrndup ( sarg - > value . str . val , sarg - > value . str . len ) ;
}
2000-09-12 10:56:25 +00:00
old_rg = PG ( register_globals ) ;
if ( argCount = = 1 ) {
PG ( register_globals ) = 1 ;
2001-07-28 11:36:37 +00:00
php_treat_data ( PARSE_STRING , res , NULL TSRMLS_CC ) ;
2000-09-12 10:56:25 +00:00
} else {
2000-09-12 10:54:45 +00:00
PG ( register_globals ) = 0 ;
2000-09-11 14:50:26 +00:00
/* Clear out the array that was passed in. */
zval_dtor ( * arrayArg ) ;
array_init ( * arrayArg ) ;
2001-07-28 11:36:37 +00:00
php_treat_data ( PARSE_STRING , res , * arrayArg TSRMLS_CC ) ;
1999-04-17 00:37:12 +00:00
}
2000-09-12 10:56:25 +00:00
PG ( register_globals ) = old_rg ;
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
2001-06-05 13:12:10 +00:00
/* {{{ php_tag_find
*
* Check if tag is in a set of tags
1999-09-20 15:50:56 +00:00
*
* states :
*
* 0 start tag
* 1 first non - whitespace char seen
*/
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 ;
}
while ( ! done ) {
1999-09-20 15:50:56 +00:00
switch ( c ) {
case ' < ' :
* ( n + + ) = c ;
break ;
case ' > ' :
done = 1 ;
break ;
default :
1999-09-21 17:06:30 +00:00
if ( ! isspace ( ( int ) c ) ) {
1999-09-20 15:50:56 +00:00
if ( state = = 0 ) {
state = 1 ;
if ( c ! = ' / ' ) * ( n + + ) = c ;
} else {
* ( n + + ) = c ;
}
} else {
if ( state = = 1 ) done = 1 ;
}
break ;
}
c = tolower ( * ( + + t ) ) ;
}
* ( n + + ) = ' > ' ;
* n = ' \0 ' ;
2001-08-11 17:03:37 +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
2001-06-05 13:12:10 +00:00
/* {{{ php_strip_tags
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 .
* 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 21:29:18 +00:00
PHPAPI void php_strip_tags ( char * rbuf , int len , int state , char * allow , int allow_len )
{
1999-09-20 15:50:56 +00:00
char * tbuf , * buf , * p , * tp , * rp , c , lc ;
int br , i = 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-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 ;
1999-09-20 15:50:56 +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
1999-09-20 15:50:56 +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 ) {
case ' < ' :
if ( state = = 0 ) {
lc = ' < ' ;
state = 1 ;
1999-09-20 15:50:56 +00:00
if ( allow ) {
* ( tp + + ) = ' < ' ;
}
* 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 ) {
if ( lc ! = ' \" ' ) {
lc = ' ( ' ;
br + + ;
}
2001-04-29 13:16:05 +00:00
} else if ( allow & & state = = 1 ) {
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 ) {
if ( lc ! = ' \" ' ) {
lc = ' ) ' ;
br - - ;
}
2001-04-29 13:16:05 +00:00
} else if ( allow & & state = = 1 ) {
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 = = 1 ) {
lc = ' > ' ;
state = 0 ;
1999-09-20 15:50:56 +00:00
if ( allow ) {
* ( tp + + ) = ' > ' ;
* tp = ' \0 ' ;
2000-02-13 15:56:07 +00:00
if ( php_tag_find ( tbuf , tp - tbuf , allow ) ) {
2001-08-11 17:03:37 +00:00
memcpy ( rp , tbuf , tp - tbuf ) ;
1999-09-20 15:50:56 +00:00
rp + = tp - tbuf ;
}
tp = tbuf ;
}
* 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 = = 2 ) {
if ( ! br & & lc ! = ' \" ' & & * ( p - 1 ) = = ' ? ' ) {
state = 0 ;
2000-07-30 05:04:06 +00:00
tp = tbuf ;
* 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-02-02 13:47:02 +00:00
} else {
* ( rp + + ) = 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 ;
case ' \" ' :
if ( state = = 2 ) {
if ( lc = = ' \" ' ) {
lc = ' \0 ' ;
} else if ( lc ! = ' \\ ' ) {
lc = ' \" ' ;
}
} else if ( state = = 0 ) {
* ( rp + + ) = c ;
1999-09-20 15:50:56 +00:00
} else if ( allow & & state = = 1 ) {
* ( 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 ;
case ' ? ' :
if ( state = = 1 & & * ( p - 1 ) = = ' < ' ) {
br = 0 ;
state = 2 ;
break ;
}
/* fall-through */
default :
if ( state = = 0 ) {
* ( rp + + ) = c ;
1999-09-20 15:50:56 +00:00
} else if ( allow & & state = = 1 ) {
* ( tp + + ) = c ;
if ( ( tp - tbuf ) > = PHP_TAG_BUF_SIZE ) { /* no buffer overflows */
tp = tbuf ;
}
}
* 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
}
* rp = ' \0 ' ;
efree ( buf ) ;
1999-09-20 15:50:56 +00:00
if ( allow ) efree ( tbuf ) ;
* 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 )
{
zval * * input_str ; /* Input string */
zval * * mult ; /* Multiplier */
char * result ; /* Resulting string */
int result_len ; /* Length of the resulting string */
int i ;
2000-06-05 19:47:54 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 2 | | zend_get_parameters_ex ( 2 , & input_str , & mult ) = = FAILURE ) {
1999-10-27 22:06:05 +00:00
WRONG_PARAM_COUNT ;
}
/* Make sure we're dealing with proper types */
convert_to_string_ex ( input_str ) ;
convert_to_long_ex ( mult ) ;
2000-09-05 18:25:58 +00:00
if ( ( * mult ) - > value . lval < 0 ) {
php_error ( E_WARNING , " Second argument to %s() has to be greater than or equal to 0 " ,
2001-07-30 08:24:42 +00:00
get_active_function_name ( TSRMLS_C ) ) ;
1999-10-27 22:06:05 +00:00
return ;
}
/* Don't waste our time if it's empty */
if ( ( * input_str ) - > value . str . len = = 0 )
RETURN_STRINGL ( empty_string , 0 , 1 ) ;
2000-09-05 18:25:58 +00:00
/* ... or if the multiplier is zero */
if ( ( * mult ) - > value . lval = = 0 )
RETURN_STRINGL ( empty_string , 0 , 1 ) ;
1999-10-27 22:06:05 +00:00
/* Initialize the result string */
result_len = ( * input_str ) - > value . str . len * ( * mult ) - > value . lval ;
result = ( char * ) emalloc ( result_len + 1 ) ;
/* Copy the input string into the result as many times as necessary */
for ( i = 0 ; i < ( * mult ) - > value . lval ; i + + ) {
memcpy ( result + ( * input_str ) - > value . str . len * i ,
( * input_str ) - > value . str . val ,
( * input_str ) - > value . str . len ) ;
}
result [ result_len ] = ' \0 ' ;
2000-01-04 09:45:28 +00:00
RETURN_STRINGL ( result , result_len , 0 ) ;
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 ) ;
mymode = ( * mode ) - > value . lval ;
if ( mymode < 0 | | mymode > 4 ) {
php_error ( E_WARNING , " unknown mode " ) ;
RETURN_FALSE ;
}
}
len = ( * input ) - > value . str . len ;
buf = ( unsigned char * ) ( * input ) - > value . str . val ;
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 ) ;
}
2001-02-15 14:01:18 +00:00
for ( inx = 0 ; inx < 256 ; inx + + ) {
1999-12-14 03:52:12 +00:00
switch ( mymode ) {
case 0 :
2001-08-11 17:03:37 +00:00
add_index_long ( return_value , inx , chars [ inx ] ) ;
1999-12-14 03:52:12 +00:00
break ;
case 1 :
if ( chars [ inx ] ! = 0 ) {
2001-08-11 17:03:37 +00:00
add_index_long ( return_value , inx , chars [ inx ] ) ;
1999-12-14 03:52:12 +00:00
}
break ;
case 2 :
if ( chars [ inx ] = = 0 ) {
2001-08-11 17:03:37 +00:00
add_index_long ( return_value , inx , chars [ inx ] ) ;
1999-12-14 03:52:12 +00:00
}
break ;
case 3 :
if ( chars [ inx ] ! = 0 ) {
retstr [ retlen + + ] = inx ;
}
break ;
case 4 :
if ( chars [ inx ] = = 0 ) {
retstr [ retlen + + ] = inx ;
}
break ;
}
}
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 ) ;
RETURN_LONG ( strnatcmp_ex ( ( * s1 ) - > value . str . val , ( * s1 ) - > value . str . len ,
( * s2 ) - > value . str . val , ( * s2 ) - > value . str . len ,
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-01-14 16:36:30 +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 ;
MAKE_STD_ZVAL ( grouping ) ;
MAKE_STD_ZVAL ( mon_grouping ) ;
/* We don't need no stinkin' parameters... */
if ( ZEND_NUM_ARGS ( ) > 0 ) {
WRONG_PARAM_COUNT ;
}
if ( array_init ( return_value ) = = FAILURE ) {
RETURN_FALSE ;
}
if ( array_init ( grouping ) = = FAILURE | | array_init ( mon_grouping ) = = FAILURE ) {
RETURN_FALSE ;
}
# ifdef HAVE_LOCALECONV
{
struct lconv currlocdata ;
localeconv_r ( & currlocdata ) ;
/* Grab the grouping data out of the array */
len = strlen ( currlocdata . grouping ) ;
for ( i = 0 ; i < len ; i + + ) {
add_index_long ( grouping , i , currlocdata . grouping [ i ] ) ;
}
/* Grab the monetary grouping data out of the array */
len = strlen ( currlocdata . mon_grouping ) ;
for ( i = 0 ; i < len ; i + + ) {
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
zend_hash_update ( return_value - > value . ht , " grouping " , 9 , & grouping , sizeof ( zval * ) , NULL ) ;
zend_hash_update ( return_value - > value . ht , " mon_grouping " , 13 , & mon_grouping , sizeof ( zval * ) , NULL ) ;
}
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
/* }}} */
2000-04-26 01:16:57 +00:00
/* {{{ proto int substr_count(string haystack, string needle)
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 )
{
zval * * haystack , * * needle ;
int i , length , count = 0 ;
char * p , * endp , cmp ;
2000-06-05 19:47:54 +00:00
if ( ZEND_NUM_ARGS ( ) ! = 2 | | zend_get_parameters_ex ( 2 , & haystack , & needle ) = = FAILURE ) {
2000-04-26 00:29:59 +00:00
WRONG_PARAM_COUNT ;
}
convert_to_string_ex ( haystack ) ;
convert_to_string_ex ( needle ) ;
if ( ( * needle ) - > value . str . len = = 0 ) {
php_error ( E_WARNING , " Empty substring " ) ;
RETURN_FALSE ;
} else if ( ( * needle ) - > value . str . len = = 1 ) {
2000-04-27 14:41:18 +00:00
/* Special optimized case to avoid calls to php_memnstr(). */
2000-04-26 00:29:59 +00:00
for ( i = 0 , p = ( * haystack ) - > value . str . val ,
length = ( * haystack ) - > value . str . len , cmp = ( * needle ) - > value . str . val [ 0 ] ;
i < length ; i + + ) {
if ( p [ i ] = = cmp ) {
count + + ;
}
}
} else {
p = ( * haystack ) - > value . str . val ;
endp = p + ( * haystack ) - > value . str . len ;
while ( p < = endp ) {
if ( ( p = php_memnstr ( p , ( * needle ) - > value . str . val , ( * needle ) - > value . str . len , endp ) ) ! = NULL ) {
p + = ( * needle ) - > value . str . len ;
count + + ;
} else {
break ;
}
}
}
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 */
2000-06-06 20:42:33 +00:00
zval * * input , /* Input string */
2000-07-08 20:38:23 +00:00
* * pad_length , /* Length to pad to */
* * pad_string , /* Padding string */
* * pad_type ; /* Padding type (left/right/both) */
/* Helper variables */
int num_pad_chars ; /* Number of padding characters (total - input size) */
2000-06-06 20:42:33 +00:00
char * result = NULL ; /* Resulting string */
int result_len = 0 ; /* Length of the resulting string */
char * pad_str_val = " " ; /* Pointer to padding string */
int pad_str_len = 1 ; /* Length of the padding string */
2000-07-08 20:38:23 +00:00
int pad_type_val = STR_PAD_RIGHT ; /* The padding type value */
2000-07-18 20:40:23 +00:00
int i , left_pad = 0 , right_pad = 0 ;
2000-06-06 20:42:33 +00:00
2000-07-08 20:38:23 +00:00
if ( ZEND_NUM_ARGS ( ) < 2 | | ZEND_NUM_ARGS ( ) > 4 | |
zend_get_parameters_ex ( ZEND_NUM_ARGS ( ) , & input , & pad_length , & pad_string , & pad_type ) = = FAILURE ) {
2000-06-06 20:42:33 +00:00
WRONG_PARAM_COUNT ;
}
/* Perform initial conversion to expected data types. */
convert_to_string_ex ( input ) ;
convert_to_long_ex ( pad_length ) ;
2000-07-08 20:38:23 +00:00
num_pad_chars = Z_LVAL_PP ( pad_length ) - Z_STRLEN_PP ( input ) ;
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 ) {
2000-06-06 20:42:33 +00:00
* return_value = * * input ;
zval_copy_ctor ( return_value ) ;
return ;
}
/* Setup the padding string values if specified. */
if ( ZEND_NUM_ARGS ( ) > 2 ) {
convert_to_string_ex ( pad_string ) ;
if ( Z_STRLEN_PP ( pad_string ) = = 0 ) {
php_error ( E_WARNING , " Padding string cannot be empty in %s() " ,
2001-07-30 08:24:42 +00:00
get_active_function_name ( TSRMLS_C ) ) ;
2000-06-06 20:42:33 +00:00
return ;
}
pad_str_val = Z_STRVAL_PP ( pad_string ) ;
pad_str_len = Z_STRLEN_PP ( pad_string ) ;
2000-07-08 20:38:23 +00:00
if ( ZEND_NUM_ARGS ( ) > 3 ) {
convert_to_long_ex ( pad_type ) ;
pad_type_val = Z_LVAL_PP ( pad_type ) ;
if ( pad_type_val < STR_PAD_LEFT | | pad_type_val > STR_PAD_BOTH ) {
2001-07-30 08:24:42 +00:00
php_error ( E_WARNING , " Padding type has to be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH in %s() " , get_active_function_name ( TSRMLS_C ) ) ;
2000-07-08 20:38:23 +00:00
return ;
}
}
2000-06-06 20:42:33 +00:00
}
2000-07-08 20:38:23 +00:00
result = ( char * ) emalloc ( Z_STRLEN_PP ( input ) + 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. */
switch ( pad_type_val ) {
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
}
2000-07-08 20:38:23 +00:00
/* First we pad on the left. */
for ( i = 0 ; i < left_pad ; i + + )
result [ result_len + + ] = pad_str_val [ i % pad_str_len ] ;
/* Then we copy the input string. */
memcpy ( result + result_len , Z_STRVAL_PP ( input ) , Z_STRLEN_PP ( input ) ) ;
result_len + = Z_STRLEN_PP ( input ) ;
/* Finally, we pad on the right. */
for ( i = 0 ; i < right_pad ; i + + )
2000-06-06 20:42:33 +00:00
result [ result_len + + ] = pad_str_val [ i % pad_str_len ] ;
2000-06-12 16:06:27 +00:00
result [ result_len ] = ' \0 ' ;
2000-06-06 20:42:33 +00:00
RETURN_STRINGL ( result , result_len , 0 ) ;
}
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 )
{
zval * * format ;
zval * * literal ;
int result ;
zval * * * args ;
int argCount ;
argCount = ZEND_NUM_ARGS ( ) ;
if ( argCount < 2 ) {
WRONG_PARAM_COUNT ;
}
args = ( zval * * * ) emalloc ( argCount * sizeof ( zval * * ) ) ;
2001-08-11 17:03:37 +00:00
if ( ! args | | ( zend_get_parameters_array_ex ( argCount , args ) = = FAILURE ) ) {
2000-06-06 18:58:15 +00:00
efree ( args ) ;
WRONG_PARAM_COUNT ;
}
literal = args [ 0 ] ;
format = args [ 1 ] ;
convert_to_string_ex ( format ) ;
convert_to_string_ex ( literal ) ;
result = php_sscanf_internal ( ( * literal ) - > value . str . val ,
( * format ) - > value . str . val ,
2001-07-30 08:24:42 +00:00
argCount , 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
}
}
/* }}} */
2000-06-01 10:07:44 +00:00
/*
* Local variables :
* tab - width : 4
* c - basic - offset : 4
* End :
2001-06-06 13:06:12 +00:00
* vim600 : sw = 4 ts = 4 tw = 78 fdm = marker
* vim < 600 : sw = 4 ts = 4 tw = 78
2000-06-01 10:07:44 +00:00
*/