diff --git a/amxmodx/string.cpp b/amxmodx/string.cpp index cab86354..b42de18f 100755 --- a/amxmodx/string.cpp +++ b/amxmodx/string.cpp @@ -352,17 +352,28 @@ static cell AMX_NATIVE_CALL amx_strtol(AMX *amx, cell *params) /* 3 param */ char *pString = get_amxstring(amx, params[1], 0, len); cell *endPos = get_amxaddr(amx, params[2]); - *endPos = -1; - char *pEnd = NULL; long result = strtol(pString, &pEnd, base); - if (pEnd != NULL && pString != pEnd) - *endPos = pEnd - pString; + *endPos = pEnd - pString; return result; } +static cell AMX_NATIVE_CALL amx_strtof(AMX *amx, cell *params) /* 2 param */ +{ + int len; + char *pString = get_amxstring(amx, params[1], 0, len); + cell *endPos = get_amxaddr(amx, params[2]); + + char *pEnd = NULL; + float result = strtod(pString, &pEnd); + + *endPos = pEnd - pString; + + return amx_ftoc(result); +} + static cell AMX_NATIVE_CALL numtostr(AMX *amx, cell *params) /* 3 param */ { char szTemp[32]; @@ -1275,6 +1286,7 @@ AMX_NATIVE_INFO string_Natives[] = {"str_to_num", strtonum}, {"strtonum", strtonum}, {"strtol", amx_strtol}, + {"strtof", amx_strtof}, {"trim", amx_trim}, {"ucfirst", amx_ucfirst}, {"strtok", amx_strtok}, diff --git a/plugins/include/string.inc b/plugins/include/string.inc index e4762781..b1fea47d 100755 --- a/plugins/include/string.inc +++ b/plugins/include/string.inc @@ -109,7 +109,7 @@ native str_to_num(const string[]); * @param string The string to parse. * @param endPos The position of the first character following the number. * On success and when containing only numbers, position is at the end of string, meaning equal to 'string' length. - * On failure, position is sets always to -1. + * On failure, position is sets always to 0. * @param base The numerical base (radix) that determines the valid characters and their interpretation. * If this is 0, the base used is determined by the format in the sequence. * @return On success, the function returns the converted integral number as integer value. @@ -117,7 +117,32 @@ native str_to_num(const string[]); * If the value read is out of the range of representable values by a cell, * the function returns 'cellmin' or 'cellmax'. */ -native strtol(const string[], &endPos = -1, base = 0); +native strtol(const string[], &endPos = 0, base = 0); + +/** + * Parses the 'string' interpreting its content as an floating point number and returns its value as a float. + * The function also sets the value of 'endPos' to point to the position of the first character after the number. + * + * This is the same as C++ strtod function with a difference on second param. + * + * The function first discards as many whitespace characters as necessary until the first + * non-whitespace character is found. Then, starting from this character, takes as many + * characters as possible that are valid and interprets them as a numerical value. + * Finally, a position of the first character following the float representation in 'string' + * is stored in 'endPos'. + * + * If the first sequence of non-whitespace characters in 'string' is not a valid float number + * as defined above, or if no such sequence exists because either 'string' is empty or it contains + * only whitespace characters, no conversion is performed. + * + * @param string The string to parse. + * @param endPos The position of the first character following the number. + * On success and when containing only numbers, position is at the end of string, meaning equal to 'string' length. + * On failure, position is sets always to 0. + * @return On success, the function returns the converted floating point number as float value. + * If no valid conversion could be performed, a zero value is returned. + */ +native Float:strtof(const string[], &endPos = 0); /* Converts float to string. */ native float_to_str(Float:fl, string[], len);