1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
/*!********************************************************************
Audacity: A Digital Audio Editor
@file FromChars.cpp
@brief Define functions to convert numeric types to string representation.
Dmitry Vedenko
**********************************************************************/
#include "FromChars.h"
#include "3party/fast_float.h"
#include <limits>
#include <algorithm>
namespace
{
// Converting c to unsigned here helps to eliminate one check
unsigned digitToInt(char c) noexcept
{
return static_cast<unsigned>(c) - '0';
}
template <typename T>
bool safeMul10Add(T& result, T a, T b)
{
static_assert(std::is_unsigned_v<T>);
constexpr auto bits = sizeof(a) * 8 - 3;
// Check for the top 3 bits of a
if (a >> bits)
return false;
const T times8 = a << 3;
// a << 1 won't overflow
const T times2 = a << 1;
// timesX are all unsigned values, if overflow occurs - times10 will be <
// times8
const T times10 = times8 + times2;
if (times10 < times8)
return false;
const T tempResult = times10 + b;
if (tempResult < times10)
return false;
result = tempResult;
return true;
}
template <typename ResultType>
FromCharsResult FastStringToInt(
const char* first, const char* last, ResultType& value,
bool isNegative) noexcept
{
using UnsignedResultType = std::make_unsigned_t<ResultType>;
const auto availableBytes = last - first;
if (availableBytes <= 0)
return { first, std::errc::invalid_argument };
UnsignedResultType result = digitToInt(*first);
if (result > 10)
return { first, std::errc::invalid_argument };
constexpr auto maxSafeDigits = std::numeric_limits<ResultType>::digits10;
const char* ptr = first;
const char* safeLast =
first + std::min<decltype(availableBytes)>(availableBytes, maxSafeDigits);
unsigned d;
// No integer overflow here
while (++ptr < safeLast && (d = digitToInt(*ptr)) <= 9)
{
result = result * 10 + d;
}
// But here live dragons
while (ptr < last && (d = digitToInt(*ptr)) <= 9)
{
if (!safeMul10Add<UnsignedResultType>(result, result, d))
return { ptr, std::errc::result_out_of_range };
// Even if there were no unsigned overflow,
// signed overflow is still possible
if constexpr (std::is_signed_v<ResultType>)
{
const UnsignedResultType max =
static_cast<UnsignedResultType>(
std::numeric_limits<ResultType>::max()) +
(isNegative ? 1 : 0);
if (result > max)
return { ptr, std::errc::result_out_of_range };
}
++ptr;
}
if constexpr (std::is_signed_v<ResultType>)
{
value = isNegative ?
// Unary minus on unsigned type makes MSVC unhappy
static_cast<ResultType>(0 - result) :
static_cast<ResultType>(result);
}
else
value = static_cast<ResultType>(result);
return { ptr, std::errc() };
}
template <typename ResultType>
FromCharsResult
IntFromChars(const char* buffer, const char* last, ResultType& value) noexcept
{
const char* origin = buffer;
if (buffer >= last)
return { buffer, std::errc::invalid_argument };
const bool isNegative = *buffer == '-';
if (isNegative)
{
if constexpr (std::is_signed_v<ResultType>)
++buffer;
else
return { origin, std::errc::invalid_argument };
}
const auto fastStringResult =
FastStringToInt(buffer, last, value, isNegative);
if (fastStringResult.ec == std::errc::invalid_argument)
return { origin, std::errc::invalid_argument };
return fastStringResult;
}
} // namespace
FromCharsResult FromChars(const char* buffer, const char* last, float& value) noexcept
{
const auto result = fast_float::from_chars(buffer, last, value);
return { result.ptr, result.ec };
}
FromCharsResult FromChars(const char* buffer, const char* last, double& value) noexcept
{
const auto result = fast_float::from_chars(buffer, last, value);
return { result.ptr, result.ec };
}
FromCharsResult FromChars(const char* buffer, const char* last, short& value) noexcept
{
return IntFromChars(buffer, last, value);
}
FromCharsResult FromChars(const char* buffer, const char* last, unsigned short& value) noexcept
{
return IntFromChars(buffer, last, value);
}
FromCharsResult FromChars(const char* buffer, const char* last, int& value) noexcept
{
return IntFromChars(buffer, last, value);
}
FromCharsResult FromChars(const char* buffer, const char* last, unsigned int& value) noexcept
{
return IntFromChars(buffer, last, value);
}
FromCharsResult FromChars(const char* buffer, const char* last, long& value) noexcept
{
return IntFromChars(buffer, last, value);
}
FromCharsResult FromChars(const char* buffer, const char* last, unsigned long& value) noexcept
{
return IntFromChars(buffer, last, value);
}
FromCharsResult FromChars(const char* buffer, const char* last, long long& value) noexcept
{
return IntFromChars(buffer, last, value);
}
FromCharsResult FromChars(const char* buffer, const char* last, unsigned long long& value) noexcept
{
return IntFromChars(buffer, last, value);
}
FromCharsResult
FromChars(const char* buffer, const char* last, bool& value) noexcept
{
if (buffer >= last)
return { buffer, std::errc::invalid_argument };
if (buffer[0] == '0')
{
value = false;
return { buffer + 1, std::errc() };
}
else if (buffer[0] == '1')
{
value = true;
return { buffer + 1, std::errc() };
}
return { buffer, std::errc::invalid_argument };
}
|