diff --git a/core/smn_string.cpp b/core/smn_string.cpp index abc75697..71e6b3b1 100644 --- a/core/smn_string.cpp +++ b/core/smn_string.cpp @@ -108,6 +108,18 @@ static cell_t sm_strconvint(IPluginContext *pCtx, const cell_t *params) return static_cast(strtol(str, &dummy, params[2])); } +static cell_t StringToIntEx(IPluginContext *pCtx, const cell_t *params) +{ + char *str, *dummy = NULL; + cell_t *addr; + pCtx->LocalToString(params[1], &str); + pCtx->LocalToPhysAddr(params[2], &addr); + + *addr = static_cast(strtol(str, &dummy, params[3])); + + return dummy - str; +} + static cell_t sm_numtostr(IPluginContext *pCtx, const cell_t *params) { char *str; @@ -127,6 +139,20 @@ static cell_t sm_strtofloat(IPluginContext *pCtx, const cell_t *params) return sp_ftoc(val); } +static cell_t StringToFloatEx(IPluginContext *pCtx, const cell_t *params) +{ + char *str, *dummy = NULL; + cell_t *addr; + pCtx->LocalToString(params[1], &str); + pCtx->LocalToPhysAddr(params[2], &addr); + + float val = (float)strtod(str, &dummy); + + *addr = sp_ftoc(val); + + return dummy - str; +} + static cell_t sm_floattostr(IPluginContext *pCtx, const cell_t *params) { char *str; @@ -539,7 +565,9 @@ REGISTER_NATIVES(basicStrings) {"strcopy", sm_strcopy}, {"StrCopy", sm_strcopy}, /* Backwards compat shim */ {"StringToInt", sm_strconvint}, + {"StringToIntEx", StringToIntEx}, {"StringToFloat", sm_strtofloat}, + {"StringToFloatEx", StringToFloatEx}, {"TrimString", TrimString}, {"VFormat", sm_vformat}, {NULL, NULL}, diff --git a/plugins/include/string.inc b/plugins/include/string.inc index 6acd0228..dc3293c6 100644 --- a/plugins/include/string.inc +++ b/plugins/include/string.inc @@ -165,6 +165,16 @@ native VFormat(String:buffer[], maxlength, const String:format[], varpos); */ native StringToInt(const String:str[], nBase=10); +/** + * Converts a string to an integer with some more options. + * + * @param str String to convert. + * @param result Variable to store the result in. + * @param nBase Numerical base to use. 10 is default. + * @return Number of characters consumed. + */ +native StringToIntEx(const String:str[], &result, nBase=10); + /** * Converts an integer to a string. * @@ -183,6 +193,15 @@ native IntToString(num, String:str[], maxlength); */ native Float:StringToFloat(const String:str[]); +/** + * Converts a string to a floating point number with some more options. + * + * @param str String to convert to a foat. + * @param result Variable to store result in. + * @return Number of characters consumed. + */ +native StringToFloatEx(const String:str[], &Float:result); + /** * Converts a floating point number to a string. *