Commit f4f31dc4 authored by Ronan's avatar Ronan
Browse files

feat(variant): add getValueAsDouble impl

parent 79604de2
......@@ -48,11 +48,11 @@ namespace Utils {
LINPHONE_PUBLIC std::string toString (long double val);
LINPHONE_PUBLIC int stoi (const std::string &str, size_t *idx = 0, int base = 10);
LINPHONE_PUBLIC double stod (const std::string &str, size_t *idx = 0);
LINPHONE_PUBLIC bool stob (const std::string &str);
LINPHONE_PUBLIC std::string stringToLower (const std::string &str);
LINPHONE_PUBLIC bool stringToBool (const std::string &str);
// Return a buffer allocated with new.
LINPHONE_PUBLIC char *utf8ToChar (uint32_t ic);
......
......@@ -88,17 +88,27 @@ int Utils::stoi (const string &str, size_t *idx, int base) {
return v;
}
string Utils::stringToLower (const string &str) {
string result(str.size(), ' ');
transform(str.cbegin(), str.cend(), result.begin(), ::tolower);
return result;
double Utils::stod (const std::string &str, size_t *idx) {
char *p;
double v = strtod(str.c_str(), &p);
if (idx)
*idx = p - str.c_str();
return v;
}
bool Utils::stringToBool (const string &str) {
bool Utils::stob (const string &str) {
const string lowerStr = stringToLower(str);
return !lowerStr.empty() && (lowerStr == "true" || lowerStr == "1");
}
string Utils::stringToLower (const string &str) {
string result(str.size(), ' ');
transform(str.cbegin(), str.cend(), result.begin(), ::tolower);
return result;
}
char *Utils::utf8ToChar (uint32_t ic) {
char *result = new char[5];
int size = 0;
......
......@@ -279,7 +279,7 @@ static inline bool getValueAsBool (const VariantPrivate &p, bool *soFarSoGood) {
return p.value.b;
case Variant::String:
return Utils::stringToBool(*p.value.str);
return Utils::stob(*p.value.str);
case Variant::Generic:
return static_cast<bool>(p.value.g);
......@@ -293,7 +293,41 @@ static inline bool getValueAsBool (const VariantPrivate &p, bool *soFarSoGood) {
}
static inline double getValueAsDouble (const VariantPrivate &p, bool *soFarSoGood) {
// TODO.
L_ASSERT(p.type > Variant::Invalid && p.type < Variant::MaxDefaultTypes);
*soFarSoGood = true;
switch (static_cast<Variant::Type>(p.type)) {
case Variant::Int:
case Variant::Short:
case Variant::Long:
case Variant::LongLong:
case Variant::Char:
case Variant::Float:
return static_cast<double>(getAssumedNumber(p));
case Variant::UnsignedInt:
case Variant::UnsignedShort:
case Variant::UnsignedLong:
case Variant::UnsignedLongLong:
return static_cast<double>(getAssumedUnsignedNumber(p));
case Variant::Double:
return p.value.d;
case Variant::Bool:
return static_cast<double>(p.value.b);
case Variant::String:
return Utils::stod(*p.value.str);
case Variant::Generic:
return static_cast<double>(!!p.value.g);
default:
*soFarSoGood = false;
break;
}
return 0.0;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment