1 #ifndef CONVERSION_UTILITIES_STRINGCONVERSION_H
2 #define CONVERSION_UTILITIES_STRINGCONVERSION_H
7 #include "../misc/traits.h"
11 #include <initializer_list>
17 #include <string_view>
18 #include <system_error>
21 #if __cplusplus >= 201709
37 std::free(stringData);
44 using StringData = std::pair<std::unique_ptr<char[], StringDataDeleter>, std::size_t>;
48 const char *fromCharset,
const char *toCharset,
const char *inputBuffer, std::size_t inputBufferSize,
float outputBufferSizeFactor = 1.0f);
56 #ifdef PLATFORM_WINDOWS
57 using WideStringData = std::pair<std::unique_ptr<wchar_t[]>,
int>;
58 CPP_UTILITIES_EXPORT WideStringData convertMultiByteToWide(std::error_code &ec,
const char *inputBuffer,
int inputBufferSize = -1);
59 CPP_UTILITIES_EXPORT WideStringData convertMultiByteToWide(std::error_code &ec,
const std::string &inputBuffer);
60 CPP_UTILITIES_EXPORT WideStringData convertMultiByteToWide(
const char *inputBuffer,
int inputBufferSize = -1);
68 #if __cplusplus >= 201709
69 template <
class Container>
70 using ContainerValueType =
typename std::conditional_t<std::ranges::range<Container>,
71 std::iterator_traits<std::remove_cvref_t<std::ranges::iterator_t<Container>>>, Container>::value_type;
73 template <
class Container>
using ContainerValueType =
typename Container::value_type;
75 template <
class Container>
using DefaultReturnTypeForContainer = ContainerValueType<Container>;
76 template <
class Container>
using StringParamForContainer = std::basic_string_view<typename ContainerValueType<Container>::value_type>;
94 template <
class Container = std::initializer_list<std::
string>,
class ReturnType = Detail::DefaultReturnTypeForContainer<Container>>
95 ReturnType
joinStrings(
const Container &strings, Detail::StringParamForContainer<Container> delimiter = Detail::StringParamForContainer<Container>(),
96 bool omitEmpty =
false, Detail::StringParamForContainer<Container> leftClosure = Detail::StringParamForContainer<Container>(),
97 Detail::StringParamForContainer<Container> rightClosure = Detail::StringParamForContainer<Container>())
100 if (!strings.size()) {
103 std::size_t entries = 0, size = 0;
104 for (
const auto &str : strings) {
105 if (omitEmpty && str.empty()) {
114 size += (entries * leftClosure.size()) + (entries * rightClosure.size()) + ((entries - 1) * delimiter.size());
116 for (
const auto &str : strings) {
117 if (omitEmpty && str.empty()) {
121 res.append(delimiter);
123 res.append(leftClosure);
125 res.append(rightClosure);
133 template <
class Container = std::initializer_list<std::
string>>
inline auto toMultiline(
const Container &arrayOfLines)
156 template <
class Container = std::list<std::
string>>
157 Container
splitString(Detail::StringParamForContainer<Container>
string, Detail::StringParamForContainer<Container> delimiter,
162 typename Container::value_type *last =
nullptr;
164 for (
typename Container::value_type::size_type
i = 0, end =
string.size(), delimPos;
i < end;
i = delimPos + delimiter.size()) {
165 delimPos =
string.find(delimiter,
i);
166 if (!merge && maxParts >= 0 && res.size() ==
static_cast<typename Container::value_type::size_type
>(maxParts)) {
173 delimPos = Container::value_type::npos;
175 if (delimPos == Container::value_type::npos) {
176 delimPos =
string.size();
180 last->append(delimiter);
181 last->append(
string,
i, delimPos -
i);
184 last = &res.emplace_back(
string,
i, delimPos -
i);
204 template <
class Container = std::list<std::
string>>
206 Detail::StringParamForContainer<Container>
string, Detail::StringParamForContainer<Container> delimiter,
int maxParts = -1)
210 for (
typename Container::value_type::size_type
i = 0, end =
string.size(), delimPos;
i < end;
i = delimPos + delimiter.size()) {
211 delimPos =
string.find(delimiter,
i);
212 if (maxParts >= 0 && res.size() ==
static_cast<typename Container::value_type::size_type
>(maxParts)) {
213 delimPos = Container::value_type::npos;
215 if (delimPos == Container::value_type::npos) {
216 delimPos =
string.size();
218 #if __cplusplus >= 201709
219 if constexpr (requires { res.emplace_back(
string); }) {
221 res.emplace_back(
string.data() +
i, delimPos -
i);
222 #if __cplusplus >= 201709
224 res.emplace(
string.data() +
i, delimPos -
i);
234 template <
class Container = std::vector<std::
string>>
inline auto toArrayOfLines(
const std::string &multilineString)
242 template <
typename StringType>
bool startsWith(
const StringType &str,
const StringType &phrase)
244 if (str.size() < phrase.size()) {
247 for (
auto stri = str.cbegin(), strend = str.cend(), phrasei = phrase.cbegin(), phraseend = phrase.cend();; ++stri, ++phrasei) {
248 if (phrasei == phraseend) {
250 }
else if (stri == strend) {
252 }
else if (*stri != *phrasei) {
262 template <
typename StringType>
bool startsWith(
const StringType &str,
const typename StringType::value_type *phrase)
264 for (
auto stri = str.cbegin(), strend = str.cend();; ++stri, ++phrase) {
267 }
else if (stri == strend) {
269 }
else if (*stri != *phrase) {
279 template <
typename StringType>
bool endsWith(
const StringType &str,
const StringType &phrase)
281 if (str.size() < phrase.size()) {
284 for (
auto stri = str.cend() -
static_cast<typename StringType::difference_type
>(phrase.size()), strend = str.cend(), phrasei = phrase.cbegin();
285 stri != strend; ++stri, ++phrasei) {
286 if (*stri != *phrasei) {
296 template <
typename StringType>
bool endsWith(
const StringType &str,
const typename StringType::value_type *phrase)
298 const auto phraseSize = std::strlen(phrase);
299 if (str.size() < phraseSize) {
302 for (
auto stri = str.cend() -
static_cast<typename StringType::difference_type
>(phraseSize), strend = str.cend(); stri != strend;
304 if (*stri != *phrase) {
315 template <
typename StringType>
bool containsSubstrings(
const StringType &str, std::initializer_list<StringType> substrings)
317 typename StringType::size_type currentPos = 0;
318 for (
const auto &substr : substrings) {
319 if ((currentPos = str.find(substr, currentPos)) == StringType::npos) {
322 currentPos += substr.size();
331 template <
typename StringType>
332 bool containsSubstrings(
const StringType &str, std::initializer_list<const typename StringType::value_type *> substrings)
334 typename StringType::size_type currentPos = 0;
335 for (
const auto *substr : substrings) {
336 if ((currentPos = str.find(substr, currentPos)) == StringType::npos) {
339 currentPos += std::strlen(substr);
347 template <
typename StringType1,
typename StringType2,
typename StringType3>
348 void findAndReplace(StringType1 &str,
const StringType2 &find,
const StringType3 &replace)
350 for (
typename StringType1::size_type
i = 0; (
i = str.find(find,
i)) != StringType1::npos;
i += replace.size()) {
351 str.replace(
i, find.size(), replace);
358 template <
typename StringType>
359 inline void findAndReplace(StringType &str,
const typename StringType::value_type *find,
const typename StringType::value_type *replace)
362 str, std::basic_string_view<typename StringType::value_type>(find), std::basic_string_view<typename StringType::value_type>(replace));
368 template <
typename StringType1,
typename StringType2>
369 inline void findAndReplace(StringType1 &str,
const StringType2 &find,
const typename StringType1::value_type *replace)
371 findAndReplace(str, find, std::basic_string_view<typename StringType1::value_type>(replace));
377 template <
typename StringType1,
typename StringType2>
378 inline void findAndReplace(StringType1 &str,
const typename StringType1::value_type *find,
const StringType2 &replace)
380 findAndReplace(str, std::basic_string_view<typename StringType1::value_type>(find), replace);
389 template <
typename CharType> constexpr CharType
digitToChar(CharType digit)
391 return digit <= 9 ? (digit +
'0') : (digit +
'A' - 10);
400 template <
typename IntegralType,
class StringType = std::string,
typename BaseType = IntegralType,
404 std::size_t resSize = 0;
405 for (
auto n = number; n; n /=
static_cast<IntegralType
>(base), ++resSize)
408 res.reserve(resSize);
410 res.insert(res.begin(), digitToChar<typename StringType::value_type>(
static_cast<typename StringType::value_type
>(number % base)));
411 number /=
static_cast<IntegralType
>(base);
422 template <
typename IntegralType,
class StringType = std::string,
typename BaseType = IntegralType,
423 Traits::EnableIf<std::is_integral<IntegralType>, std::is_signed<IntegralType>> * =
nullptr>
424 StringType
numberToString(IntegralType number, BaseType base = 10)
426 const bool negative = number < 0;
429 number = -number, resSize = 1;
433 for (
auto n = number; n; n /=
static_cast<IntegralType
>(base), ++resSize)
436 res.reserve(resSize);
438 res.insert(res.begin(),
439 digitToChar<typename StringType::value_type>(
static_cast<typename StringType::value_type
>(number %
static_cast<IntegralType
>(base))));
440 number /=
static_cast<IntegralType
>(base);
443 res.insert(res.begin(),
'-');
456 template <
typename FloatingType,
class StringType = std::
string, Traits::EnableIf<std::is_
floating_po
int<FloatingType>> * =
nullptr>
459 std::basic_stringstream<typename StringType::value_type> ss;
460 ss << std::setbase(base) << number;
468 template <
typename CharType> CharType
charToDigit(CharType character, CharType base)
471 if (character >=
'0' && character <=
'9') {
472 res = character -
'0';
473 }
else if (character >=
'a' && character <=
'z') {
474 res = character -
'a' + 10;
475 }
else if (character >=
'A' && character <=
'Z') {
476 res = character -
'A' + 10;
481 std::string errorMsg;
482 errorMsg.reserve(36);
483 errorMsg +=
"The character \"";
484 errorMsg += character >=
' ' && character <= '~' ? static_cast<std::string::value_type>(character) :
'?';
485 errorMsg +=
"\" is no valid digit.";
496 template <
typename IntegralType,
class StringType,
typename BaseType = IntegralType,
497 Traits::EnableIf<std::is_integral<IntegralType>, std::is_unsigned<IntegralType>, Traits::Not<std::is_scalar<std::decay_t<StringType>>>>
501 IntegralType result = 0;
502 for (
const auto &c :
string) {
506 result *=
static_cast<IntegralType
>(base);
507 result +=
static_cast<IntegralType
>(charToDigit<typename StringType::value_type>(c,
static_cast<typename StringType::value_type
>(base)));
519 template <
typename IntegralType,
class StringType,
typename BaseType = IntegralType,
520 Traits::EnableIf<std::is_integral<IntegralType>, std::is_signed<IntegralType>, Traits::Not<std::is_scalar<std::decay_t<StringType>>>> * =
nullptr>
523 auto i =
string.begin();
524 auto end =
string.end();
525 for (;
i != end && *
i ==
' '; ++
i)
530 const bool negative = (*
i ==
'-');
534 IntegralType result = 0;
535 for (;
i != end; ++
i) {
539 result *=
static_cast<IntegralType
>(base);
540 result +=
static_cast<IntegralType
>(charToDigit<typename StringType::value_type>(*
i,
static_cast<typename StringType::value_type
>(base)));
542 return negative ? -result : result;
554 template <
typename FloatingType,
class StringType,
555 Traits::EnableIf<std::is_floating_point<FloatingType>, Traits::Not<std::is_scalar<std::decay_t<StringType>>>> * =
nullptr>
558 std::basic_stringstream<typename StringType::value_type> ss;
559 ss << std::setbase(base) << string;
561 if ((ss >> result) && ss.eof()) {
564 std::string errorMsg;
565 errorMsg.reserve(42 +
string.size());
566 errorMsg +=
"The string \"";
568 errorMsg +=
"\" is no valid floating number.";
579 template <
typename IntegralType,
typename CharType,
typename BaseType = IntegralType,
580 Traits::EnableIf<std::is_integral<IntegralType>, std::is_unsigned<IntegralType>> * =
nullptr>
583 IntegralType result = 0;
584 for (; *string; ++string) {
585 if (*
string ==
' ') {
588 result *=
static_cast<IntegralType
>(base);
589 result +=
static_cast<IntegralType
>(charToDigit<CharType>(*
string,
static_cast<CharType
>(base)));
603 template <
typename FloatingType,
class CharType, Traits::EnableIf<std::is_
floating_po
int<FloatingType>> * =
nullptr>
606 std::basic_stringstream<CharType> ss;
607 ss << std::setbase(base) << string;
609 if ((ss >> result) && ss.eof()) {
612 std::string errorMsg;
613 errorMsg.reserve(42 + std::char_traits<CharType>::length(
string));
614 errorMsg +=
"The string \"";
616 errorMsg +=
"\" is no valid floating number.";
627 template <
typename IntegralType,
class CharType,
typename BaseType = IntegralType,
628 Traits::EnableIf<std::is_integral<IntegralType>, std::is_unsigned<IntegralType>> * =
nullptr>
629 IntegralType
bufferToNumber(
const CharType *
string, std::size_t size, BaseType base = 10)
631 IntegralType result = 0;
632 for (
const CharType *end =
string + size;
string != end; ++string) {
633 if (*
string ==
' ') {
636 result *=
static_cast<IntegralType
>(base);
637 result +=
static_cast<IntegralType
>(charToDigit<CharType>(*
string,
static_cast<CharType
>(base)));
649 template <
typename IntegralType,
typename CharType,
typename BaseType = IntegralType,
650 Traits::EnableIf<std::is_integral<IntegralType>, std::is_signed<IntegralType>> * =
nullptr>
656 for (; *
string && *
string ==
' '; ++string)
661 const bool negative = (*
string ==
'-');
665 IntegralType result = 0;
666 for (; *string; ++string) {
667 if (*
string ==
' ') {
670 result *=
static_cast<IntegralType
>(base);
671 result +=
static_cast<IntegralType
>(charToDigit<CharType>(*
string,
static_cast<CharType
>(base)));
673 return negative ? -result : result;
683 template <
typename IntegralType,
typename CharType,
typename BaseType = IntegralType,
684 Traits::EnableIf<std::is_integral<IntegralType>, std::is_signed<IntegralType>> * =
nullptr>
685 IntegralType
bufferToNumber(
const CharType *
string, std::size_t size, BaseType base = 10)
690 const CharType *end =
string + size;
691 for (;
string != end && *
string ==
' '; ++string)
696 const bool negative = (*
string ==
'-');
700 IntegralType result = 0;
701 for (;
string != end; ++string) {
702 if (*
string ==
' ') {
705 result *=
static_cast<IntegralType
>(base);
706 result +=
static_cast<IntegralType
>(charToDigit<CharType>(*
string,
static_cast<CharType
>(base)));
708 return negative ? -result : result;
722 char buffer[
sizeof(T)];
723 BE::getBytes(integer, buffer);
724 return std::string(buffer + startOffset,
sizeof(T) -
static_cast<std::size_t
>(startOffset));
The ConversionException class is thrown by the various conversion functions of this library when a co...
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
typename std::enable_if< All< Condition... >::value, Detail::Enabler >::type EnableIf
Shortcut for std::enable_if to omit ::value and ::type.
Contains all utilities provides by the c++utilities library.
void findAndReplace(StringType1 &str, const StringType2 &find, const StringType3 &replace)
Replaces all occurences of find with relpace in the specified str.
CPP_UTILITIES_EXPORT StringData convertUtf8ToUtf16BE(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified UTF-8 string to UTF-16 (big-endian).
CPP_UTILITIES_EXPORT StringData convertString(const char *fromCharset, const char *toCharset, const char *inputBuffer, std::size_t inputBufferSize, float outputBufferSizeFactor=1.0f)
Converts the specified string from one character set to another.
StringType numberToString(IntegralType number, BaseType base=10)
Converts the given number to its equivalent string representation using the specified base.
CPP_UTILITIES_EXPORT StringData convertLatin1ToUtf8(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified Latin-1 string to UTF-8.
CPP_UTILITIES_EXPORT StringData convertUtf16LEToUtf8(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified UTF-16 (little-endian) string to UTF-8.
EmptyPartsTreat
Specifies the role of empty parts when splitting strings.
CPP_UTILITIES_EXPORT std::pair< std::unique_ptr< std::uint8_t[]>, std::uint32_t > decodeBase64(const char *encodedStr, const std::uint32_t strSize)
Decodes the specified Base64 encoded string.
ReturnType 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.
bool startsWith(const StringType &str, const StringType &phrase)
Returns whether str starts with phrase.
IntegralType stringToNumber(const StringType &string, BaseType base=10)
Converts the given string to an unsigned number assuming string uses the specified base.
CPP_UTILITIES_EXPORT void truncateString(std::string &str, char terminationChar='\0')
Truncates all characters after the first occurrence of the specified terminationChar and the terminat...
std::pair< std::unique_ptr< char[], StringDataDeleter >, std::size_t > StringData
Type used to return string encoding conversion result.
CPP_UTILITIES_EXPORT StringData convertUtf16BEToUtf8(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified UTF-16 (big-endian) string to UTF-8.
constexpr CharType digitToChar(CharType digit)
Returns the character representation of the specified digit.
Container 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.
auto toArrayOfLines(const std::string &multilineString)
Converts the specified multilineString to an array of lines.
bool containsSubstrings(const StringType &str, std::initializer_list< StringType > substrings)
Returns whether str contains the specified substrings.
CPP_UTILITIES_EXPORT StringData convertUtf8ToLatin1(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified UTF-8 string to Latin-1.
std::string interpretIntegerAsString(T integer, int startOffset=0)
Interprets the given integer at the specified position as std::string using the specified byte order.
bool endsWith(const StringType &str, const StringType &phrase)
Returns whether str ends with phrase.
auto toMultiline(const Container &arrayOfLines)
Converts the specified arrayOfLines to a multiline string.
CPP_UTILITIES_EXPORT StringData convertUtf8ToUtf16LE(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified UTF-8 string to UTF-16 (little-endian).
CPP_UTILITIES_EXPORT std::string bitrateToString(double speedInKbitsPerSecond, bool useByteInsteadOfBits=false)
Converts the specified bitrate in kbit/s to its equivalent std::string representation.
CharType charToDigit(CharType character, CharType base)
Returns number/digit of the specified character representation using the specified base.
CPP_UTILITIES_EXPORT std::string encodeBase64(const std::uint8_t *data, std::uint32_t dataSize)
Encodes the specified data to Base64.
CPP_UTILITIES_EXPORT std::string dataSizeToString(std::uint64_t sizeInByte, bool includeByte=false)
Converts the specified data size in byte to its equivalent std::string representation.
IntegralType 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.
Container splitString(Detail::StringParamForContainer< Container > string, Detail::StringParamForContainer< Container > delimiter, EmptyPartsTreat emptyPartsRole=EmptyPartsTreat::Keep, int maxParts=-1)
Splits the given string at the specified delimiter.
The StringDataDeleter struct deletes the data of a StringData instance.
void operator()(char *stringData)
Deletes the specified stringData with std::free(), because the memory has been allocated using std::m...