Kodi Documentation 18.0
Kodi is an open source media player and entertainment hub.
StringUtils Class Reference

#include <StringUtils.h>

Static Public Member Functions

template<typename... Args>
static std::string Format (const std::string &fmt, Args &&... args)
 Get a formatted string similar to sprintf. More...
 
template<typename... Args>
static std::wstring Format (const std::wstring &fmt, Args &&... args)
 
static std::string FormatV (PRINTF_FORMAT_STRING const char *fmt, va_list args)
 
static std::wstring FormatV (PRINTF_FORMAT_STRING const wchar_t *fmt, va_list args)
 
static void ToUpper (std::string &str)
 
static void ToUpper (std::wstring &str)
 
static void ToLower (std::string &str)
 
static void ToLower (std::wstring &str)
 
static void ToCapitalize (std::string &str)
 
static void ToCapitalize (std::wstring &str)
 
static bool EqualsNoCase (const std::string &str1, const std::string &str2)
 
static bool EqualsNoCase (const std::string &str1, const char *s2)
 
static bool EqualsNoCase (const char *s1, const char *s2)
 
static int CompareNoCase (const std::string &str1, const std::string &str2)
 
static int CompareNoCase (const char *s1, const char *s2)
 
static int ReturnDigits (const std::string &str)
 
static std::string Left (const std::string &str, size_t count)
 
static std::string Mid (const std::string &str, size_t first, size_t count=std::string::npos)
 
static std::string Right (const std::string &str, size_t count)
 
static std::string & Trim (std::string &str)
 
static std::string & Trim (std::string &str, const char *const chars)
 
static std::string & TrimLeft (std::string &str)
 
static std::string & TrimLeft (std::string &str, const char *const chars)
 
static std::string & TrimRight (std::string &str)
 
static std::string & TrimRight (std::string &str, const char *const chars)
 
static std::string & RemoveDuplicatedSpacesAndTabs (std::string &str)
 
static int Replace (std::string &str, char oldChar, char newChar)
 
static int Replace (std::string &str, const std::string &oldStr, const std::string &newStr)
 
static int Replace (std::wstring &str, const std::wstring &oldStr, const std::wstring &newStr)
 
static bool StartsWith (const std::string &str1, const std::string &str2)
 
static bool StartsWith (const std::string &str1, const char *s2)
 
static bool StartsWith (const char *s1, const char *s2)
 
static bool StartsWithNoCase (const std::string &str1, const std::string &str2)
 
static bool StartsWithNoCase (const std::string &str1, const char *s2)
 
static bool StartsWithNoCase (const char *s1, const char *s2)
 
static bool EndsWith (const std::string &str1, const std::string &str2)
 
static bool EndsWith (const std::string &str1, const char *s2)
 
static bool EndsWithNoCase (const std::string &str1, const std::string &str2)
 
static bool EndsWithNoCase (const std::string &str1, const char *s2)
 
template<typename CONTAINER >
static std::string Join (const CONTAINER &strings, const std::string &delimiter)
 
static std::vector< std::string > Split (const std::string &input, const std::string &delimiter, unsigned int iMaxStrings=0)
 Splits the given input string using the given delimiter into separate strings. More...
 
static std::vector< std::string > Split (const std::string &input, const char delimiter, size_t iMaxStrings=0)
 
static std::vector< std::string > Split (const std::string &input, const std::vector< std::string > &delimiters)
 
template<typename OutputIt >
static OutputIt SplitTo (OutputIt d_first, const std::string &input, const std::string &delimiter, unsigned int iMaxStrings=0)
 Splits the given input string using the given delimiter into separate strings. More...
 
template<typename OutputIt >
static OutputIt SplitTo (OutputIt d_first, const std::string &input, const char delimiter, size_t iMaxStrings=0)
 
template<typename OutputIt >
static OutputIt SplitTo (OutputIt d_first, const std::string &input, const std::vector< std::string > &delimiters)
 
static std::vector< std::string > SplitMulti (const std::vector< std::string > &input, const std::vector< std::string > &delimiters, unsigned int iMaxStrings=0)
 Splits the given input strings using the given delimiters into further separate strings. More...
 
static int FindNumber (const std::string &strInput, const std::string &strFind)
 
static int64_t AlphaNumericCompare (const wchar_t *left, const wchar_t *right)
 
static long TimeStringToSeconds (const std::string &timeString)
 
static void RemoveCRLF (std::string &strLine)
 
static size_t utf8_strlen (const char *s)
 utf8 version of strlen - skips any non-starting bytes in the count, thus returning the number of utf8 characters More...
 
static std::string SecondsToTimeString (long seconds, TIME_FORMAT format=TIME_FORMAT_GUESS)
 convert a time in seconds to a string based on the given time format More...
 
static bool IsNaturalNumber (const std::string &str)
 check whether a string is a natural number. Matches [ \t]*[0-9]+[ \t]* More...
 
static bool IsInteger (const std::string &str)
 check whether a string is an integer. Matches [ \t]*[-]*[0-9]+[ \t]* More...
 
static bool isasciidigit (char chr)
 
static bool isasciixdigit (char chr)
 
static int asciidigitvalue (char chr)
 
static int asciixdigitvalue (char chr)
 
static bool isasciiuppercaseletter (char chr)
 
static bool isasciilowercaseletter (char chr)
 
static bool isasciialphanum (char chr)
 
static std::string SizeToString (int64_t size)
 
static size_t FindWords (const char *str, const char *wordLowerCase)
 
static int FindEndBracket (const std::string &str, char opener, char closer, int startPos=0)
 
static int DateStringToYYYYMMDD (const std::string &dateString)
 
static void WordToDigits (std::string &word)
 
static std::string CreateUUID ()
 
static bool ValidateUUID (const std::string &uuid)
 
static double CompareFuzzy (const std::string &left, const std::string &right)
 
static int FindBestMatch (const std::string &str, const std::vector< std::string > &strings, double &matchscore)
 
static bool ContainsKeyword (const std::string &str, const std::vector< std::string > &keywords)
 
static std::string BinaryStringToString (const std::string &in)
 Convert the string of binary chars to the actual string. More...
 
static std::string ToHexadecimal (const std::string &in)
 
template<typename T >
static std::string FormatNumber (T num)
 Format the string with locale separators. More...
 
static std::string Paramify (const std::string &param)
 Escapes the given string to be able to be used as a parameter. More...
 
static std::vector< std::string > Tokenize (const std::string &input, const std::string &delimiters)
 Split a string by the specified delimiters. Splits a string using one or more delimiting characters, ignoring empty tokens. Differs from Split() in two ways: More...
 
static void Tokenize (const std::string &input, std::vector< std::string > &tokens, const std::string &delimiters)
 
static std::vector< std::string > Tokenize (const std::string &input, const char delimiter)
 
static void Tokenize (const std::string &input, std::vector< std::string > &tokens, const char delimiter)
 
static uint64_t ToUint64 (std::string str, uint64_t fallback) noexcept
 
static std::string FormatFileSize (uint64_t bytes)
 

Static Public Attributes

static const std::string Empty = ""
 

Member Function Documentation

◆ AlphaNumericCompare()

int64_t StringUtils::AlphaNumericCompare ( const wchar_t *  left,
const wchar_t *  right 
)
static

◆ asciidigitvalue()

int StringUtils::asciidigitvalue ( char  chr)
static

◆ asciixdigitvalue()

int StringUtils::asciixdigitvalue ( char  chr)
static

◆ BinaryStringToString()

std::string StringUtils::BinaryStringToString ( const std::string &  in)
static

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
paramString to convert
Returns
Converted string

◆ CompareFuzzy()

double StringUtils::CompareFuzzy ( const std::string &  left,
const std::string &  right 
)
static

◆ CompareNoCase() [1/2]

int StringUtils::CompareNoCase ( const char *  s1,
const char *  s2 
)
static

◆ CompareNoCase() [2/2]

int StringUtils::CompareNoCase ( const std::string &  str1,
const std::string &  str2 
)
static

◆ ContainsKeyword()

bool StringUtils::ContainsKeyword ( const std::string &  str,
const std::vector< std::string > &  keywords 
)
static

◆ CreateUUID()

std::string StringUtils::CreateUUID ( )
static

◆ DateStringToYYYYMMDD()

int StringUtils::DateStringToYYYYMMDD ( const std::string &  dateString)
static

◆ EndsWith() [1/2]

bool StringUtils::EndsWith ( const std::string &  str1,
const char *  s2 
)
static

◆ EndsWith() [2/2]

bool StringUtils::EndsWith ( const std::string &  str1,
const std::string &  str2 
)
static

◆ EndsWithNoCase() [1/2]

bool StringUtils::EndsWithNoCase ( const std::string &  str1,
const char *  s2 
)
static

◆ EndsWithNoCase() [2/2]

bool StringUtils::EndsWithNoCase ( const std::string &  str1,
const std::string &  str2 
)
static

◆ EqualsNoCase() [1/3]

bool StringUtils::EqualsNoCase ( const char *  s1,
const char *  s2 
)
static

◆ EqualsNoCase() [2/3]

bool StringUtils::EqualsNoCase ( const std::string &  str1,
const char *  s2 
)
static

◆ EqualsNoCase() [3/3]

bool StringUtils::EqualsNoCase ( const std::string &  str1,
const std::string &  str2 
)
static

◆ FindBestMatch()

int StringUtils::FindBestMatch ( const std::string &  str,
const std::vector< std::string > &  strings,
double &  matchscore 
)
static

◆ FindEndBracket()

int StringUtils::FindEndBracket ( const std::string &  str,
char  opener,
char  closer,
int  startPos = 0 
)
static

◆ FindNumber()

int StringUtils::FindNumber ( const std::string &  strInput,
const std::string &  strFind 
)
static

◆ FindWords()

size_t StringUtils::FindWords ( const char *  str,
const char *  wordLowerCase 
)
static

◆ Format() [1/2]

template<typename... Args>
static std::string StringUtils::Format ( const std::string &  fmt,
Args &&...  args 
)
inlinestatic

Get a formatted string similar to sprintf.

Beware that this does not support directly passing in std::string objects. You need to call c_str() to pass the const char* buffer representing the value of the std::string object.

Parameters
fmtFormat of the resulting string
...variable number of value type arguments
Returns
Formatted string

◆ Format() [2/2]

template<typename... Args>
static std::wstring StringUtils::Format ( const std::wstring &  fmt,
Args &&...  args 
)
inlinestatic

◆ FormatFileSize()

std::string StringUtils::FormatFileSize ( uint64_t  bytes)
static

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.

◆ FormatNumber()

template<typename T >
static std::string StringUtils::FormatNumber ( num)
inlinestatic

Format the string with locale separators.

Format the string with locale separators. For example 10000.57 in en-us is '10,000.57' but in italian is '10.000,57'

Parameters
paramString to format
Returns
Formatted string

◆ FormatV() [1/2]

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

◆ FormatV() [2/2]

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

◆ isasciialphanum()

static bool StringUtils::isasciialphanum ( char  chr)
inlinestatic

◆ isasciidigit()

static bool StringUtils::isasciidigit ( char  chr)
inlinestatic

◆ isasciilowercaseletter()

static bool StringUtils::isasciilowercaseletter ( char  chr)
inlinestatic

◆ isasciiuppercaseletter()

static bool StringUtils::isasciiuppercaseletter ( char  chr)
inlinestatic

◆ isasciixdigit()

static bool StringUtils::isasciixdigit ( char  chr)
inlinestatic

◆ IsInteger()

bool StringUtils::IsInteger ( const std::string &  str)
static

check whether a string is an integer. Matches [ \t]*[-]*[0-9]+[ \t]*

Parameters
strthe string to check
Returns
true if the string is an integer, false otherwise.

◆ IsNaturalNumber()

bool StringUtils::IsNaturalNumber ( const std::string &  str)
static

check whether a string is a natural number. Matches [ \t]*[0-9]+[ \t]*

Parameters
strthe string to check
Returns
true if the string is a natural number, false otherwise.

◆ Join()

template<typename CONTAINER >
static std::string StringUtils::Join ( const CONTAINER &  strings,
const std::string &  delimiter 
)
inlinestatic

◆ Left()

std::string StringUtils::Left ( const std::string &  str,
size_t  count 
)
static

◆ Mid()

std::string StringUtils::Mid ( const std::string &  str,
size_t  first,
size_t  count = std::string::npos 
)
static

◆ Paramify()

std::string StringUtils::Paramify ( const std::string &  param)
static

Escapes the given string to be able to be used as a parameter.

Escapes backslashes and double-quotes with an additional backslash and adds double-quotes around the whole string.

Parameters
paramString to escape/paramify
Returns
Escaped/Paramified string

◆ RemoveCRLF()

void StringUtils::RemoveCRLF ( std::string &  strLine)
static

◆ RemoveDuplicatedSpacesAndTabs()

std::string & StringUtils::RemoveDuplicatedSpacesAndTabs ( std::string &  str)
static

◆ Replace() [1/3]

int StringUtils::Replace ( std::string &  str,
char  oldChar,
char  newChar 
)
static

◆ Replace() [2/3]

int StringUtils::Replace ( std::string &  str,
const std::string &  oldStr,
const std::string &  newStr 
)
static

◆ Replace() [3/3]

int StringUtils::Replace ( std::wstring &  str,
const std::wstring &  oldStr,
const std::wstring &  newStr 
)
static

◆ ReturnDigits()

int StringUtils::ReturnDigits ( const std::string &  str)
static

◆ Right()

std::string StringUtils::Right ( const std::string &  str,
size_t  count 
)
static

◆ SecondsToTimeString()

std::string StringUtils::SecondsToTimeString ( long  seconds,
TIME_FORMAT  format = TIME_FORMAT_GUESS 
)
static

convert a time in seconds to a string based on the given time format

Parameters
secondstime in seconds
formatthe format we want the time in.
Returns
the formatted time
See also
TIME_FORMAT

◆ SizeToString()

std::string StringUtils::SizeToString ( int64_t  size)
static

◆ Split() [1/3]

std::vector< std::string > StringUtils::Split ( const std::string &  input,
const char  delimiter,
size_t  iMaxStrings = 0 
)
static

◆ Split() [2/3]

std::vector< std::string > StringUtils::Split ( const std::string &  input,
const std::string &  delimiter,
unsigned int  iMaxStrings = 0 
)
static

Splits the given input string using the given delimiter into separate strings.

If the given input string is empty the result will be an empty array (not an array containing an empty string).

Parameters
inputInput string to be split
delimiterDelimiter to be used to split the input string
iMaxStrings(optional) Maximum number of splitted strings

◆ Split() [3/3]

std::vector< std::string > StringUtils::Split ( const std::string &  input,
const std::vector< std::string > &  delimiters 
)
static

◆ SplitMulti()

std::vector< std::string > StringUtils::SplitMulti ( const std::vector< std::string > &  input,
const std::vector< std::string > &  delimiters,
unsigned int  iMaxStrings = 0 
)
static

Splits the given input strings using the given delimiters into further separate strings.

If the given input string vector is empty the result will be an empty array (not an array containing an empty string).

Delimiter strings are applied in order, so once the (optional) maximum number of items is produced no other delimiters are applied. This produces different results to applying all delimiters at once e.g. "a/b#c/d" becomes "a", "b#c", "d" rather than "a", "b", "c/d"

Parameters
inputInput vector of strings each to be split
delimitersDelimiter strings to be used to split the input strings
iMaxStrings(optional) Maximum number of resulting split strings

◆ SplitTo() [1/3]

template<typename OutputIt >
static OutputIt StringUtils::SplitTo ( OutputIt  d_first,
const std::string &  input,
const char  delimiter,
size_t  iMaxStrings = 0 
)
inlinestatic

◆ SplitTo() [2/3]

template<typename OutputIt >
static OutputIt StringUtils::SplitTo ( OutputIt  d_first,
const std::string &  input,
const std::string &  delimiter,
unsigned int  iMaxStrings = 0 
)
inlinestatic

Splits the given input string using the given delimiter into separate strings.

If the given input string is empty nothing will be put into the target iterator.

Parameters
d_firstthe beginning of the destination range
inputInput string to be split
delimiterDelimiter to be used to split the input string
iMaxStrings(optional) Maximum number of splitted strings
Returns
output iterator to the element in the destination range, one past the last element that was put there

◆ SplitTo() [3/3]

template<typename OutputIt >
static OutputIt StringUtils::SplitTo ( OutputIt  d_first,
const std::string &  input,
const std::vector< std::string > &  delimiters 
)
inlinestatic

◆ StartsWith() [1/3]

bool StringUtils::StartsWith ( const char *  s1,
const char *  s2 
)
static

◆ StartsWith() [2/3]

bool StringUtils::StartsWith ( const std::string &  str1,
const char *  s2 
)
static

◆ StartsWith() [3/3]

bool StringUtils::StartsWith ( const std::string &  str1,
const std::string &  str2 
)
static

◆ StartsWithNoCase() [1/3]

bool StringUtils::StartsWithNoCase ( const char *  s1,
const char *  s2 
)
static

◆ StartsWithNoCase() [2/3]

bool StringUtils::StartsWithNoCase ( const std::string &  str1,
const char *  s2 
)
static

◆ StartsWithNoCase() [3/3]

bool StringUtils::StartsWithNoCase ( const std::string &  str1,
const std::string &  str2 
)
static

◆ TimeStringToSeconds()

long StringUtils::TimeStringToSeconds ( const std::string &  timeString)
static

◆ ToCapitalize() [1/2]

void StringUtils::ToCapitalize ( std::string &  str)
static

◆ ToCapitalize() [2/2]

void StringUtils::ToCapitalize ( std::wstring &  str)
static

◆ ToHexadecimal()

std::string StringUtils::ToHexadecimal ( const std::string &  in)
static

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

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

◆ Tokenize() [1/4]

std::vector< std::string > StringUtils::Tokenize ( const std::string &  input,
const char  delimiter 
)
static

◆ Tokenize() [2/4]

std::vector< std::string > StringUtils::Tokenize ( const std::string &  input,
const std::string &  delimiters 
)
static

Split a string by the specified delimiters. Splits a string using one or more delimiting characters, ignoring empty tokens. Differs from Split() in two ways:

  1. The delimiters are treated as individual characters, rather than a single delimiting string.
  2. Empty tokens are ignored.
    Returns
    a vector of tokens

◆ Tokenize() [3/4]

void StringUtils::Tokenize ( const std::string &  input,
std::vector< std::string > &  tokens,
const char  delimiter 
)
static

◆ Tokenize() [4/4]

void StringUtils::Tokenize ( const std::string &  input,
std::vector< std::string > &  tokens,
const std::string &  delimiters 
)
static

◆ ToLower() [1/2]

void StringUtils::ToLower ( std::string &  str)
static

◆ ToLower() [2/2]

void StringUtils::ToLower ( std::wstring &  str)
static

◆ ToUint64()

uint64_t StringUtils::ToUint64 ( std::string  str,
uint64_t  fallback 
)
staticnoexcept

◆ ToUpper() [1/2]

void StringUtils::ToUpper ( std::string &  str)
static

◆ ToUpper() [2/2]

void StringUtils::ToUpper ( std::wstring &  str)
static

◆ Trim() [1/2]

std::string & StringUtils::Trim ( std::string &  str)
static

◆ Trim() [2/2]

std::string & StringUtils::Trim ( std::string &  str,
const char *const  chars 
)
static

◆ TrimLeft() [1/2]

std::string & StringUtils::TrimLeft ( std::string &  str)
static

◆ TrimLeft() [2/2]

std::string & StringUtils::TrimLeft ( std::string &  str,
const char *const  chars 
)
static

◆ TrimRight() [1/2]

std::string & StringUtils::TrimRight ( std::string &  str)
static

◆ TrimRight() [2/2]

std::string & StringUtils::TrimRight ( std::string &  str,
const char *const  chars 
)
static

◆ utf8_strlen()

size_t StringUtils::utf8_strlen ( const char *  s)
static

utf8 version of strlen - skips any non-starting bytes in the count, thus returning the number of utf8 characters

Parameters
sc-string to find the length of.
Returns
the number of utf8 characters in the string.

◆ ValidateUUID()

bool StringUtils::ValidateUUID ( const std::string &  uuid)
static

◆ WordToDigits()

void StringUtils::WordToDigits ( std::string &  word)
static

Member Data Documentation

◆ Empty

const std::string StringUtils::Empty = ""
static

The documentation for this class was generated from the following files: