aboutsummaryrefslogtreecommitdiffstats
path: root/erts/emulator/beam/utils.c
diff options
context:
space:
mode:
authorStanislav Mayorov <[email protected]>2018-12-13 11:39:16 +0700
committerJohn Högberg <[email protected]>2019-01-10 09:39:29 +0100
commita56bc7180c639f1f08355f22e22cf539db16982c (patch)
tree5b9549f2cd5c5a9d10dead20c1c40f501dbfa185 /erts/emulator/beam/utils.c
parent6120af3481f9deac2be1ff4bbb2684f24ef172eb (diff)
downloadotp-a56bc7180c639f1f08355f22e22cf539db16982c.tar.gz
otp-a56bc7180c639f1f08355f22e22cf539db16982c.tar.bz2
otp-a56bc7180c639f1f08355f22e22cf539db16982c.zip
Accept base in all integer-printing functions
Diffstat (limited to 'erts/emulator/beam/utils.c')
-rw-r--r--erts/emulator/beam/utils.c55
1 files changed, 36 insertions, 19 deletions
diff --git a/erts/emulator/beam/utils.c b/erts/emulator/beam/utils.c
index d81bd89a48..1a6bcbb66e 100644
--- a/erts/emulator/beam/utils.c
+++ b/erts/emulator/beam/utils.c
@@ -3681,30 +3681,47 @@ erts_unicode_list_to_buf_len(Eterm list)
}
}
-/*
-** Convert an integer to a byte list
-** return pointer to converted stuff (need not to be at start of buf!)
-*/
-char* Sint_to_buf(Sint n, struct Sint_buf *buf)
+/* Prints an integer in the given base, returning the number of digits printed.
+ *
+ * (*buf) is a pointer to the buffer, and is set to the start of the string
+ * when returning. */
+int Sint_to_buf(Sint n, int base, char **buf, size_t buf_size)
{
- char* p = &buf->s[sizeof(buf->s)-1];
- int sign = 0;
-
- *p-- = '\0'; /* null terminate */
- if (n == 0)
- *p-- = '0';
- else if (n < 0) {
- sign = 1;
- n = -n;
+ char *p = &(*buf)[buf_size - 1];
+ int sign = 0, size = 0;
+
+ ASSERT(base >= 2 && base <= 36);
+
+ if (n == 0) {
+ *p-- = '0';
+ size++;
+ } else if (n < 0) {
+ sign = 1;
+ n = -n;
}
while (n != 0) {
- *p-- = (n % 10) + '0';
- n /= 10;
+ int digit = n % base;
+
+ if (digit < 10) {
+ *p-- = '0' + digit;
+ } else {
+ *p-- = 'A' + (digit - 10);
+ }
+
+ size++;
+
+ n /= base;
+ }
+
+ if (sign) {
+ *p-- = '-';
+ size++;
}
- if (sign)
- *p-- = '-';
- return p+1;
+
+ *buf = p + 1;
+
+ return size;
}
/* Build a list of integers in some safe memory area