Added bin/oct/hex string->integer conversions

This commit is contained in:
Justin Ethier 2016-02-19 21:59:10 -05:00
parent 3ce0ff6936
commit 46e15ceffc
2 changed files with 40 additions and 0 deletions

View file

@ -154,6 +154,9 @@ object Cyc_symbol2string(void *d, object cont, object sym) ;
object Cyc_string2symbol(void *d, object str);
object Cyc_list2string(void *d, object cont, object lst);
common_type Cyc_string2number(void *d, object str);
int binstr2int(const char *str);
int octstr2int(const char *str);
int hexstr2int(const char *str);
object Cyc_string_append(void *data, object cont, int argc, object str1, ...);
integer_type Cyc_string_length(void *data, object str);
object Cyc_substring(void *data, object cont, object str, object start, object end);

View file

@ -1057,6 +1057,43 @@ common_type Cyc_string2number(void *data, object str){
return result;
}
int binstr2int(const char *str)
{
int num = 0;
while (*str) {
num <<= 1;
if (*str++ == '1') num++;
}
return num;
}
int octstr2int(const char *str)
{
int num = 0;
while (*str) {
num <<= 3;
num += ((*str++) - '0');
}
return num;
}
int hexstr2int(const char *str)
{
int num = 0;
while (*str) {
num <<= 4;
if (*str >= 'A' && *str <= 'F'){
num += (((*str) - 'A') + 10);
} else if (*str >= 'a' && *str <= 'f'){
num += (((*str) - 'a') + 10);
} else {
num += ((*str) - '0');
}
*str++;
}
return num;
}
integer_type Cyc_string_cmp(void *data, object str1, object str2) {
Cyc_check_str(data, str1);
Cyc_check_str(data, str2);