[Include] strlib - String functions!
#1

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"
Reply
#2

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
Reply
#3

Slice do you ever think to stop releasing stuff man

OT good job man this can be useful.
Reply
#4

This is totally awesome I already use the strreplace function, I'm gonna check out this strfrombin!
Reply
#5

Great library Slice, strreplace is the best from this.
Reply
#6

Nice job Slice!
Reply
#7

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.
Reply
#8

Woaw! I love this
Reply
#9

Why did you have to use Jolly Rancher as an example... reminds of that one story I shall not post here...

Nice include anyways
Reply
#10

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);
}
Reply
#11

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
Reply
#12

Speed differences ? Can this be taken into for optimizations ?
Reply
#13

The original strfind is a lot faster because it's written in C. I doubt a faster PAWN version could be made.
Reply
#14

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.
Reply
#15

great set of functions
this got examples unlike Westle's Sstrlib :P
thanks for it !
Reply
#16

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;
}
Reply
#17

I've actually avoided adding functions related to the alphabet, because it won't work properly with other languages (special characters and stuff).
Reply
#18

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
Reply
#19

Can you give example of strfromliteral and strtoliteral
Reply
#20

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.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)