谁有strstr函数的实现原码?

ksyou 2003-08-22 08:48:35
能贴出来吗?
...全文
553 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Wolf0403 2003-08-26
  • 打赏
  • 举报
回复
个人比较喜欢加州大学那个:)
第二个人写的这句话太个性了:
You should have at least as much fun trying to understand it, as I had to write it :-).
_______________________________________________________________________
对加州版本的 52-66行的一点修改
for ( ; *string != 0; string ++) {
if (*string != *b) {
continue;
}
a = string;
while ((*a++ == *b++) && (*b != 0))
; // 对照每个字符, 直到 *b 为0 或两者不等
if (*b == 0)
return string;
b = substring;
}
new1mm 2003-08-23
  • 打赏
  • 举报
回复
/* Return the offset of one string within another.
Copyright (C) 1994, 1996, 1997, 2000, 2001 Free Software Foundation, Inc.
This file is part of the GNU C Library.

The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */

/*
* My personal strstr() implementation that beats most other algorithms.
* Until someone tells me otherwise, I assume that this is the
* fastest implementation of strstr() in C.
* I deliberately chose not to comment it. You should have at least
* as much fun trying to understand it, as I had to write it :-).
*
* Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de */

#if HAVE_CONFIG_H
# include <config.h>
#endif

#if defined _LIBC || defined HAVE_STRING_H
# include <string.h>
#endif

typedef unsigned chartype;

#undef strstr

char *
strstr (phaystack, pneedle)
const char *phaystack;
const char *pneedle;
{
const unsigned char *haystack, *needle;
chartype b;
const unsigned char *rneedle;

haystack = (const unsigned char *) phaystack;

if ((b = *(needle = (const unsigned char *) pneedle)))
{
chartype c;
haystack--; /* possible ANSI violation */

{
chartype a;
do
if (!(a = *++haystack))
goto ret0;
while (a != b);
}

if (!(c = *++needle))
goto foundneedle;
++needle;
goto jin;

for (;;)
{
{
chartype a;
if (0)
jin:{
if ((a = *++haystack) == c)
goto crest;
}
else
a = *++haystack;
do
{
for (; a != b; a = *++haystack)
{
if (!a)
goto ret0;
if ((a = *++haystack) == b)
break;
if (!a)
goto ret0;
}
}
while ((a = *++haystack) != c);
}
crest:
{
chartype a;
{
const unsigned char *rhaystack;
if (*(rhaystack = haystack-- + 1) == (a = *(rneedle = needle)))
do
{
if (!a)
goto foundneedle;
if (*++rhaystack != (a = *++needle))
break;
if (!a)
goto foundneedle;
}
while (*++rhaystack == (a = *++needle));
needle = rneedle; /* took the register-poor aproach */
}
if (!a)
break;
}
}
}
foundneedle:
return (char *) haystack;
ret0:
return 0;
}
njuhuangmy 2003-08-22
  • 打赏
  • 举报
回复
我来接分

呵呵
ksyou 2003-08-22
  • 打赏
  • 举报
回复
哈哈,我找到了,拿来和大家共享

1 /*
2 * strstr.c --
3 *
4 * Source code for the "strstr" library routine.
5 *
6 * Copyright (c) 1988-1993 The Regents of the University of California.
7 * Copyright (c) 1994 Sun Microsystems, Inc.
8 *
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 *
12 * RCS: @(#) $Id: strstr.c,v 1.1.1.2 2001/07/09 18:46:58 lim Exp $
13 */
14
15 /*
16 *----------------------------------------------------------------------
17 *
18 * strstr --
19 *
20 * Locate the first instance of a substring in a string.
21 *
22 * Results:
23 * If string contains substring, the return value is the
24 * location of the first matching instance of substring
25 * in string. If string doesn't contain substring, the
26 * return value is 0. Matching is done on an exact
27 * character-for-character basis with no wildcards or special
28 * characters.
29 *
30 * Side effects:
31 * None.
32 *
33 *----------------------------------------------------------------------
34 */
35
36 char *
37 strstr(string, substring)
38 register char *string; /* String to search. */
39 char *substring; /* Substring to try to find in string. */
40 {
41 register char *a, *b;
42
43 /* First scan quickly through the two strings looking for a
44 * single-character match. When it's found, then compare the
45 * rest of the substring.
46 */
47
48 b = substring;
49 if (*b == 0) {
50 return string;
51 }
52 for ( ; *string != 0; string += 1) {
53 if (*string != *b) {
54 continue;
55 }
56 a = string;
57 while (1) {
58 if (*b == 0) {
59 return string;
60 }
61 if (*a++ != *b++) {
62 break;
63 }
64 }
65 b = substring;
66 }
67 return (char *) 0;
68 }
69

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧