Files
checker_host/elec/mystring.cpp
2023-11-27 14:31:00 +08:00

129 lines
1.6 KiB
C++

/*
*
* 把整数字符串传化为int,直到遇到非数字字符
*
*/
static int str_ainttoi(const char *s)
{
int ret=0;
int sig=1;
if(*s=='-'){
s++;
sig=-1;
}
while(*s)
{
if(*s>='0'&&*s<='9')
{
ret*=10;
ret+=*s-'0';
}
else return ret;
s++;
}
return ret*sig;
}
int str_ahextoi(const char *s)
{
int ret=0;
while(*s)
{
if(*s>='0'&&*s<='9')
{
ret*=16;
ret+=*s-'0';
}
else if(*s>='a'&&*s<='f')
{
ret*=16;
ret+=*s-'a'+10;
}
else if(*s>='A'&&*s<='F')
{
ret*=16;
ret+=*s-'A'+10;
}
else return ret;
s++;
}
return ret;
}
int str_atoi(const char *s)
{
if(s[0]=='0'&&((s[1]=='x')||(s[1]=='X'))){
return str_ahextoi(&s[2]);
}else{
return str_ainttoi(s);
}
}
/*
*
* 从左向右找到字符串s中首次出现字符c的指针,没找到返回0
* 例如 char *s=str_find_char_right("abcdef",'c')
* s="cdef"
*
*/
const char *str_find_char_right(const char *s,char c)
{
while(*s){
if(*s==c) return s;
s++;
}
return 0;
}
/*
*
* 判断字符是否是空白字符,是返回1,不是返回0
*
*/
static inline int str_is_empty_char(char c)
{
const char table[]="\t\n\v\f\r ";
if(str_find_char_right(table,c)!=0)
return 1;
else
return 0;
}
/*
*
* 判断字符串是否是可打印,是返回1,不是返回0
*
*/
int str_is_print_str(const char *str,int len)
{
if(len==0){
return 0;
}
for(int i=0;i<len;i++)
{
// 既不是空白字符也不是可打印字符
if(!(str_is_empty_char(str[i])||(str[i]>=' '&&str[i]<='~')))
return 0;
}
return 1;
}