C++ Utilities  4.17.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
Classes | Namespaces | Typedefs | Enumerations | Functions
stringconversion.h File Reference
#include "./binaryconversion.h"
#include "./conversionexception.h"
#include "../misc/traits.h"
#include <cstdlib>
#include <cstring>
#include <initializer_list>
#include <iomanip>
#include <list>
#include <memory>
#include <sstream>
#include <string>
#include <vector>
Include dependency graph for stringconversion.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  ConversionUtilities::StringDataDeleter
 The StringDataDeleter struct deletes the data of a StringData instance. More...
 

Namespaces

 ConversionUtilities
 Contains several functions providing conversions between different data types.
 

Typedefs

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

Enumerations

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

Functions

CPP_UTILITIES_EXPORT 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. More...
 
CPP_UTILITIES_EXPORT StringData ConversionUtilities::convertUtf8ToUtf16LE (const char *inputBuffer, std::size_t inputBufferSize)
 Converts the specified UTF-8 string to UTF-16 (little-endian). More...
 
CPP_UTILITIES_EXPORT StringData ConversionUtilities::convertUtf16LEToUtf8 (const char *inputBuffer, std::size_t inputBufferSize)
 Converts the specified UTF-16 (little-endian) string to UTF-8. More...
 
CPP_UTILITIES_EXPORT StringData ConversionUtilities::convertUtf8ToUtf16BE (const char *inputBuffer, std::size_t inputBufferSize)
 Converts the specified UTF-8 string to UTF-16 (big-endian). More...
 
CPP_UTILITIES_EXPORT StringData ConversionUtilities::convertUtf16BEToUtf8 (const char *inputBuffer, std::size_t inputBufferSize)
 Converts the specified UTF-16 (big-endian) string to UTF-8. More...
 
CPP_UTILITIES_EXPORT StringData ConversionUtilities::convertLatin1ToUtf8 (const char *inputBuffer, std::size_t inputBufferSize)
 Converts the specified Latin-1 string to UTF-8. More...
 
CPP_UTILITIES_EXPORT StringData ConversionUtilities::convertUtf8ToLatin1 (const char *inputBuffer, std::size_t inputBufferSize)
 Converts the specified UTF-8 string to Latin-1. More...
 
CPP_UTILITIES_EXPORT void ConversionUtilities::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 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. More...
 
template<class Container = std::initializer_list<std::string>>
std::vector< std::string > ConversionUtilities::toMultiline (const Container &arrayOfLines)
 Converts the specified arrayOfLines to a multiline string. More...
 
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. More...
 
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. More...
 
template<class Container = std::vector<std::string>>
std::vector< std::string > ConversionUtilities::toArrayOfLines (const std::string &multilineString)
 Converts the specified multilineString to an array of lines. More...
 
template<typename StringType >
bool ConversionUtilities::startsWith (const StringType &str, const StringType &phrase)
 Returns whether str starts with phrase. More...
 
template<typename StringType >
bool ConversionUtilities::startsWith (const StringType &str, const typename StringType::value_type *phrase)
 Returns whether str starts with phrase. More...
 
template<typename StringType >
bool ConversionUtilities::containsSubstrings (const StringType &str, std::initializer_list< StringType > substrings)
 Returns whether str contains the specified substrings. More...
 
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. More...
 
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. More...
 
template<typename CharType >
constexpr CharType ConversionUtilities::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 ConversionUtilities::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 ConversionUtilities::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 ConversionUtilities::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 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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
template<typename T >
std::string ConversionUtilities::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 ConversionUtilities::dataSizeToString (uint64 sizeInByte, bool includeByte)
 Converts the specified data size in byte to its equivalent std::string representation. More...
 
CPP_UTILITIES_EXPORT std::string ConversionUtilities::bitrateToString (double bitrateInKbitsPerSecond, bool useIecBinaryPrefixes)
 Converts the specified bitrate in kbit/s to its equivalent std::string representation. More...
 
CPP_UTILITIES_EXPORT std::string ConversionUtilities::encodeBase64 (const byte *data, uint32 dataSize)
 Encodes the specified data to Base64. More...
 
CPP_UTILITIES_EXPORT std::pair< std::unique_ptr< byte[]>, uint32ConversionUtilities::decodeBase64 (const char *encodedStr, const uint32 strSize)
 Decodes the specified Base64 encoded string. More...