From 3510a2131139e773ff04a2c091d89adfbd15c95d Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Fri, 8 May 2020 18:25:05 +0300 Subject: [PATCH] String to int/float with specific base. --- module/Library/String.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/module/Library/String.cpp b/module/Library/String.cpp index 30595f17..ad907bc8 100644 --- a/module/Library/String.cpp +++ b/module/Library/String.cpp @@ -846,6 +846,26 @@ static String StrCharacterSwap(SQInteger a, SQInteger b, StackStrF & val) return str; } +// ------------------------------------------------------------------------------------------------ +static SQInteger SqStrToI(SQInteger base, StackStrF & s) +{ +#ifdef _SQ64 + return std::stoll(s.mPtr, nullptr, ConvTo< int >::From((base))); +#else + return std::stoi(s.mPtr, nullptr, ConvTo< int >::From((base))); +#endif +} + +// ------------------------------------------------------------------------------------------------ +static SQFloat SqStrToF(StackStrF & s) +{ +#ifdef SQUSEDOUBLE + return std::strtod(s.mPtr, nullptr); +#else + return std::strtof(s.mPtr, nullptr); +#endif +} + // ================================================================================================ void Register_String(HSQUIRRELVM vm) { @@ -865,6 +885,8 @@ void Register_String(HSQUIRRELVM vm) .FmtFunc(_SC("Lowercase"), &SqToLowercase) .FmtFunc(_SC("Uppercase"), &SqToUppercase) .FmtFunc(_SC("JustAlnum"), &SqJustAlphaNum) + .FmtFunc(_SC("ToInt"), &SqStrToI) + .FmtFunc(_SC("ToFloat"), &SqStrToF) .FmtFunc(_SC("AreAllSpace"), &SqAllChars< std::isspace >) .FmtFunc(_SC("AreAllPrint"), &SqAllChars< std::isprint >) .FmtFunc(_SC("AreAllCntrl"), &SqAllChars< std::iscntrl >)