2014-11-21 13:40 UTC+0100 Przemyslaw Czerpak (druzus/at/poczta.onet.pl)

* src/rtl/replic.c
    % optimized REPLICATE() function
This commit is contained in:
Przemysław Czerpak
2014-11-21 13:40:58 +01:00
parent ed3dd0415f
commit 61e4dba3f1
2 changed files with 22 additions and 14 deletions

View File

@@ -10,6 +10,10 @@
* Change, ! Fix, % Optimization, + Addition, - Removal, ; Comment
*/
2014-11-21 13:40 UTC+0100 Przemyslaw Czerpak (druzus/at/poczta.onet.pl)
* src/rtl/replic.c
% optimized REPLICATE() function
2014-11-19 08:37 UTC+0100 Przemyslaw Czerpak (druzus/at/poczta.onet.pl)
* contrib/hbnetio/netiosrv.c
* small simplification

View File

@@ -54,28 +54,32 @@
HB_FUNC( REPLICATE )
{
if( HB_ISCHAR( 1 ) && HB_ISNUM( 2 ) )
const char * szText = hb_parc( 1 );
if( szText && HB_ISNUM( 2 ) )
{
HB_SIZE nLen = hb_parclen( 1 );
HB_ISIZ nTimes = hb_parns( 2 );
if( nTimes > 0 )
if( nLen > 0 && nTimes > 0 )
{
HB_SIZE nLen = hb_parclen( 1 );
if( ( double ) ( ( double ) nLen * ( double ) nTimes ) < ( double ) ULONG_MAX )
if( ( double ) nLen * nTimes < HB_SIZE_MAX )
{
const char * szText = hb_parc( 1 );
char * szResult = ( char * ) hb_xgrab( ( nLen * nTimes ) + 1 );
char * szPtr = szResult;
HB_ISIZ n;
HB_SIZE nSize = nLen * nTimes;
char * szResult, * szPtr;
for( n = 0; n < nTimes; ++n )
szResult = szPtr = ( char * ) hb_xgrab( nSize + 1 );
if( nLen == 1 )
memset( szResult, szText[ 0 ], nSize );
else
{
hb_xmemcpy( szPtr, szText, nLen );
szPtr += nLen;
while( nTimes-- > 0 )
{
hb_xmemcpy( szPtr, szText, nLen );
szPtr += nLen;
}
}
hb_retclen_buffer( szResult, nLen * nTimes );
hb_retclen_buffer( szResult, nSize );
}
else
hb_errRT_BASE_SubstR( EG_STROVERFLOW, 1234, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS );