回复:D.E。 Knuth的strlen [英] Re: D.E. Knuth's strlen

查看:47
本文介绍了回复:D.E。 Knuth的strlen的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jacob navia< ja *** @ nospam.comwrites:

[...]


Knuth''s strlen然后,看起来像这样。


#include< stdio.h>

#include< string.h>

#define H 0x8080808080808080ULL

#define L 0x0101010101010101ULL



H和L仅在myStrlen中使用;我会在

函数中声明它们。是的,我知道宏定义不是那种方式,但

它对文档很有用。


size_t myStrlen( char * s)

{



[snip]


}


#ifdef TEST

int main(int argc,char * argv [])

{

char * str ="懒惰的狐狸跳过慢狗;


if(argc 1){

str = argv [1];

}

printf(

"'%s''的strlen'是%d(%d)\ n",

str,strlen(str),myStrlen(str));



你正在使用%d格式的size_t值。


我会添加一个 ;返回0;在这里,即使在C99中没有必要严格的



}

#endif



正如我提到的那样,由于对齐问题,算法(我在此处剪断)不是可移植的,我相信有''无法制作

100%便携式。但是* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *当然,strlen()的

实现不必用100%便携式写入

C;它可以使用它喜欢的任何系统特定的技巧。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

诺基亚

" ;我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

解决方案

Keith Thompson写道:


jacob navia< ja *** @ nospam.comwrites:

[...]


> Knuth'的strlen然后,看起来像这样。

#include< stdio.h>
#include< string.h>
#define H 0x8080808080808080ULL
#define L 0x0101010101010101ULL



H和L仅在myStrlen中使用;我会在

函数中声明它们。是的,我知道宏定义不是那样的,但

它对文档很有用。



所以制作它们const unsigned long long。


-

Ian Collins


Ian Collins< ia * *****@hotmail.comwrites:


Keith Thompson写道:


> jacob navia < ja *** @ nospam.com写道:
[...]


>> Knuth'的strlen然后,看起来像这样..................................................... / blockquote>
H和L仅在myStrlen中使用;我会在
函数中声明它们。是的,我知道宏定义不是那种方式,但是它对文档很有用。



所以让它们为const unsigned long long 。



当然可以。有些情况下,const对象的声明

不能替换宏定义;我应该已经意识到这不是其中之一。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

诺基亚

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长


jacob navia <ja***@nospam.comwrites:
[...]

Knuth''s strlen then, looks like this.

#include <stdio.h>
#include <string.h>
#define H 0x8080808080808080ULL
#define L 0x0101010101010101ULL

H and L are used only within myStrlen; I''d declare them inside the
function. Yes, I know macro definitions aren''t scoped that way, but
it''s useful for documentation.

size_t myStrlen(char *s)
{

[snip]

}

#ifdef TEST
int main(int argc,char *argv[])
{
char *str = "The lazy fox jumped over the slow dog";

if (argc 1) {
str = argv[1];
}
printf(
"Strlen of ''%s'' is %d (%d)\n",
str,strlen(str),myStrlen(str));

You''re using a %d format for a size_t value.

And I''d add a "return 0;" here, even though it isn''t strictly
necessary in C99.

}
#endif

As I mentioned elsethread, the algorithm (which I snipped here) is not
portable due to alignment issues, and I believe there''s no way to make
it 100% portable. But there are *mostly* portable tricks that can
detect pointer alignment on *most* systems (and testing can detect
systems where the tricks don''t work). And, of course, an
implementation of strlen() doesn''t have to be written in 100% portable
C; it can use whatever system-specific tricks it likes.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

解决方案

Keith Thompson wrote:

jacob navia <ja***@nospam.comwrites:
[...]

>Knuth''s strlen then, looks like this.

#include <stdio.h>
#include <string.h>
#define H 0x8080808080808080ULL
#define L 0x0101010101010101ULL


H and L are used only within myStrlen; I''d declare them inside the
function. Yes, I know macro definitions aren''t scoped that way, but
it''s useful for documentation.

So make them const unsigned long long.

--
Ian Collins


Ian Collins <ia******@hotmail.comwrites:

Keith Thompson wrote:

>jacob navia <ja***@nospam.comwrites:
[...]

>>Knuth''s strlen then, looks like this.

#include <stdio.h>
#include <string.h>
#define H 0x8080808080808080ULL
#define L 0x0101010101010101ULL


H and L are used only within myStrlen; I''d declare them inside the
function. Yes, I know macro definitions aren''t scoped that way, but
it''s useful for documentation.

So make them const unsigned long long.

Yes, of course. There are cases where a declaration of a const object
can''t replace a macro definition; I should have realized this isn''t
one of them.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


这篇关于回复:D.E。 Knuth的strlen的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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