mirror of
https://gitlab.linphone.org/BC/public/linphone-iphone.git
synced 2026-05-03 20:46:28 +00:00
feat(variant): add getValueAsDouble impl
This commit is contained in:
parent
79604de277
commit
f4f31dc422
3 changed files with 53 additions and 9 deletions
|
|
@ -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;
|
||||
}
|
||||
|
||||
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::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;
|
||||
}
|
||||
|
||||
bool Utils::stringToBool (const string &str) {
|
||||
const string lowerStr = stringToLower(str);
|
||||
return !lowerStr.empty() && (lowerStr == "true" || lowerStr == "1");
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue