需要实现strdup,strnicmp和stricmp [英] Need to implement strdup, strnicmp and stricmp

查看:183
本文介绍了需要实现strdup,strnicmp和stricmp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在操作系统中使用strdup,strnicmp和stricmp,而不是
在OS的string.h函数中有一个实现。有人有这些功能的实现吗?你可以发贴它们吗。


谢谢

Jami

解决方案

" jamihuq" < JA ********* @ yahoo.com>写道:

我正在尝试在操作系统中使用strdup,strnicmp和stricmp,它们在OS的string.h函数中没有实现。有人有这些功能的实现,你可以发布它们吗。



http://www.debian.org/
http://www.netbsd.org/
http: //www.opensolaris.org/
http://www.freebsd .org /
http://www.gentoo.org/
http://www.openbsd.org/


jamihuq写道:

我正在尝试在没有实现的操作系统中使用strdup,strnicmp和stricmp在OSs string.h函数中。有人有这些功能的实现吗?你可以发布它们吗。




glibc似乎实现了strdup,虽然它不应该对

自己编写。我会从名字中猜出其他两个人应该是

进行与案例无关的比较。循环通过tolower()首先

然后做一个正常的strcmp / strncmp会猜测(虽然

可能更慢,需要一个重复的步骤来做非破坏性的

与区域设置比较相比)。


-

imalone


< blockquote> jamihuq写道:


我正在尝试在操作系统中使用strdup,strnicmp和stricmp,这些操作系统在OS中没有实现string.h功能。有人有这些功能的实现吗,你可以发帖吗。




这是我不区分大小写的版本的当前形式

strcmp和strncmp:


int str_ccmp(const char * s1,const char * s2)

{

for(;;){

if(* s1!= * s2){

int c1 = toupper((unsigned char)* s1);

int c2 = toupper((unsigned char)* s2);


if(c2!= c1){

返回c2> c1? -1:1;

}

}否则{

if(* s1 ==''\'''){

返回0;

}

}

++ s1;

++ s2 ;

}

}


int str_cncmp(const char * s1,const char * s2,size_t n)

{

for(;;){

if(n-- == 0){

返回0;

}

if(* s1!= * s2){

int c1 = toupper((unsigned char)* s1);

int c2 = toupper((unsigned char)* s2);


if(c2!= c1){

返回c2> c1? -1:1;

}

}否则{

if(* s1 ==''\'''){

返回0;

}

}

++ s1;

++ s2 ;

}

}


-

pete


I''m trying to use strdup, strnicmp and stricmp in an OS that doesn''t
have an implementation in the OSs string.h function. Does someone have
the implementation for these functions and can you please post them.

Thanks
Jami

解决方案

"jamihuq" <ja*********@yahoo.com> wrote:

I''m trying to use strdup, strnicmp and stricmp in an OS that doesn''t
have an implementation in the OSs string.h function. Does someone have
the implementation for these functions and can you please post them.



http://www.debian.org/
http://www.netbsd.org/
http://www.opensolaris.org/
http://www.freebsd.org/
http://www.gentoo.org/
http://www.openbsd.org/


jamihuq wrote:

I''m trying to use strdup, strnicmp and stricmp in an OS that doesn''t
have an implementation in the OSs string.h function. Does someone have
the implementation for these functions and can you please post them.



glibc appears to implement strdup, although it shouldn''t be taxing to
write your own. I''ll guess from the names the other two are supposed
to be case independent comparisons. Looping through tolower() first
then doing a normal strcmp/strncmp would work at a guess (although
probably slower and needs a duplicating step to do non-destructively
compared to a locale aware comparison).

--
imalone


jamihuq wrote:


I''m trying to use strdup, strnicmp and stricmp in an OS that doesn''t
have an implementation in the OSs string.h function. Does someone have
the implementation for these functions and can you please post them.



This is the current form of my case insensitive versions
of strcmp and strncmp:

int str_ccmp(const char *s1, const char *s2)
{
for (;;) {
if (*s1 != *s2) {
int c1 = toupper((unsigned char)*s1);
int c2 = toupper((unsigned char)*s2);

if (c2 != c1) {
return c2 > c1 ? -1 : 1;
}
} else {
if (*s1 == ''\0'') {
return 0;
}
}
++s1;
++s2;
}
}

int str_cncmp(const char *s1, const char *s2, size_t n)
{
for (;;) {
if (n-- == 0) {
return 0;
}
if (*s1 != *s2) {
int c1 = toupper((unsigned char)*s1);
int c2 = toupper((unsigned char)*s2);

if (c2 != c1) {
return c2 > c1 ? -1 : 1;
}
} else {
if (*s1 == ''\0'') {
return 0;
}
}
++s1;
++s2;
}
}

--
pete


这篇关于需要实现strdup,strnicmp和stricmp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆