From a3fb4dfe5e383c87e82b3b23a5fab109b9f58278 Mon Sep 17 00:00:00 2001 From: Ayrton09 <47057206+Ayrton09@users.noreply.github.com> Date: Thu, 9 Apr 2026 10:58:35 -0300 Subject: [PATCH] Add `%lu` & `%li` as format for int64 (#2421) --- core/logic/sprintf.cpp | 38 +++++++++++++++++++++++++++++++------- plugins/include/string.inc | 16 +++++++++++++++- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/core/logic/sprintf.cpp b/core/logic/sprintf.cpp index d68df7df..a6a47fb3 100644 --- a/core/logic/sprintf.cpp +++ b/core/logic/sprintf.cpp @@ -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(val); } else { - unsignedVal = val; + unsignedVal = static_cast(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(addr), width, flags); + } + else + { + AddInt(&buf_p, llen, *reinterpret_cast(addr), width, flags); + } + + arg++; + break; + } case '1': case '2': case '3': diff --git a/plugins/include/string.inc b/plugins/include/string.inc index 8f396d6f..c3851009 100644 --- a/plugins/include/string.inc +++ b/plugins/include/string.inc @@ -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. *