Merge pull request #350 from Arkshine/feature/inline_fmt

Add fmt() native to format and return a string inline
This commit is contained in:
Vincent Herbet 2016-02-23 20:09:41 +01:00
commit 8d89104241
2 changed files with 38 additions and 1 deletions

View File

@ -1409,6 +1409,20 @@ static cell AMX_NATIVE_CALL vformat(AMX *amx, cell *params)
}
#define MAX_FMT_LENGTH 256
// native [MAX_FMT_LENGTH] fmt(const fmt[], any:...)
static cell AMX_NATIVE_CALL fmt(AMX *amx, cell *params)
{
int length;
const char *string = format_amxstring(amx, params, 1, length);
size_t num_params = *params / sizeof(cell);
set_amxstring_utf8_char(amx, params[num_params + 1], string, length, MAX_FMT_LENGTH - 1);
return 1;
};
AMX_NATIVE_INFO string_Natives[] =
@ -1420,6 +1434,7 @@ AMX_NATIVE_INFO string_Natives[] =
{"copyc", copyc},
{"equal", equal},
{"equali", equali},
{"fmt", fmt},
{"format", format},
{"formatex", formatex},
{"format_args", format_args},

View File

@ -25,6 +25,11 @@
* copy(string, charsmax(string), ...)
*/
/**
* Buffer size used by fmt().
*/
#define MAX_FMT_LENGTH 256
/**
* Calculates the length of a string.
*
@ -167,6 +172,23 @@ native format(output[], len, const format[], any:...);
*/
native formatex(output[], len, const format[], any:...);
/**
* Formats and returns a string according to the AMX Mod X format rules
* (see documentation).
*
* @note Example: menu_additem(menu, fmt("My first %s", "item")).
* @note This should only be used for simple inline formatting like in the above example.
* Avoid using this function to store strings into variables as an additional
* copying step is required.
* @note The buffer size is defined by MAX_FMT_LENGTH.
*
* @param format Formatting rules.
* @param ... Variable number of format parameters.
*
* @return Formatted string
*/
native [MAX_FMT_LENGTH]fmt(const format[], any:...);
/**
* Formats a string according to the AMX Mod X format rules (see documentation).
*