aboutsummaryrefslogtreecommitdiffstats
path: root/erts/emulator/beam/utils.c
diff options
context:
space:
mode:
authorJohn Högberg <[email protected]>2019-01-10 09:41:23 +0100
committerJohn Högberg <[email protected]>2019-01-10 09:41:23 +0100
commit78e67434b34813b8efe61f8e8dca445dd12b0f7b (patch)
tree38212f3401c5c5c9d839ca65f906112b1cc51ca6 /erts/emulator/beam/utils.c
parent6120af3481f9deac2be1ff4bbb2684f24ef172eb (diff)
parent46c7a85d70ee6d060a393ca4b22b06eef6fbb324 (diff)
downloadotp-78e67434b34813b8efe61f8e8dca445dd12b0f7b.tar.gz
otp-78e67434b34813b8efe61f8e8dca445dd12b0f7b.tar.bz2
otp-78e67434b34813b8efe61f8e8dca445dd12b0f7b.zip
Merge branch 'john/erts/int_to_list_by_base/OTP-15503/PR-2052' into maint
* john/erts/int_to_list_by_base/OTP-15503/PR-2052: Implement integer_to_list/2 and integer_to_binary/2 as CIFs 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