Add strtol native (bug 3233, r=joropito)
Former-commit-id: 9508f9f86db99e93de1819f585c741396d8e4c30
This commit is contained in:
parent
1000fdc902
commit
873fb6924f
|
@ -341,6 +341,28 @@ static cell AMX_NATIVE_CALL strtonum(AMX *amx, cell *params) /* 1 param */
|
|||
return atoi(get_amxstring(amx, params[1], 0, iLen));
|
||||
}
|
||||
|
||||
static cell AMX_NATIVE_CALL amx_strtol(AMX *amx, cell *params) /* 3 param */
|
||||
{
|
||||
int len;
|
||||
int base = params[3];
|
||||
|
||||
if( base != 0 && ( base < 2 || base > 36 ) )
|
||||
base = 0;
|
||||
|
||||
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;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static cell AMX_NATIVE_CALL numtostr(AMX *amx, cell *params) /* 3 param */
|
||||
{
|
||||
char szTemp[32];
|
||||
|
@ -1130,6 +1152,7 @@ AMX_NATIVE_INFO string_Natives[] =
|
|||
{"strtoupper", strtoupper},
|
||||
{"str_to_num", strtonum},
|
||||
{"strtonum", strtonum},
|
||||
{"strtol", amx_strtol},
|
||||
{"trim", amx_trim},
|
||||
{"ucfirst", amx_ucfirst},
|
||||
{"strtok", amx_strtok},
|
||||
|
|
|
@ -78,6 +78,47 @@ native num_to_str(num,string[],len);
|
|||
/* Returns converted string to number. */
|
||||
native str_to_num(const string[]);
|
||||
|
||||
/**
|
||||
* Parses the 'string' interpreting its content as an integral number of the specified 'base',
|
||||
* which is returned as integer value. 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++ strtol 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 following a syntax that depends on the 'base' parameter,
|
||||
* and interprets them as a numerical value. Finally, a position of the first character following
|
||||
* the integer representation in 'string' is stored in 'endPos'.
|
||||
*
|
||||
* If the value of 'base' is zero, the syntax expected is similar to that of integer constants,
|
||||
* which is formed by a succession of :
|
||||
* An optional sign character (+ or -)
|
||||
* An optional prefix indicating octal or hexadecimal base ("0" or "0x"/"0X" respectively)
|
||||
* A sequence of decimal digits (if no base prefix was specified) or either octal or hexadecimal digits if a specific prefix is present
|
||||
*
|
||||
* If the 'base' value is between 2 and 36, the format expected for the integral number is a succession
|
||||
* of any of the valid digits and/or letters needed to represent integers of the specified radix
|
||||
* (starting from '0' and up to 'z'/'Z' for radix 36). The sequence may optionally be preceded by
|
||||
* a sign (either + or -) and, if base is 16, an optional "0x" or "0X" prefix.
|
||||
*
|
||||
* If the first sequence of non-whitespace characters in 'string' is not a valid integral 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 -1.
|
||||
* @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.
|
||||
* If no valid conversion could be performed, a zero value is returned.
|
||||
* 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 );
|
||||
|
||||
/* Converts float to string. */
|
||||
native float_to_str(Float:fl, string[], len);
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user