[Include] strlib - String functions!
#21

Quote:
Originally Posted by [MM]RoXoR[FS]
Посмотреть сообщение
I did a speed comparision, using this code
pawn Код:
main()
{
    new str[2048],f;
    for(new i = 0;i<2048;++i) str[i] = random(90-65)+65;
    new stime,ftime,loc;
    stime = GetTickCount();
    for(new i=0;i<10000000;++i)
    {
        f=random(90-65)+65;
        loc = strfind1(str,f);
    }
    ftime = GetTickCount();
    printf("strfind1 took %d ",ftime-stime);
   
    stime = GetTickCount();
    for(new i=0;i<10000000;++i)
    {
        f=random(90-65)+65;
        loc = strfind1(str,f);
    }
    ftime = GetTickCount();
    printf("strfind took %d ",ftime-stime);

}
Results were pretty same.
Код:
[15:55:34] strfind1 took 28715 
[15:56:03] strfind took 28718
So it would be useless only to use mine strfind seeing very little difference.
You used strfind1 twice.. lol..

Also, yours doesn't work properly. It returns 1 if found at 0, etc. It should return -1 if not found, 0 if at 0, 1 if at 1, etc..
Reply
#22

Quote:
Originally Posted by [MM]RoXoR[FS]
Посмотреть сообщение
I did a speed comparision, using this code
pawn Код:
main()
{
    new str[2048],f;
    for(new i = 0;i<2048;++i) str[i] = random(90-65)+65;
    new stime,ftime,loc;
    stime = GetTickCount();
    for(new i=0;i<10000000;++i)
    {
        f=random(90-65)+65;
        loc = strfind1(str,f);
    }
    ftime = GetTickCount();
    printf("strfind1 took %d ",ftime-stime);
   
    stime = GetTickCount();
    for(new i=0;i<10000000;++i)
    {
        f=random(90-65)+65;
        loc = strfind1(str,f);
    }
    ftime = GetTickCount();
    printf("strfind took %d ",ftime-stime);

}
Results were pretty same.
Код:
[15:55:34] strfind1 took 28715 
[15:56:03] strfind took 28718
So it would be useless only to use mine strfind seeing very little difference.
woaw!

I will head over to Slice's reaction upon this.

EDIT: was late -.-"
Reply
#23

Quote:
Originally Posted by Slice
Посмотреть сообщение
You used strfind1 twice.. lol..

Also, yours doesn't work properly. It returns 1 if found at 0, etc. It should return -1 if not found, 0 if at 0, 1 if at 1, etc..
Oh god never saw that I used strfind1 twice.

I made it return a greater value because my teacher told me that I should show location of char in the string.

Like if str is abc ,a=1,b=2 and so on.

I redid test and strfind is actually wayyy faster.
Reply
#24

Strswap to swap 2 strings given that they have the memory needed

pawn Код:
stock strswap(s1[],s2[])
{
    new g=0;
    if(strlen(s1) > strlen(s2)) g = 1;
    if(strlen(s2) > strlen(s1)) g = 2;
   
    switch(g)
    {
        case 0:
        {
            for(new i=0;s1[i]!='\0';++i)
            {
                new temp = s1[i];
                s1[i] = s2[i];
                s2[i] = temp;
            }
        }
        case 1:
        {
            for(new i=0;s1[i]!='\0';++i)
            {
                new temp = s1[i];
                s1[i] = s2[i];
                s2[i] = temp;
            }
        }
        case 2:
        {
            for(new i=0;s2[i]!='\0';++i)
            {
                new temp = s1[i];
                s1[i] = s2[i];
                s2[i] = temp;
            }
        }
    }
    return 1;
}
Example
pawn Код:
new a[10] = "Money";
new b[] = "Goods";

printf("Before swap, buyer has %s and seller have %s",a,b);
strswap(a,b);
printf("After swap, buyer has %s and seller have %s",a,b);
Result :
Код:
Before swap, buyer has money and seller has goods
 After swap, buyer has goods and seller has money
PS : I need example how to use strfromliteral and strtoliteral
Reply
#25

Copying strings can be done a lot faster using strcat, strpack, strunpack, or memcpy.

strtoliteral:
pawn Код:
new string[128] = "hello\nworld!\t\t\"abc\"";

strtoliteral(string, string);

print(string);

strfromliteral(string, string);

print(string);
Output:
Код:
"hello\nworld!\t\t\"abc\""
hello
world!		"abc"
Reply
#26

So strtoliteral just escapes the \n \t and other literals?
Reply
#27

http://en.wikipedia.org/wiki/String_literal

A string literal is practically the string you type in the source code.
Reply
#28

Update
All string functions now have similar functions with the same names, prefixed by "ret_".

pawn Код:
ret_strcatmid(string[], source[], start = 0, end = -1)
ret_strfrombin(input[], inputlength = sizeof(input))
ret_strimplode(glue[], ...)
ret_strreplace(string[], search[], replacement[], ignorecase = false, pos = 0, limit = -1)
ret_strfromliteral(input[], &pos = 0)
ret_strtoliteral(input[])
ret_strtrim(string[], chars[] = "", trim_edges:edges = trim_both)
ret_strurldecode(input[])
ret_strurlencode(input[], pack = false)
ret_strpack(source[])
ret_strunpack(source[])
ret_strcat(string1[], string2[])
ret_strmid(source[], start, end)
ret_strins(string[], substr[], pos)
ret_strdel(string[], start, end)
ret_valstr(value, pack = false)
ret_GetPlayerName(playerid, pack = false)
You can now do things like this:
pawn Код:
print(
    ret_strtoliteral(
        ret_strimplode(
            " ~ ",
            ret_strtrim(ret_strfromliteral("\"  hello   \"")),
            ret_strtrim("yyworldxx", "xy")
        )
    )
);
Output:
Код:
"hello ~ world"

The size of strings returned is determined by the constant "STRLIB_RETURN_SIZE" (default value: 12.

If you know you won't need that much, you can change this by defining STRLIB_RETURN_SIZE before including strlib:
pawn Код:
#define STRLIB_RETURN_SIZE 64
#include <strlib>
Reply
#29

I get this error, any idea?

pawn Код:
C:\Users\Milan\Downloads\pawno\pawno\include\strlib.inc(154) : error 025: function heading differs from prototype
C:\Users\Milan\Downloads\pawno\pawno\include\strlib.inc(154) : error 025: function heading differs from prototype
C:\Users\Milan\Downloads\pawno\pawno\include\strlib.inc(154) : error 025: function heading differs from prototype
C:\Users\Milan\Downloads\pawno\pawno\include\strlib.inc(154) : fatal error 107: too many error messages on one line
which is line forward
pawn Код:
strreplace(string[], const search[], const replacement[], bool:ignorecase = false, pos = 0, limit = -1, maxlength = sizeof(string));
Reply
#30

Do you have a "strreplace" function already?
Reply
#31

Hi
When i use this include console says this error:
Код:
[21:17:01]    Error: Function not registered: 'OnVehiclePaintjob'
[21:17:01] Script[gamemodes/slfd.amx]: Run time error 19: "File or function is not found"
http://forum.sa-mp.com/showthread.ph...11#post2042011
Reply
#32

That's.. weird. What other includes, plugins, etc. do you use?
Reply
#33

#include <a_samp> // SA-MP Team
#include <zcmd> // idk
#include <foreach>// Y_Less
#include <YSI\y_ini>// Y_Less
#include <YSI\y_hooks> // Y_Less
//#include <strlib> // Slice
#include <sqlitei> // Slice

All includes, plugin loaded Whirlpool
FS: noone
Reply
#34

DAMN GOOD WORK. Loved sprintf function.
Reply
#35

I really like this include, because it is very simple to use and you can easily manipulate with string.
I especially like strtoliteral and sprintf.
Reply
#36

strcount will an infinity loop, here is the fix:
Код:
stock
	strcount(const text[], const find[], bool:ignorecase = false)
{
	new
		pos = 0 - strlen(find),
		found = 0;
	
	while(-1 != (pos = strfind(text, find, ignorecase, pos + strlen(find))))
		found++;
		
	return found;
}
Reply
#37

Quote:
Originally Posted by Drake1994
Посмотреть сообщение
strcount will an infinity loop, here is the fix:
Код:
stock
	strcount(const text[], const find[], bool:ignorecase = false)
{
	new
		pos = 0 - strlen(find),
		found = 0;
	
	while(-1 != (pos = strfind(text, find, ignorecase, pos + strlen(find))))
		found++;
		
	return found;
}
Thanks. I made something similar to yours. strcount now has an additional parameter called "count_overlapped":
pawn Код:
printf("%d", strcount("abcabcabc", "abcabc"));
// output: 1
printf("%d", strcount("abcabcabc", "abcabc", .count_overlapped = true));
// output: 2
As you can see, by default it will not count strings that overlap (this is the most common default behavior).
Reply
#38

like
Reply
#39

strlib.inc(154) : error 025: function heading differs from prototype
strlib.inc(154) : error 025: function heading differs from prototype
strlib.inc(154) : error 025: function heading differs from prototype
strlib.inc(154) : fatal error 107: too many error messages on one line
Reply
#40

What other include files do you use?
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)