uint v1.0.3
As soon as the value exceeds int limit (2147483647), SourcePawn starts to have problems, example:
PHP Code:
int x = 2147483650 / 2; // Must be: 1073741825
PrintToServer("%%u = %u\n%%d = %d\n%u > 1000 = %s", x, x, x, x > 1000 ? "yes" : "no");
Result: all values are incorrect + comparison does not work:
Code:
%u = 3221225473
%d = -1073741823
3221225473 > 1000 = no
Now you can:
PHP Code:
int x = uint(2147483650, '/', 2);
PrintToServer("%u", x); // 1073741825
PrintToServer("3221225473 > 1000 = %s", uintcmp(3221225473, 1000) > 0 ? "yes" : "no"); // 3221225473 > 1000 = yes
Before:
PHP Code:
FormatTime(s, sizeof(s), "%d.%m.%Y - %H:%M:%S", 2147483648);
// plugin crash with error: [SM] Exception reported: Invalid time format or buffer too small
Now:
PHP Code:
FormatUTime(s, sizeof(s), "%d.%m.%Y - %H:%M:%S", 2147483648);
PrintToServer(s); // 19.01.2038 - 05:14:08
FormatTime safe version:
PHP Code:
stock void FormatTime_(char[] buffer, int maxlength, const char[] format, int stamp=-1) {
FormatTime(buffer, maxlength, format, stamp >= -1 ? stamp : 2147483647); // but int limit = bad
}
uint.inc
PHP Code:
#define UINT_SIZE 12
/**
* Math operations with unsigned int ('+', '-', '*', '/', '%')
*
* Example:
* int i = uint(2147483650, '/', 2);
* PrintToServer("%u", i); // 1073741825
*
* Note:
* unsigned int limit (4294967295) is not violated:
* 4294967295 + 1 = 4294967295
* 0 - 1 = 0
* 4294967295 * 2 = 4294967295
*/
native int uint(int a, int op, int b);
/**
* Comparison two unsigned int values
*
* Example:
* bool b = uintcmp(5, 3) >= 0; // true, if 5 >= 3 (instead of operator >=, use any. 0 must be always).
*/
native int uintcmp(int a, int b);
/**
* Replacement FormatTime, because int limit and:
*
* char s[128];
* FormatTime(s, sizeof(s), "%d.%m.%Y - %H:%M", 2147483648);
* Plugin crash with error: [SM] Exception reported: Invalid time format or buffer too small
*/
native bool FormatUTime(char[] buffer, int maxlength, const char[] format, int stamp=0);
// Convert unsigned int to string (IntToString can't) (return: Number of cells written)
stock int UIntToString(int value, char[] s, int size) {
return FormatEx(s, size, "%u", value);
}
// Extract unsigned int from the field (SQL_FetchInt can't)
stock int SQL_FetchUInt(Handle results, int field, DBResult &result=DBVal_Error, int error=0)
{
char s[UINT_SIZE];
return SQL_FetchString(results, field, s, sizeof(s), result) && result == DBVal_Data ? StringToInt(s) : error;
}
Changelog
v1.0.3
Function FormatUTime is faster at 46.72% (cycles for 4294967295 was: 73005, now: 49759)