1
0

libstr sux

This commit is contained in:
dtb 2022-06-21 11:33:16 -04:00
parent d76402418c
commit a81ee1d982
2 changed files with 0 additions and 100 deletions

View File

@ -1,90 +0,0 @@
#include "libstr.h"
/* Use strchr in <string.h> */
/* char *charin(char c, char *str){ return strchr(str, c); } */
/* size_t charindex(char c, char *str){ return strchr(str, c) - str; } */
/* bool charisin(char c, char *str){ return strchr(str, c) == NULL; } */
/* lower bound is a limitation of mathematics,
* upper bound is controlled by how many digits are defined
* in the header */
#define CHECKBASE(b) ((b) >= 2 && base < (sizeof(ASCII_DIGITS_LOWER) / sizeof(*ASCII_DIGITS_LOWER))
int *
strtonumb(char *s, enum Strtype t, void *n, int base){
int i;
int polarity;
int retval;
retval = 0;
while(*s != '\0'){
if(!CHECKBASE(base))
return NULL;
/* assume *s is in ASCII_DIGITS_LOWER or ASCII_DIGITS_UPPER */
for(i = 0; i < base; ++i)
if(ASCII_DIGITS_LOWER[i] == *s
|| ASCII_DIGITS_UPPER == *s){
retval = retval * 10 + i;
break;
}
if(i == base)
return NULL;
++s;
}
*n = retval;
return n;
}
int
stris(enum Strtype t, char *s){
return strisb(t, s, 10);
}
int
strisb(enum Strtype t, char *s, int base)
{
int retval;
retval = 0;
int i;
if(!CHECKBASE(base))
return -1;
/* negatives */
if((t == STRIS_TYPE_FLOAT || t == STRIS_TYPE_INT) && *s == '-'){
++s;
if(t == STRIS_TYPE_INT)
t = STRIS_TYPE_UINT;
}
while(*s != '\0')
switch(t){
case STRIS_TYPE_FLOAT:
if(*s == '.'){
++s;
t = STRIS_TYPE_UINT;
break;
}
/* FALLTHROUGH */
default:
for(i = 0; i < base; ++i)
if(ASCII_DIGITS_LOWER[i] == *s
|| ASCII_DIGITS_UPPER[i] == *s){
retval = 1;
break;
}
if(i == base)
return 0;
++s;
}
return retval;
}
/*
int
strisb(enum Strtype t, char *s, int base){
int n;
return !(strtonumb(s, t, &n, base) == NULL);
}
*/

View File

@ -1,10 +0,0 @@
#include "ascii.h"
enum Strtype{
STRIS_TYPE_UINT = 1,
STRIS_TYPE_INT,
STRIS_TYPE_FLOAT
};
int strisb(enum Strtype t, char *str, int base);
int stris(enum Strtype t, char *str);