[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


Messages In This Thread
formatex - Improved "format" function! - by Slice - 26.01.2012, 08:00
Re: formatex - Improved "format" function! - by KingHual - 26.01.2012, 08:02
Re: formatex - Improved "format" function! - by kizla - 26.01.2012, 08:03
Re: formatex - Improved "format" function! - by [HK]Ryder[AN] - 26.01.2012, 08:05
Re: formatex - Improved "format" function! - by Slice - 26.01.2012, 08:10
Re: formatex - Improved "format" function! - by iPLEOMAX - 26.01.2012, 08:19
Re: formatex - Improved "format" function! - by fiki574 - 26.01.2012, 08:30
Re: formatex - Improved "format" function! - by T0pAz - 26.01.2012, 08:37
Re: formatex - Improved "format" function! - by Kaperstone - 26.01.2012, 08:48
Re: formatex - Improved "format" function! - by System64 - 26.01.2012, 09:02
Re: formatex - Improved "format" function! - by Lorenc_ - 26.01.2012, 09:07
Re: formatex - Improved "format" function! - by [Diablo] - 26.01.2012, 10:56
Re: formatex - Improved "format" function! - by [XST]O_x - 26.01.2012, 13:46
Re: formatex - Improved "format" function! - by hossa - 26.01.2012, 14:13
Re: formatex - Improved "format" function! - by iZN - 26.01.2012, 14:24
Re: formatex - Improved "format" function! - by TheArcher - 26.01.2012, 16:29
Re: formatex - Improved "format" function! - by TheBetaFox - 26.01.2012, 16:39
Respuesta: formatex - Improved "format" function! - by [Nikk] - 26.01.2012, 16:49
Re: formatex - Improved "format" function! - by Slice - 29.01.2012, 13:32
Re: formatex - Improved "format" function! - by Niko_boy - 29.01.2012, 14:26
Re: formatex - Improved "format" function! - by DeathTone - 19.02.2012, 02:00
Re: formatex - Improved "format" function! - by Slice - 19.02.2012, 10:33
Re: formatex - Improved "format" function! - by Shelby - 19.02.2012, 23:05
Re: formatex - Improved "format" function! - by shitbird - 21.02.2012, 15:03
Re: formatex - Improved "format" function! - by Marricio - 21.02.2012, 19:54
Re: formatex - Improved "format" function! - by Joey^ - 03.03.2012, 16:49
Re: formatex - Improved "format" function! - by Niko_boy - 04.06.2012, 14:28
Re: formatex - Improved "format" function! - by Slice - 04.06.2012, 14:40
Re: formatex - Improved "format" function! - by Slice - 04.06.2012, 16:35
Re: formatex - Improved "format" function! - by QuaTTrO - 22.07.2013, 07:33
Re: formatex - Improved "format" function! - by Omirrow - 23.07.2013, 19:38
Re: formatex - Improved "format" function! - by Darnell - 24.07.2013, 13:07
Re: formatex - Improved "format" function! - by Ryan_Bowe - 14.03.2015, 20:55
Re: formatex - Improved "format" function! - by Dayvison_ - 01.04.2016, 19:17
Re: formatex - Improved "format" function! - by bsHoX - 03.04.2016, 03:18
Re: formatex - Improved "format" function! - by AndySedeyn - 02.10.2016, 10:02
Re: formatex - Improved "format" function! - by Adiiti - 20.08.2017, 14:55

Forum Jump:


Users browsing this thread: 2 Guest(s)