#include "config.h"
#include "Length.h"
#include "CalculationValue.h"
#include <wtf/ASCIICType.h>
#include <wtf/HashMap.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/StdLibExtras.h>
#include <wtf/text/StringBuffer.h>
#include <wtf/text/StringView.h>
using namespace WTF;
namespace WebCore {
static Length parseLength(const UChar* data, unsigned length)
{
if (length == 0)
return Length(1, Relative);
unsigned i = 0;
while (i < length && isSpaceOrNewline(data[i]))
++i;
if (i < length && (data[i] == '+' || data[i] == '-'))
++i;
while (i < length && isASCIIDigit(data[i]))
++i;
unsigned intLength = i;
while (i < length && (isASCIIDigit(data[i]) || data[i] == '.'))
++i;
unsigned doubleLength = i;
while (i < length && isSpaceOrNewline(data[i]))
++i;
bool ok;
UChar next = (i < length) ? data[i] : ' ';
if (next == '%') {
double r = charactersToDouble(data, doubleLength, &ok);
if (ok)
return Length(r, Percent);
return Length(1, Relative);
}
int r = charactersToIntStrict(data, intLength, &ok);
if (next == '*') {
if (ok)
return Length(r, Relative);
return Length(1, Relative);
}
if (ok)
return Length(r, Fixed);
return Length(0, Relative);
}
static unsigned countCharacter(StringImpl& string, UChar character)
{
unsigned count = 0;
unsigned length = string.length();
for (unsigned i = 0; i < length; ++i)
count += string[i] == character;
return count;
}
std::unique_ptr<Length[]> newCoordsArray(const String& string, int& len)
{
unsigned length = string.length();
StringBuffer<UChar> spacified(length);
for (unsigned i = 0; i < length; i++) {
UChar cc = string[i];
if (cc > '9' || (cc < '0' && cc != '-' && cc != '*' && cc != '.'))
spacified[i] = ' ';
else
spacified[i] = cc;
}
RefPtr<StringImpl> str = StringImpl::adopt(spacified);
str = str->simplifyWhiteSpace();
len = countCharacter(*str, ' ') + 1;
auto r = std::make_unique<Length[]>(len);
int i = 0;
unsigned pos = 0;
size_t pos2;
auto upconvertedCharacters = StringView(str.get()).upconvertedCharacters();
while ((pos2 = str->find(' ', pos)) != notFound) {
r[i++] = parseLength(upconvertedCharacters + pos, pos2 - pos);
pos = pos2+1;
}
r[i] = parseLength(upconvertedCharacters + pos, str->length() - pos);
ASSERT(i == len - 1);
return r;
}
std::unique_ptr<Length[]> newLengthArray(const String& string, int& len)
{
RefPtr<StringImpl> str = string.impl()->simplifyWhiteSpace();
if (!str->length()) {
len = 1;
return nullptr;
}
len = countCharacter(*str, ',') + 1;
auto r = std::make_unique<Length[]>(len);
int i = 0;
unsigned pos = 0;
size_t pos2;
auto upconvertedCharacters = StringView(str.get()).upconvertedCharacters();
while ((pos2 = str->find(',', pos)) != notFound) {
r[i++] = parseLength(upconvertedCharacters + pos, pos2 - pos);
pos = pos2+1;
}
ASSERT(i == len - 1);
if (str->length()-pos > 0)
r[i] = parseLength(upconvertedCharacters + pos, str->length() - pos);
else
len--;
return r;
}
class CalculationValueMap {
public:
CalculationValueMap();
unsigned insert(PassRef<CalculationValue>);
void ref(unsigned handle);
void deref(unsigned handle);
CalculationValue& get(unsigned handle) const;
private:
struct Entry {
uint64_t referenceCountMinusOne;
CalculationValue* value;
Entry();
Entry(CalculationValue&);
};
unsigned m_nextAvailableHandle;
HashMap<unsigned, Entry> m_map;
};
inline CalculationValueMap::Entry::Entry()
: referenceCountMinusOne(0)
, value(nullptr)
{
}
inline CalculationValueMap::Entry::Entry(CalculationValue& value)
: referenceCountMinusOne(0)
, value(&value)
{
}
inline CalculationValueMap::CalculationValueMap()
: m_nextAvailableHandle(1)
{
}
inline unsigned CalculationValueMap::insert(PassRef<CalculationValue> value)
{
ASSERT(m_nextAvailableHandle);
Entry leakedValue = value.leakRef();
while (!m_map.isValidKey(m_nextAvailableHandle) || !m_map.add(m_nextAvailableHandle, leakedValue).isNewEntry)
++m_nextAvailableHandle;
return m_nextAvailableHandle++;
}
inline CalculationValue& CalculationValueMap::get(unsigned handle) const
{
ASSERT(m_map.contains(handle));
return *m_map.find(handle)->value.value;
}
inline void CalculationValueMap::ref(unsigned handle)
{
ASSERT(m_map.contains(handle));
++m_map.find(handle)->value.referenceCountMinusOne;
}
inline void CalculationValueMap::deref(unsigned handle)
{
ASSERT(m_map.contains(handle));
auto it = m_map.find(handle);
if (it->value.referenceCountMinusOne) {
--it->value.referenceCountMinusOne;
return;
}
Ref<CalculationValue> value { adoptRef(*it->value.value) };
m_map.remove(it);
}
static CalculationValueMap& calculationValues()
{
static NeverDestroyed<CalculationValueMap> map;
return map;
}
Length::Length(PassRef<CalculationValue> value)
: m_hasQuirk(false)
, m_type(Calculated)
, m_isFloat(false)
{
m_calculationValueHandle = calculationValues().insert(WTF::move(value));
}
Length Length::blendMixedTypes(const Length& from, double progress) const
{
if (progress <= 0.0)
return from;
if (progress >= 1.0)
return *this;
auto blend = std::make_unique<CalcExpressionBlendLength>(from, *this, progress);
return Length(CalculationValue::create(WTF::move(blend), CalculationRangeAll));
}
CalculationValue& Length::calculationValue() const
{
ASSERT(isCalculated());
return calculationValues().get(m_calculationValueHandle);
}
void Length::ref() const
{
ASSERT(isCalculated());
calculationValues().ref(m_calculationValueHandle);
}
void Length::deref() const
{
ASSERT(isCalculated());
calculationValues().deref(m_calculationValueHandle);
}
float Length::nonNanCalculatedValue(int maxValue) const
{
ASSERT(isCalculated());
float result = calculationValue().evaluate(maxValue);
if (std::isnan(result))
return 0;
return result;
}
bool Length::isCalculatedEqual(const Length& other) const
{
return calculationValue() == other.calculationValue();
}
struct SameSizeAsLength {
int32_t value;
int32_t metaData;
};
COMPILE_ASSERT(sizeof(Length) == sizeof(SameSizeAsLength), length_should_stay_small);
}