mirror of
https://git.stationery.faith/corn/corn.git
synced 2024-11-22 06:32:18 +00:00
better libs uwu
This commit is contained in:
parent
7bbdccc05c
commit
1a3c3cacc3
2 changed files with 94 additions and 43 deletions
109
include/lib.h
109
include/lib.h
|
@ -34,27 +34,106 @@ extern int strncmp(const char *restrict s1, const char *restrict s2, unsigned lo
|
||||||
/**
|
/**
|
||||||
* @returns 1 if c is a space
|
* @returns 1 if c is a space
|
||||||
*/
|
*/
|
||||||
int isspace(int c);
|
extern int isspace(int c);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @returns 1 if c is a digit (0 - 9)
|
* @returns 1 if c is a digit (0 - 9)
|
||||||
*/
|
*/
|
||||||
int isdigit(int c);
|
extern int isdigit(int c);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* converts single digit int to base 36
|
||||||
|
* @param i - int
|
||||||
|
* @returns c - base 36 char
|
||||||
|
*/
|
||||||
|
extern char itoc(int i);
|
||||||
|
|
||||||
char itoc(int i);
|
/**
|
||||||
int ctoi(char c);
|
* converts single base 36 chat into int
|
||||||
|
* @param c - base 36 char
|
||||||
|
* @returns i - int
|
||||||
|
*/
|
||||||
|
extern int ctoi(char c);
|
||||||
|
|
||||||
int atoi(const char* s);
|
/**
|
||||||
long int atol(const char* s);
|
* Converts the initial portiion of the string pointed to by s to int.
|
||||||
long long int atoll(const char* s);
|
* @param s - the string to convert
|
||||||
|
* @returns the number inside s or 0 on error
|
||||||
|
*/
|
||||||
|
extern int atoi(const char* s);
|
||||||
|
|
||||||
char *itoa(int n, char *buffer, int radix);
|
/**
|
||||||
char *ltoa(long int n, char *buffer, int radix);
|
* Converts the initial portiion of the string pointed to by s to long.
|
||||||
char *utoa(unsigned int n, char *buffer, int radix);
|
* @param s - the string to convert
|
||||||
char *ultoa(unsigned long int n, char *buffer, int radix);
|
* @returns the number inside s or 0 on error
|
||||||
char *ftoa(float f, char *buffer);
|
*/
|
||||||
|
extern long int atol(const char* s);
|
||||||
|
|
||||||
int strtoi(const char *str, char **endptr, int base);
|
/**
|
||||||
long int strtol(const char *str, char **endptr, int base);
|
* Converts the initial portiion of the string pointed to by s to long long.
|
||||||
long long int strtoll(const char *str, char **endptr, int base);
|
* @param s - the string to convert
|
||||||
|
* @returns the number inside s or 0 on error
|
||||||
|
*/
|
||||||
|
extern long long int atoll(const char* s);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a integer to asci inside a string with a given radix (base).
|
||||||
|
* @param n - the number to conver
|
||||||
|
* @param buffer - the string buffer
|
||||||
|
* @param radix - the base to convert
|
||||||
|
*/
|
||||||
|
extern char *itoa(int n, char *buffer, int radix);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a long to asci inside a string with a given radix (base).
|
||||||
|
* @param n - the number to conver
|
||||||
|
* @param buffer - the string buffer
|
||||||
|
* @param radix - the base to convert
|
||||||
|
*/
|
||||||
|
extern char *ltoa(long int n, char *buffer, int radix);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a unsigned integer to asci inside a string with a given radix (base).
|
||||||
|
* @param n - the number to conver
|
||||||
|
* @param buffer - the string buffer
|
||||||
|
* @param radix - the base to convert
|
||||||
|
*/
|
||||||
|
extern char *utoa(unsigned int n, char *buffer, int radix);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a unsigned long to asci inside a string with a given radix (base).
|
||||||
|
* @param n - the number to conver
|
||||||
|
* @param buffer - the string buffer
|
||||||
|
* @param radix - the base to convert
|
||||||
|
*/
|
||||||
|
extern char *ultoa(unsigned long int n, char *buffer, int radix);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts the string in str to an int value based on the given base.
|
||||||
|
* The endptr is updated to where the string was no longer valid.
|
||||||
|
* @param str - the string buffer
|
||||||
|
* @param endptr - the endptr
|
||||||
|
* @param base - the base to convert to
|
||||||
|
* @returns 0 on error or success, error if endptr is still equal to str
|
||||||
|
*/
|
||||||
|
extern int strtoi(const char *str, char **endptr, int base);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts the string in str to an long value based on the given base.
|
||||||
|
* The endptr is updated to where the string was no longer valid.
|
||||||
|
* @param str - the string buffer
|
||||||
|
* @param endptr - the endptr
|
||||||
|
* @param base - the base to convert to
|
||||||
|
* @returns 0 on error or success, error if endptr is still equal to str
|
||||||
|
*/
|
||||||
|
extern long int strtol(const char *str, char **endptr, int base);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts the string in str to an long long value based on the given base.
|
||||||
|
* The endptr is updated to where the string was no longer valid.
|
||||||
|
* @param str - the string buffer
|
||||||
|
* @param endptr - the endptr
|
||||||
|
* @param base - the base to convert to
|
||||||
|
* @returns 0 on error or success, error if endptr is still equal to str
|
||||||
|
*/
|
||||||
|
extern long long int strtoll(const char *str, char **endptr, int base);
|
||||||
|
|
28
src/lib.c
28
src/lib.c
|
@ -155,34 +155,6 @@ UXTOA(long int, ultoa)
|
||||||
XTOA(int, itoa)
|
XTOA(int, itoa)
|
||||||
XTOA(long int, ltoa)
|
XTOA(long int, ltoa)
|
||||||
|
|
||||||
char *ftoa(float f, char *buffer) {
|
|
||||||
char *b = buffer;
|
|
||||||
if (f < 0 || f == -0)
|
|
||||||
*buffer++ = '-';
|
|
||||||
int n, i=0, k=0;
|
|
||||||
n = (int) f;
|
|
||||||
while (n > 0) {
|
|
||||||
f /= 10;
|
|
||||||
n = (int)f;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
*(buffer+i) = '.';
|
|
||||||
f *= 10;
|
|
||||||
n = (int) f;
|
|
||||||
f -= n;
|
|
||||||
while ((n > 0.1) || (i > k)) {
|
|
||||||
if (k == i)
|
|
||||||
k++;
|
|
||||||
*(buffer+k) = '0' + n;
|
|
||||||
f *= 10;
|
|
||||||
n = (int) f;
|
|
||||||
f -= n;
|
|
||||||
k++;
|
|
||||||
}
|
|
||||||
*(buffer+k) = '\0';
|
|
||||||
return b;
|
|
||||||
}
|
|
||||||
|
|
||||||
#define STRTOX(name, type) \
|
#define STRTOX(name, type) \
|
||||||
type name(const char *s, char **endptr, int radix) { \
|
type name(const char *s, char **endptr, int radix) { \
|
||||||
*endptr = NULL; \
|
*endptr = NULL; \
|
||||||
|
|
Loading…
Reference in a new issue