Add StringToInt64() and Int64ToString() natives (#1511)

This commit is contained in:
Vladimir 2021-06-28 23:51:49 +03:00 committed by GitHub
parent a9d3cf4574
commit f603b7aec3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 0 deletions

View File

@ -139,6 +139,22 @@ static cell_t StringToIntEx(IPluginContext *pCtx, const cell_t *params)
return dummy - str;
}
static cell_t StringToInt64(IPluginContext *pCtx, const cell_t *params)
{
char *str, *dummy = NULL;
cell_t *addr;
pCtx->LocalToString(params[1], &str);
pCtx->LocalToPhysAddr(params[2], &addr);
// uint64_t for correct signed right shift.
uint64_t number = (uint64_t)strtoll(str, &dummy, params[3]);
addr[0] = (cell_t)(number & 0xFFFFFFFFull);
addr[1] = (cell_t)(number >> 32ull);
return dummy - str;
}
static cell_t sm_numtostr(IPluginContext *pCtx, const cell_t *params)
{
char *str;
@ -148,6 +164,17 @@ static cell_t sm_numtostr(IPluginContext *pCtx, const cell_t *params)
return static_cast<cell_t>(res);
}
static cell_t Int64ToString(IPluginContext *pCtx, const cell_t *params)
{
cell_t *num;
char *str;
pCtx->LocalToPhysAddr(params[1], &num);
pCtx->LocalToString(params[2], &str);
size_t res = ke::SafeSprintf(str, params[3], "%" KE_FMT_I64, (int64_t)num[1] << 32ll | (int64_t)num[0]);
return static_cast<cell_t>(res);
}
static cell_t sm_strtofloat(IPluginContext *pCtx, const cell_t *params)
{
char *str, *dummy;
@ -591,6 +618,7 @@ REGISTER_NATIVES(basicStrings)
{"FormatEx", sm_formatex},
{"GetCharBytes", GetCharBytes},
{"IntToString", sm_numtostr},
{"Int64ToString", Int64ToString},
{"IsCharAlpha", IsCharAlpha},
{"IsCharLower", IsCharLower},
{"IsCharMB", IsCharMB},
@ -607,6 +635,7 @@ REGISTER_NATIVES(basicStrings)
{"strcopy", sm_strcopy},
{"StringToInt", sm_strconvint},
{"StringToIntEx", StringToIntEx},
{"StringToInt64", StringToInt64},
{"StringToFloat", sm_strtofloat},
{"StringToFloatEx", StringToFloatEx},
{"StripQuotes", StripQuotes},

View File

@ -195,6 +195,17 @@ native int StringToInt(const char[] str, int nBase=10);
*/
native int StringToIntEx(const char[] str, int &result, int nBase=10);
/**
* Converts a string to a 64-bit integer.
*
* @param str String to convert.
* @param result Array to store the upper and lower
* 32-bits of the 64-bit integer.
* @param nBase Numerical base to use. 10 is default.
* @return Number of characters consumed.
*/
native int StringToInt64(const char[] str, int result[2], int nBase=10);
/**
* Converts an integer to a string.
*
@ -205,6 +216,18 @@ native int StringToIntEx(const char[] str, int &result, int nBase=10);
*/
native int IntToString(int num, char[] str, int maxlength);
/**
* Converts a 64-bit integer to a string.
*
* @param num Array containing the upper and lower
* 32-bits of a 64-bit integer.
* @param str Buffer to store string in.
* @param maxlength Maximum length of string buffer.
* @return Number of characters written to the buffer,
* not including the null terminator.
*/
native int Int64ToString(const int num[2], char[] str, int maxlength);
/**
* Converts a string to a floating point number.
*