Skip to content
This repository has been archived by the owner on Feb 25, 2023. It is now read-only.

Commit

Permalink
allow printing negative integers in cc_print (twitter#141)
Browse files Browse the repository at this point in the history
* allow printing negative integers in cc_print

* fix overflow problem with llabs

* pick up seppo0010's optimization
  • Loading branch information
thinkingfish authored Mar 17, 2017
1 parent ab0edc8 commit ce0b9ea
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/cc_print.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ extern "C" {

/* behavior undefined if there isn't enough space in buf */
size_t cc_print_uint64_unsafe(char *buf, uint64_t n);
size_t cc_print_int64_unsafe(char *buf, int64_t n);

size_t cc_print_uint64(char *buf, size_t size, uint64_t n);
size_t cc_print_int64(char *buf, size_t size, int64_t n);

size_t _scnprintf(char *buf, size_t size, const char *fmt, ...);
size_t _vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
Expand Down
4 changes: 4 additions & 0 deletions include/cc_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,17 @@ extern "C" {
* # define UINT16_MAX (65535)
* # define UINT32_MAX (4294967295U)
* # define UINT64_MAX (__UINT64_C(18446744073709551615))
*
* # define INT64_MIN -9223372036854775808LL
*/
#define CC_UINT8_MAXLEN (3 + 1)
#define CC_UINT16_MAXLEN (5 + 1)
#define CC_UINT32_MAXLEN (10 + 1)
#define CC_UINT64_MAXLEN (20 + 1)
#define CC_UINTMAX_MAXLEN CC_UINT64_MAXLEN

#define CC_INT64_MAXLEN (1 + 19 + 1)

/* alignment */
/* Make data 'd' or pointer 'p', n-byte aligned, where n is a power of 2 */
#define CC_ALIGNMENT sizeof(unsigned long) /* platform word */
Expand Down
40 changes: 40 additions & 0 deletions src/cc_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
* implementation as a reference (folly/Conv.h)
*/

/* use our own macro instead of llabs() to make sure it works with INT64_MIN */
#define abs_int64(_x) ((_x) >= 0 ? (_x) : -(_x))

static inline void
_print_uint64(char *buf, size_t d, uint64_t n)
{
Expand All @@ -46,6 +49,22 @@ cc_print_uint64_unsafe(char *buf, uint64_t n)
return d;
}

size_t
cc_print_int64_unsafe(char *buf, int64_t n)
{
size_t d;
uint64_t ab = abs_int64(n);

if (n < 0) {
*buf++ = '-';
}

d = digits(ab);
_print_uint64(buf, d, n);

return (n < 0) ? d + 1 : d;
}

size_t
cc_print_uint64(char *buf, size_t size, uint64_t n)
{
Expand All @@ -61,6 +80,27 @@ cc_print_uint64(char *buf, size_t size, uint64_t n)
return d;
}

size_t
cc_print_int64(char *buf, size_t size, int64_t n)
{
size_t d;
uint64_t ab = abs_int64(n);

d = digits(ab) + (n < 0);
if (size < d) {
return 0;
}

if (n < 0) {
*buf++ = '-';
d--;
}

_print_uint64(buf, d, n);

return (n < 0) ? d + 1 : d;
}

size_t
_vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
{
Expand Down

0 comments on commit ce0b9ea

Please sign in to comment.