C++ Utilities 5.27.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
Loading...
Searching...
No Matches
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 <string_view>
#include <system_error>
#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  CppUtilities::StringDataDeleter
 The StringDataDeleter struct deletes the data of a StringData instance. More...
 

Namespaces

namespace  CppUtilities
 Contains all utilities provides by the c++utilities library.
 

Typedefs

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

Enumerations

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

Functions

CPP_UTILITIES_EXPORT StringData CppUtilities::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.
 
CPP_UTILITIES_EXPORT StringData CppUtilities::convertUtf8ToUtf16LE (const char *inputBuffer, std::size_t inputBufferSize)
 Converts the specified UTF-8 string to UTF-16 (little-endian).
 
CPP_UTILITIES_EXPORT StringData CppUtilities::convertUtf16LEToUtf8 (const char *inputBuffer, std::size_t inputBufferSize)
 Converts the specified UTF-16 (little-endian) string to UTF-8.
 
CPP_UTILITIES_EXPORT StringData CppUtilities::convertUtf8ToUtf16BE (const char *inputBuffer, std::size_t inputBufferSize)
 Converts the specified UTF-8 string to UTF-16 (big-endian).
 
CPP_UTILITIES_EXPORT StringData CppUtilities::convertUtf16BEToUtf8 (const char *inputBuffer, std::size_t inputBufferSize)
 Converts the specified UTF-16 (big-endian) string to UTF-8.
 
CPP_UTILITIES_EXPORT StringData CppUtilities::convertLatin1ToUtf8 (const char *inputBuffer, std::size_t inputBufferSize)
 Converts the specified Latin-1 string to UTF-8.
 
CPP_UTILITIES_EXPORT StringData CppUtilities::convertUtf8ToLatin1 (const char *inputBuffer, std::size_t inputBufferSize)
 Converts the specified UTF-8 string to Latin-1.
 
CPP_UTILITIES_EXPORT void CppUtilities::truncateString (string &str, char terminationChar)
 Truncates all characters after the first occurrence of the specified terminationChar and the termination character as well.
 
template<class Container = std::initializer_list<std::string>, class ReturnType = Detail::DefaultReturnTypeForContainer<Container>>
ReturnType CppUtilities::joinStrings (const Container &strings, Detail::StringParamForContainer< Container > delimiter=Detail::StringParamForContainer< Container >(), bool omitEmpty=false, Detail::StringParamForContainer< Container > leftClosure=Detail::StringParamForContainer< Container >(), Detail::StringParamForContainer< Container > rightClosure=Detail::StringParamForContainer< Container >())
 Joins the given strings using the specified delimiter.
 
template<class Container = std::initializer_list<std::string>>
auto CppUtilities::toMultiline (const Container &arrayOfLines)
 Converts the specified arrayOfLines to a multiline string.
 
template<class Container = std::list<std::string>>
Container CppUtilities::splitString (Detail::StringParamForContainer< Container > string, Detail::StringParamForContainer< Container > delimiter, EmptyPartsTreat emptyPartsRole=EmptyPartsTreat::Keep, int maxParts=-1)
 Splits the given string at the specified delimiter.
 
template<class Container = std::list<std::string>>
Container CppUtilities::splitStringSimple (Detail::StringParamForContainer< Container > string, Detail::StringParamForContainer< Container > delimiter, int maxParts=-1)
 Splits the given string (which might also be a string view) at the specified delimiter.
 
template<class Container = std::vector<std::string>>
auto CppUtilities::toArrayOfLines (const std::string &multilineString)
 Converts the specified multilineString to an array of lines.
 
template<typename StringType >
bool CppUtilities::startsWith (const StringType &str, const StringType &phrase)
 Returns whether str starts with phrase.
 
template<typename StringType >
bool CppUtilities::startsWith (const StringType &str, const typename StringType::value_type *phrase)
 Returns whether str starts with phrase.
 
template<typename StringType >
bool CppUtilities::endsWith (const StringType &str, const StringType &phrase)
 Returns whether str ends with phrase.
 
template<typename StringType >
bool CppUtilities::endsWith (const StringType &str, const typename StringType::value_type *phrase)
 Returns whether str ends with phrase.
 
template<typename StringType >
bool CppUtilities::containsSubstrings (const StringType &str, std::initializer_list< StringType > substrings)
 Returns whether str contains the specified substrings.
 
template<typename StringType >
bool CppUtilities::containsSubstrings (const StringType &str, std::initializer_list< const typename StringType::value_type * > substrings)
 Returns whether str contains the specified substrings.
 
template<typename StringType1 , typename StringType2 , typename StringType3 >
void CppUtilities::findAndReplace (StringType1 &str, const StringType2 &find, const StringType3 &replace)
 Replaces all occurrences of find with relpace in the specified str.
 
template<typename StringType >
void CppUtilities::findAndReplace (StringType &str, const typename StringType::value_type *find, const typename StringType::value_type *replace)
 Replaces all occurrences of find with relpace in the specified str.
 
template<typename StringType1 , typename StringType2 >
void CppUtilities::findAndReplace (StringType1 &str, const StringType2 &find, const typename StringType1::value_type *replace)
 Replaces all occurrences of find with relpace in the specified str.
 
template<typename StringType1 , typename StringType2 >
void CppUtilities::findAndReplace (StringType1 &str, const typename StringType1::value_type *find, const StringType2 &replace)
 Replaces all occurrences of find with relpace in the specified str.
 
template<typename CharType >
constexpr CharType CppUtilities::digitToChar (CharType digit)
 Returns the character representation of the specified digit.
 
template<typename IntegralType , class StringType = std::string, typename BaseType = IntegralType, CppUtilities::Traits::EnableIf< std::is_integral< IntegralType >, std::is_unsigned< IntegralType > > * = nullptr>
StringType CppUtilities::numberToString (IntegralType number, BaseType base=10)
 Converts the given number to its equivalent string representation using the specified base.
 
template<typename FloatingType , class StringType = std::string, Traits::EnableIf< std::is_floating_point< FloatingType > > * = nullptr>
StringType CppUtilities::numberToString (FloatingType number, int base=10)
 Converts the given number to its equivalent string representation using the specified base.
 
template<typename CharType >
CharType CppUtilities::charToDigit (CharType character, CharType base)
 Returns number/digit of the specified character representation using the specified base.
 
template<typename IntegralType , class CharType , typename BaseType = IntegralType, Traits::EnableIf< std::is_integral< IntegralType >, std::is_unsigned< IntegralType > > * = nullptr>
IntegralType CppUtilities::bufferToNumber (const CharType *string, std::size_t size, BaseType base=10)
 Converts the given string of size characters to an unsigned numeric value using the specified base.
 
template<typename IntegralType , class StringType , typename BaseType = IntegralType, Traits::EnableIf< std::is_integral< IntegralType >, Traits::Not< std::is_scalar< std::decay_t< StringType > > > > * = nullptr>
IntegralType CppUtilities::stringToNumber (const StringType &string, BaseType base=10)
 Converts the given string to an unsigned/signed number assuming string uses the specified base.
 
template<typename FloatingType , class StringViewType , Traits::EnableIf< std::is_floating_point< FloatingType >, Traits::IsSpecializationOf< StringViewType, std::basic_string_view > > * = nullptr>
FloatingType CppUtilities::stringToNumber (StringViewType stringView, int base=10)
 Converts the given stringView to a number assuming stringView uses the specified base.
 
template<typename FloatingType , class StringType , Traits::EnableIf< std::is_floating_point< FloatingType >, Traits::Not< std::is_scalar< std::decay_t< StringType > > >, Traits::Not< Traits::IsSpecializationOf< StringType, std::basic_string_view > > > * = nullptr>
FloatingType CppUtilities::stringToNumber (const StringType &string, int base=10)
 Converts the given string to a number assuming string uses the specified base.
 
template<typename IntegralType , typename CharType , typename BaseType = IntegralType, Traits::EnableIf< std::is_integral< IntegralType >, std::is_unsigned< IntegralType > > * = nullptr>
IntegralType CppUtilities::stringToNumber (const CharType *string, BaseType base=10)
 Converts the given null-terminated string to an unsigned numeric value using the specified base.
 
template<typename FloatingType , class CharType , Traits::EnableIf< std::is_floating_point< FloatingType > > * = nullptr>
FloatingType CppUtilities::stringToNumber (const CharType *string, int base=10)
 Converts the given null-terminated string to a number assuming string uses the specified base.
 
template<typename IntegralType , typename CharType , typename BaseType = IntegralType, Traits::EnableIf< std::is_integral< IntegralType >, std::is_signed< IntegralType > > * = nullptr>
IntegralType CppUtilities::stringToNumber (const CharType *string, IntegralType base=10)
 Converts the given null-terminated string to a signed numeric value using the specified base.
 
template<typename T >
std::string CppUtilities::interpretIntegerAsString (T integer, int startOffset=0)
 Interprets the given integer at the specified position as std::string using the specified byte order.
 
CPP_UTILITIES_EXPORT std::string CppUtilities::dataSizeToString (std::uint64_t sizeInByte, bool includeByte)
 Converts the specified data size in byte to its equivalent std::string representation.
 
CPP_UTILITIES_EXPORT std::string CppUtilities::bitrateToString (double bitrateInKbitsPerSecond, bool useIecBinaryPrefixes)
 Converts the specified bitrate in kbit/s to its equivalent std::string representation.
 
CPP_UTILITIES_EXPORT std::string CppUtilities::encodeBase64 (const std::uint8_t *data, std::uint32_t dataSize)
 Encodes the specified data to Base64.
 
CPP_UTILITIES_EXPORT std::pair< std::unique_ptr< std::uint8_t[]>, std::uint32_t > CppUtilities::decodeBase64 (const char *encodedStr, const std::uint32_t strSize)
 Decodes the specified Base64 encoded string.