Kodi Documentation 22.0
Kodi is an open source media player and entertainment hub.
Loading...
Searching...
No Matches

Formatting functions
Used to output the given values in newly formatted text using functions. More...

Functions

static std::string kodi::tools::StringUtils::Format (const char *fmt,...)
 Returns the C++ string pointed by given format. If format includes format specifiers (subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers.
 
static std::wstring kodi::tools::StringUtils::Format (const wchar_t *fmt,...)
 Returns the C++ wide string pointed by given format.
 
static std::string kodi::tools::StringUtils::FormatV (PRINTF_FORMAT_STRING const char *fmt, va_list args)
 Returns the C++ string pointed by given format list.
 
static std::wstring kodi::tools::StringUtils::FormatV (PRINTF_FORMAT_STRING const wchar_t *fmt, va_list args)
 Returns the C++ wide string pointed by given format list.
 
static std::string kodi::tools::StringUtils::FormatFileSize (uint64_t bytes)
 Returns bytes in a human readable format using the smallest unit that will fit bytes in at most three digits. The number of decimals are adjusted with significance such that 'small' numbers will have more decimals than larger ones.
 
static std::string kodi::tools::StringUtils::BinaryStringToString (const std::string &in)
 Convert the string of binary chars to the actual string.
 
static std::string kodi::tools::StringUtils::ToHexadecimal (const std::string &in)
 Convert each character in the string to its hexadecimal representation and return the concatenated result.
 

Detailed Description

Formatting functions
Used to output the given values in newly formatted text using functions.

Function Documentation

◆ BinaryStringToString()

static std::string kodi::tools::StringUtils::BinaryStringToString ( const std::string & in)
inlinestatic

Convert the string of binary chars to the actual string.

Convert the string representation of binary chars to the actual string. For example \1\2\3 is converted to a string with binary char \1, \2 and \3

Parameters
[in]inString to convert
Returns
Converted string

◆ Format() [1/2]

static std::string kodi::tools::StringUtils::Format ( const char * fmt,
... )
inlinestatic

Returns the C++ string pointed by given format. If format includes format specifiers (subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers.

After the format parameter, the function expects at least as many additional arguments as specified by format.

Parameters
[in]fmtThe format of the text to process for output. C string that contains the text to be written to the stream. It can optionally contain embedded format specifiers that are replaced by the values specified in subsequent additional arguments and formatted as requested.
specifier Output Example
d or i Signed decimal integer 392
u Unsigned decimal integer 7235
o Unsigned octal 610
x Unsigned hexadecimal integer 7fa
X Unsigned hexadecimal integer (uppercase) 7FA
f Decimal floating point, lowercase 392.65
F Decimal floating point, uppercase 392.65
e Scientific notation (mantissa/exponent), lowercase 3.9265e+2
E Scientific notation (mantissa/exponent), uppercase 3.9265E+2
g Use the shortest representation: e or f 392.65
G Use the shortest representation: E or F 392.65
a Hexadecimal floating point, lowercase -0xc.90fep-2
A Hexadecimal floating point, uppercase -0XC.90FEP-2
c Character a
s String of characters sample
p Pointer address b8000000
% A % followed by another % character will write a single % to the stream. %
The length sub-specifier modifies the length of the data type. This is a chart showing the types used to interpret the corresponding arguments with and without length specifier (if a different type is used, the proper type promotion or conversion is performed, if allowed):
length d i u o x X f F e E g G a A c s p n
(none) int unsigned int double int char* void* int*
hh signed char unsigned char signed char*
h short int unsigned short int short int*
l long int unsigned long int wint_t wchar_t* long int*
ll long long int unsigned long long int long long int*
j intmax_t uintmax_t intmax_t*
z size_t size_t size_t*
t ptrdiff_t ptrdiff_t ptrdiff_t*
L long double
Note: that the c specifier takes an int (or wint_t) as argument, but performs the proper conversion to a char value (or a wchar_t) before formatting it for output.
[in]...(additional arguments)
Depending on the format string, the function may expect a sequence of additional arguments, each containing a value to be used to replace a format specifier in the format string (or a pointer to a storage location, for n).
There should be at least as many of these arguments as the number of values specified in the format specifiers. Additional arguments are ignored by the function.
Returns
Formatted string

Example:

std::string str = kodi::tools::StringUtils::Format("Hello {} {}", "World", 2020);
static std::string Format(const char *fmt,...)
Returns the C++ string pointed by given format. If format includes format specifiers (subsequences be...
Definition addons/kodi-dev-kit/include/kodi/tools/StringUtils.h:558

◆ Format() [2/2]

static std::wstring kodi::tools::StringUtils::Format ( const wchar_t * fmt,
... )
inlinestatic

Returns the C++ wide string pointed by given format.

Parameters
[in]fmtThe format of the text to process for output (see Format(const char* fmt, ...) for details).
[in]...(additional arguments)
Depending on the format string, the function may expect a sequence of additional arguments, each containing a value to be used to replace a format specifier in the format string (or a pointer to a storage location, for n).
There should be at least as many of these arguments as the number of values specified in the format specifiers. Additional arguments are ignored by the function.
Returns
Formatted string

◆ FormatFileSize()

static std::string kodi::tools::StringUtils::FormatFileSize ( uint64_t bytes)
inlinestatic

Returns bytes in a human readable format using the smallest unit that will fit bytes in at most three digits. The number of decimals are adjusted with significance such that 'small' numbers will have more decimals than larger ones.

For example: 1024 bytes will be formatted as "1.00kB", 10240 bytes as "10.0kB" and 102400 bytes as "100kB". See TestStringUtils for more examples.

Supported file sizes:

Value Short Metric
1 B byte
1024¹ kB kilobyte
1024² MB megabyte
1024³ GB gigabyte
1024 exp 4 TB terabyte
1024 exp 5 PB petabyte
1024 exp 6 EB exabyte
1024 exp 7 ZB zettabyte
1024 exp 8 YB yottabyte
Parameters
[in]bytesBytes amount to return as human readable string
Returns
Size as string

Example:

EXPECT_STREQ("0B", kodi::tools::StringUtils::FormatFileSize(0).c_str());
EXPECT_STREQ("999B", kodi::tools::StringUtils::FormatFileSize(999).c_str());
EXPECT_STREQ("0.98kB", kodi::tools::StringUtils::FormatFileSize(1000).c_str());
EXPECT_STREQ("1.00kB", kodi::tools::StringUtils::FormatFileSize(1024).c_str());
EXPECT_STREQ("9.99kB", kodi::tools::StringUtils::FormatFileSize(10229).c_str());
EXPECT_STREQ("10.1kB", kodi::tools::StringUtils::FormatFileSize(10387).c_str());
EXPECT_STREQ("99.9kB", kodi::tools::StringUtils::FormatFileSize(102297).c_str());
EXPECT_STREQ("100kB", kodi::tools::StringUtils::FormatFileSize(102400).c_str());
EXPECT_STREQ("999kB", kodi::tools::StringUtils::FormatFileSize(1023431).c_str());
EXPECT_STREQ("0.98MB", kodi::tools::StringUtils::FormatFileSize(1023897).c_str());
EXPECT_STREQ("0.98MB", kodi::tools::StringUtils::FormatFileSize(1024000).c_str());
EXPECT_STREQ("5.30EB", kodi::tools::StringUtils::FormatFileSize(6115888293969133568).c_str());
static std::string FormatFileSize(uint64_t bytes)
Returns bytes in a human readable format using the smallest unit that will fit bytes in at most three...
Definition addons/kodi-dev-kit/include/kodi/tools/StringUtils.h:756

◆ FormatV() [1/2]

static std::string kodi::tools::StringUtils::FormatV ( PRINTF_FORMAT_STRING const char * fmt,
va_list args )
inlinestatic

Returns the C++ string pointed by given format list.

Parameters
[in]fmtThe format of the text to process for output (see Format(const char* fmt, ...) for details).
[in]argsA value identifying a variable arguments list initialized with va_start.
Returns
Formatted string

◆ FormatV() [2/2]

static std::wstring kodi::tools::StringUtils::FormatV ( PRINTF_FORMAT_STRING const wchar_t * fmt,
va_list args )
inlinestatic

Returns the C++ wide string pointed by given format list.

Parameters
[in]fmtThe format of the text to process for output (see Format(const char* fmt, ...) for details).
[in]argsA value identifying a variable arguments list initialized with va_start.
Returns
Formatted string

◆ ToHexadecimal()

static std::string kodi::tools::StringUtils::ToHexadecimal ( const std::string & in)
inlinestatic

Convert each character in the string to its hexadecimal representation and return the concatenated result.

Example: "abc\n" -> "6162630a"

Parameters
[in]inString to convert
Returns
Converted string

Example:

EXPECT_STREQ("", kodi::tools::StringUtils::ToHexadecimal("").c_str());
EXPECT_STREQ("616263", kodi::tools::StringUtils::ToHexadecimal("abc").c_str());
std::string a{"a\0b\n", 4};
EXPECT_STREQ("6100620a", kodi::tools::StringUtils::ToHexadecimal(a).c_str());
std::string nul{"\0", 1};
EXPECT_STREQ("00", kodi::tools::StringUtils::ToHexadecimal(nul).c_str());
std::string ff{"\xFF", 1};
EXPECT_STREQ("ff", kodi::tools::StringUtils::ToHexadecimal(ff).c_str());
static std::string ToHexadecimal(const std::string &in)
Convert each character in the string to its hexadecimal representation and return the concatenated re...
Definition addons/kodi-dev-kit/include/kodi/tools/StringUtils.h:838