Skip to content

Commit a3fb4df

Browse files
authored
Add %lu & %li as format for int64 (#2421)
1 parent 337b755 commit a3fb4df

2 files changed

Lines changed: 46 additions & 8 deletions

File tree

core/logic/sprintf.cpp

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ void AddBinary(char **buf_p, size_t &maxlen, unsigned int val, int width, int fl
434434
*buf_p = buf;
435435
}
436436

437-
void AddUInt(char **buf_p, size_t &maxlen, unsigned int val, int width, int flags)
437+
void AddUInt(char **buf_p, size_t &maxlen, uint64_t val, int width, int flags)
438438
{
439439
char text[32];
440440
int digits;
@@ -478,24 +478,23 @@ void AddUInt(char **buf_p, size_t &maxlen, unsigned int val, int width, int flag
478478
*buf_p = buf;
479479
}
480480

481-
void AddInt(char **buf_p, size_t &maxlen, int val, int width, int flags)
481+
void AddInt(char **buf_p, size_t &maxlen, int64_t val, int width, int flags)
482482
{
483483
char text[32];
484484
int digits;
485-
int signedVal;
485+
int64_t signedVal;
486486
char *buf;
487-
unsigned int unsignedVal;
487+
uint64_t unsignedVal;
488488

489489
digits = 0;
490490
signedVal = val;
491491
if (val < 0)
492492
{
493-
/* we want the unsigned version */
494-
unsignedVal = abs(val);
493+
unsignedVal = 0 - static_cast<uint64_t>(val);
495494
}
496495
else
497496
{
498-
unsignedVal = val;
497+
unsignedVal = static_cast<uint64_t>(val);
499498
}
500499

501500
do
@@ -1116,6 +1115,31 @@ size_t atcprintf(char *buffer, size_t maxlen, const char *format, IPluginContext
11161115
flags |= ZEROPAD;
11171116
goto rflag;
11181117
}
1118+
case 'l':
1119+
{
1120+
CHECK_ARGS(0);
1121+
ch = *fmt++;
1122+
1123+
if (ch != 'd' && ch != 'i' && ch != 'u')
1124+
{
1125+
return pCtx->ThrowNativeError("Invalid formatter. Only %%ld, %%li, %%lu are allowed.");
1126+
}
1127+
1128+
cell_t *addr;
1129+
pCtx->LocalToPhysAddr(params[arg], &addr);
1130+
1131+
if (ch == 'u')
1132+
{
1133+
AddUInt(&buf_p, llen, *reinterpret_cast<uint64_t *>(addr), width, flags);
1134+
}
1135+
else
1136+
{
1137+
AddInt(&buf_p, llen, *reinterpret_cast<int64_t *>(addr), width, flags);
1138+
}
1139+
1140+
arg++;
1141+
break;
1142+
}
11191143
case '1':
11201144
case '2':
11211145
case '3':

plugins/include/string.inc

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ native int StringToInt64(const char[] str, int result[2], int nBase=10);
224224
native int IntToString(int num, char[] str, int maxlength);
225225
226226
/**
227-
* Converts a 64-bit integer to a string.
227+
* Converts a 64-bit integer in the legacy 2-cell representation to a string.
228228
*
229229
* @param num Array containing the upper and lower
230230
* 32-bits of a 64-bit integer.
@@ -235,6 +235,20 @@ native int IntToString(int num, char[] str, int maxlength);
235235
*/
236236
native int Int64ToString(const int num[2], char[] str, int maxlength);
237237
238+
/**
239+
* Converts an int64 value to a string.
240+
*
241+
* @param num int64 value to convert.
242+
* @param str Buffer to store string in.
243+
* @param maxlength Maximum length of string buffer.
244+
* @return Number of characters written to the buffer,
245+
* not including the null terminator.
246+
*/
247+
stock int Int64ToStringEx(int64 num, char[] str, int maxlength)
248+
{
249+
return Format(str, maxlength, "%li", num);
250+
}
251+
238252
/**
239253
* Converts a string to a floating point number.
240254
*

0 commit comments

Comments
 (0)