17 template<
typename Target,
typename Source>
19 if (value < (Source)(std::numeric_limits<Target>::min)() || value > (Source)(std::numeric_limits<Target>::max)()) {
20 throw OverflowException(
"Value was either too large or too small for the target type.");
32 std::string s = (
const char*)sValue;
33 std::transform(s.begin(), s.end(), s.begin(), [](
char c) { return (char)std::towlower(c); });
34 if (s ==
"true")
return true;
35 if (s ==
"false")
return false;
53 long long llVal = std::stoll((
const char*)sValue, &nPos, iFromBase);
54 if (nPos != sValue.GetLength())
throw FormatException(
"Input string was not in a correct format.");
56 }
catch (
const std::invalid_argument&) {
58 }
catch (
const std::out_of_range&) {
59 throw OverflowException(
"Value was either too large or too small for an unsigned byte.");
75 long long llVal = std::stoll((
const char*)sValue, &nPos, 10);
76 if (nPos != sValue.GetLength())
throw FormatException(
"Input string was not in a correct format.");
78 }
catch (
const std::invalid_argument&) {
80 }
catch (
const std::out_of_range&) {
81 throw OverflowException(
"Value was either too large or too small for a signed byte.");
103 double val = std::stod((
const char*)sValue, &nPos);
104 if (nPos != sValue.GetLength())
throw FormatException(
"Input string was not in a correct format.");
106 }
catch (
const std::invalid_argument&) {
108 }
catch (
const std::out_of_range&) {
109 throw OverflowException(
"Value was either too large or too small for a Double.");
121 float val = std::stof((
const char*)sValue, &nPos);
122 if (nPos != sValue.GetLength())
throw FormatException(
"Input string was not in a correct format.");
124 }
catch (
const std::invalid_argument&) {
126 }
catch (
const std::out_of_range&) {
127 throw OverflowException(
"Value was either too large or too small for a Single.");
143 long long llVal = std::stoll((
const char*)sValue, &nPos, iFromBase);
144 if (nPos != sValue.GetLength())
throw FormatException(
"Input string was not in a correct format.");
146 }
catch (
const std::invalid_argument&) {
148 }
catch (
const std::out_of_range&) {
149 throw OverflowException(
"Value was either too large or too small for an Int16.");
164 long long llVal = std::stoll((
const char*)sValue, &nPos, iFromBase);
165 if (nPos != sValue.GetLength())
throw FormatException(
"Input string was not in a correct format.");
167 }
catch (
const std::invalid_argument&) {
169 }
catch (
const std::out_of_range&) {
170 throw OverflowException(
"Value was either too large or too small for an Int32.");
185 long long llVal = std::stoll((
const char*)sValue, &nPos, iFromBase);
186 if (nPos != sValue.GetLength())
throw FormatException(
"Input string was not in a correct format.");
188 }
catch (
const std::invalid_argument&) {
190 }
catch (
const std::out_of_range&) {
191 throw OverflowException(
"Value was either too large or too small for an Int64.");
204 unsigned long long llVal = std::stoull((
const char*)sValue, &nPos, 10);
205 if (nPos != sValue.GetLength())
throw FormatException(
"Input string was not in a correct format.");
207 }
catch (
const std::invalid_argument&) {
209 }
catch (
const std::out_of_range&) {
210 throw OverflowException(
"Value was either too large or too small for a UInt16.");
223 unsigned long long llVal = std::stoull((
const char*)sValue, &nPos, 10);
224 if (nPos != sValue.GetLength())
throw FormatException(
"Input string was not in a correct format.");
226 }
catch (
const std::invalid_argument&) {
228 }
catch (
const std::out_of_range&) {
229 throw OverflowException(
"Value was either too large or too small for a UInt32.");
242 unsigned long long llVal = std::stoull((
const char*)sValue, &nPos, 10);
243 if (nPos != sValue.GetLength())
throw FormatException(
"Input string was not in a correct format.");
245 }
catch (
const std::invalid_argument&) {
247 }
catch (
const std::out_of_range&) {
248 throw OverflowException(
"Value was either too large or too small for a UInt64.");
259 snprintf(buf,
sizeof(buf),
"%g", value);
264 snprintf(buf,
sizeof(buf),
"%g",
static_cast<double>(value));
275 if (iToBase != 2 && iToBase != 8 && iToBase != 10 && iToBase != 16)
278 if (llValue == 0)
return "0";
281 const char* digits =
"0123456789ABCDEF";
282 while (llValue > 0) {
283 res += digits[llValue % iToBase];
286 std::reverse(res.begin(), res.end());
287 return String(res.c_str());
294 return ToBaseString((
unsigned long long)llValue, iToBase);
298 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
299 "abcdefghijklmnopqrstuvwxyz"
303 return (isalnum(c) || (c ==
'+') || (c ==
'/'));
307 unsigned char char_array_4[4];
308 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
309 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
310 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
311 char_array_4[3] = char_array_3[2] & 0x3f;
312 for (
int i = 0; i < 4; i++) ret +=
base64_chars[char_array_4[i]];
316 unsigned char char_array_4[4];
317 for (
int j = remainderLen; j < 3; ++j) char_array_3[j] =
'\0';
318 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
319 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
320 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
321 for (
int j = 0; j < remainderLen + 1; ++j) ret +=
base64_chars[char_array_4[j]];
322 for (
int pad = remainderLen; pad < 3; ++pad) ret +=
'=';
327 if (in_len == 0)
return String(
"");
328 const unsigned char* bytes =
reinterpret_cast<const unsigned char*
>(inArray.
GetData());
331 unsigned char char_array_3[3];
333 char_array_3[i++] = *(bytes++);
337 return String(ret.c_str());
341 for (
int k = 0; k < 4; k++) {
342 const char* ptr = std::strchr(
base64_chars, char_array_4[k]);
344 char_array_4[k] =
static_cast<unsigned char>(ptr -
base64_chars);
346 decoded.push_back((char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4));
347 decoded.push_back(((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2));
348 decoded.push_back(((char_array_4[2] & 0x3) << 6) + char_array_4[3]);
353 for (
int j = 0; j < i; j++) {
354 const char* ptr = std::strchr(
base64_chars, char_array_4[j]);
356 char_array_4[j] =
static_cast<unsigned char>(ptr -
base64_chars);
360 unsigned char char_array_3[2];
361 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
363 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
364 decoded.push_back(char_array_3[0]);
365 decoded.push_back(char_array_3[1]);
367 decoded.push_back(char_array_3[0]);
373 size_t in_len = sInput.size();
375 unsigned char char_array_4[4];
376 while (in_len-- && (sInput[in_] !=
'=') &&
IsBase64(sInput[in_])) {
377 char_array_4[i++] = sInput[in_++];
394 std::vector<char> decoded;
396 Array<char> result(
static_cast<int>(decoded.size()));
397 for (
size_t idx = 0; idx < decoded.size(); idx++) {
398 result[
static_cast<int>(idx)] = decoded[idx];
Defines the exception thrown when an invalid argument is provided to a method.
Provides methods for creating, manipulating, searching, and sorting arrays.
Converts a base data type to another base data type, and encodes/decodes Base64 data.
Defines the exception thrown when an arithmetic, casting, or conversion operation results in an overf...
ArgumentException(const String &sMessage)
Initializes a new instance of the ArgumentException class with a specified error message.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
int GetLength() const
Gets the total number of elements in all dimensions of the Array.
T * GetData()
Gets a pointer to the contiguous internal element buffer.
static long long ToInt64(bool value)
static bool ToBoolean(bool value)
static unsigned char ToByte(bool value)
static String ToString(bool value)
static Array< char > FromBase64String(const String &s)
static unsigned long long ToUInt64(bool value)
static unsigned short ToUInt16(bool value)
static double ToDouble(int value)
static String ToBase64String(const Array< char > &inArray)
static signed char ToSByte(bool value)
static char ToChar(unsigned short value)
static unsigned int ToUInt32(bool value)
static short ToInt16(bool value)
static int ToInt32(bool value)
static float ToSingle(int value)
OverflowException(const String &sMessage)
Initializes a new instance of the OverflowException class with a specified error message.
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
String()
Initializes a new instance of the String class to an empty string.
const char * GetRawString() const
static void DecodeBase64Loop(const std::string &sInput, std::vector< char > &decoded)
static void EncodeBase64Remainder(unsigned char *char_array_3, int remainderLen, std::string &ret)
static void DecodeBase64Chunk4(unsigned char *char_array_4, std::vector< char > &decoded)
static Target CheckRange(Source value)
static const char base64_chars[]
static void DecodeBase64Remainder(unsigned char *char_array_4, int i, std::vector< char > &decoded)
static String ToBaseString(unsigned long long llValue, int iToBase)
static void EncodeBase64Chunk4(const unsigned char *char_array_3, std::string &ret)
static bool IsBase64(unsigned char c)