ref: 0964650171a5444e6dd63752e8986725fd68a5ab
parent: 91bae667fa4bb7089069694306e62b7b845560f5
author: mkf <mkf@cloud9p.org>
date: Fri Nov 24 03:47:45 EST 2023
libc/itoa: add into libc.h, style updates
--- a/include/libc.h
+++ b/include/libc.h
@@ -19,4 +19,6 @@
int lldel(ll_t*);
int printf(const char* restrict format, ...);
-int scanf (char * str, ...);
\ No newline at end of file
+int scanf (char * str, ...);
+
+char* itoa(int value, int base);
--- a/libc/itoa.c
+++ b/libc/itoa.c
@@ -1,39 +1,39 @@
char*
-itoa( int value, int base )
+itoa(int value, int base )
{
- char *rc;
- char *ptr;
- char *low;
- char *str;
- // Check for supported base.
- if ( base < 2 || base > 36 )
- {
- *str = '\0';
- return str;
- }
- rc = ptr = str;
- // Set '-' for negative decimals.
- if ( value < 0 && base == 10 )
- {
- *ptr++ = '-';
- }
- // Remember where the numbers start.
- low = ptr;
- // The actual conversion.
- do
- {
- // Modulo is negative for negative value. This trick makes abs() unnecessary.
- *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + value % base];
- value /= base;
- } while ( value );
- // Terminating the string.
- *ptr-- = '\0';
- // Invert the numbers.
- while ( low < ptr )
- {
- char tmp = *low;
- *low++ = *ptr;
- *ptr-- = tmp;
- }
- return rc;
-}
\ No newline at end of file
+ char *rc;
+ char *ptr;
+ char *low;
+ char *str;
+ // Check for supported base.
+ if ( base < 2 || base > 36 )
+ {
+ *str = '\0';
+ return str;
+ }
+ rc = ptr = str;
+ // Set '-' for negative decimals.
+ if ( value < 0 && base == 10 )
+ {
+ *ptr++ = '-';
+ }
+ // Remember where the numbers start.
+ low = ptr;
+ // The actual conversion.
+ do
+ {
+ // Modulo is negative for negative value. This trick makes abs() unnecessary.
+ *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + value % base];
+ value /= base;
+ } while ( value );
+ // Terminating the string.
+ *ptr-- = '\0';
+ // Invert the numbers.
+ while ( low < ptr )
+ {
+ char tmp = *low;
+ *low++ = *ptr;
+ *ptr-- = tmp;
+ }
+ return rc;
+}