Add %p format specifier for printing pointers
This commit is contained in:
parent
617b861524
commit
602a7525d2
@ -34,18 +34,25 @@ size_t writef(string s, va_list args, ch_out_fn ch_out)
|
|||||||
length_written++;
|
length_written++;
|
||||||
escape = false;
|
escape = false;
|
||||||
}
|
}
|
||||||
|
else if (c == 'p')
|
||||||
|
{
|
||||||
|
ulong v;
|
||||||
|
va_arg(args, v);
|
||||||
|
length_written += write_hex(v, true, true, pad, width, ch_out);
|
||||||
|
escape = false;
|
||||||
|
}
|
||||||
else if (c == 'x')
|
else if (c == 'x')
|
||||||
{
|
{
|
||||||
ulong v;
|
ulong v;
|
||||||
va_arg(args, v);
|
va_arg(args, v);
|
||||||
length_written += write_hex(v, false, pad, width, ch_out);
|
length_written += write_hex(v, false, false, pad, width, ch_out);
|
||||||
escape = false;
|
escape = false;
|
||||||
}
|
}
|
||||||
else if (c == 'X')
|
else if (c == 'X')
|
||||||
{
|
{
|
||||||
ulong v;
|
ulong v;
|
||||||
va_arg(args, v);
|
va_arg(args, v);
|
||||||
length_written += write_hex(v, true, pad, width, ch_out);
|
length_written += write_hex(v, true, false, pad, width, ch_out);
|
||||||
escape = false;
|
escape = false;
|
||||||
}
|
}
|
||||||
else if (c == 'u')
|
else if (c == 'u')
|
||||||
@ -88,27 +95,35 @@ size_t writef(string s, va_list args, ch_out_fn ch_out)
|
|||||||
*
|
*
|
||||||
* @param v Value to format.
|
* @param v Value to format.
|
||||||
* @param upper Whether to use uppercase letters.
|
* @param upper Whether to use uppercase letters.
|
||||||
|
* @param pointer Whether to format as a pointer.
|
||||||
* @param pad Pad character to use.
|
* @param pad Pad character to use.
|
||||||
* @param width Field width.
|
* @param width Field width.
|
||||||
* @param ch_out Character output function.
|
* @param ch_out Character output function.
|
||||||
*
|
*
|
||||||
* @return Number of characters written.
|
* @return Number of characters written.
|
||||||
*/
|
*/
|
||||||
private size_t write_hex(ulong v, bool upper, char pad, size_t width, ch_out_fn ch_out)
|
private size_t write_hex(ulong v, bool upper, bool pointer, char pad, size_t width, ch_out_fn ch_out)
|
||||||
{
|
{
|
||||||
static __gshared const(char)[16] hex_chars_lower = "0123456789abcdef";
|
static __gshared const(char)[16] hex_chars_lower = "0123456789abcdef";
|
||||||
static __gshared const(char)[16] hex_chars_upper = "0123456789ABCDEF";
|
static __gshared const(char)[16] hex_chars_upper = "0123456789ABCDEF";
|
||||||
char[16] buf;
|
char[19] buf;
|
||||||
size_t i;
|
size_t digit;
|
||||||
|
size_t buf_idx;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
ulong n = v & 0xFu;
|
ulong n = v & 0xFu;
|
||||||
buf[i] = upper ? hex_chars_upper[n] : hex_chars_lower[n];
|
buf[buf_idx] = upper ? hex_chars_upper[n] : hex_chars_lower[n];
|
||||||
v >>= 4u;
|
v >>= 4u;
|
||||||
i++;
|
buf_idx++;
|
||||||
|
digit++;
|
||||||
|
if (pointer && ((digit % 4) == 0) && (v != 0u))
|
||||||
|
{
|
||||||
|
buf[buf_idx] = '_';
|
||||||
|
buf_idx++;
|
||||||
|
}
|
||||||
} while (v != 0u);
|
} while (v != 0u);
|
||||||
size_t length_written = i;
|
size_t length_written = buf_idx;
|
||||||
while (width > i)
|
while (width > buf_idx)
|
||||||
{
|
{
|
||||||
ch_out(pad);
|
ch_out(pad);
|
||||||
width--;
|
width--;
|
||||||
@ -116,9 +131,9 @@ private size_t write_hex(ulong v, bool upper, char pad, size_t width, ch_out_fn
|
|||||||
}
|
}
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
i--;
|
buf_idx--;
|
||||||
ch_out(buf[i]);
|
ch_out(buf[buf_idx]);
|
||||||
} while (i != 0u);
|
} while (buf_idx != 0u);
|
||||||
return length_written;
|
return length_written;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user