1 #ifndef CONVERSION_UTILITIES_STRINGCONVERSION_H 2 #define CONVERSION_UTILITIES_STRINGCONVERSION_H 7 #include "../misc/traits.h" 10 #include <initializer_list> 30 std::free(stringData);
37 typedef std::pair<std::unique_ptr<char[], StringDataDeleter>, std::size_t>
StringData;
41 const char *fromCharset,
const char *toCharset,
const char *inputBuffer, std::size_t inputBufferSize,
float outputBufferSizeFactor = 1.0f);
64 template <
class Container = std::initializer_list<std::
string>>
65 typename Container::value_type
joinStrings(
const Container &strings,
66 const typename Container::value_type &delimiter =
typename Container::value_type(),
bool omitEmpty =
false,
67 const typename Container::value_type &leftClosure =
typename Container::value_type(),
68 const typename Container::value_type &rightClosure =
typename Container::value_type())
70 typename Container::value_type res;
72 size_t entries = 0, size = 0;
73 for (
const auto &str : strings) {
74 if (!omitEmpty || !str.empty()) {
80 size += (entries * leftClosure.size()) + (entries * rightClosure.size()) + ((entries - 1) * delimiter.size());
82 for (
const auto &str : strings) {
83 if (!omitEmpty || !str.empty()) {
85 res.append(delimiter);
87 res.append(leftClosure);
89 res.append(rightClosure);
100 template <
class Container = std::initializer_list<std::
string>>
inline std::vector<std::string>
toMultiline(
const Container &arrayOfLines)
123 template <
class Container = std::list<std::
string>>
124 Container
splitString(
const typename Container::value_type &
string,
const typename Container::value_type &delimiter,
130 for (
typename Container::value_type::size_type i = 0, end =
string.size(), delimPos; i < end; i = delimPos + delimiter.size()) {
131 delimPos =
string.find(delimiter, i);
132 if (!merge && maxParts >= 0 && res.size() ==
static_cast<typename Container::value_type::size_type
>(maxParts)) {
139 delimPos = Container::value_type::npos;
141 if (delimPos == Container::value_type::npos) {
142 delimPos =
string.size();
146 res.back().append(delimiter);
147 res.back().append(
string.substr(i, delimPos - i));
150 res.emplace_back(
string.substr(i, delimPos - i));
170 template <
class Container = std::list<std::
string>>
171 Container
splitStringSimple(
const typename Container::value_type &
string,
const typename Container::value_type &delimiter,
int maxParts = -1)
175 for (
typename Container::value_type::size_type i = 0, end =
string.size(), delimPos; i < end; i = delimPos + delimiter.size()) {
176 delimPos =
string.find(delimiter, i);
177 if (maxParts >= 0 && res.size() ==
static_cast<typename Container::value_type::size_type
>(maxParts)) {
178 delimPos = Container::value_type::npos;
180 if (delimPos == Container::value_type::npos) {
181 delimPos =
string.size();
183 res.emplace_back(
string.substr(i, delimPos - i));
191 template <
class Container = std::vector<std::
string>>
inline std::vector<std::string>
toArrayOfLines(
const std::string &multilineString)
199 template <
typename StringType>
bool startsWith(
const StringType &str,
const StringType &phrase)
201 if (str.size() < phrase.size()) {
204 for (
auto stri = str.cbegin(), strend = str.cend(), phrasei = phrase.cbegin(), phraseend = phrase.cend(); stri != strend; ++stri, ++phrasei) {
205 if (phrasei == phraseend) {
207 }
else if (*stri != *phrasei) {
217 template <
typename StringType>
bool startsWith(
const StringType &str,
const typename StringType::value_type *phrase)
219 for (
auto stri = str.cbegin(), strend = str.cend(); stri != strend; ++stri, ++phrase) {
222 }
else if (*stri != *phrase) {
233 template <
typename StringType>
bool containsSubstrings(
const StringType &str, std::initializer_list<StringType> substrings)
235 typename StringType::size_type currentPos = 0;
236 for (
const auto &substr : substrings) {
237 if ((currentPos = str.find(substr, currentPos)) == StringType::npos) {
240 currentPos += substr.size();
249 template <
typename StringType>
250 bool containsSubstrings(
const StringType &str, std::initializer_list<const typename StringType::value_type *> substrings)
252 typename StringType::size_type currentPos = 0;
253 for (
const auto *substr : substrings) {
254 if ((currentPos = str.find(substr, currentPos)) == StringType::npos) {
257 currentPos += std::strlen(substr);
265 template <
typename StringType>
void findAndReplace(StringType &str,
const StringType &find,
const StringType &replace)
267 for (
typename StringType::size_type i = 0; (i = str.find(find, i)) != StringType::npos; i += replace.size()) {
268 str.replace(i, find.size(), replace);
278 template <
typename CharType> constexpr CharType
digitToChar(CharType digit)
280 return digit <= 9 ? (digit +
'0') : (digit +
'A' - 10);
289 template <
typename IntegralType,
class StringType = std::string,
291 StringType
numberToString(IntegralType number,
typename StringType::value_type base = 10)
293 std::size_t resSize = 0;
294 for (
auto n = number; n; n /= base, ++resSize)
297 res.reserve(resSize);
299 res.insert(res.begin(), digitToChar<typename StringType::value_type>(number % base));
311 template <
typename IntegralType,
class StringType = std::
string, Traits::EnableIf<std::is_
integral<IntegralType>, std::is_
signed<IntegralType>>...>
312 StringType
numberToString(IntegralType number,
typename StringType::value_type base = 10)
314 const bool negative = number < 0;
317 number = -number, resSize = 1;
321 for (
auto n = number; n; n /= base, ++resSize)
324 res.reserve(resSize);
326 res.insert(res.begin(), digitToChar<typename StringType::value_type>(number % base));
330 res.insert(res.begin(),
'-');
343 template <
typename FloatingType,
class StringType = std::
string, Traits::EnableIf<std::is_
floating_po
int<FloatingType>>...>
344 StringType
numberToString(FloatingType number,
typename StringType::value_type base = 10)
346 std::basic_stringstream<typename StringType::value_type> ss;
347 ss << std::setbase(base) << number;
355 template <
typename CharType> CharType
charToDigit(CharType character, CharType base)
358 if (character >=
'0' && character <=
'9') {
359 res = character -
'0';
360 }
else if (character >=
'a' && character <=
'z') {
361 res = character -
'a' + 10;
362 }
else if (character >=
'A' && character <=
'Z') {
363 res = character -
'A' + 10;
380 template <
typename IntegralType,
class StringType, Traits::EnableIf<std::is_
integral<IntegralType>, Traits::Not<std::is_
signed<IntegralType>>>...>
381 IntegralType
stringToNumber(
const StringType &
string,
typename StringType::value_type base = 10)
383 IntegralType result = 0;
384 for (
const auto &c :
string) {
389 result += charToDigit<typename StringType::value_type>(c, base);
401 template <
typename IntegralType,
class StringType, Traits::EnableIf<std::is_
integral<IntegralType>, std::is_
signed<IntegralType>>...>
402 IntegralType
stringToNumber(
const StringType &
string,
typename StringType::value_type base = 10)
404 auto i =
string.begin();
405 auto end =
string.end();
406 for (; i != end && *i ==
' '; ++i)
411 const bool negative = (*i ==
'-');
415 IntegralType result = 0;
416 for (; i != end; ++i) {
421 result += charToDigit<typename StringType::value_type>(*i, base);
423 return negative ? -result : result;
435 template <
typename FloatingType,
class StringType, Traits::EnableIf<std::is_
floating_po
int<FloatingType>>...>
436 FloatingType
stringToNumber(
const StringType &
string,
typename StringType::value_type base = 10)
438 std::basic_stringstream<typename StringType::value_type> ss;
439 ss << std::setbase(base) << string;
441 if ((ss >> result) && ss.eof()) {
455 template <
typename IntegralType,
class CharType, Traits::EnableIf<std::is_
integral<IntegralType>, Traits::Not<std::is_
signed<IntegralType>>>...>
458 IntegralType result = 0;
459 for (; *string; ++string) {
460 if (*
string ==
' ') {
464 result += charToDigit<CharType>(*string, base);
476 template <
typename IntegralType,
class CharType, Traits::EnableIf<std::is_
integral<IntegralType>, Traits::Not<std::is_
signed<IntegralType>>>...>
477 IntegralType
bufferToNumber(
const CharType *
string, std::size_t size,
unsigned char base = 10)
479 IntegralType result = 0;
480 for (
const CharType *end =
string + size;
string != end; ++string) {
481 if (*
string ==
' ') {
485 result += charToDigit<CharType>(*string, base);
497 template <
typename IntegralType,
class CharType, Traits::EnableIf<std::is_
integral<IntegralType>, std::is_
signed<IntegralType>>...>
498 IntegralType
stringToNumber(
const CharType *
string,
unsigned char base = 10)
503 for (; *
string && *
string ==
' '; ++string)
508 const bool negative = (*
string ==
'-');
512 IntegralType result = 0;
513 for (; *string; ++string) {
514 if (*
string ==
' ') {
518 result += charToDigit<CharType>(*string, base);
520 return negative ? -result : result;
530 template <
typename IntegralType,
class CharType, Traits::EnableIf<std::is_
integral<IntegralType>, std::is_
signed<IntegralType>>...>
531 IntegralType
bufferToNumber(
const CharType *
string, std::size_t size,
unsigned char base = 10)
536 const CharType *end =
string + size;
537 for (;
string != end && *
string ==
' '; ++string)
542 const bool negative = (*
string ==
'-');
546 IntegralType result = 0;
547 for (;
string != end; ++string) {
548 if (*
string ==
' ') {
552 result += charToDigit<CharType>(*string, base);
554 return negative ? -result : result;
568 char buffer[
sizeof(T)];
569 ConversionUtilities::BE::getBytes(integer, buffer);
570 return std::string(buffer + startOffset,
sizeof(T) - startOffset);
579 #endif // CONVERSION_UTILITIES_STRINGCONVERSION_H CPP_UTILITIES_EXPORT StringData convertUtf8ToUtf16LE(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified UTF-8 string to UTF-16 (little-endian).
bool startsWith(const StringType &str, const StringType &phrase)
Returns whether str starts with phrase.
void operator()(char *stringData)
Deletes the specified stringData with std::free(), because the memory has been allocated using std::m...
CPP_UTILITIES_EXPORT std::string encodeBase64(const byte *data, uint32 dataSize)
Encodes the specified data to Base64.
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...
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.
bool containsSubstrings(const StringType &str, std::initializer_list< StringType > substrings)
Returns whether str contains the specified substrings.
The ConversionException class is thrown by the various conversion functions of this library when a co...
std::vector< std::string > toArrayOfLines(const std::string &multilineString)
Converts the specified multilineString to an array of lines.
CPP_UTILITIES_EXPORT StringData convertUtf16BEToUtf8(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified UTF-16 (big-endian) string to UTF-8.
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...
StringType numberToString(IntegralType number, typename StringType::value_type base=10)
Converts the given number to its equivalent string representation using the specified base...
std::uint64_t uint64
unsigned 64-bit integer
The StringDataDeleter struct deletes the data of a StringData instance.
typename std::enable_if< All< Condition... >::value, Detail::Enabler >::type EnableIf
std::string 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 StringData convertUtf8ToUtf16BE(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified UTF-8 string to UTF-16 (big-endian).
EmptyPartsTreat
Specifies the role of empty parts when splitting strings.
CPP_UTILITIES_EXPORT StringData convertUtf8ToLatin1(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified UTF-8 string to Latin-1.
void findAndReplace(StringType &str, const StringType &find, const StringType &replace)
Replaces all occurences of find with relpace in the specified str.
Contains several functions providing conversions between different data types.
std::uint32_t uint32
unsigned 32-bit integer
std::pair< std::unique_ptr< char[], StringDataDeleter >, std::size_t > StringData
Type used to return string encoding conversion result.
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...
CPP_UTILITIES_EXPORT StringData convertUtf16LEToUtf8(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified UTF-16 (little-endian) string to UTF-8.
CharType charToDigit(CharType character, CharType base)
Returns number/digit of the specified character representation using the specified base...
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.
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.
std::uint8_t byte
unsigned byte
CPP_UTILITIES_EXPORT StringData convertLatin1ToUtf8(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified Latin-1 string to UTF-8.
std::vector< std::string > toMultiline(const Container &arrayOfLines)
Converts the specified arrayOfLines to a multiline 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.
CPP_UTILITIES_EXPORT std::string dataSizeToString(uint64 sizeInByte, bool includeByte=false)
Converts the specified data size in byte to its equivalent std::string representation.
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
CPP_UTILITIES_EXPORT std::pair< std::unique_ptr< byte[]>, uint32 > decodeBase64(const char *encodedStr, const uint32 strSize)
Decodes the specified Base64 encoded string.
constexpr CharType digitToChar(CharType digit)
Returns the character representation of the specified digit.
CPP_UTILITIES_EXPORT std::string bitrateToString(double speedInKbitsPerSecond, bool useByteInsteadOfBits=false)
Converts the specified bitrate in kbit/s to its equivalent std::string representation.