mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-02-03 03:30:06 +00:00
Added a couple of useful constructors to some stream classes. Replaced some old static functions with anonymous namespaces.
This commit is contained in:
parent
1577be257c
commit
377b9ff2c4
65 changed files with 1844 additions and 1632 deletions
|
|
@ -477,150 +477,153 @@ int64 CharacterFunctions::getInt64Value (const juce_wchar* s) throw()
|
|||
}
|
||||
|
||||
//==============================================================================
|
||||
static double juce_mulexp10 (const double value, int exponent) throw()
|
||||
namespace
|
||||
{
|
||||
if (exponent == 0)
|
||||
return value;
|
||||
|
||||
if (value == 0)
|
||||
return 0;
|
||||
|
||||
const bool negative = (exponent < 0);
|
||||
if (negative)
|
||||
exponent = -exponent;
|
||||
|
||||
double result = 1.0, power = 10.0;
|
||||
for (int bit = 1; exponent != 0; bit <<= 1)
|
||||
double juce_mulexp10 (const double value, int exponent) throw()
|
||||
{
|
||||
if ((exponent & bit) != 0)
|
||||
if (exponent == 0)
|
||||
return value;
|
||||
|
||||
if (value == 0)
|
||||
return 0;
|
||||
|
||||
const bool negative = (exponent < 0);
|
||||
if (negative)
|
||||
exponent = -exponent;
|
||||
|
||||
double result = 1.0, power = 10.0;
|
||||
for (int bit = 1; exponent != 0; bit <<= 1)
|
||||
{
|
||||
exponent ^= bit;
|
||||
result *= power;
|
||||
if (exponent == 0)
|
||||
break;
|
||||
}
|
||||
power *= power;
|
||||
}
|
||||
|
||||
return negative ? (value / result) : (value * result);
|
||||
}
|
||||
|
||||
template <class CharType>
|
||||
double juce_atof (const CharType* const original) throw()
|
||||
{
|
||||
double result[3] = { 0, 0, 0 }, accumulator[2] = { 0, 0 };
|
||||
int exponentAdjustment[2] = { 0, 0 }, exponentAccumulator[2] = { -1, -1 };
|
||||
int exponent = 0, decPointIndex = 0, digit = 0;
|
||||
int lastDigit = 0, numSignificantDigits = 0;
|
||||
bool isNegative = false, digitsFound = false;
|
||||
const int maxSignificantDigits = 15 + 2;
|
||||
|
||||
const CharType* s = original;
|
||||
while (CharacterFunctions::isWhitespace (*s))
|
||||
++s;
|
||||
|
||||
switch (*s)
|
||||
{
|
||||
case '-': isNegative = true; // fall-through..
|
||||
case '+': ++s;
|
||||
}
|
||||
|
||||
if (*s == 'n' || *s == 'N' || *s == 'i' || *s == 'I')
|
||||
return atof (String (original).toUTF8()); // Let the c library deal with NAN and INF
|
||||
|
||||
for (;;)
|
||||
{
|
||||
if (CharacterFunctions::isDigit (*s))
|
||||
{
|
||||
lastDigit = digit;
|
||||
digit = *s++ - '0';
|
||||
digitsFound = true;
|
||||
|
||||
if (decPointIndex != 0)
|
||||
exponentAdjustment[1]++;
|
||||
|
||||
if (numSignificantDigits == 0 && digit == 0)
|
||||
continue;
|
||||
|
||||
if (++numSignificantDigits > maxSignificantDigits)
|
||||
if ((exponent & bit) != 0)
|
||||
{
|
||||
if (digit > 5)
|
||||
++accumulator [decPointIndex];
|
||||
else if (digit == 5 && (lastDigit & 1) != 0)
|
||||
++accumulator [decPointIndex];
|
||||
exponent ^= bit;
|
||||
result *= power;
|
||||
if (exponent == 0)
|
||||
break;
|
||||
}
|
||||
power *= power;
|
||||
}
|
||||
|
||||
if (decPointIndex > 0)
|
||||
exponentAdjustment[1]--;
|
||||
else
|
||||
exponentAdjustment[0]++;
|
||||
return negative ? (value / result) : (value * result);
|
||||
}
|
||||
|
||||
while (CharacterFunctions::isDigit (*s))
|
||||
template <class CharType>
|
||||
double juce_atof (const CharType* const original) throw()
|
||||
{
|
||||
double result[3] = { 0, 0, 0 }, accumulator[2] = { 0, 0 };
|
||||
int exponentAdjustment[2] = { 0, 0 }, exponentAccumulator[2] = { -1, -1 };
|
||||
int exponent = 0, decPointIndex = 0, digit = 0;
|
||||
int lastDigit = 0, numSignificantDigits = 0;
|
||||
bool isNegative = false, digitsFound = false;
|
||||
const int maxSignificantDigits = 15 + 2;
|
||||
|
||||
const CharType* s = original;
|
||||
while (CharacterFunctions::isWhitespace (*s))
|
||||
++s;
|
||||
|
||||
switch (*s)
|
||||
{
|
||||
case '-': isNegative = true; // fall-through..
|
||||
case '+': ++s;
|
||||
}
|
||||
|
||||
if (*s == 'n' || *s == 'N' || *s == 'i' || *s == 'I')
|
||||
return atof (String (original).toUTF8()); // Let the c library deal with NAN and INF
|
||||
|
||||
for (;;)
|
||||
{
|
||||
if (CharacterFunctions::isDigit (*s))
|
||||
{
|
||||
lastDigit = digit;
|
||||
digit = *s++ - '0';
|
||||
digitsFound = true;
|
||||
|
||||
if (decPointIndex != 0)
|
||||
exponentAdjustment[1]++;
|
||||
|
||||
if (numSignificantDigits == 0 && digit == 0)
|
||||
continue;
|
||||
|
||||
if (++numSignificantDigits > maxSignificantDigits)
|
||||
{
|
||||
++s;
|
||||
if (decPointIndex == 0)
|
||||
if (digit > 5)
|
||||
++accumulator [decPointIndex];
|
||||
else if (digit == 5 && (lastDigit & 1) != 0)
|
||||
++accumulator [decPointIndex];
|
||||
|
||||
if (decPointIndex > 0)
|
||||
exponentAdjustment[1]--;
|
||||
else
|
||||
exponentAdjustment[0]++;
|
||||
|
||||
while (CharacterFunctions::isDigit (*s))
|
||||
{
|
||||
++s;
|
||||
if (decPointIndex == 0)
|
||||
exponentAdjustment[0]++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
const double maxAccumulatorValue = (double) ((std::numeric_limits<unsigned int>::max() - 9) / 10);
|
||||
if (accumulator [decPointIndex] > maxAccumulatorValue)
|
||||
{
|
||||
result [decPointIndex] = juce_mulexp10 (result [decPointIndex], exponentAccumulator [decPointIndex])
|
||||
+ accumulator [decPointIndex];
|
||||
accumulator [decPointIndex] = 0;
|
||||
exponentAccumulator [decPointIndex] = 0;
|
||||
}
|
||||
|
||||
accumulator [decPointIndex] = accumulator[decPointIndex] * 10 + digit;
|
||||
exponentAccumulator [decPointIndex]++;
|
||||
}
|
||||
}
|
||||
else if (decPointIndex == 0 && *s == '.')
|
||||
{
|
||||
++s;
|
||||
decPointIndex = 1;
|
||||
|
||||
if (numSignificantDigits > maxSignificantDigits)
|
||||
{
|
||||
while (CharacterFunctions::isDigit (*s))
|
||||
++s;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
const double maxAccumulatorValue = (double) ((std::numeric_limits<unsigned int>::max() - 9) / 10);
|
||||
if (accumulator [decPointIndex] > maxAccumulatorValue)
|
||||
{
|
||||
result [decPointIndex] = juce_mulexp10 (result [decPointIndex], exponentAccumulator [decPointIndex])
|
||||
+ accumulator [decPointIndex];
|
||||
accumulator [decPointIndex] = 0;
|
||||
exponentAccumulator [decPointIndex] = 0;
|
||||
}
|
||||
|
||||
accumulator [decPointIndex] = accumulator[decPointIndex] * 10 + digit;
|
||||
exponentAccumulator [decPointIndex]++;
|
||||
}
|
||||
}
|
||||
else if (decPointIndex == 0 && *s == '.')
|
||||
{
|
||||
++s;
|
||||
decPointIndex = 1;
|
||||
|
||||
if (numSignificantDigits > maxSignificantDigits)
|
||||
{
|
||||
while (CharacterFunctions::isDigit (*s))
|
||||
++s;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
result[0] = juce_mulexp10 (result[0], exponentAccumulator[0]) + accumulator[0];
|
||||
|
||||
if (decPointIndex != 0)
|
||||
result[1] = juce_mulexp10 (result[1], exponentAccumulator[1]) + accumulator[1];
|
||||
|
||||
if ((*s == 'e' || *s == 'E') && digitsFound)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
bool negativeExponent = false;
|
||||
|
||||
result[0] = juce_mulexp10 (result[0], exponentAccumulator[0]) + accumulator[0];
|
||||
switch (*++s)
|
||||
{
|
||||
case '-': negativeExponent = true; // fall-through..
|
||||
case '+': ++s;
|
||||
}
|
||||
|
||||
if (decPointIndex != 0)
|
||||
result[1] = juce_mulexp10 (result[1], exponentAccumulator[1]) + accumulator[1];
|
||||
while (CharacterFunctions::isDigit (*s))
|
||||
exponent = (exponent * 10) + (*s++ - '0');
|
||||
|
||||
if ((*s == 'e' || *s == 'E') && digitsFound)
|
||||
{
|
||||
bool negativeExponent = false;
|
||||
|
||||
switch (*++s)
|
||||
{
|
||||
case '-': negativeExponent = true; // fall-through..
|
||||
case '+': ++s;
|
||||
if (negativeExponent)
|
||||
exponent = -exponent;
|
||||
}
|
||||
|
||||
while (CharacterFunctions::isDigit (*s))
|
||||
exponent = (exponent * 10) + (*s++ - '0');
|
||||
double r = juce_mulexp10 (result[0], exponent + exponentAdjustment[0]);
|
||||
if (decPointIndex != 0)
|
||||
r += juce_mulexp10 (result[1], exponent - exponentAdjustment[1]);
|
||||
|
||||
if (negativeExponent)
|
||||
exponent = -exponent;
|
||||
return isNegative ? -r : r;
|
||||
}
|
||||
|
||||
double r = juce_mulexp10 (result[0], exponent + exponentAdjustment[0]);
|
||||
if (decPointIndex != 0)
|
||||
r += juce_mulexp10 (result[1], exponent - exponentAdjustment[1]);
|
||||
|
||||
return isNegative ? -r : r;
|
||||
}
|
||||
|
||||
double CharacterFunctions::getDoubleValue (const char* const s) throw()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue