Ru:Format Class Functions (SourceMod Scripting)

From AlliedModders Wiki
Revision as of 12:44, 27 December 2008 by Frenzzy (talk | contribs) (Введение)
Jump to: navigation, search

Введение

Формат классов функций, аргументов переменной - функции в SourceMod которые позволяют Вам форматировать строки. Простой пример этой Format() функции, выглядит следующим образом:

decl String:buffer[512];
Format(buffer, sizeof(buffer), "Ваше имя: %s", userName);

Если userName содержит "Frenzzy", содержимое buffer будет: "Ваше имя: Frenzzy". Прототип этих функций почти всегда содержит следующие параметры:

const String:fmt[], {Handle,Float,_}:...

Например, обратите внимание на эти два выражения:

native Format(String:buffer[], maxlength, const String:fmt[], {Handle,Float,_}:...);
native PrintToClient(client, String:fmt[], {Handle,Float,_}:...);

Таким образом, PrintToClient является формат-классовой функцией. Она может быть использована точно так же, как было показано ранее:

PrintToClient(client, "Ваше имя: %s", userName);

Format Specifiers

A format specifier is a code that allows you to specify what data-type to print. The most common specifiers are:

  • Numerical
    • d or i: Integer number as decimal
    • b: Binary digits in the value
    • f: Floating-point number
    • x or X: Hexadecimal representation of the binary value (capitalization affects hex letter casing)
  • Character(s)
    • s: String
    • t or T: Translates a phrase (explained in Inline Translations)
    • c: Prints one character (UTF-8 compliant)
  • Special
    • L: Requires a client index; expands to 1<2><3><> where 1 is the player's name, 2 is the player's userid, and 3 is the player's Steam ID. If the client index is 0, the string will be: Console<0><Console><Console>
    • N: Requires a client index; expands to a string containing the player's name. If the client index is 0, the string will be: Console

Испрльзование

Format specifiers are denoted with a '%s' symbol. For example, to print a float, a number, and a string, you might use this code:

new Float:fNum = 5.0;
new iNum = 5
new String:str[] = "5"
 
PrintToClient(client, "Number: %d Float: %f String: %s", iNum, fNum, str);

Note: Using the wrong data type with a specifier can be very dangerous. Always make sure you are printing as the right type. For example, specifying a string and passing a number can crash the server.

Расширенное форматирование

Format specifiers have an extended syntax for controlling various aspects of how data is printed. The full syntax is: %[flags][width][.precision]specifier

Each bracketed section is an optional extension. Explanations of supported SourceMod format extensions:

  • %: Obviously, this is always required.
  • flags:
    • -: Left-justify (right-justify is set by default)
    • 0: Pads with 0s instead of spaces when needed (see width below).
  • width: Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.
  • precision:
    • For integers: specifies the minimum number of digits to print (or pad with spaces/zeroes if below the minimum).
    • For strings: specifies the maximum number of characters to print.
    • For floats: specifies the number of digits to be printed after the decimal point.
    • For all other types: no effect.
  • specifier: character specifying the data type (always required).

For more information, see printf from the C Standard Library, although not all modes are supported from C.