diff --git a/ChangeLog.txt b/ChangeLog.txt index 7c16acaae8..f001108c82 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -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 diff --git a/src/rtl/replic.c b/src/rtl/replic.c index f9b422a465..e8bed934bf 100644 --- a/src/rtl/replic.c +++ b/src/rtl/replic.c @@ -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 );