Useful Functions

What about this:

pawn Код:
public OnPlayerText( playerid, text[ ] )
{
    new
        bool:bInSpaces,
             iSpaces,
             i = -1
    ;
   
    while ( text[ ++i ] )
    {
        if ( text[ i ] <= ' ' )
        {
            if ( !bInSpaces )
            {
                bInSpaces = true;
               
                iSpaces = 0;
            }
           
            ++iSpaces;
        }
        else
        {
            if ( bInSpaces )
            {
                bInSpaces = false;
               
                if ( iSpaces > 1 )
                {
                    strdel( text, i - iSpaces, i - 1 );
                   
                    i -= iSpaces;
                }
            }
        }
    }
   
    SendPlayerMessageToAll( playerid, text );
   
    return 0;
}
Reply

Quote:
Originally Posted by willsuckformoney
Посмотреть сообщение
About to test, keeping the ~ that you made since I'm to lazy to delete it.

EDIT: The space still doesn't work.
Well, I wrote quickly a trimString function. Just like in C#. Here you go:
pawn Код:
stock trimString(string[])
{
    new
        trimStart,
        trimEnd,
        inputLength = strlen(string)
    ;
    for( ; trimStart < inputLength; ++trimStart)
    {
        switch(string[trimStart])
        {
            case '\n', '\t', '\f', '\r', ' ': continue;
            default: break;
        }
    }
    for(trimEnd = (inputLength - 1); trimEnd > trimStart; --trimEnd)
    {
        switch(string[trimEnd])
        {
            case '\n', '\t', '\f', '\r', ' ': continue;
            default: break;
        }
    }
    strmid(string, string, trimStart, (trimEnd + 1), cellmax);
    return 1;
}
Usage:
pawn Код:
public OnPlayerText(playerid, text[])
{
    trimString(text);
    return 1;
}
Also, I just tested and it worked perfect.
Reply

Was just testing the "new writing style".

Код:
private function String::ToInteger(const string[])
private function String::Copy(const string[], destination[])
private function Integer::ToString(int)
private function bool:Integer::ToBoolean(int)
pawn Код:
#define String:: str_
#define Integer:: int_
#define function
#define private static stock

private function String::ToInteger(const string[]) {
    return strval(string);
}

private function String::Copy(const string[], destination[]) {
    for(new i=0; i < strlen(string); ++i) destination[i] = string[i];
}
   
private function Integer::ToString(int) {
    new output[32]; format(output, sizeof(output), "%i", int); return output;
}

private function bool:Integer::ToBoolean(int) {
    return (int > 0) ? (true) : (false);
}
Reply

Don't try be smart and code like this,code can be much faster and easier written.
Reply

Quote:
Originally Posted by [HLF]Southclaw
Посмотреть сообщение
Takes too long to write, seems to be no advantage over just doing it normally
I'll stick to the usual way

Maybe practical things, like making forward/publics all in one definition
or shortening certain things for faster writing, but this just makes it a lot longer.
I use that sometimes. For example, when I split up my code in "fake classes" so I have for example GUI::Initialize(). If anything is wrong with GUI-functions, I know where to find them instantly; also, their names doesn't collide with other names and it looks better than an underscore.



RyDeR, seeing as you've been all picky about performance in your latest posts, I'll go back to the trim function you made.
You're returning the string that you're modifying, which is pointless for the usage of that function.

If you don't return the strings, the function can be executed 55574 times more each second.
Reply

@g_aSlice:
Alright, I didn't know that the return value had some effect on the performance. I'll change it.
Thanks for the information though.
Reply

Quote:
Originally Posted by ******
Посмотреть сообщение
What the heck is that? There is no way that can work - the function doesn't even take an array as a parameter. Also, why use 128 and format for a string that can never be longer than 11 characters? And strlen doesn't work with arrays because it's "STRING LENGTH", and arrays aren't strings.
Yeah, it works, try it out.

I'll change the string size, thanks, and yes, I know that 'strlen' is STRING LENGHT.
Reply

pawn Код:
stock arrlen(&constarray)
{
    new tmp[12];
    format(tmp, 12, "%d", constarray);
    return strlen(tmp);
}
It compiles fine and returns the correct value to me.
Reply

Quote:
Originally Posted by The_Moddler
Посмотреть сообщение
pawn Код:
stock arrlen(&constarray)
{
    new tmp[12];
    format(tmp, 12, "%d", constarray);
    return strlen(tmp);
}
It compiles fine and returns the correct value to me.
..??!

Your function is converting the 1st argument to a string, then returning the number of characters. How would that be related in any way to the array's length?
Reply

Try this:

pawn Код:
new array;

main()
{
    array = 894645;
    printf("%d", arrlen(array));
}
It will output 6.
Reply

That' not an array at all, but a normal integer variable. Example of an array: {1,2,3,4,5,6,7,8,9,10,11}
Reply

Aw fuck my knowledge, sorry guys :$
Reply

Quote:
Originally Posted by pliva_sb
Посмотреть сообщение
Don't try be smart and code like this,code can be much faster and easier written.
What you want to prove? I don't insult you, so please if I can ask you to be so kind not to insult me. If you don't find this way of writing useful, then don't use that definitions. It's simple as that. I know PAWN is not a OO language and there is no need for this style of writing, but (IMO) this looks way better than normal writing "style".
Reply

Edited..
Reply

HoodRat, I don't think that will work.

You're creating a variable. Then using the switch function. It will always be 0.
Reply

Quote:
Originally Posted by Retardedwolf
Посмотреть сообщение
HoodRat, I don't think that will work.

You're creating a variable. Then using the switch function. It will always be 0.
I know, failed once again.

I created it once, but I lost the code somewhere, so I can't retrieve it back.

Hmm I'll think of something else..
Reply

Modulus for variables with the Float tag:
pawn Код:
stock Float:operator%(Float:dividend, Float:divisor)
    return (dividend - ((((dividend / divisor) - floatfract(dividend / divisor)) + float((_:(dividend / divisor) & cellmin) != 0)) * divisor));
Example usage -
"Normalize" an angle (in degrees):
pawn Код:
NormalizeAngle(&Float:angle)
{
    angle = angle % 360.0;

    if(angle < 0.0)
        angle += 360.0;
}
Enjoy.
Reply

Angle can't be below 0 after a proper modulus though, can it?
Reply

Quote:
Originally Posted by g_aSlice
Посмотреть сообщение
Angle can't be below 0 after a proper modulus though, can it?
It can be.

Quote:

The message you have entered is too short. Please lengthen your message to at least 8 characters.

Reply

Yeah, well, if the congruence (2nd value) is negative; which, in this case, it isn't.
Reply


Forum Jump:


Users browsing this thread: 19 Guest(s)