Add new string natives/stocks, make some UTF-8 safe (bug 6110, r=ds)

This commit is contained in:
Arkshine
2014-04-30 09:33:03 +02:00
parent c99a518ba4
commit a86ca1491f
12 changed files with 1560 additions and 161 deletions

View File

@ -149,6 +149,12 @@ void AddString(U **buf_p, size_t &maxlen, const cell *string, int width, int pre
if (size > (int)maxlen)
size = maxlen;
/* If precision is provided, make sure we don't truncate a multi-byte character */
if (prec >= size && (string[size - 1] & 1 << 7))
{
size -= UTIL_CheckValidChar((cell *)string + size - 1);
}
maxlen -= size;
width -= size;
@ -286,6 +292,58 @@ void AddFloat(U **buf_p, size_t &maxlen, double fval, int width, int prec, int f
*buf_p = buf;
}
template <typename U>
void AddBinary(U **buf_p, size_t &maxlen, unsigned int val, int width, int flags)
{
char text[32];
int digits;
U *buf;
digits = 0;
do
{
if (val & 1)
{
text[digits++] = '1';
}
else
{
text[digits++] = '0';
}
val >>= 1;
} while (val);
buf = *buf_p;
if (!(flags & LADJUST))
{
while (digits < width && maxlen)
{
*buf++ = (flags & ZEROPAD) ? '0' : ' ';
width--;
maxlen--;
}
}
while (digits-- && maxlen)
{
*buf++ = text[digits];
width--;
maxlen--;
}
if (flags & LADJUST)
{
while (width-- && maxlen)
{
*buf++ = (flags & ZEROPAD) ? '0' : ' ';
maxlen--;
}
}
*buf_p = buf;
}
template <typename U>
void AddUInt(U **buf_p, size_t &maxlen, unsigned int val, int width, int flags)
{
@ -527,6 +585,11 @@ reswitch:
llen--;
arg++;
break;
case 'b':
CHECK_ARGS(0);
AddBinary(&buf_p, llen, *get_amxaddr(amx, params[arg]), width, flags);
arg++;
break;
case 'd':
case 'i':
CHECK_ARGS(0);
@ -635,6 +698,14 @@ break_to_normal_string:
done:
*buf_p = static_cast<D>(0);
*param = arg;
/* if max buffer length consumed, make sure we don't truncate a multi-byte character */
if (llen <= 0 && *(buf_p - 1) & 1 << 7)
{
llen += UTIL_CheckValidChar(buf_p - 1);
*(buf_p - llen) = static_cast<D>(0);
}
return maxlen-llen;
}