stock GetBiggestArrayNumber(array[], len = sizeof(array))
{
new
biggest = cellmin
;
for(new i ; i != len; ++i)
{
if(array[i] > biggest)
{
biggest = array[i];
}
}
return biggest;
}
stock GetSmallestArrayNumber(array[], len = sizeof(array))
{
new
smallest = cellmax
;
for(new i ; i != len; ++i)
{
if(array[i] < smallest)
{
smallest = array[i];
}
}
return smallest;
}
new Float:MaxHealth[MAX_PLAYERS];
stock SetPlayerMaxHealth(playerid, Float:maxhealth)
{
new name[24]; GetPlayerName(playerid,name,24);
MaxHealth[playerid] = maxhealth;
printf("%s (%d) Has A New Max Health Which Is %f.",name,playerid,maxhealth);
return 1;
}
new Float:MaxHealth[MAX_PLAYERS];
stock GetPlayerMaxHealth(playerid, &Float:maxhealth)
{
new name[24]; GetPlayerName(playerid,name,24);
printf("%s (%d) Max Health Is %f",name,playerid);
return MaxHealth[playerid];
}
stock CompassCorners(Float:X, Float:Y, Float:TargetX, Float:TargetY) return (X < TargetX) ? (Y < TargetY ? 1 : 2) : (Y < TargetY ? 3 : 4);
stock CompassCorners(Float:a,Float:b,Float:c,Float:d)return a<c?b<d?1:2:b<d?3:4;
CompassCorners(Float:a,Float:b,Float:c,Float:d)return a<c?b<d?1:2:b<d?3:4;
I don't understand why you guys repost the corrected code by me over and over with different ways of spacing, tabbing. What's the point of this?
For me the code without { } looks ugly. That's how I am. I don't care how anyone else is feeling about this. |
stock countControlCharacters(character, inSz[]) {
new
ccCount;
switch(character) {
case 'n': {
for(new it = 0; it < strlen(inSz); it++) {
if(inSz[it] == '\n') ccCount++;
}
}
case 'r': {
for(new it = 0; it < strlen(inSz); it++) {
if(inSz[it] == '\r') ccCount++;
}
}
case 't': {
for(new it = 0; it < strlen(inSz); it++) {
if(inSz[it] == '\t') ccCount++;
}
}
default: {
print("Critical error: Type undefined for countControlCharacters.");
}
}
return ccCount;
}
if(!(inputtext[0] == 0 || (inputtext[0] == 1 && inputtext[1] == 0))) for(new strPos; inputtext[strPos] > 0; strPos++) if(inputtext[strPos] == '%') inputtext[strPos] = ' ';
Bench for Looping the string: executes, by average, 159 times/ms (159164 times, 1000 ms). Bench for Using strfind: executes, by average, 825 times/ms (825567 times, 1000 ms).
CountChars( haystack[], needless[] ); // Counts each char in the needless string and returns the number of matches.
CountMatches( haystack[](, needle[], ...) ); // Counts the string(s) passed after the haystack - it's possible to match up to 62 strings.
stock CountChars( const szString[ ], const szChars[ ] )
{
new iCount = 0, iPos;
for ( new szSearchChar[ 2 ], i = 0; ( szSearchChar[ 0 ] = szChars[ i ] ); i++ )
{
iPos = -1;
while ( ( iPos = strfind( szString, szSearchChar, false, ++iPos ) ) != -1 )
++iCount;
}
return iCount;
}
stock CountMatches( const szString[ ], bool:bIgnoreCase = false, ... )
{
new iMatches, szBuffer[ 64 ], i, iArgs, c, j, iPos;
iMatches = 0;
iArgs = numargs( );
for ( i = 2; i < iArgs; i++ )
{
for ( j = 0; ( c = getarg( i, j ) ); j++ )
szBuffer[ j ] = c;
szBuffer[ j + 1 ] = 0;
iPos = -1;
while ( ( iPos = strfind( szString, szBuffer, bIgnoreCase, ++iPos ) ) != -1 )
++iMatches;
}
return iMatches;
}
public OnFilterScriptInit( )
{
printf( "%d", CountChars( "I\nLike\nNew\nLines\n:D.\t\t\t\ttabs!", "\n\t" ) );
printf( "%d", CountMatches( "I like to repeat myself. I like to repeat myself. I like to repeat myself.", false, "I", "repeat" ) );
new iMatches = CountMatches( "Fuckin.. What the fuckin fuck?! Who the fuck fucked this fucking...?? How did you two fucking fucks...?! FUCK!!", false, "fuck" );
if ( iMatches > 5 )
printf( "Enough cursing!!" );
}
stock strreplace2(string[], const find[], const replace[], bool:ignorecase=false, count=cellmax, maxlength=sizeof(string))
{
if(isnull(find) || isnull(string)) return 0;
new matches;
for(new idx, flen = strlen(find), rlen = strlen(replace), pos = strfind(string, find, ignorecase); pos != -1 && idx < maxlength && matches < count; pos = strfind(string, find, ignorecase, idx))
{
strdel(string, pos, pos + flen);
if(rlen) strins(string, replace, pos, maxlength);
idx = pos + rlen;
matches++;
}
return matches;
}
haha, I'd never thought strfind would be so damn fast. Pays to try these things I guess
|
GetParam( string[], param_num );
new params[] = " I would like to \t\t\nhave\n 50 bags of cheetos.\r\n";
printf( "1: \"%s\"", GetParam( params, 1 ) ); // Prints "I"
printf( "2: \"%s\"", GetParam( params, 2 ) ); // Prints "would"
printf( "3: \"%s\"", GetParam( params, 3 ) ); // Prints "like"
printf( "4: \"%s\"", GetParam( params, 4 ) ); // Prints "to"
printf( "5: \"%s\"", GetParam( params, 5 ) ); // Prints "have"
printf( "6: \"%s\"", GetParam( params, 6 ) ); // Prints "50"
printf( "7: \"%s\"", GetParam( params, 7 ) ); // Prints "bags"
printf( "8: \"%s\"", GetParam( params, 8 ) ); // Prints "of"
printf( "9: \"%s\"", GetParam( params, 9 ) ); // Prints "cheetos."
stock GetParam( const szString[ ], iParam )
{
new iSearchPos, iPos, iHoles, szReturn[ 128 ], iFoundParam = -1;
if ( szString[ 0 ] <= 1 ) // Treat \1 as a null character for ZCMD/YCMD compatibility.
return szReturn; // If the string is empty there's no point in looking for stuff.
iPos = -1;
while ( 2 <= szString[ ++iPos ] <= ' ' ) {} // Trim the beginning of the string.
if ( --iParam == 0 ) // Wether or not we're looking for the first parameter.
{
if (
( iSearchPos = strfind( szString, " ", _, iPos ) ) != -1 // Search for a space/tab
|| ( iSearchPos = strfind( szString, "\t", _, iPos ) ) != -1
)
strcat( szReturn, szString[ iPos ], iSearchPos + 1 - iPos ); // Put the found part into the return.
else
strcat( szReturn, szString[ iPos ] ); // Put the whole thing (except for the first tabs/spaces, if any) into the return.
}
else
{
while (
( iSearchPos = strfind( szString, " ", _, iPos ) ) != -1 // Search for a space/tab
|| ( iSearchPos = strfind( szString, "\t", _, iPos ) ) != -1
)
{
back_in_action:
iPos = iSearchPos;
while ( 2 <= szString[ ++iPos ] <= ' ' ) {} // Skip any whitespace.
if ( iFoundParam != -1 ) // If the previous iteration found a param..
{
strcat( szReturn, szString[ iFoundParam ], iPos - iFoundParam ); // Put it into the return string.
iFoundParam = -1;
break; // Then BREAK YOSELF.
}
if ( ++iHoles == iParam ) // If this current "hole" is in front of the param we're looking for..
iFoundParam = iPos; // ..let the script know that.
}
if ( iFoundParam != -1 ) // If a param was found in the last iteration.. (end of the string)
{
iSearchPos = strlen( szString ); // Set the next position to the end of the string.
goto back_in_action;
}
}
if ( szReturn[ 0 ] )
{
// Trim any whitespace on the right side.
for ( iPos = strlen( szReturn ) - 1; iPos >= 0 && szReturn[ iPos ] <= ' '; iPos-- )
szReturn[ iPos ] = 0;
}
return szReturn;
}
pawn Код:
|
I use goto sometimes to jump back into loops. I have extensively tested its stability and never found any issues as long as I use it carefully. In assembly, a while-loop ultimately uses the same asm opcodes to loop as goto does.
|
I use goto sometimes to jump back into loops. I have extensively tested its stability and never found any issues as long as I use it carefully. In assembly, a while-loop ultimately uses the same asm opcodes to loop as goto does.
I'm going to hide now, because I have a feeling someone with a HL2-style logo won't like my using goto! |