summaryrefslogtreecommitdiff
path: root/lib/itoa.cc
diff options
context:
space:
mode:
Diffstat (limited to 'lib/itoa.cc')
-rw-r--r--lib/itoa.cc23
1 files changed, 23 insertions, 0 deletions
diff --git a/lib/itoa.cc b/lib/itoa.cc
new file mode 100644
index 0000000..54c927d
--- /dev/null
+++ b/lib/itoa.cc
@@ -0,0 +1,23 @@
+#include "itoa.h"
+
+const char *itoa(long v, int digits)
+{
+ static char buf[INTLENGTH];
+ bool neg = false;
+ if(v < 0) {
+ v = -v;
+ neg = true;
+ }
+ char* ptr = buf + INTLENGTH;
+ *--ptr = '\0';
+ do {
+ *--ptr = (v % 10) + '0';
+ v /= 10;
+ --digits;
+ } while(v != 0);
+ while(digits > 0 && ptr > buf-1)
+ *--ptr = '0', --digits;
+ if(neg)
+ *--ptr = '-';
+ return ptr;
+}