Add %lu & %li as format for int64 (#2421)

This commit is contained in:
Ayrton09
2026-04-09 15:58:35 +02:00
committed by GitHub
parent 337b755f34
commit a3fb4dfe5e
2 changed files with 46 additions and 8 deletions
+31 -7
View File
@@ -434,7 +434,7 @@ void AddBinary(char **buf_p, size_t &maxlen, unsigned int val, int width, int fl
*buf_p = buf;
}
void AddUInt(char **buf_p, size_t &maxlen, unsigned int val, int width, int flags)
void AddUInt(char **buf_p, size_t &maxlen, uint64_t val, int width, int flags)
{
char text[32];
int digits;
@@ -478,24 +478,23 @@ void AddUInt(char **buf_p, size_t &maxlen, unsigned int val, int width, int flag
*buf_p = buf;
}
void AddInt(char **buf_p, size_t &maxlen, int val, int width, int flags)
void AddInt(char **buf_p, size_t &maxlen, int64_t val, int width, int flags)
{
char text[32];
int digits;
int signedVal;
int64_t signedVal;
char *buf;
unsigned int unsignedVal;
uint64_t unsignedVal;
digits = 0;
signedVal = val;
if (val < 0)
{
/* we want the unsigned version */
unsignedVal = abs(val);
unsignedVal = 0 - static_cast<uint64_t>(val);
}
else
{
unsignedVal = val;
unsignedVal = static_cast<uint64_t>(val);
}
do
@@ -1116,6 +1115,31 @@ reswitch:
flags |= ZEROPAD;
goto rflag;
}
case 'l':
{
CHECK_ARGS(0);
ch = *fmt++;
if (ch != 'd' && ch != 'i' && ch != 'u')
{
return pCtx->ThrowNativeError("Invalid formatter. Only %%ld, %%li, %%lu are allowed.");
}
cell_t *addr;
pCtx->LocalToPhysAddr(params[arg], &addr);
if (ch == 'u')
{
AddUInt(&buf_p, llen, *reinterpret_cast<uint64_t *>(addr), width, flags);
}
else
{
AddInt(&buf_p, llen, *reinterpret_cast<int64_t *>(addr), width, flags);
}
arg++;
break;
}
case '1':
case '2':
case '3':
+15 -1
View File
@@ -224,7 +224,7 @@ native int StringToInt64(const char[] str, int result[2], int nBase=10);
native int IntToString(int num, char[] str, int maxlength);
/**
* Converts a 64-bit integer to a string.
* Converts a 64-bit integer in the legacy 2-cell representation to a string.
*
* @param num Array containing the upper and lower
* 32-bits of a 64-bit integer.
@@ -235,6 +235,20 @@ native int IntToString(int num, char[] str, int maxlength);
*/
native int Int64ToString(const int num[2], char[] str, int maxlength);
/**
* Converts an int64 value to a string.
*
* @param num int64 value to convert.
* @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.
*/
stock int Int64ToStringEx(int64 num, char[] str, int maxlength)
{
return Format(str, maxlength, "%li", num);
}
/**
* Converts a string to a floating point number.
*