aboutsummaryrefslogtreecommitdiff
path: root/src/String/ToIntS.inl
diff options
context:
space:
mode:
Diffstat (limited to 'src/String/ToIntS.inl')
-rw-r--r--src/String/ToIntS.inl37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/String/ToIntS.inl b/src/String/ToIntS.inl
new file mode 100644
index 0000000..f6ba991
--- /dev/null
+++ b/src/String/ToIntS.inl
@@ -0,0 +1,37 @@
+char tmp[sizeof(value) * CHAR_BIT + 2];
+char *current, *end;
+int sign;
+
+end = tmp + sizeof(tmp);
+current = end;
+sign = 0;
+
+/* Check for sign */
+if (value < 0)
+{
+ sign = 1;
+ value = -value;
+}
+
+/* Fill buffer from the end */
+*(--current) = 0;
+while (value)
+{
+ *(--current) = digits[value % base];
+ value /= base;
+}
+
+/* Append sign */
+if (sign)
+ *(--current) = '-';
+
+/* Check that string have space for the result */
+if (size < (size_t)(end - current))
+ return BH_ERROR;
+
+/* Copy data */
+memcpy(string, current, end - current);
+if (actual)
+ *actual = end - current;
+
+return BH_OK;