[Include] formatex - Improved "format" function!
#1

Hey,

After seeing a great new feature in sscanf that allows you to create custom specifiers, I figured it was only right to do the same to format and printf!

For example, the following code could be made a lot shorter:
pawn Код:
// This will put into "msg" the weapon you were given by a player, and the color of the player's name will match the player's color.

new msg[128], name[MAX_PLAYER_NAME], color;

color = GetPlayerColor(playerid);

GetPlayerName(playerid, name, sizeof(name));

format(msg, sizeof(msg), "You were given a %s by {%06x}%s", g_WeaponNameArray[weapon], color >>> 8, name);
This will do the same thing:
Код:
new msg[128];

format(msg, _, "You were given %w by %P", weapon, playerid);

// msg is for example: You were given an M4 by Slice
New specifiers
SpecifierDescription
%pName of the player ID given.
%PName of the player ID given, with the player's color before it.
%CInline color (ex. {FFFFFF}) from a normal color (ex. colors you get from GetPlayerColor).
%vVehicle model name from the model given (not vehicle ID, vehicle model).
%wWeapon name, lower-case singular (to be used in sentences). Example: "an M4", "a combat shotgun", "a knife".
%WWeapon's name.
%X8-byte, unsigned hex string (ex. FFFFFFFF).
%uUnsigned integer.
You can create your own specifiers!
You can do this very easily; example of one that puts an upper-case string at %S:
Add this anywhere outside of a function:
pawn Код:
// Upper-case string
FormatSpecifier<'S'>(output[], const param[]) {
    for (new i = 0, l = min(sizeof(output), strlen(param)); i < l; i++)
        output[i] = toupper(param[i]);
}
You can now do this:
pawn Код:
printf("hello %S!", "world");

// prints: hello WORLD!
If you want an integer, float, or anything else instead of a string you just change it:
pawn Код:
// You can call the argument whatever you like, and it can be a string/array:
FormatSpecifier<'A'>(output[], Float:health) { ... }
FormatSpecifier<'B'>(output[], objectid) { ... }
FormatSpecifier<'C'>(output[], playerid) { ... }
FormatSpecifier<'D'>(output[], Text:td) { ... }
FormatSpecifier<'D'>(output[], const string[]) { ... }
Additional notes
Worth mentioning is this is a superset of format, meaning it has exactly all the features of format and those behave like always (the only exception is it's a bit friendlier to packed strings, though %s still doesn't support it).
It actually uses the original native "format" function to do the heavy lifting.

Download

Download: http://pastebin.com/xMAMtB6M
Reply
#2

Kewl, I actually need this.
Reply
#3

very nice, i gonna use it for sure

edit: Why you use "GetMaxPlayers" instead of "MAX_PLAYERS" ??
Reply
#4

Great.Really Great.Don't know what to say anymore.....
Reply
#5

Quote:
Originally Posted by kizla
Посмотреть сообщение
very nice, i gonna use it for sure

edit: Why you use "GetMaxPlayers" instead of "MAX_PLAYERS" ??
Well I was using it to see if the player ID was in range. It can't be more than GetMaxPlayers, so that's why I used it.
Reply
#6

There you go, another awesome release. I'm obviously gonna use it.
Reply
#7

Nice one, Slice!

rep+
Reply
#8

Yet another useful release. Nice work Slice(The next ******).
Reply
#9

awesome,thanks
Reply
#10

fucking awesome man

rep+

edit: shit I can't give rep
Reply
#11

Nice release! You're making great usage for emit!
Reply
#12

awesome and useful include.
Reply
#13

Amazing work there!
Reply
#14

awesome


Reply
#15

Fabulous work Slice!
Reply
#16

I have understand everything LOL
Reply
#17

>Inline color from normal color.
Slice, I love you. I love you from the bottom of my heart.
+rep every day for the rest of infinity
Reply
#18

very nice, useful, thanks
Reply
#19

Here's a neat way to always have nicely formatted money values!

Example:
pawn Код:
printf("You just earned %m. You now have a total of %m in your bank.", 12000, 554279811);
printf("%m", 1000000000);
printf("%m", 100000000);
printf("%m", 10000000);
printf("%m", 1000000);
printf("%m", 100000);
printf("%m", 10000);
printf("%m", 1000);
printf("%m", 100);
printf("%m", 10);
printf("%m", 1);
printf("%m", 0);
Output:
Код:
You just earned $12,000. You now have a total of $554,279,811 in your bank.
$1,000,000,000
$100,000,000
$10,000,000
$1,000,000
$100,000
$10,000
$1,000
$100
$10
$1
$0
Specifier code:
pawn Код:
// Money
FormatSpecifier<'m'>(output[], amount) {
    if (amount) {
        new
            i = 18,
            neg = amount < 0
        ;
       
        // The spaces must be there, otherwise strdel below won't work
        // on lower numbers.
        output = "$                ";
       
        // Null-out the end of it
        output[i] = 0;
       
        if (neg)
            amount = -amount;
       
        // Going right-left, add one number each time
        while (amount) {
            // Add a thousand separator every 3rd time
            if (!((i + 1) % 4))
                output[--i] = ',';
           
            // Now add the last digit of the number
            output[--i] = '0' + (amount % 10);
           
            // Then divide the number by 10, so digit in the end will be gone
            amount /= 10;
        }
       
        // Delete the spaces between the $-sign and the first (last) number
        strdel(output, 1, i);
       
        // Add a minus sign if needed
        if (neg)
            strins(output, "-", 1);
    } else {
        output = "$0";
    }
}
Reply
#20

Kewl i knew that you will / or any one will make a thing like thAT
Its very kewl awesome buddy!
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)