strlib - String functions! -
Slice - 25.07.2012
strlib
String functions. This project is hosted on GitHub, and I encourage you to
contribute to this include.
Post your own string functions and I'll add them!
Download: At the
GitHub page.
Functions
- sprintf - Returns a formatted string.
- strgetc - Get a character from a specific index in a string (packed or not packed).
- isempty - Find out if a string is empty.
- isequal - Compare two strings.
- strexplode - Split a string by a given delimiter.
- strimplode - Glue together strings into one.
- strreplace - Replace occurrences of the search string with the replacement string.
- strtrim - Trim whitespace or a specific group of characters from a string.
- strcount - Count substrings.
- strfromliteral - Read a string from a PAWN string literal.
- strtoliteral - Build a PAWN string literal from a given string.
- strfrombin - Convert an array to a string.
- strtobin - Convert a string to an array.
- utf8encode/utf8decode - Encode/decode UTF-8 strings.
Examples
sprintf
pawn Код:
SetGameModeText(sprintf("Hello %s. %d %d %d.", "world", 1, 2, 3));
// GameMode text is now: Hello world. 1 2 3.
isequal
pawn Код:
new str1[] = "HELLO", str2[] = "hello";
if (isequal(str1, str2)) // false
if (isequal(str1, str2, .ignorecase = true)) // true
strexplode
pawn Код:
new output[10][10], count;
count = strexplode(output, "I, like, jolly, ranchers", ",");
for (new i = 0; i < count; i++)
print(output[i]);
/* Output:
I
like
jolly
ranchers
*/
strimplode
pawn Код:
new output[128];
strimplode(" ~~ ", output, sizeof(output), "I", "like", "jolly", "ranchers");
// output = "I ~~ like ~~ jolly ~~ ranchers"
strreplace
pawn Код:
new string[128] = "Hello world";
strreplace(string, "world", "earth"); // string = "Hello earth"
strreplace(string, "HELLO", "Hola"); // string = "Hello earth" (no match for "HELLO")
strreplace(string, "HELLO", "Hola", .ignorecase = true); // string = "Hola earth"
strtrim
pawn Код:
new string[128] = " XXbla blaY ";
strtrim(string); // string = "XXbla blaY"
strtrim(string, "XY"); // string = "bla bla"
string = "/Users/home/bla/bla/";
strtrim(string, "/\\", .edges = trim_right); // string = "/Users/home/bla/bla"
strcount
pawn Код:
new string[] = "cow COW cow COW sheep cow", count;
count = strcount(strong, "cow"); // count = 3
count = strcount(strong, "cow", .ignorecase = true); // count = 5
strfrombin
pawn Код:
new data[] = {0x11223344, 0x55667788, 0x99AABBCC, 0xDDEEFF00};
new string[128];
strfrombin(string, data); // string = "112233445566778899AABBCCDDEEFF00"
Re: strlib - String functions! -
FireCat - 25.07.2012
Love it!
pawn Код:
stock strrest(const string[], &index)
{
new length = strlen(string);
while ((index < length) && (string[index] <= ' '))
{
index++;
}
new offset = index;
new result[128];
while ((index < length) && ((index - offset) < (sizeof(result) - 1)))
{
result[index - offset] = string[index];
index++;
}
result[index - offset] = EOS;
return result;
}
Taken from gl_common
Re: strlib - String functions! - Glint - 25.07.2012
Slice do you ever think to stop releasing stuff man
OT good job man this can be useful.
Re: strlib - String functions! -
Kar - 25.07.2012
This is totally awesome

I already use the strreplace function, I'm gonna check out this strfrombin!
Re: strlib - String functions! -
IstuntmanI - 25.07.2012
Great library Slice, strreplace is the best from this.
Re: strlib - String functions! -
Lorenc_ - 27.07.2012
Nice job Slice!
Re: strlib - String functions! -
noobre - 28.07.2012
pawn Код:
SetGameModeText(sprintf("Hello %s. %d %d %d.", "world", 1, 2, 3));
// GameMode text is now: Hello world. 1 2 3.
Love this function! OMG.
Re: strlib - String functions! -
Ronaldo_raul™ - 28.07.2012
Woaw! I love this
AW: strlib - String functions! -
FufLa - 28.07.2012
Why did you have to use Jolly Rancher as an example... reminds of that one story I shall not post here...
Nice include anyways
Re: strlib - String functions! -
[MM]RoXoR[FS] - 30.07.2012
Dont know if needed.
I was given this as homework in school. Converted for pawn(never tested for pawn).
This use a binary search, returns location of char.
pawn Код:
stock strfind(a[],find,bool:ignorecase = false,size = -1)
{
if(size == -1) size = strlen(a);
new i,j,loc = -1;
for(i=0,j=size-1 ; i<size ;++i,--j)
{
if(ignorecase == true)
{
find = toupper(find);
if(toupper(a[i]) == find) {loc= i; break;}
if(toupper(a[j]) == find) {loc= j; break;}
}
else
{
if(a[i] == find {loc =i;break; }
if(a[j] == find{loc = j;break;}
}
}
return (loc +1);
}
Re: strlib - String functions! -
Slice - 30.07.2012
Quote:
Originally Posted by [MM]RoXoR[FS]
Dont know if needed.
I was given this as homework in school. Converted for pawn(never tested for pawn).
This use a binary search, returns location of char.
pawn Код:
stock strfind(a[],find,bool:ignorecase = false,size = -1) { if(size == -1) size = strlen(a); new i,j,loc = -1; for(i=0,j=size-1 ; i<size ;++i,--j) { if(ignorecase == true) { find = toupper(find); if(toupper(a[i]) == find) {loc= i; break;} if(toupper(a[j]) == find) {loc= j; break;} } else { if(a[i] == find {loc =i;break; } if(a[j] == find{loc = j;break;} } } return (loc +1); }
|
https://sampwiki.blast.hk/wiki/Strfind
Re: strlib - String functions! -
Ronaldo_raul™ - 31.07.2012
Speed differences ? Can this be taken into for optimizations ?
Re: strlib - String functions! -
Slice - 31.07.2012
The original strfind is a lot faster because it's written in C. I doubt a faster PAWN version could be made.
Re: strlib - String functions! -
Ronaldo_raul™ - 31.07.2012
Quote:
Originally Posted by Slice
The original strfind is a lot faster because it's written in C. I doubt a faster PAWN version could be made.
|
Ok, Thanks.
Re: strlib - String functions! -
Niko_boy - 31.07.2012
great set of functions

this got examples unlike Westle's Sstrlib :P
thanks for it !
Re: strlib - String functions! -
[KHK]Khalid - 01.08.2012
Not that useful but here they're anyway:
CountUppercaseLetters - counts all the uppercase letters (if there) in a string then returns a count.
pawn Код:
CountUppercaseLetters(str[])
{
new ct = 0;
for(new i = 0; i < strlen(str); i ++)
{
if(str[i] >= 'A' && str[i] <= 'Z')
ct ++;
}
return ct;
}
CountLowercaseLetters - counts all the lowercase letters (if there) in a string then returns a count.
pawn Код:
CountLowercaseLetters(str[])
{
new ct = 0;
for(new i = 0; i < strlen(str); i ++)
{
if(str[i] >= 'a' && str[i] <= 'z')
ct ++;
}
return ct;
}
CountLettersInString - counts how many letters in a string then returns a count.
pawn Код:
CountLettersInString(str[])
{
new ct = 0;
for(new i = 0; i < strlen(str); i ++)
{
if((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
ct ++;
}
return ct;
}
Re: strlib - String functions! -
Slice - 01.08.2012
I've actually avoided adding functions related to the alphabet, because it won't work properly with other languages (special characters and stuff).
Re: strlib - String functions! -
Amit_B - 01.08.2012
pawn Код:
#define sprintf(%1) \
(format(g_StrlibBuffer, sizeof(g_StrlibBuffer), %1), g_StrlibBuffer)
Can't believe that I have never thought of it. :>
Cool release
Re: strlib - String functions! -
[MM]RoXoR[FS] - 03.08.2012
Can you give example of strfromliteral and strtoliteral
Re: strlib - String functions! -
[MM]RoXoR[FS] - 03.08.2012
Quote:
Originally Posted by Ronaldo_raul™
Speed differences ? Can this be taken into for optimizations ?
|
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.