3#ifndef CPP_UTILITIES_NO_THREAD_LOCAL
4#include "../feature_detection/features.h"
7#ifndef CPP_UTILITIES_THREAD_LOCAL
8#define CPP_UTILITIES_THREAD_LOCAL
22#ifdef PLATFORM_WINDOWS
40 size_t operator()(
size_t value)
46 size_t operator()(
size_t value)
52 size_t operator()(
size_t value)
60 size_t operator()(
size_t value)
62 return static_cast<size_t>(
static_cast<float>(value) * factor);
67template <
class OutputSizeH
int>
class ConversionDescriptor {
69 ConversionDescriptor(
const char *fromCharset,
const char *toCharset)
70 : m_ptr(iconv_open(toCharset, fromCharset))
71 , m_outputSizeHint(OutputSizeHint())
73 if (m_ptr ==
reinterpret_cast<iconv_t
>(-1)) {
74 throw ConversionException(
"Unable to allocate descriptor for character set conversion.");
78 ConversionDescriptor(
const char *fromCharset,
const char *toCharset, OutputSizeHint outputSizeHint)
79 : m_ptr(iconv_open(toCharset, fromCharset))
80 , m_outputSizeHint(outputSizeHint)
82 if (m_ptr ==
reinterpret_cast<iconv_t
>(-1)) {
83 throw ConversionException(
"Unable to allocate descriptor for character set conversion.");
87 ~ConversionDescriptor()
93 StringData convertString(
const char *inputBuffer,
size_t inputBufferSize)
96 size_t inputBytesLeft = inputBufferSize;
97 size_t outputSize = m_outputSizeHint(inputBufferSize);
98 size_t outputBytesLeft = outputSize;
99 char *outputBuffer =
reinterpret_cast<char *
>(malloc(outputSize));
102 char *currentOutputOffset = outputBuffer;
103 for (;; currentOutputOffset = outputBuffer + bytesWritten) {
104 bytesWritten = iconv(m_ptr,
const_cast<char **
>(&inputBuffer), &inputBytesLeft, ¤tOutputOffset, &outputBytesLeft);
105 if (bytesWritten ==
static_cast<size_t>(-1)) {
106 if (errno == EINVAL) {
108 bytesWritten =
static_cast<size_t>(currentOutputOffset - outputBuffer);
110 }
else if (errno == E2BIG) {
112 bytesWritten =
static_cast<size_t>(currentOutputOffset - outputBuffer);
113 outputBytesLeft = (outputSize += m_outputSizeHint(inputBytesLeft)) - bytesWritten;
114 outputBuffer =
reinterpret_cast<char *
>(realloc(outputBuffer, outputSize));
118 throw ConversionException(
"Invalid multibyte sequence in the input.");
125 return StringData(std::unique_ptr<
char[], StringDataDeleter>(outputBuffer), currentOutputOffset - outputBuffer);
130 OutputSizeHint m_outputSizeHint;
145 const char *fromCharset,
const char *toCharset,
const char *inputBuffer, std::size_t inputBufferSize,
float outputBufferSizeFactor)
147 return ConversionDescriptor<Factor>(fromCharset, toCharset, outputBufferSizeFactor).convertString(inputBuffer, inputBufferSize);
156 return descriptor.convertString(inputBuffer, inputBufferSize);
165 return descriptor.convertString(inputBuffer, inputBufferSize);
174 return descriptor.convertString(inputBuffer, inputBufferSize);
183 return descriptor.convertString(inputBuffer, inputBufferSize);
192 return descriptor.convertString(inputBuffer, inputBufferSize);
201 return descriptor.convertString(inputBuffer, inputBufferSize);
204#ifdef PLATFORM_WINDOWS
211std::wstring convertMultiByteToWide(std::error_code &ec, std::string_view inputBuffer)
214 auto widePath = std::wstring();
215 auto bufferSize =
static_cast<int>(std::clamp<std::size_t>(inputBuffer.size(), 0, std::numeric_limits<int>::max()));
216 auto size = MultiByteToWideChar(CP_UTF8, 0, inputBuffer.data(), bufferSize,
nullptr, 0);
218 ec = std::error_code(
static_cast<int>(GetLastError()), std::system_category());
222 widePath.resize(
static_cast<std::wstring::size_type
>(size));
223 size = MultiByteToWideChar(CP_UTF8, 0, inputBuffer.data(), bufferSize, widePath.data(), size);
225 ec = std::error_code(
static_cast<int>(GetLastError()), std::system_category());
237WideStringData convertMultiByteToWide(std::error_code &ec,
const char *inputBuffer,
int inputBufferSize)
240 WideStringData widePath;
241 widePath.second = MultiByteToWideChar(CP_UTF8, 0, inputBuffer, inputBufferSize,
nullptr, 0);
242 if (widePath.second <= 0) {
243 ec = std::error_code(
static_cast<int>(GetLastError()), std::system_category());
247 widePath.first = make_unique<wchar_t[]>(
static_cast<size_t>(widePath.second));
248 widePath.second = MultiByteToWideChar(CP_UTF8, 0, inputBuffer, inputBufferSize, widePath.first.get(), widePath.second);
249 if (widePath.second <= 0) {
250 ec = std::error_code(
static_cast<int>(GetLastError()), std::system_category());
251 widePath.first.reset();
260WideStringData convertMultiByteToWide(std::error_code &ec,
const std::string &inputBuffer)
262 return convertMultiByteToWide(ec, inputBuffer.data(),
263 inputBuffer.size() <
static_cast<std::size_t
>(std::numeric_limits<int>::max() - 1) ?
static_cast<int>(inputBuffer.size() + 1) : -1);
272WideStringData convertMultiByteToWide(
const char *inputBuffer,
int inputBufferSize)
275 return convertMultiByteToWide(ec, inputBuffer, inputBufferSize);
282WideStringData convertMultiByteToWide(
const std::string &inputBuffer)
285 return convertMultiByteToWide(ec, inputBuffer);
295 string::size_type firstNullByte = str.find(terminationChar);
296 if (firstNullByte != string::npos) {
297 str.resize(firstNullByte);
308 stringstream res(stringstream::in | stringstream::out);
309 res.setf(ios::fixed, ios::floatfield);
310 res << setprecision(2);
311 if (sizeInByte < 1024LL) {
312 res << sizeInByte <<
" bytes";
313 }
else if (sizeInByte < 1048576LL) {
314 res << (static_cast<double>(sizeInByte) / 1024.0) <<
" KiB";
315 }
else if (sizeInByte < 1073741824LL) {
316 res << (static_cast<double>(sizeInByte) / 1048576.0) <<
" MiB";
317 }
else if (sizeInByte < 1099511627776LL) {
318 res << (static_cast<double>(sizeInByte) / 1073741824.0) <<
" GiB";
320 res << (static_cast<double>(sizeInByte) / 1099511627776.0) <<
" TiB";
322 if (includeByte && sizeInByte > 1024LL) {
323 res <<
' ' <<
'(' << sizeInByte <<
" byte)";
340 stringstream res(stringstream::in | stringstream::out);
341 res << setprecision(3);
342 if (std::isnan(bitrateInKbitsPerSecond)) {
343 res <<
"indeterminable";
344 }
else if (useIecBinaryPrefixes) {
345 if (bitrateInKbitsPerSecond < 8.0) {
346 res << (bitrateInKbitsPerSecond * 125.0) <<
" byte/s";
347 }
else if (bitrateInKbitsPerSecond < 8000.0) {
348 res << (bitrateInKbitsPerSecond * 0.125) <<
" KiB/s";
349 }
else if (bitrateInKbitsPerSecond < 8000000.0) {
350 res << (bitrateInKbitsPerSecond * 0.000125) <<
" MiB/s";
352 res << (bitrateInKbitsPerSecond * 0.000000125) <<
" GiB/s";
355 if (bitrateInKbitsPerSecond < 1.0) {
356 res << (bitrateInKbitsPerSecond * 1000.0) <<
" bit/s";
357 }
else if (bitrateInKbitsPerSecond < 1000.0) {
358 res << (bitrateInKbitsPerSecond) <<
" kbit/s";
359 }
else if (bitrateInKbitsPerSecond < 1000000.0) {
360 res << (bitrateInKbitsPerSecond * 0.001) <<
" Mbit/s";
362 res << (bitrateInKbitsPerSecond * 0.000001) <<
" Gbit/s";
369const char *
const base64Chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
370const char base64Pad =
'=';
379 auto encoded = std::string();
380 auto mod =
static_cast<std::uint8_t
>(dataSize % 3);
381 auto temp = std::uint32_t();
382 encoded.reserve(((dataSize / 3) + (mod > 0)) * 4);
383 for (
const std::uint8_t *end = --data + dataSize - mod; data != end;) {
384 temp =
static_cast<std::uint32_t
>(*++data << 16);
385 temp |=
static_cast<std::uint32_t
>(*++data << 8);
387 encoded.push_back(base64Chars[(temp & 0x00FC0000) >> 18]);
388 encoded.push_back(base64Chars[(temp & 0x0003F000) >> 12]);
389 encoded.push_back(base64Chars[(temp & 0x00000FC0) >> 6]);
390 encoded.push_back(base64Chars[(temp & 0x0000003F)]);
394 temp =
static_cast<std::uint32_t
>(*++data << 16);
395 encoded.push_back(base64Chars[(temp & 0x00FC0000) >> 18]);
396 encoded.push_back(base64Chars[(temp & 0x0003F000) >> 12]);
397 encoded.push_back(base64Pad);
398 encoded.push_back(base64Pad);
401 temp =
static_cast<std::uint32_t
>(*++data << 16);
402 temp |=
static_cast<std::uint32_t
>(*++data << 8);
403 encoded.push_back(base64Chars[(temp & 0x00FC0000) >> 18]);
404 encoded.push_back(base64Chars[(temp & 0x0003F000) >> 12]);
405 encoded.push_back(base64Chars[(temp & 0x00000FC0) >> 6]);
406 encoded.push_back(base64Pad);
417std::pair<unique_ptr<std::uint8_t[]>, std::uint32_t>
decodeBase64(
const char *encodedStr,
const std::uint32_t strSize)
420 return std::make_pair(std::make_unique<std::uint8_t[]>(0), 0);
425 std::uint32_t decodedSize = (strSize / 4) * 3;
426 const char *
const end = encodedStr + strSize;
427 if (*(end - 1) == base64Pad) {
430 if (*(end - 2) == base64Pad) {
433 auto buffer = std::make_unique<std::uint8_t[]>(decodedSize);
434 auto *iter = buffer.get() - 1;
435 while (encodedStr < end) {
436 std::int32_t temp = 0;
437 for (std::uint8_t quantumPos = 0; quantumPos < 4; ++quantumPos, ++encodedStr) {
439 if (*encodedStr >=
'A' && *encodedStr <=
'Z') {
440 temp |= *encodedStr -
'A';
441 }
else if (*encodedStr >=
'a' && *encodedStr <=
'z') {
442 temp |= *encodedStr -
'a' + 26;
443 }
else if (*encodedStr >=
'0' && *encodedStr <=
'9') {
444 temp |= *encodedStr -
'0' + 2 * 26;
445 }
else if (*encodedStr ==
'+') {
447 }
else if (*encodedStr ==
'/') {
448 temp |= 2 * 26 + 10 + 1;
449 }
else if (*encodedStr == base64Pad) {
450 switch (end - encodedStr) {
452 *++iter =
static_cast<std::uint8_t
>((temp >> 16) & 0xFF);
453 *++iter =
static_cast<std::uint8_t
>((temp >> 8) & 0xFF);
454 return std::make_pair(std::move(buffer), decodedSize);
456 *++iter =
static_cast<std::uint8_t
>((temp >> 10) & 0xFF);
457 return std::make_pair(std::move(buffer), decodedSize);
465 *++iter =
static_cast<std::uint8_t
>((temp >> 16) & 0xFF);
466 *++iter =
static_cast<std::uint8_t
>((temp >> 8) & 0xFF);
467 *++iter =
static_cast<std::uint8_t
>(temp & 0xFF);
469 return std::make_pair(std::move(buffer), decodedSize);
The ConversionException class is thrown by the various conversion functions of this library when a co...
Contains all utilities provides by the c++utilities library.
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.
std::pair< std::unique_ptr< char[], StringDataDeleter >, std::size_t > StringData
Type used to return string encoding conversion result.
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.
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.
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...
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 StringData convertUtf8ToLatin1(const char *inputBuffer, std::size_t inputBufferSize)
Converts the specified UTF-8 string to Latin-1.
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.
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.
#define CPP_UTILITIES_THREAD_LOCAL