| 1 | #cxx . . $ `<file>/conversion/stringconversion.cpp`/include "./stringconversion.h"cxx . . $ `<file>/conversion/stringconversion.h`/ |
| 2 | |
| 3 | #ifndef CPP_UTILITIES_NO_THREAD_LOCAL |
| 4 | #include "../feature_detection/features.h"cxx . . $ `<file>/feature_detection/features.h`/ |
| 5 | #endif |
| 6 | |
| 7 | #ifndef CPP_UTILITIES_THREAD_LOCALcxx . . $ `feature_detection/features.h:232:13`! |
| 8 | #define CPP_UTILITIES_THREAD_LOCAL |
| 9 | #endif |
| 10 | |
| 11 | #include <cmath> |
| 12 | #include <cstdlib> |
| 13 | #include <iomanip> |
| 14 | #include <memory> |
| 15 | #include <sstream> |
| 16 | |
| 17 | #ifdef PLATFORM_WINDOWS |
| 18 | #include <algorithm> // for std::clamp |
| 19 | #include <limits> |
| 20 | #endif |
| 21 | |
| 22 | #include <errno.h> |
| 23 | |
| 24 | #ifndef CPP_UTILITIES_NO_ICONV |
| 25 | #include <iconv.h> |
| 26 | #endif |
| 27 | |
| 28 | #ifdef PLATFORM_WINDOWS |
| 29 | #include <windows.h> |
| 30 | // note: The windows header seriously defines a macro called "max" breaking the (common) use |
| 31 | // of std::numeric_limits in the subsequent code. So we need to undefine this macro. Note that |
| 32 | // this is not the case using mingw-w64 but it is happening with windows.h from Windows Kits |
| 33 | // version 10.0.22000.0 via Visual Studio 2022. |
| 34 | #ifdef max |
| 35 | #undef max |
| 36 | #endif |
| 37 | #endif |
| 38 | |
| 39 | using namespace std; |
| 40 | |
| 41 | namespace CppUtilitiescxx . . $ CppUtilities/ { |
| 42 | |
| 43 | #ifndef CPP_UTILITIES_NO_ICONV |
| 44 | |
| 45 | /// \cond |
| 46 | |
| 47 | struct Keepcxx . . $ CppUtilities/Keep# { |
| 48 | size_t operatorcxx . . $ CppUtilities/Keep#`operator()`(ecdaa1632f49f591).()(size_t value) |
| 49 | { |
| 50 | return value; |
| 51 | } |
| 52 | }; |
| 53 | struct Doublecxx . . $ CppUtilities/Double# { |
| 54 | size_t operatorcxx . . $ CppUtilities/Double#`operator()`(ecdaa1632f49f591).()(size_t value) |
| 55 | { |
| 56 | return value + value; |
| 57 | } |
| 58 | }; |
| 59 | struct Halfcxx . . $ CppUtilities/Half# { |
| 60 | size_t operatorcxx . . $ CppUtilities/Half#`operator()`(ecdaa1632f49f591).()(size_t value) |
| 61 | { |
| 62 | return value / 2; |
| 63 | } |
| 64 | }; |
| 65 | struct Factorcxx . . $ CppUtilities/Factor# { |
| 66 | Factorcxx . . $ CppUtilities/Factor#Factor(9364e6e7bff7660).(float factor) |
| 67 | : factorcxx . . $ CppUtilities/Factor#factor.(factor) {}; |
| 68 | size_t operatorcxx . . $ CppUtilities/Factor#`operator()`(ecdaa1632f49f591).()(size_t value) |
| 69 | { |
| 70 | return static_cast<size_t>(static_cast<float>(value) * factorcxx . . $ CppUtilities/Factor#factor.); |
| 71 | } |
| 72 | float factorcxx . . $ CppUtilities/Factor#factor.; |
| 73 | }; |
| 74 | |
| 75 | template <class OutputSizeHint> class ConversionDescriptorcxx . . $ CppUtilities/ConversionDescriptor# { |
| 76 | public: |
| 77 | ConversionDescriptorcxx . . $ CppUtilities/ConversionDescriptor#`ConversionDescriptor<OutputSizeHint>`(2fa786934fe80e24).(const char *fromCharset, const char *toCharset) |
| 78 | : m_ptrcxx . . $ CppUtilities/ConversionDescriptor#m_ptr.(iconv_open(toCharset, fromCharset)) |
| 79 | , m_outputSizeHintcxx . . $ CppUtilities/ConversionDescriptor#m_outputSizeHint.(OutputSizeHint()) |
| 80 | { |
| 81 | if (m_ptrcxx . . $ CppUtilities/ConversionDescriptor#m_ptr. == reinterpret_cast<iconv_t>(-1)) { |
| 82 | throw ConversionExceptioncxx . . $ CppUtilities/ConversionException#("Unable to allocate descriptor for character set conversion."); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | ConversionDescriptorcxx . . $ CppUtilities/ConversionDescriptor#`ConversionDescriptor<OutputSizeHint>`(8d990f455eb4a69b).(const char *fromCharset, const char *toCharset, OutputSizeHint outputSizeHint) |
| 87 | : m_ptrcxx . . $ CppUtilities/ConversionDescriptor#m_ptr.(iconv_open(toCharset, fromCharset)) |
| 88 | , m_outputSizeHintcxx . . $ CppUtilities/ConversionDescriptor#m_outputSizeHint.(outputSizeHint) |
| 89 | { |
| 90 | if (m_ptrcxx . . $ CppUtilities/ConversionDescriptor#m_ptr. == reinterpret_cast<iconv_t>(-1)) { |
| 91 | throw ConversionExceptioncxx . . $ CppUtilities/ConversionException#("Unable to allocate descriptor for character set conversion."); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | ~cxx . . $ CppUtilities/ConversionDescriptor#`~ConversionDescriptor<OutputSizeHint>`(49f6e7a06ebc5aa8).ConversionDescriptor() |
| 96 | { |
| 97 | iconv_close(m_ptrcxx . . $ CppUtilities/ConversionDescriptor#m_ptr.); |
| 98 | } |
| 99 | |
| 100 | public: |
| 101 | StringDatacxx . . $ CppUtilities/StringData# convertStringcxx . . $ CppUtilities/ConversionDescriptor#convertString(c808ac3c7ba7226b).(const char *inputBuffer, size_t inputBufferSize) |
| 102 | { |
| 103 | // setup input and output buffer |
| 104 | size_t inputBytesLeft = inputBufferSize; |
| 105 | size_t outputSize = m_outputSizeHintcxx . . $ CppUtilities/ConversionDescriptor#m_outputSizeHint.(inputBufferSize); |
| 106 | size_t outputBytesLeft = outputSize; |
| 107 | char *outputBuffer = reinterpret_cast<char *>(malloc(outputSize)); |
| 108 | size_t bytesWritten; |
| 109 | |
| 110 | char *currentOutputOffset = outputBuffer; |
| 111 | for (;; currentOutputOffset = outputBuffer + bytesWritten) { |
| 112 | bytesWritten = iconv(m_ptrcxx . . $ CppUtilities/ConversionDescriptor#m_ptr., const_cast<char **>(&inputBuffer), &inputBytesLeft, ¤tOutputOffset, &outputBytesLeft); |
| 113 | if (bytesWritten == static_cast<size_t>(-1)) { |
| 114 | if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!) { |
| 115 | // ignore incomplete multibyte sequence in the input |
| 116 | bytesWritten = static_cast<size_t>(currentOutputOffset - outputBuffer); |
| 117 | break; |
| 118 | } else if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == E2BIGcxx . . $ `<external>/8987895939074593727/errno-base.h:11:9`!) { |
| 119 | // output buffer has no more room for next converted character |
| 120 | bytesWritten = static_cast<size_t>(currentOutputOffset - outputBuffer); |
| 121 | outputBytesLeft = (outputSize += m_outputSizeHintcxx . . $ CppUtilities/ConversionDescriptor#m_outputSizeHint.(inputBytesLeft)) -cxx . . $ std/operator-(a6e85813984128c7). bytesWritten; |
| 122 | outputBuffer = reinterpret_cast<char *>(realloc(outputBuffer, outputSize)); |
| 123 | } else /*if(errno == EILSEQ)*/ { |
| 124 | // invalid multibyte sequence in the input |
| 125 | free(outputBuffer); |
| 126 | throw ConversionExceptioncxx . . $ CppUtilities/ConversionException#("Invalid multibyte sequence in the input."); |
| 127 | } |
| 128 | } else { |
| 129 | // conversion completed without (further) errors |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | return StringDatacxx . . $ std/pair#pair(14d64bafa7379ebc).(stdcxx . . $ std/unique_ptr#unique_ptr(2bd1d077c542d025).::unique_ptrcxx . . $ std/unique_ptr#<char[], StringDataDeletercxx . . $ CppUtilities/StringDataDeleter#>(outputBuffer), currentOutputOffset - outputBuffer); |
| 134 | } |
| 135 | |
| 136 | private: |
| 137 | iconv_t m_ptrcxx . . $ CppUtilities/ConversionDescriptor#m_ptr.; |
| 138 | OutputSizeHint m_outputSizeHintcxx . . $ CppUtilities/ConversionDescriptor#m_outputSizeHint.; |
| 139 | }; |
| 140 | |
| 141 | /// \endcond |
| 142 | |
| 143 | /*! |
| 144 | * \brief Converts the specified string from one character set to another. |
| 145 | * \remarks |
| 146 | * - The term "size" refers here always to the actual number of bytes and not to the number of characters |
| 147 | * (eg. the size of the UTF-8 string "ö" is 2 and not 1). |
| 148 | * - The expected size of the output buffer can be specified via \a outputBufferSizeFactor. This hint helps |
| 149 | * to reduce buffer reallocations during the conversion (eg. for the conversion from Latin-1 to UTF-16 |
| 150 | * the factor would be 2, for the conversion from UTF-16 to Latin-1 the factor would be 0.5). |
| 151 | */ |
| 152 | StringDatacxx . . $ CppUtilities/StringData# convertStringcxx . . $ CppUtilities/convertString(6db75fa61320dee9).( |
| 153 | const char *fromCharset, const char *toCharset, const char *inputBuffer, std::size_tcxx . . $ std/size_t# inputBufferSize, float outputBufferSizeFactor) |
| 154 | { |
| 155 | return ConversionDescriptorcxx . . $ CppUtilities/ConversionDescriptor#ConversionDescriptor(8d990f455eb4a69b).<Factorcxx . . $ CppUtilities/Factor#>(fromCharset, toCharset, outputBufferSizeFactorcxx . . $ CppUtilities/Factor#Factor(9364e6e7bff7660).).convertStringcxx . . $ CppUtilities/ConversionDescriptor#convertString(c808ac3c7ba7226b).(inputBuffer, inputBufferSize); |
| 156 | } |
| 157 | |
| 158 | /*! |
| 159 | * \brief Converts the specified UTF-8 string to UTF-16 (little-endian). |
| 160 | */ |
| 161 | StringDatacxx . . $ CppUtilities/StringData# convertUtf8ToUtf16LEcxx . . $ CppUtilities/convertUtf8ToUtf16LE(c808ac3c7ba7226b).(const char *inputBuffer, std::size_tcxx . . $ std/size_t# inputBufferSize) |
| 162 | { |
| 163 | CPP_UTILITIES_THREAD_LOCALcxx . . $ `feature_detection/features.h:232:13`! ConversionDescriptorcxx . . $ CppUtilities/ConversionDescriptor#<Doublecxx . . $ CppUtilities/Double#> descriptorcxx . . $ CppUtilities/ConversionDescriptor#ConversionDescriptor(2fa786934fe80e24).("UTF-8", "UTF-16LE"); |
| 164 | return descriptor.convertStringcxx . . $ CppUtilities/ConversionDescriptor#convertString(c808ac3c7ba7226b).(inputBuffer, inputBufferSize); |
| 165 | } |
| 166 | |
| 167 | /*! |
| 168 | * \brief Converts the specified UTF-16 (little-endian) string to UTF-8. |
| 169 | */ |
| 170 | StringDatacxx . . $ CppUtilities/StringData# convertUtf16LEToUtf8cxx . . $ CppUtilities/convertUtf16LEToUtf8(c808ac3c7ba7226b).(const char *inputBuffer, std::size_tcxx . . $ std/size_t# inputBufferSize) |
| 171 | { |
| 172 | CPP_UTILITIES_THREAD_LOCALcxx . . $ `feature_detection/features.h:232:13`! ConversionDescriptorcxx . . $ CppUtilities/ConversionDescriptor#<Halfcxx . . $ CppUtilities/Half#> descriptorcxx . . $ CppUtilities/ConversionDescriptor#ConversionDescriptor(2fa786934fe80e24).("UTF-16LE", "UTF-8"); |
| 173 | return descriptor.convertStringcxx . . $ CppUtilities/ConversionDescriptor#convertString(c808ac3c7ba7226b).(inputBuffer, inputBufferSize); |
| 174 | } |
| 175 | |
| 176 | /*! |
| 177 | * \brief Converts the specified UTF-8 string to UTF-16 (big-endian). |
| 178 | */ |
| 179 | StringDatacxx . . $ CppUtilities/StringData# convertUtf8ToUtf16BEcxx . . $ CppUtilities/convertUtf8ToUtf16BE(c808ac3c7ba7226b).(const char *inputBuffer, std::size_tcxx . . $ std/size_t# inputBufferSize) |
| 180 | { |
| 181 | CPP_UTILITIES_THREAD_LOCALcxx . . $ `feature_detection/features.h:232:13`! ConversionDescriptorcxx . . $ CppUtilities/ConversionDescriptor#<Doublecxx . . $ CppUtilities/Double#> descriptorcxx . . $ CppUtilities/ConversionDescriptor#ConversionDescriptor(2fa786934fe80e24).("UTF-8", "UTF-16BE"); |
| 182 | return descriptor.convertStringcxx . . $ CppUtilities/ConversionDescriptor#convertString(c808ac3c7ba7226b).(inputBuffer, inputBufferSize); |
| 183 | } |
| 184 | |
| 185 | /*! |
| 186 | * \brief Converts the specified UTF-16 (big-endian) string to UTF-8. |
| 187 | */ |
| 188 | StringDatacxx . . $ CppUtilities/StringData# convertUtf16BEToUtf8cxx . . $ CppUtilities/convertUtf16BEToUtf8(c808ac3c7ba7226b).(const char *inputBuffer, std::size_tcxx . . $ std/size_t# inputBufferSize) |
| 189 | { |
| 190 | CPP_UTILITIES_THREAD_LOCALcxx . . $ `feature_detection/features.h:232:13`! ConversionDescriptorcxx . . $ CppUtilities/ConversionDescriptor#<Halfcxx . . $ CppUtilities/Half#> descriptorcxx . . $ CppUtilities/ConversionDescriptor#ConversionDescriptor(2fa786934fe80e24).("UTF-16BE", "UTF-8"); |
| 191 | return descriptor.convertStringcxx . . $ CppUtilities/ConversionDescriptor#convertString(c808ac3c7ba7226b).(inputBuffer, inputBufferSize); |
| 192 | } |
| 193 | |
| 194 | /*! |
| 195 | * \brief Converts the specified Latin-1 string to UTF-8. |
| 196 | */ |
| 197 | StringDatacxx . . $ CppUtilities/StringData# convertLatin1ToUtf8cxx . . $ CppUtilities/convertLatin1ToUtf8(c808ac3c7ba7226b).(const char *inputBuffer, std::size_tcxx . . $ std/size_t# inputBufferSize) |
| 198 | { |
| 199 | CPP_UTILITIES_THREAD_LOCALcxx . . $ `feature_detection/features.h:232:13`! ConversionDescriptorcxx . . $ CppUtilities/ConversionDescriptor#<Keepcxx . . $ CppUtilities/Keep#> descriptorcxx . . $ CppUtilities/ConversionDescriptor#ConversionDescriptor(2fa786934fe80e24).("ISO-8859-1", "UTF-8"); |
| 200 | return descriptor.convertStringcxx . . $ CppUtilities/ConversionDescriptor#convertString(c808ac3c7ba7226b).(inputBuffer, inputBufferSize); |
| 201 | } |
| 202 | |
| 203 | /*! |
| 204 | * \brief Converts the specified UTF-8 string to Latin-1. |
| 205 | */ |
| 206 | StringDatacxx . . $ CppUtilities/StringData# convertUtf8ToLatin1cxx . . $ CppUtilities/convertUtf8ToLatin1(c808ac3c7ba7226b).(const char *inputBuffer, std::size_tcxx . . $ std/size_t# inputBufferSize) |
| 207 | { |
| 208 | CPP_UTILITIES_THREAD_LOCALcxx . . $ `feature_detection/features.h:232:13`! ConversionDescriptorcxx . . $ CppUtilities/ConversionDescriptor#<Keepcxx . . $ CppUtilities/Keep#> descriptorcxx . . $ CppUtilities/ConversionDescriptor#ConversionDescriptor(2fa786934fe80e24).("UTF-8", "ISO-8859-1"); |
| 209 | return descriptor.convertStringcxx . . $ CppUtilities/ConversionDescriptor#convertString(c808ac3c7ba7226b).(inputBuffer, inputBufferSize); |
| 210 | } |
| 211 | |
| 212 | #endif |
| 213 | |
| 214 | #ifdef PLATFORM_WINDOWS |
| 215 | /*! |
| 216 | * \brief Converts the specified multi-byte string (assumed to be UTF-8) to a wide string using the WinAPI. |
| 217 | * \remarks |
| 218 | * - Only available under Windows. |
| 219 | * - If \a inputBuffer exceeds std::numeric_limits<int>::max() it will be truncated. |
| 220 | */ |
| 221 | std::wstring convertMultiByteToWide(std::error_code &ec, std::string_view inputBuffer) |
| 222 | { |
| 223 | // calculate required size |
| 224 | auto widePath = std::wstring(); |
| 225 | auto bufferSize = static_cast<int>(std::clamp<std::size_t>(inputBuffer.size(), 0, std::numeric_limits<int>::max())); |
| 226 | auto size = MultiByteToWideChar(CP_UTF8, 0, inputBuffer.data(), bufferSize, nullptr, 0); |
| 227 | if (size <= 0) { |
| 228 | ec = std::error_code(static_cast<int>(GetLastError()), std::system_category()); |
| 229 | return widePath; |
| 230 | } |
| 231 | // do the actual conversion |
| 232 | widePath.resize(static_cast<std::wstring::size_type>(size)); |
| 233 | size = MultiByteToWideChar(CP_UTF8, 0, inputBuffer.data(), bufferSize, widePath.data(), size); |
| 234 | if (size <= 0) { |
| 235 | ec = std::error_code(static_cast<int>(GetLastError()), std::system_category()); |
| 236 | widePath.clear(); |
| 237 | } |
| 238 | return widePath; |
| 239 | } |
| 240 | |
| 241 | /*! |
| 242 | * \brief Converts the specified multi-byte string (assumed to be UTF-8) to a wide string using the WinAPI. |
| 243 | * \remarks |
| 244 | * - Only available under Windows. |
| 245 | * - If \a inputBufferSize is -1, \a inputBuffer is considered null-terminated. |
| 246 | */ |
| 247 | WideStringData convertMultiByteToWide(std::error_code &ec, const char *inputBuffer, int inputBufferSize) |
| 248 | { |
| 249 | // calculate required size |
| 250 | WideStringData widePath; |
| 251 | widePath.second = MultiByteToWideChar(CP_UTF8, 0, inputBuffer, inputBufferSize, nullptr, 0); |
| 252 | if (widePath.second <= 0) { |
| 253 | ec = std::error_code(static_cast<int>(GetLastError()), std::system_category()); |
| 254 | return widePath; |
| 255 | } |
| 256 | // do the actual conversion |
| 257 | widePath.first = make_unique<wchar_t[]>(static_cast<size_t>(widePath.second)); |
| 258 | widePath.second = MultiByteToWideChar(CP_UTF8, 0, inputBuffer, inputBufferSize, widePath.first.get(), widePath.second); |
| 259 | if (widePath.second <= 0) { |
| 260 | ec = std::error_code(static_cast<int>(GetLastError()), std::system_category()); |
| 261 | widePath.first.reset(); |
| 262 | } |
| 263 | return widePath; |
| 264 | } |
| 265 | |
| 266 | /*! |
| 267 | * \brief Converts the specified multi-byte string (assumed to be UTF-8) to a wide string using the WinAPI. |
| 268 | * \remarks Only available under Windows. |
| 269 | */ |
| 270 | WideStringData convertMultiByteToWide(std::error_code &ec, const std::string &inputBuffer) |
| 271 | { |
| 272 | return convertMultiByteToWide(ec, inputBuffer.data(), |
| 273 | inputBuffer.size() < static_cast<std::size_t>(std::numeric_limits<int>::max() - 1) ? static_cast<int>(inputBuffer.size() + 1) : -1); |
| 274 | } |
| 275 | |
| 276 | /*! |
| 277 | * \brief Converts the specified multi-byte string (assumed to be UTF-8) to a wide string using the WinAPI. |
| 278 | * \remarks |
| 279 | * - Only available under Windows. |
| 280 | * - If \a inputBufferSize is -1, \a inputBuffer is considered null-terminated. |
| 281 | */ |
| 282 | WideStringData convertMultiByteToWide(const char *inputBuffer, int inputBufferSize) |
| 283 | { |
| 284 | std::error_code ec; |
| 285 | return convertMultiByteToWide(ec, inputBuffer, inputBufferSize); |
| 286 | } |
| 287 | |
| 288 | /*! |
| 289 | * \brief Converts the specified multi-byte string (assumed to be UTF-8) to a wide string using the WinAPI. |
| 290 | * \remarks Only available under Windows. |
| 291 | */ |
| 292 | WideStringData convertMultiByteToWide(const std::string &inputBuffer) |
| 293 | { |
| 294 | std::error_code ec; |
| 295 | return convertMultiByteToWide(ec, inputBuffer); |
| 296 | } |
| 297 | #endif |
| 298 | |
| 299 | /*! |
| 300 | * \brief Truncates all characters after the first occurrence of the |
| 301 | * specified \a terminationChar and the termination character as well. |
| 302 | */ |
| 303 | void truncateStringcxx . . $ CppUtilities/truncateString(21193be99a1fe6f1).(stringcxx . . $ std/string# &str, char terminationChar) |
| 304 | { |
| 305 | stringcxx . . $ std/string#::size_typecxx . . $ std/__cxx11/basic_string#size_type# firstNullByte = str.findcxx . . $ std/__cxx11/basic_string#find(2dda2fc6b78abe68).(terminationChar); |
| 306 | if (firstNullByte != stringcxx . . $ std/string#::nposcxx . . $ std/__cxx11/basic_string#npos.) { |
| 307 | str.resizecxx . . $ std/__cxx11/basic_string#resize(9dac5f4bebd06212).(firstNullByte); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | /*! |
| 312 | * \brief Converts the specified data size in byte to its equivalent std::string representation. |
| 313 | * |
| 314 | * The unit with appropriate binary prefix will be appended. |
| 315 | */ |
| 316 | stringcxx . . $ std/string# dataSizeToStringcxx . . $ CppUtilities/dataSizeToString(6294d3b82760181d).(std::uint64_tcxx . . $ std/uint64_t# sizeInByte, bool includeByte) |
| 317 | { |
| 318 | stringstreamcxx . . $ std/stringstream# rescxx . . $ std/__cxx11/basic_stringstream#basic_stringstream(322f8929844464d3).(stringstreamcxx . . $ std/stringstream#::incxx . . $ std/ios_base#in. |cxx . . $ std/`operator|`(2dd9eb40323da8bf). stringstreamcxx . . $ std/stringstream#::outcxx . . $ std/ios_base#out.); |
| 319 | res.setfcxx . . $ std/ios_base#setf(9272523886439dff).(ioscxx . . $ std/ios#::fixedcxx . . $ std/ios_base#fixed., ioscxx . . $ std/ios#::floatfieldcxx . . $ std/ios_base#floatfield.); |
| 320 | res <<cxx . . $ std/`operator<<`(f54f2ca72cfad5a6). setprecisioncxx . . $ std/setprecision(94726062283fda89).(2); |
| 321 | if (sizeInByte < 1024LL) { |
| 322 | res <<cxx . . $ std/basic_ostream#`operator<<`(b09cd8ec4365c7ed). sizeInByte <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " bytes"; |
| 323 | } else if (sizeInByte < 1048576LL) { |
| 324 | res <<cxx . . $ std/basic_ostream#`operator<<`(968942d578224538). (static_cast<double>(sizeInByte) / 1024.0) <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " KiB"; |
| 325 | } else if (sizeInByte < 1073741824LL) { |
| 326 | res <<cxx . . $ std/basic_ostream#`operator<<`(968942d578224538). (static_cast<double>(sizeInByte) / 1048576.0) <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " MiB"; |
| 327 | } else if (sizeInByte < 1099511627776LL) { |
| 328 | res <<cxx . . $ std/basic_ostream#`operator<<`(968942d578224538). (static_cast<double>(sizeInByte) / 1073741824.0) <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " GiB"; |
| 329 | } else { |
| 330 | res <<cxx . . $ std/basic_ostream#`operator<<`(968942d578224538). (static_cast<double>(sizeInByte) / 1099511627776.0) <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " TiB"; |
| 331 | } |
| 332 | if (includeByte && sizeInByte > 1024LL) { |
| 333 | res <<cxx . . $ std/`operator<<`(dd93bb5737bfac24). ' ' <<cxx . . $ std/`operator<<`(dd93bb5737bfac24). '(' <<cxx . . $ std/basic_ostream#`operator<<`(b09cd8ec4365c7ed). sizeInByte <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " byte)"; |
| 334 | } |
| 335 | return res.strcxx . . $ std/__cxx11/basic_stringstream#str(d33e1a6fd36255f7).(); |
| 336 | } |
| 337 | |
| 338 | /*! |
| 339 | * \brief Converts the specified bitrate in kbit/s to its equivalent std::string representation. |
| 340 | * |
| 341 | * The unit with appropriate binary prefix will be appended. |
| 342 | * |
| 343 | * \param bitrateInKbitsPerSecond Specifies the bitrate in kbit/s. |
| 344 | * \param useIecBinaryPrefixes Indicates whether IEC binary prefixes should be used (eg. KiB/s). |
| 345 | * |
| 346 | * \sa <a href="http://en.wikipedia.org/wiki/Binary_prefix">Binary prefix - Wikipedia</a> |
| 347 | */ |
| 348 | stringcxx . . $ std/string# bitrateToStringcxx . . $ CppUtilities/bitrateToString(47f9b21bd6629f82).(double bitrateInKbitsPerSecond, bool useIecBinaryPrefixes) |
| 349 | { |
| 350 | stringstreamcxx . . $ std/stringstream# rescxx . . $ std/__cxx11/basic_stringstream#basic_stringstream(322f8929844464d3).(stringstreamcxx . . $ std/stringstream#::incxx . . $ std/ios_base#in. |cxx . . $ std/`operator|`(2dd9eb40323da8bf). stringstreamcxx . . $ std/stringstream#::outcxx . . $ std/ios_base#out.); |
| 351 | res <<cxx . . $ std/`operator<<`(f54f2ca72cfad5a6). setprecisioncxx . . $ std/setprecision(94726062283fda89).(3); |
| 352 | if (std::isnan(bitrateInKbitsPerSecond)) { |
| 353 | res <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "indeterminable"; |
| 354 | } else if (useIecBinaryPrefixes) { |
| 355 | if (bitrateInKbitsPerSecond < 8.0) { |
| 356 | res <<cxx . . $ std/basic_ostream#`operator<<`(968942d578224538). (bitrateInKbitsPerSecond * 125.0) <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " byte/s"; |
| 357 | } else if (bitrateInKbitsPerSecond < 8000.0) { |
| 358 | res <<cxx . . $ std/basic_ostream#`operator<<`(968942d578224538). (bitrateInKbitsPerSecond * 0.125) <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " KiB/s"; |
| 359 | } else if (bitrateInKbitsPerSecond < 8000000.0) { |
| 360 | res <<cxx . . $ std/basic_ostream#`operator<<`(968942d578224538). (bitrateInKbitsPerSecond * 0.000125) <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " MiB/s"; |
| 361 | } else { |
| 362 | res <<cxx . . $ std/basic_ostream#`operator<<`(968942d578224538). (bitrateInKbitsPerSecond * 0.000000125) <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " GiB/s"; |
| 363 | } |
| 364 | } else { |
| 365 | if (bitrateInKbitsPerSecond < 1.0) { |
| 366 | res <<cxx . . $ std/basic_ostream#`operator<<`(968942d578224538). (bitrateInKbitsPerSecond * 1000.0) <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " bit/s"; |
| 367 | } else if (bitrateInKbitsPerSecond < 1000.0) { |
| 368 | res <<cxx . . $ std/basic_ostream#`operator<<`(968942d578224538). (bitrateInKbitsPerSecond) <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " kbit/s"; |
| 369 | } else if (bitrateInKbitsPerSecond < 1000000.0) { |
| 370 | res <<cxx . . $ std/basic_ostream#`operator<<`(968942d578224538). (bitrateInKbitsPerSecond * 0.001) <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " Mbit/s"; |
| 371 | } else { |
| 372 | res <<cxx . . $ std/basic_ostream#`operator<<`(968942d578224538). (bitrateInKbitsPerSecond * 0.000001) <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " Gbit/s"; |
| 373 | } |
| 374 | } |
| 375 | return res.strcxx . . $ std/__cxx11/basic_stringstream#str(d33e1a6fd36255f7).(); |
| 376 | } |
| 377 | |
| 378 | //! \cond |
| 379 | const char *const base64Charscxx . . $ CppUtilities/base64Chars. = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 380 | const char base64Padcxx . . $ CppUtilities/base64Pad. = '='; |
| 381 | //! \endcond |
| 382 | |
| 383 | /*! |
| 384 | * \brief Encodes the specified \a data to Base64. |
| 385 | * \sa [RFC 4648](http://www.ietf.org/rfc/rfc4648.txt) |
| 386 | */ |
| 387 | stringcxx . . $ std/string# encodeBase64cxx . . $ CppUtilities/encodeBase64(e95b56f114df2c56).(const std::uint8_tcxx . . $ std/uint8_t# *data, std::uint32_tcxx . . $ std/uint32_t# dataSize) |
| 388 | { |
| 389 | auto encoded = stdcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).::stringcxx . . $ std/string#(); |
| 390 | auto mod = static_cast<std::uint8_tcxx . . $ std/uint8_t#>(dataSize % 3); |
| 391 | auto temp = std::uint32_tcxx . . $ std/uint32_t#(); |
| 392 | encoded.reservecxx . . $ std/__cxx11/basic_string#reserve(9dac5f4bebd06212).(((dataSize / 3) + (mod > 0)) * 4); |
| 393 | for (const std::uint8_tcxx . . $ std/uint8_t# *end = --data + dataSize - mod; data != end;) { |
| 394 | temp = static_cast<std::uint32_tcxx . . $ std/uint32_t#>(*++data << 16); |
| 395 | temp |= static_cast<std::uint32_tcxx . . $ std/uint32_t#>(*++data << 8); |
| 396 | temp |= *++data; |
| 397 | encoded.push_backcxx . . $ std/__cxx11/basic_string#push_back(9b289cee16747614).(base64Charscxx . . $ CppUtilities/base64Chars.[(temp & 0x00FC0000) >> 18]); |
| 398 | encoded.push_backcxx . . $ std/__cxx11/basic_string#push_back(9b289cee16747614).(base64Charscxx . . $ CppUtilities/base64Chars.[(temp & 0x0003F000) >> 12]); |
| 399 | encoded.push_backcxx . . $ std/__cxx11/basic_string#push_back(9b289cee16747614).(base64Charscxx . . $ CppUtilities/base64Chars.[(temp & 0x00000FC0) >> 6]); |
| 400 | encoded.push_backcxx . . $ std/__cxx11/basic_string#push_back(9b289cee16747614).(base64Charscxx . . $ CppUtilities/base64Chars.[(temp & 0x0000003F)]); |
| 401 | } |
| 402 | switch (mod) { |
| 403 | case 1: |
| 404 | temp = static_cast<std::uint32_tcxx . . $ std/uint32_t#>(*++data << 16); |
| 405 | encoded.push_backcxx . . $ std/__cxx11/basic_string#push_back(9b289cee16747614).(base64Charscxx . . $ CppUtilities/base64Chars.[(temp & 0x00FC0000) >> 18]); |
| 406 | encoded.push_backcxx . . $ std/__cxx11/basic_string#push_back(9b289cee16747614).(base64Charscxx . . $ CppUtilities/base64Chars.[(temp & 0x0003F000) >> 12]); |
| 407 | encoded.push_backcxx . . $ std/__cxx11/basic_string#push_back(9b289cee16747614).(base64Padcxx . . $ CppUtilities/base64Pad.); |
| 408 | encoded.push_backcxx . . $ std/__cxx11/basic_string#push_back(9b289cee16747614).(base64Padcxx . . $ CppUtilities/base64Pad.); |
| 409 | break; |
| 410 | case 2: |
| 411 | temp = static_cast<std::uint32_tcxx . . $ std/uint32_t#>(*++data << 16); |
| 412 | temp |= static_cast<std::uint32_tcxx . . $ std/uint32_t#>(*++data << 8); |
| 413 | encoded.push_backcxx . . $ std/__cxx11/basic_string#push_back(9b289cee16747614).(base64Charscxx . . $ CppUtilities/base64Chars.[(temp & 0x00FC0000) >> 18]); |
| 414 | encoded.push_backcxx . . $ std/__cxx11/basic_string#push_back(9b289cee16747614).(base64Charscxx . . $ CppUtilities/base64Chars.[(temp & 0x0003F000) >> 12]); |
| 415 | encoded.push_backcxx . . $ std/__cxx11/basic_string#push_back(9b289cee16747614).(base64Charscxx . . $ CppUtilities/base64Chars.[(temp & 0x00000FC0) >> 6]); |
| 416 | encoded.push_backcxx . . $ std/__cxx11/basic_string#push_back(9b289cee16747614).(base64Padcxx . . $ CppUtilities/base64Pad.); |
| 417 | break; |
| 418 | } |
| 419 | return encodedcxx . . $ std/__cxx11/basic_string#basic_string(8f3aaafa33d05d0b).; |
| 420 | } |
| 421 | |
| 422 | /*! |
| 423 | * \brief Decodes the specified Base64 encoded string. |
| 424 | * \throw Throws a ConversionException if the specified string is no valid Base64. |
| 425 | * \sa [RFC 4648](http://www.ietf.org/rfc/rfc4648.txt) |
| 426 | */ |
| 427 | std::paircxx . . $ std/pair#<std::unique_ptrcxx . . $ std/unique_ptr#<std::uint8_tcxx . . $ std/uint8_t#[]>, std::uint32_tcxx . . $ std/uint32_t#> decodeBase64cxx . . $ CppUtilities/decodeBase64(ae13223512d6be22).(const char *encodedStr, const std::uint32_tcxx . . $ std/uint32_t# strSize) |
| 428 | { |
| 429 | if (!strSize) { |
| 430 | return stdcxx . . $ std/pair#pair(f166319c93b32899).::make_paircxx . . $ std/make_pair(1d30b6e115203116).(std::make_uniquecxx . . $ std/make_unique(dfd06a9f52fea874).<std::uint8_tcxx . . $ std/uint8_t#[]>(0), 0); // early return to prevent clazy warning |
| 431 | } |
| 432 | if (strSize % 4) { |
| 433 | throw ConversionExceptioncxx . . $ CppUtilities/ConversionException#("invalid size of base64"); |
| 434 | } |
| 435 | std::uint32_tcxx . . $ std/uint32_t# decodedSize = (strSize / 4) * 3; |
| 436 | const char *const end = encodedStr + strSize; |
| 437 | if (*(end - 1) == base64Padcxx . . $ CppUtilities/base64Pad.) { |
| 438 | --decodedSize; |
| 439 | } |
| 440 | if (*(end - 2) == base64Padcxx . . $ CppUtilities/base64Pad.) { |
| 441 | --decodedSize; |
| 442 | } |
| 443 | auto buffer = std::make_uniquecxx . . $ std/make_unique(dfd06a9f52fea874).<std::uint8_tcxx . . $ std/uint8_t#[]>(decodedSize); |
| 444 | auto *iter = buffer.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).() - 1; |
| 445 | while (encodedStr < end) { |
| 446 | std::int32_tcxx . . $ std/int32_t# temp = 0; |
| 447 | for (std::uint8_tcxx . . $ std/uint8_t# quantumPos = 0; quantumPos < 4; ++quantumPos, ++encodedStr) { |
| 448 | temp <<= 6; |
| 449 | if (*encodedStr >= 'A' && *encodedStr <= 'Z') { |
| 450 | temp |= *encodedStr - 'A'; |
| 451 | } else if (*encodedStr >= 'a' && *encodedStr <= 'z') { |
| 452 | temp |= *encodedStr - 'a' + 26; |
| 453 | } else if (*encodedStr >= '0' && *encodedStr <= '9') { |
| 454 | temp |= *encodedStr - '0' + 2 * 26; |
| 455 | } else if (*encodedStr == '+') { |
| 456 | temp |= 2 * 26 + 10; |
| 457 | } else if (*encodedStr == '/') { |
| 458 | temp |= 2 * 26 + 10 + 1; |
| 459 | } else if (*encodedStr == base64Padcxx . . $ CppUtilities/base64Pad.) { |
| 460 | switch (end - encodedStr) { |
| 461 | case 1: |
| 462 | *++iter = static_cast<std::uint8_tcxx . . $ std/uint8_t#>((temp >> 16) & 0xFF); |
| 463 | *++iter = static_cast<std::uint8_tcxx . . $ std/uint8_t#>((temp >> 8) & 0xFF); |
| 464 | return std::make_paircxx . . $ std/make_pair(1d30b6e115203116).(std::movecxx . . $ std/move(e35c19a1ba7baa26).(buffer), decodedSize); |
| 465 | case 2: |
| 466 | *++iter = static_cast<std::uint8_tcxx . . $ std/uint8_t#>((temp >> 10) & 0xFF); |
| 467 | return std::make_paircxx . . $ std/make_pair(1d30b6e115203116).(std::movecxx . . $ std/move(e35c19a1ba7baa26).(buffer), decodedSize); |
| 468 | default: |
| 469 | throw ConversionExceptioncxx . . $ CppUtilities/ConversionException#("invalid padding in base64"); |
| 470 | } |
| 471 | } else { |
| 472 | throw ConversionExceptioncxx . . $ CppUtilities/ConversionException#("invalid character in base64"); |
| 473 | } |
| 474 | } |
| 475 | *++iter = static_cast<std::uint8_tcxx . . $ std/uint8_t#>((temp >> 16) & 0xFF); |
| 476 | *++iter = static_cast<std::uint8_tcxx . . $ std/uint8_t#>((temp >> 8) & 0xFF); |
| 477 | *++iter = static_cast<std::uint8_tcxx . . $ std/uint8_t#>(temp & 0xFF); |
| 478 | } |
| 479 | return std::make_paircxx . . $ std/make_pair(1d30b6e115203116).(std::movecxx . . $ std/move(e35c19a1ba7baa26).(buffer), decodedSize); |
| 480 | } |
| 481 | } // namespace CppUtilities |