C++ Utilities  4.17.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
Namespaces | Classes | Typedefs | Enumerations | Functions
ConversionUtilities Namespace Reference

Contains several functions providing conversions between different data types. More...

Namespaces

 BE
 Encapsulates binary conversion functions using the big endian byte order.
 
 LE
 Encapsulates binary conversion functions using the little endian byte order.
 

Classes

class  ConversionException
 The ConversionException class is thrown by the various conversion functions of this library when a conversion error occurs. More...
 
struct  StringDataDeleter
 The StringDataDeleter struct deletes the data of a StringData instance. More...
 
class  Widen
 Converts a std::string to a wide string using the specified locale. More...
 

Typedefs

using StringData = std::pair< std::unique_ptr< char[], StringDataDeleter >, std::size_t >
 Type used to return string encoding conversion result. More...
 

Enumerations

enum  EmptyPartsTreat { EmptyPartsTreat::Keep, EmptyPartsTreat::Omit, EmptyPartsTreat::Merge }
 Specifies the role of empty parts when splitting strings. More...
 

Functions

CPP_UTILITIES_EXPORT constexpr uint16 toFixed8 (float32 float32value)
 Returns the 8.8 fixed point representation converted from the specified 32-bit floating point number. More...
 
CPP_UTILITIES_EXPORT constexpr float32 toFloat32 (uint16 fixed8value)
 Returns a 32-bit floating point number converted from the specified 8.8 fixed point representation. More...
 
CPP_UTILITIES_EXPORT constexpr uint32 toFixed16 (float32 float32value)
 Returns the 16.16 fixed point representation converted from the specified 32-bit floating point number. More...
 
CPP_UTILITIES_EXPORT constexpr float32 toFloat32 (uint32 fixed16value)
 Returns a 32-bit floating point number converted from the specified 16.16 fixed point representation. More...
 
CPP_UTILITIES_EXPORT constexpr uint32 toSynchsafeInt (uint32 normalInt)
 Returns a 32-bit synchsafe integer converted from a normal 32-bit integer. More...
 
CPP_UTILITIES_EXPORT constexpr uint32 toNormalInt (uint32 synchsafeInt)
 Returns a normal 32-bit integer converted from a 32-bit synchsafe integer. More...
 
CPP_UTILITIES_EXPORT constexpr uint16 swapOrder (uint16 value)
 Swaps the byte order of the specified 16-bit unsigned integer. More...
 
CPP_UTILITIES_EXPORT constexpr uint32 swapOrder (uint32 value)
 Swaps the byte order of the specified 32-bit unsigned integer. More...
 
CPP_UTILITIES_EXPORT constexpr uint64 swapOrder (uint64 value)
 Swaps the byte order of the specified 64-bit unsigned integer. More...
 
CPP_UTILITIES_EXPORT StringData convertString (const char *fromCharset, const char *toCharset, const char *inputBuffer, std::size_t inputBufferSize, float outputBufferSizeFactor)
 Converts the specified string from one character set to another. More...
 
CPP_UTILITIES_EXPORT StringData convertUtf8ToUtf16LE (const char *inputBuffer, std::size_t inputBufferSize)
 Converts the specified UTF-8 string to UTF-16 (little-endian). More...
 
CPP_UTILITIES_EXPORT StringData convertUtf16LEToUtf8 (const char *inputBuffer, std::size_t inputBufferSize)
 Converts the specified UTF-16 (little-endian) string to UTF-8. More...
 
CPP_UTILITIES_EXPORT StringData convertUtf8ToUtf16BE (const char *inputBuffer, std::size_t inputBufferSize)
 Converts the specified UTF-8 string to UTF-16 (big-endian). More...
 
CPP_UTILITIES_EXPORT StringData convertUtf16BEToUtf8 (const char *inputBuffer, std::size_t inputBufferSize)
 Converts the specified UTF-16 (big-endian) string to UTF-8. More...
 
CPP_UTILITIES_EXPORT StringData convertLatin1ToUtf8 (const char *inputBuffer, std::size_t inputBufferSize)
 Converts the specified Latin-1 string to UTF-8. More...
 
CPP_UTILITIES_EXPORT StringData convertUtf8ToLatin1 (const char *inputBuffer, std::size_t inputBufferSize)
 Converts the specified UTF-8 string to Latin-1. More...
 
CPP_UTILITIES_EXPORT void truncateString (string &str, char terminationChar)
 Truncates all characters after the first occurrence of the specified terminationChar and the termination character as well. More...
 
template<class Container = std::initializer_list<std::string>>
Container::value_type joinStrings (const Container &strings, const typename Container::value_type &delimiter=typename Container::value_type(), bool omitEmpty=false, const typename Container::value_type &leftClosure=typename Container::value_type(), const typename Container::value_type &rightClosure=typename Container::value_type())
 Joins the given strings using the specified delimiter. More...
 
template<class Container = std::initializer_list<std::string>>
std::vector< std::string > toMultiline (const Container &arrayOfLines)
 Converts the specified arrayOfLines to a multiline string. More...
 
template<class Container = std::list<std::string>>
Container splitString (const typename Container::value_type &string, const typename Container::value_type &delimiter, EmptyPartsTreat emptyPartsRole=EmptyPartsTreat::Keep, int maxParts=-1)
 Splits the given string at the specified delimiter. More...
 
template<class Container = std::list<std::string>>
Container splitStringSimple (const typename Container::value_type &string, const typename Container::value_type &delimiter, int maxParts=-1)
 Splits the given string (which might also be a string view) at the specified delimiter. More...
 
template<class Container = std::vector<std::string>>
std::vector< std::string > toArrayOfLines (const std::string &multilineString)
 Converts the specified multilineString to an array of lines. More...
 
template<typename StringType >
bool startsWith (const StringType &str, const StringType &phrase)
 Returns whether str starts with phrase. More...
 
template<typename StringType >
bool startsWith (const StringType &str, const typename StringType::value_type *phrase)
 Returns whether str starts with phrase. More...
 
template<typename StringType >
bool containsSubstrings (const StringType &str, std::initializer_list< StringType > substrings)
 Returns whether str contains the specified substrings. More...
 
template<typename StringType >
bool containsSubstrings (const StringType &str, std::initializer_list< const typename StringType::value_type * > substrings)
 Returns whether str contains the specified substrings. More...
 
template<typename StringType >
void findAndReplace (StringType &str, const StringType &find, const StringType &replace)
 Replaces all occurences of find with relpace in the specified str. More...
 
template<typename CharType >
constexpr CharType digitToChar (CharType digit)
 Returns the character representation of the specified digit. More...
 
template<typename IntegralType , class StringType = std::string, Traits::EnableIf< std::is_integral< IntegralType >, std::is_unsigned< IntegralType >> * = nullptr>
StringType numberToString (IntegralType number, typename StringType::value_type base=10)
 Converts the given number to its equivalent string representation using the specified base. More...
 
template<typename FloatingType , class StringType = std::string, Traits::EnableIf< std::is_floating_point< FloatingType >> * = nullptr>
StringType numberToString (FloatingType number, typename StringType::value_type base=10)
 Converts the given number to its equivalent string representation using the specified base. More...
 
template<typename CharType >
CharType charToDigit (CharType character, CharType base)
 Returns number/digit of the specified character representation using the specified base. More...
 
template<typename IntegralType , typename StringType , Traits::EnableIf< std::is_integral< IntegralType >, std::is_unsigned< IntegralType >> * = nullptr>
IntegralType stringToNumber (const StringType &string, typename StringType::value_type base=10)
 Converts the given string to an unsigned number assuming string uses the specified base. More...
 
template<typename FloatingType , class StringType , Traits::EnableIf< std::is_floating_point< FloatingType >> * = nullptr>
FloatingType stringToNumber (const StringType &string, typename StringType::value_type base=10)
 Converts the given string to a number assuming string uses the specified base. More...
 
template<typename IntegralType , class CharType , Traits::EnableIf< std::is_integral< IntegralType >, std::is_unsigned< IntegralType >> * = nullptr>
IntegralType stringToNumber (const CharType *string, unsigned char base=10)
 Converts the given null-terminated string to an unsigned numeric value using the specified base. More...
 
template<typename FloatingType , class CharType , Traits::EnableIf< std::is_floating_point< FloatingType >> * = nullptr>
FloatingType stringToNumber (const CharType *string, unsigned char base=10)
 Converts the given null-terminated string to a number assuming string uses the specified base. More...
 
template<typename IntegralType , class CharType , Traits::EnableIf< std::is_integral< IntegralType >, std::is_unsigned< IntegralType >> * = nullptr>
IntegralType bufferToNumber (const CharType *string, std::size_t size, unsigned char base=10)
 Converts the given string of size characters to an unsigned numeric value using the specified base. More...
 
template<typename T >
std::string interpretIntegerAsString (T integer, int startOffset=0)
 Interprets the given integer at the specified position as std::string using the specified byte order. More...
 
CPP_UTILITIES_EXPORT std::string dataSizeToString (uint64 sizeInByte, bool includeByte)
 Converts the specified data size in byte to its equivalent std::string representation. More...
 
CPP_UTILITIES_EXPORT std::string bitrateToString (double bitrateInKbitsPerSecond, bool useIecBinaryPrefixes)
 Converts the specified bitrate in kbit/s to its equivalent std::string representation. More...
 
CPP_UTILITIES_EXPORT std::string encodeBase64 (const byte *data, uint32 dataSize)
 Encodes the specified data to Base64. More...
 
CPP_UTILITIES_EXPORT std::pair< std::unique_ptr< byte[]>, uint32decodeBase64 (const char *encodedStr, const uint32 strSize)
 Decodes the specified Base64 encoded string. More...
 
template<class StringType = std::string, class... Args>
StringType tupleToString (const std::tuple< Args... > &tuple)
 Concatenates all strings hold by the specified tuple. More...
 
template<class StringType = std::string, class... Args>
constexpr StringType argsToString (Args &&... args)
 
template<class Tuple >
constexpr auto operator% (const Tuple &lhs, const std::string &rhs) -> decltype(std::tuple_cat(lhs, std::make_tuple(&rhs)))
 Allows construction of string-tuples via %-operator, eg. More...
 
template<class Tuple >
constexpr auto operator% (const Tuple &lhs, const char *rhs) -> decltype(std::tuple_cat(lhs, std::make_tuple(rhs)))
 Allows construction of string-tuples via %-operator, eg. More...
 
template<class Tuple , typename IntegralType , Traits::EnableIf< std::is_integral< IntegralType >> * = nullptr>
constexpr auto operator% (const Tuple &lhs, IntegralType rhs) -> decltype(std::tuple_cat(lhs, std::make_tuple(rhs)))
 Allows construction of string-tuples via %-operator, eg. More...
 
constexpr auto operator% (const std::string &lhs, const std::string &rhs) -> decltype(std::make_tuple(&lhs, &rhs))
 Allows construction of string-tuples via %-operator, eg. More...
 
constexpr auto operator% (const char *lhs, const std::string &rhs) -> decltype(std::make_tuple(lhs, &rhs))
 Allows construction of string-tuples via %-operator, eg. More...
 
constexpr auto operator% (const std::string &lhs, const char *rhs) -> decltype(std::make_tuple(&lhs, rhs))
 Allows construction of string-tuples via %-operator, eg. More...
 
constexpr auto operator% (const std::string &lhs, char rhs) -> decltype(std::make_tuple(&lhs, rhs))
 Allows construction of string-tuples via %-operator, eg. More...
 
constexpr auto operator% (char lhs, const std::string &rhs) -> decltype(std::make_tuple(lhs, &rhs))
 Allows construction of string-tuples via %-operator, eg. More...
 
template<class Tuple , Traits::EnableIf< Traits::IsSpecializationOf< Tuple, std::tuple >> * = nullptr>
std::string operator+ (const Tuple &lhs, const std::string &rhs)
 Allows construction of final string from previously constructed string-tuple and trailing string via +-operator. More...
 
template<class Tuple , Traits::EnableIf< Traits::IsSpecializationOf< Tuple, std::tuple >> * = nullptr>
std::string operator+ (const Tuple &lhs, const char *rhs)
 Allows construction of final string from previously constructed string-tuple and trailing string via +-operator. More...
 
template<class Tuple , typename IntegralType , Traits::EnableIf< Traits::IsSpecializationOf< Tuple, std::tuple >, std::is_integral< IntegralType >> * = nullptr>
std::string operator+ (const Tuple &lhs, IntegralType rhs)
 Allows construction of final string from previously constructed string-tuple and trailing char via +-operator. More...
 

Detailed Description

Contains several functions providing conversions between different data types.

binaryconversion.h declares functions which convert base data types to an array of bytes and vice versa. stringconversion.h declares different functions around string conversion such as converting a number to a string and vice versa.

Typedef Documentation

◆ StringData

using ConversionUtilities::StringData = typedef std::pair<std::unique_ptr<char[], StringDataDeleter>, std::size_t>

Type used to return string encoding conversion result.

Definition at line 38 of file stringconversion.h.

Enumeration Type Documentation

◆ EmptyPartsTreat

Specifies the role of empty parts when splitting strings.

Enumerator
Keep 

empty parts are kept

Omit 

empty parts are omitted

Merge 

empty parts are omitted but cause the adjacent parts being joined using the delimiter

Definition at line 119 of file stringconversion.h.

Function Documentation

◆ argsToString()

template<class StringType = std::string, class... Args>
constexpr StringType ConversionUtilities::argsToString ( Args &&...  args)

Definition at line 147 of file stringbuilder.h.

◆ bitrateToString()

string ConversionUtilities::bitrateToString ( double  bitrateInKbitsPerSecond,
bool  useIecBinaryPrefixes 
)

Converts the specified bitrate in kbit/s to its equivalent std::string representation.

The unit with appropriate binary prefix will be appended.

Parameters
bitrateInKbitsPerSecondSpecifies the bitrate in kbit/s.
useIecBinaryPrefixesIndicates whether IEC binary prefixes should be used (eg. KiB/s).
See also
Binary prefix - Wikipedia

Definition at line 284 of file stringconversion.cpp.

◆ bufferToNumber()

template<typename IntegralType , class CharType , Traits::EnableIf< std::is_integral< IntegralType >, std::is_unsigned< IntegralType >> * = nullptr>
IntegralType ConversionUtilities::bufferToNumber ( const CharType *  string,
std::size_t  size,
unsigned char  base = 10 
)

Converts the given string of size characters to an unsigned numeric value using the specified base.

Converts the given string of size characters to a signed numeric value using the specified base.

Template Parameters
IntegralTypeThe data type used to store the converted value.
CharTypeThe character type.
Exceptions
AConversionException will be thrown if the provided string is not a valid number.
See also
numberToString(), stringToNumber()

Definition at line 528 of file stringconversion.h.

◆ charToDigit()

template<typename CharType >
CharType ConversionUtilities::charToDigit ( CharType  character,
CharType  base 
)

Returns number/digit of the specified character representation using the specified base.

Exceptions
AConversionException will be thrown if the provided character does not represent a valid digit for the specified base.
Todo:
Provide an alternative using std::expected (when switching to C++17).

Definition at line 368 of file stringconversion.h.

◆ containsSubstrings() [1/2]

template<typename StringType >
bool ConversionUtilities::containsSubstrings ( const StringType &  str,
std::initializer_list< StringType >  substrings 
)

Returns whether str contains the specified substrings.

Remarks
The substrings must occur in the specified order.

Definition at line 244 of file stringconversion.h.

◆ containsSubstrings() [2/2]

template<typename StringType >
bool ConversionUtilities::containsSubstrings ( const StringType &  str,
std::initializer_list< const typename StringType::value_type * >  substrings 
)

Returns whether str contains the specified substrings.

Remarks
The substrings must occur in the specified order.

Definition at line 261 of file stringconversion.h.

◆ convertLatin1ToUtf8()

StringData ConversionUtilities::convertLatin1ToUtf8 ( const char *  inputBuffer,
std::size_t  inputBufferSize 
)

Converts the specified Latin-1 string to UTF-8.

Definition at line 185 of file stringconversion.cpp.

◆ convertString()

StringData ConversionUtilities::convertString ( const char *  fromCharset,
const char *  toCharset,
const char *  inputBuffer,
std::size_t  inputBufferSize,
float  outputBufferSizeFactor 
)

Converts the specified string from one character set to another.

Remarks
  • The term "size" referes here always to the actual number of bytes and not to the number of characters (eg. the size of the UTF-8 string "ö" is 2 and not 1).
  • The expected size of the output buffer can be specified via outputBufferSizeFactor. This hint helps to reduce buffer reallocations during the conversion (eg. for the conversion from Latin-1 to UTF-16 the factor would be 2, for the conversion from UTF-16 to Latin-1 the factor would be 0.5).

Definition at line 140 of file stringconversion.cpp.

◆ convertUtf16BEToUtf8()

StringData ConversionUtilities::convertUtf16BEToUtf8 ( const char *  inputBuffer,
std::size_t  inputBufferSize 
)

Converts the specified UTF-16 (big-endian) string to UTF-8.

Definition at line 176 of file stringconversion.cpp.

◆ convertUtf16LEToUtf8()

StringData ConversionUtilities::convertUtf16LEToUtf8 ( const char *  inputBuffer,
std::size_t  inputBufferSize 
)

Converts the specified UTF-16 (little-endian) string to UTF-8.

Definition at line 158 of file stringconversion.cpp.

◆ convertUtf8ToLatin1()

StringData ConversionUtilities::convertUtf8ToLatin1 ( const char *  inputBuffer,
std::size_t  inputBufferSize 
)

Converts the specified UTF-8 string to Latin-1.

Definition at line 194 of file stringconversion.cpp.

◆ convertUtf8ToUtf16BE()

StringData ConversionUtilities::convertUtf8ToUtf16BE ( const char *  inputBuffer,
std::size_t  inputBufferSize 
)

Converts the specified UTF-8 string to UTF-16 (big-endian).

Definition at line 167 of file stringconversion.cpp.

◆ convertUtf8ToUtf16LE()

StringData ConversionUtilities::convertUtf8ToUtf16LE ( const char *  inputBuffer,
std::size_t  inputBufferSize 
)

Converts the specified UTF-8 string to UTF-16 (little-endian).

Definition at line 149 of file stringconversion.cpp.

◆ dataSizeToString()

string ConversionUtilities::dataSizeToString ( uint64  sizeInByte,
bool  includeByte 
)

Converts the specified data size in byte to its equivalent std::string representation.

The unit with appropriate binary prefix will be appended.

Definition at line 252 of file stringconversion.cpp.

◆ decodeBase64()

pair< unique_ptr< byte[]>, uint32 > ConversionUtilities::decodeBase64 ( const char *  encodedStr,
const uint32  strSize 
)

Decodes the specified Base64 encoded string.

Exceptions
Throwsa ConversionException if the specified string is no valid Base64.
See also
RFC 4648

Definition at line 361 of file stringconversion.cpp.

◆ digitToChar()

template<typename CharType >
constexpr CharType ConversionUtilities::digitToChar ( CharType  digit)

Returns the character representation of the specified digit.

Remarks
  • Uses capital letters.
  • Valid values for digit: 0 <= digit <= 35

Definition at line 289 of file stringconversion.h.

◆ encodeBase64()

string ConversionUtilities::encodeBase64 ( const byte data,
uint32  dataSize 
)

Encodes the specified data to Base64.

See also
RFC 4648

Definition at line 321 of file stringconversion.cpp.

◆ findAndReplace()

template<typename StringType >
void ConversionUtilities::findAndReplace ( StringType &  str,
const StringType &  find,
const StringType &  replace 
)

Replaces all occurences of find with relpace in the specified str.

Definition at line 276 of file stringconversion.h.

◆ interpretIntegerAsString()

template<typename T >
std::string ConversionUtilities::interpretIntegerAsString ( integer,
int  startOffset = 0 
)

Interprets the given integer at the specified position as std::string using the specified byte order.

Example: interpretation of ID3v2 frame IDs (stored as 32-bit integer) as string

  • 0x54495432/1414091826 will be interpreted as "TIT2".
  • 0x00545432/5526578 will be interpreted as "TT2" using start offset 1 to omit the first byte.
Template Parameters
TThe data type of the integer to be interpreted.

Definition at line 617 of file stringconversion.h.

◆ joinStrings()

template<class Container = std::initializer_list<std::string>>
Container::value_type ConversionUtilities::joinStrings ( const Container &  strings,
const typename Container::value_type &  delimiter = typename Container::value_type(),
bool  omitEmpty = false,
const typename Container::value_type &  leftClosure = typename Container::value_type(),
const typename Container::value_type &  rightClosure = typename Container::value_type() 
)

Joins the given strings using the specified delimiter.

The strings will be enclosed using the provided closures leftClosure and rightClosure.

Parameters
stringsThe string parts to be joined.
delimiterSpecifies a delimiter to be used (empty string by default).
omitEmptyIndicates whether empty part should be omitted.
leftClosureSpecifies a string to be inserted before each string (empty string by default).
rightClosureSpecifies a string to be appendend after each string (empty string by default).
Template Parameters
ContainerThe STL-container used to provide the strings.
Returns
Returns the joined string.

Definition at line 72 of file stringconversion.h.

◆ numberToString() [1/2]

template<typename IntegralType , class StringType = std::string, Traits::EnableIf< std::is_integral< IntegralType >, std::is_unsigned< IntegralType >> * = nullptr>
StringType ConversionUtilities::numberToString ( IntegralType  number,
typename StringType::value_type  base = 10 
)

Converts the given number to its equivalent string representation using the specified base.

Template Parameters
IntegralTypeThe data type of the given number.
StringTypeThe string type (should be an instantiation of the basic_string class template).
See also
stringToNumber()

Definition at line 302 of file stringconversion.h.

◆ numberToString() [2/2]

template<typename FloatingType , class StringType = std::string, Traits::EnableIf< std::is_floating_point< FloatingType >> * = nullptr>
StringType ConversionUtilities::numberToString ( FloatingType  number,
typename StringType::value_type  base = 10 
)

Converts the given number to its equivalent string representation using the specified base.

Template Parameters
FloatingTypeThe data type of the given number.
StringTypeThe string type (should be an instantiation of the basic_string class template).
Remarks
This function is using std::basic_stringstream interanlly and hence also has its limitations (eg. regarding base and types).
See also
stringToNumber(), bufferToNumber()

Definition at line 356 of file stringconversion.h.

◆ operator%() [1/8]

template<class Tuple >
constexpr auto ConversionUtilities::operator% ( const Tuple &  lhs,
const std::string &  rhs 
) -> decltype(std::tuple_cat(lhs, std::make_tuple(&rhs)))

Allows construction of string-tuples via %-operator, eg.

string1 % "string2" % string3.

Definition at line 155 of file stringbuilder.h.

◆ operator%() [2/8]

template<class Tuple >
constexpr auto ConversionUtilities::operator% ( const Tuple &  lhs,
const char *  rhs 
) -> decltype(std::tuple_cat(lhs, std::make_tuple(rhs)))

Allows construction of string-tuples via %-operator, eg.

string1 % "string2" % string3.

Definition at line 163 of file stringbuilder.h.

◆ operator%() [3/8]

template<class Tuple , typename IntegralType , Traits::EnableIf< std::is_integral< IntegralType >> * = nullptr>
constexpr auto ConversionUtilities::operator% ( const Tuple &  lhs,
IntegralType  rhs 
) -> decltype(std::tuple_cat(lhs, std::make_tuple(rhs)))

Allows construction of string-tuples via %-operator, eg.

string1 % "string2" % string3.

Definition at line 172 of file stringbuilder.h.

◆ operator%() [4/8]

constexpr auto ConversionUtilities::operator% ( const std::string &  lhs,
const std::string &  rhs 
) -> decltype(std::make_tuple(&lhs, &rhs))

Allows construction of string-tuples via %-operator, eg.

string1 % "string2" % string3.

Definition at line 180 of file stringbuilder.h.

◆ operator%() [5/8]

constexpr auto ConversionUtilities::operator% ( const char *  lhs,
const std::string &  rhs 
) -> decltype(std::make_tuple(lhs, &rhs))

Allows construction of string-tuples via %-operator, eg.

string1 % "string2" % string3.

Definition at line 188 of file stringbuilder.h.

◆ operator%() [6/8]

constexpr auto ConversionUtilities::operator% ( const std::string &  lhs,
const char *  rhs 
) -> decltype(std::make_tuple(&lhs, rhs))

Allows construction of string-tuples via %-operator, eg.

string1 % "string2" % string3.

Definition at line 196 of file stringbuilder.h.

◆ operator%() [7/8]

constexpr auto ConversionUtilities::operator% ( const std::string &  lhs,
char  rhs 
) -> decltype(std::make_tuple(&lhs, rhs))

Allows construction of string-tuples via %-operator, eg.

string1 % "string2" % string3.

Definition at line 204 of file stringbuilder.h.

◆ operator%() [8/8]

constexpr auto ConversionUtilities::operator% ( char  lhs,
const std::string &  rhs 
) -> decltype(std::make_tuple(lhs, &rhs))

Allows construction of string-tuples via %-operator, eg.

string1 % "string2" % string3.

Definition at line 212 of file stringbuilder.h.

◆ operator+() [1/3]

template<class Tuple , Traits::EnableIf< Traits::IsSpecializationOf< Tuple, std::tuple >> * = nullptr>
std::string ConversionUtilities::operator+ ( const Tuple &  lhs,
const std::string &  rhs 
)
inline

Allows construction of final string from previously constructed string-tuple and trailing string via +-operator.

This is meant to be used for fast string building without multiple heap allocation, eg.

printVelocity("velocity: " % numberToString(velocityExample) % " km/h (" % numberToString(velocityExample / 3.6) + " m/s)"));

Definition at line 227 of file stringbuilder.h.

◆ operator+() [2/3]

template<class Tuple , Traits::EnableIf< Traits::IsSpecializationOf< Tuple, std::tuple >> * = nullptr>
std::string ConversionUtilities::operator+ ( const Tuple &  lhs,
const char *  rhs 
)
inline

Allows construction of final string from previously constructed string-tuple and trailing string via +-operator.

This is meant to be used for fast string building without multiple heap allocation, eg.

printVelocity("velocity: " % numberToString(velocityExample) % " km/h (" % numberToString(velocityExample / 3.6) + " m/s)"));

Definition at line 242 of file stringbuilder.h.

◆ operator+() [3/3]

template<class Tuple , typename IntegralType , Traits::EnableIf< Traits::IsSpecializationOf< Tuple, std::tuple >, std::is_integral< IntegralType >> * = nullptr>
std::string ConversionUtilities::operator+ ( const Tuple &  lhs,
IntegralType  rhs 
)
inline

Allows construction of final string from previously constructed string-tuple and trailing char via +-operator.

This is meant to be used for fast string building without multiple heap allocation, eg.

printVelocity("velocity: " % numberToString(velocityExample) % " km/h (" % numberToString(velocityExample / 3.6) + " m/s)"));

Definition at line 258 of file stringbuilder.h.

◆ splitString()

template<class Container = std::list<std::string>>
Container ConversionUtilities::splitString ( const typename Container::value_type &  string,
const typename Container::value_type &  delimiter,
EmptyPartsTreat  emptyPartsRole = EmptyPartsTreat::Keep,
int  maxParts = -1 
)

Splits the given string at the specified delimiter.

Parameters
stringThe string to be splitted.
delimiterSpecifies the delimiter.
emptyPartsRoleSpecifies the treatment of empty parts.
maxPartsSpecifies the maximal number of parts. Values less or equal zero indicate an unlimited number of parts.
Template Parameters
ContainerThe STL-container used to return the parts.
Returns
Returns the parts.

Definition at line 135 of file stringconversion.h.

◆ splitStringSimple()

template<class Container = std::list<std::string>>
Container ConversionUtilities::splitStringSimple ( const typename Container::value_type &  string,
const typename Container::value_type &  delimiter,
int  maxParts = -1 
)

Splits the given string (which might also be a string view) at the specified delimiter.

Parameters
stringThe string to be splitted.
delimiterSpecifies the delimiter.
maxPartsSpecifies the maximal number of parts. Values less or equal zero indicate an unlimited number of parts.
Template Parameters
ContainerThe STL-container used to return the parts.
Returns
Returns the parts.
Remarks
This is a simplified version of splitString() where emptyPartsRole is always EmptyPartsTreat::Keep.

Definition at line 182 of file stringconversion.h.

◆ startsWith() [1/2]

template<typename StringType >
bool ConversionUtilities::startsWith ( const StringType &  str,
const StringType &  phrase 
)

Returns whether str starts with phrase.

Definition at line 210 of file stringconversion.h.

◆ startsWith() [2/2]

template<typename StringType >
bool ConversionUtilities::startsWith ( const StringType &  str,
const typename StringType::value_type *  phrase 
)

Returns whether str starts with phrase.

Definition at line 228 of file stringconversion.h.

◆ stringToNumber() [1/4]

template<typename IntegralType , typename StringType , Traits::EnableIf< std::is_integral< IntegralType >, std::is_unsigned< IntegralType >> * = nullptr>
IntegralType ConversionUtilities::stringToNumber ( const StringType &  string,
typename StringType::value_type  base = 10 
)

Converts the given string to an unsigned number assuming string uses the specified base.

Converts the given string to a signed number assuming string uses the specified base.

Template Parameters
IntegralTypeThe data type used to store the converted value.
StringTypeThe string type (should be an instantiation of the basic_string class template).
Exceptions
AConversionException will be thrown if the provided string is not a valid number.
See also
numberToString(), bufferToNumber()
Todo:
Provide an alternative using std::expected (when switching to C++17).
Template Parameters
IntegralTypeThe data type used to store the converted value.
StringTypeThe string type (should be an instantiation of the basic_string class template).
Exceptions
AConversionException will be thrown if the provided string is not a valid number.
See also
numberToString(), bufferToNumber()
Todo:
Provide an alternative using std::expected (when switching to C++17).

Definition at line 398 of file stringconversion.h.

◆ stringToNumber() [2/4]

template<typename FloatingType , class StringType , Traits::EnableIf< std::is_floating_point< FloatingType >> * = nullptr>
FloatingType ConversionUtilities::stringToNumber ( const StringType &  string,
typename StringType::value_type  base = 10 
)

Converts the given string to a number assuming string uses the specified base.

Template Parameters
FloatingTypeThe data type used to store the converted value.
StringTypeThe string type (should be an instantiation of the basic_string class template).
Exceptions
AConversionException will be thrown if the provided string is not a valid number.
Remarks
This function is using std::basic_stringstream interanlly and hence also has its limitations (eg. regarding base and types).
See also
numberToString(), bufferToNumber()
Todo:
Provide an alternative using std::expected (when switching to C++17).

Definition at line 455 of file stringconversion.h.

◆ stringToNumber() [3/4]

template<typename IntegralType , class CharType , Traits::EnableIf< std::is_integral< IntegralType >, std::is_unsigned< IntegralType >> * = nullptr>
IntegralType ConversionUtilities::stringToNumber ( const CharType *  string,
unsigned char  base = 10 
)

Converts the given null-terminated string to an unsigned numeric value using the specified base.

Converts the given null-terminated string to a signed numeric value using the specified base.

Template Parameters
IntegralTypeThe data type used to store the converted value.
CharTypeThe character type.
Exceptions
AConversionException will be thrown if the provided string is not a valid number.
See also
numberToString(), bufferToNumber()
Todo:
Provide an alternative using std::expected (when switching to C++17).
Template Parameters
IntegralTypeThe data type used to store the converted value.
CharTypeThe character type.
Exceptions
AConversionException will be thrown if the provided string is not a valid number.
See also
numberToString(), bufferToNumber()

Definition at line 480 of file stringconversion.h.

◆ stringToNumber() [4/4]

template<typename FloatingType , class CharType , Traits::EnableIf< std::is_floating_point< FloatingType >> * = nullptr>
FloatingType ConversionUtilities::stringToNumber ( const CharType *  string,
unsigned char  base = 10 
)

Converts the given null-terminated string to a number assuming string uses the specified base.

Template Parameters
FloatingTypeThe data type used to store the converted value.
CharTypeThe character type.
Exceptions
AConversionException will be thrown if the provided string is not a valid number.
Remarks
This function is using std::basic_stringstream interanlly and hence also has its limitations (eg. regarding base and types).
See also
numberToString(), bufferToNumber()
Todo:
Provide an alternative using std::expected (when switching to C++17).

Definition at line 504 of file stringconversion.h.

◆ swapOrder() [1/3]

CPP_UTILITIES_EXPORT constexpr uint16 ConversionUtilities::swapOrder ( uint16  value)

Swaps the byte order of the specified 16-bit unsigned integer.

Definition at line 154 of file binaryconversion.h.

◆ swapOrder() [2/3]

CPP_UTILITIES_EXPORT constexpr uint32 ConversionUtilities::swapOrder ( uint32  value)

Swaps the byte order of the specified 32-bit unsigned integer.

Definition at line 162 of file binaryconversion.h.

◆ swapOrder() [3/3]

CPP_UTILITIES_EXPORT constexpr uint64 ConversionUtilities::swapOrder ( uint64  value)

Swaps the byte order of the specified 64-bit unsigned integer.

Definition at line 170 of file binaryconversion.h.

◆ toArrayOfLines()

template<class Container = std::vector<std::string>>
std::vector<std::string> ConversionUtilities::toArrayOfLines ( const std::string &  multilineString)
inline

Converts the specified multilineString to an array of lines.

Definition at line 202 of file stringconversion.h.

◆ toFixed16()

CPP_UTILITIES_EXPORT constexpr uint32 ConversionUtilities::toFixed16 ( float32  float32value)

Returns the 16.16 fixed point representation converted from the specified 32-bit floating point number.

Definition at line 117 of file binaryconversion.h.

◆ toFixed8()

CPP_UTILITIES_EXPORT constexpr uint16 ConversionUtilities::toFixed8 ( float32  float32value)

Returns the 8.8 fixed point representation converted from the specified 32-bit floating point number.

Definition at line 101 of file binaryconversion.h.

◆ toFloat32() [1/2]

CPP_UTILITIES_EXPORT constexpr float32 ConversionUtilities::toFloat32 ( uint16  fixed8value)

Returns a 32-bit floating point number converted from the specified 8.8 fixed point representation.

Definition at line 109 of file binaryconversion.h.

◆ toFloat32() [2/2]

CPP_UTILITIES_EXPORT constexpr float32 ConversionUtilities::toFloat32 ( uint32  fixed16value)

Returns a 32-bit floating point number converted from the specified 16.16 fixed point representation.

Definition at line 125 of file binaryconversion.h.

◆ toMultiline()

template<class Container = std::initializer_list<std::string>>
std::vector<std::string> ConversionUtilities::toMultiline ( const Container &  arrayOfLines)
inline

Converts the specified arrayOfLines to a multiline string.

Definition at line 111 of file stringconversion.h.

◆ toNormalInt()

CPP_UTILITIES_EXPORT constexpr uint32 ConversionUtilities::toNormalInt ( uint32  synchsafeInt)

Returns a normal 32-bit integer converted from a 32-bit synchsafe integer.

Remarks
Synchsafe integers appear in ID3 tags that are attached to an MP3 file.
See also
ID3 tag version 2.4.0 - Main Structure

Definition at line 145 of file binaryconversion.h.

◆ toSynchsafeInt()

CPP_UTILITIES_EXPORT constexpr uint32 ConversionUtilities::toSynchsafeInt ( uint32  normalInt)

Returns a 32-bit synchsafe integer converted from a normal 32-bit integer.

Remarks
Synchsafe integers appear in ID3 tags that are attached to an MP3 file.
See also
ID3 tag version 2.4.0 - Main Structure

Definition at line 135 of file binaryconversion.h.

◆ truncateString()

void ConversionUtilities::truncateString ( std::string &  str,
char  terminationChar = '\0' 
)

Truncates all characters after the first occurrence of the specified terminationChar and the termination character as well.

Definition at line 239 of file stringconversion.cpp.

◆ tupleToString()

template<class StringType = std::string, class... Args>
StringType ConversionUtilities::tupleToString ( const std::tuple< Args... > &  tuple)

Concatenates all strings hold by the specified tuple.

Definition at line 139 of file stringbuilder.h.