SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Filterscripts > Includes

Reply
 
Thread Tools Display Modes
Old 27/07/2009, 04:21 PM   #11
Marcel
Gangsta
 
Join Date: Nov 2007
Posts: 544
Reputation: 2
Default Re: [INC] strlib - Simple string modification (Update: 27/07/09)

It's like strtok, or like sscanf with the p<delimiter>.
http://wiki.sa-mp.com/wiki/Fast_Commands#Data_types
Marcel is offline   Reply With Quote
Old 27/07/2009, 05:36 PM   #12
Westie
Gangsta
 
Westie's Avatar
 
Join Date: Jul 2009
Posts: 863
Reputation: 63
Default Re: [INC] strlib - Simple string modification (Update: 27/07/09)

...except it's a lot faster than strtok.

Testing explode()
My first test was to test the speeds of various functions to do with exploding strings. explode() and explodea() are both written by me, and they are very similar, except explodea() is slightly more complex. On the other hand, split() is a function found in GF-RP, and seeing as that is one of the most popular game modes (and the most hated, by me), I thought I should include it. Also, for fair comparison, I used ysi_explode(), this function was renamed from explode(), and this can be found in YSI.

Code:
explode(): 0860, explodea(): 0828, split(): 2187, ysi_explode(): 1922
explode(): 0766, explodea(): 0812, split(): 2156, ysi_explode(): 1875
explode(): 0735, explodea(): 0797, split(): 2187, ysi_explode(): 1875
explode(): 0719, explodea(): 0828, split(): 2172, ysi_explode(): 1859
explode(): 0735, explodea(): 0812, split(): 2891, ysi_explode(): 2531
As you can see, using explode() has a lot of savings. These new versions are even faster than the 'inefficient' previous core, which were miles faster than the other functions anyway.

Testing str_replace()
I used my str_replace() function, and compared it to the one that is in dUtils, as well as the one found from here (Dabombber):

Code:
str_replace(): 0266, dutils_strreplace(): 7125, dabombber_strreplace(): 0516
str_replace(): 0266, dutils_strreplace(): 6625, dabomber_strreplace(): 0484
str_replace(): 0266, dutils_strreplace(): 6328, dabomber_strreplace(): 0500
str_replace(): 0282, dutils_strreplace(): 6078, dabomber_strreplace(): 0515
str_replace(): 0266, dutils_strreplace(): 6703, dabomber_strreplace(): 0500
Westie is offline   Reply With Quote
Old 18/08/2009, 12:28 PM   #13
Brendan_Thomson
Huge Clucker
 
Join Date: Oct 2007
Posts: 292
Reputation: 2
Default Re: [INC] strlib - Simple string modification (Update: 27/07/09)

for those wanting to replace split with explode you can do this to easily replace all instances of split with explode.

Code:
#define explode_(%1,%2, explode(%2,%1
this is because the first two parameters for explode and split are backwards so the first parameter in split is the second parameter in explode, vice versa.

^ using that you will have to change the delimiter to a string aswell, so ',' to ",".

or you could possibly use

Code:
#define explode_(%1,%2, chr_explode(%2,%1

and not have to change the delimiter to a string. But westie was talking of removing chr_explode.

so using that define you can find & replace all instances of split( with explode_(
Brendan_Thomson is offline   Reply With Quote
Old 21/08/2009, 05:40 AM   #14
Dabombber
Big Clucker
 
Join Date: May 2006
Posts: 81
Reputation: 1
Default Re: [INC] strlib - Simple string modification (Update: 27/07/09)

Quote:
Originally Posted by /^We(stie|z+[e|a
r)$/ ]
Testing str_replace()
I used my str_replace() function, and compared it to the one that is in dUtils, as well as the one found from here (Dabombber):

Code:
str_replace(): 0266, dutils_strreplace(): 7125, dabombber_strreplace(): 0516
str_replace(): 0266, dutils_strreplace(): 6625, dabomber_strreplace(): 0484
str_replace(): 0266, dutils_strreplace(): 6328, dabomber_strreplace(): 0500
str_replace(): 0282, dutils_strreplace(): 6078, dabomber_strreplace(): 0515
str_replace(): 0266, dutils_strreplace(): 6703, dabomber_strreplace(): 0500
Just out of curiosity, did you use strinsEx in both functions? Mine is a few times faster than yours if the native strins is used but it's possible to get buffer overflows. There's also a couple of bugs in yours

[code=pawn]str_replace("dog", "cat", "The quick brown fox jumps over the lazy dog")[/code]

doesn't replace and

[code=pawn]strins(sReturn, sReplace, iIndex, iLengthReplace);[/code]

should be

[code=pawn]strins(sReturn, sReplace, iIndex, sizeof(sReturn));[/code]

not that it really matters since strins doesn't seem to do any bounds checking
Dabombber is offline   Reply With Quote
Old 21/08/2009, 07:29 AM   #15
nuriel8833
High-roller
 
nuriel8833's Avatar
 
Join Date: Jun 2009
Location: Israel
Posts: 1,174
Reputation: 16
Default Re: [INC] strlib - Simple string modification (Update: 27/07/09)

Tanks
__________________
Back to forums
Although I am not scripting much,only my gamemode on my free time

My works:
80.179.219.89:7778
»[Tutorial] Kills Table BonusLast.fm
nuriel8833 is offline   Reply With Quote
Old 21/08/2009, 09:06 AM   #16
Brendan_Thomson
Huge Clucker
 
Join Date: Oct 2007
Posts: 292
Reputation: 2
Default Re: [INC] strlib - Simple string modification (Update: 27/07/09)

Explode doesn't read the the last part of a string if the delimiter isn't at the end, for example

1|2|3|4

if you use explode to split those, it will only read 1, 2 and 3 but not 4.

Waiting for a fix for this.
Brendan_Thomson is offline   Reply With Quote
Old 21/08/2009, 09:16 AM   #17
Y_Less
Spam Machine
 
Y_Less's Avatar
 
Join Date: Jun 2008
Location: 629 - git.io/Y
Posts: 14,666
Reputation: 3061
Default Re: [INC] strlib - Simple string modification (Update: 27/07/09)

Code:
/*
	(DEPRECIATED) chr_explode:
		Creates an array of values from 'sSource', where only the exact amount of
		values matching sizeof(aExplode) are returned.
*/

pawn-lang.pdf is a wonderful thing:

Quote:
#pragma deprecated value
The subsequent symbol is flagged as “deprecated”. If a script uses it, the parser issues a warning.
However if this is a first release and that function is already deprecated - why include it in the first place? Also, there's a difference between "depreciated", which you used and "deprecated", which is the correct word here.
Y_Less is offline   Reply With Quote
Old 05/09/2009, 03:35 PM   #18
Brendan_Thomson
Huge Clucker
 
Join Date: Oct 2007
Posts: 292
Reputation: 2
Default Re: [INC] strlib - Simple string modification (Update: 27/07/09)

To fix up the bug with explode, I added a temporary solution.

Code:
/*
	explode:
		Creates an array of values from 'sSource', where only the exact amount of
		values matching sizeof(aExplode) are returned.

	Arguments:
		aExplode[][]  The exploded array
		sSource[]    Source string.
		sDelimiter   The string to use as the delimiter.
		iVertices    Vertices of aExplode, detected automatically.
		iLength     Length of a string in aExplode, detected automatically.

	Returns:
		Returns -1 on failure, otherwise success.
*/
stock explode(aExplode[][], sSource[], const sDelimiter[] = " ", iVertices = sizeof aExplode, iLength = sizeof aExplode[])
{
	strins(sSource, sDelimiter, strlen(sSource), 28);
	new
		iNode,
		iPointer,
		iPrevious = -1,
		iDelimiter = strlen(sDelimiter);
	
	while(iNode < iVertices)
	{
		iPointer = strfind(sSource, sDelimiter, false, iPointer);

		if(iPointer == -1)
		{
			strmid(aExplode[iNode], sSource, iPrevious, iLength, iLength);
			break;
		}
		else
		{
			strmid(aExplode[iNode], sSource, iPrevious, iPointer, iLength);
		}
		
		iPrevious = (iPointer += iDelimiter);
		++iNode;
	}
	
	return iPrevious;
}
Brendan_Thomson is offline   Reply With Quote
Old 13/09/2009, 10:09 AM   #19
Westie
Gangsta
 
Westie's Avatar
 
Join Date: Jul 2009
Posts: 863
Reputation: 63
Default Re: [INC] strlib - Simple string modification (Update: 27/07/09)

Quote:
Originally Posted by Y_Leѕѕ
Code:
/*
	(DEPRECIATED) chr_explode:
		Creates an array of values from 'sSource', where only the exact amount of
		values matching sizeof(aExplode) are returned.
*/

pawn-lang.pdf is a wonderful thing:

Quote:
#pragma deprecated value
The subsequent symbol is flagged as “deprecated”. If a script uses it, the parser issues a warning.
However if this is a first release and that function is already deprecated - why include it in the first place? Also, there's a difference between "depreciated", which you used and "deprecated", which is the correct word here.
I forgot about that directive. I haven't used it in such a long time, it escaped my mind. And it is the second release, but I decided to keep it in there. Because I use the word 'depreciation' a lot when talking about other words, 'i' tends to creep into there. Bla bla. I'll go ahead and fix/optimise it now.

_________________________________________________

Quote:
Originally Posted by Dabombber
...
Sorry for the late reply to that particular post.

Code:
main()
{
    print(str_replace("dog", "cat", "dog dog dog dog"));
    // Returns: cat cat cat dog
}

You're right, the last match doesn't get replaced. I fixed it with adding + 1 to a variable.
Westie is offline   Reply With Quote
Old 13/09/2009, 10:15 AM   #20
Calgon
Godfather
 
Calgon's Avatar
 
Join Date: Jan 2009
Posts: 6,475
Reputation: 1079
Default Re: [INC] strlib - Simple string modification (Update: 27/07/09)

Great work, I love the string functions in PHP, makes me miss how easy it is to format strings. Anyway, nice work .
__________________
Calgon is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Payday Modification SeQualX Help Archive 3 12/07/2010 03:31 PM
[FilterScript] [FS] Drift3r's Simple Vehicle Spawner *0.1b Update* ray8995 Filterscripts 20 15/05/2010 01:22 PM
[Modification] | SAMP Gui - Simple modification of samp Interface (InGame) LuxurioN™ Everything and Nothing 21 15/03/2010 01:55 PM
MOVED: SA-MP Server (EXE) modification - Do you like it? Woet Help Archive 0 02/11/2009 06:43 AM
GTA modification compatibility with SA-MP Lordician General 1 19/08/2009 07:03 PM


All times are GMT. The time now is 03:56 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.