tmemlook.c

来自「linux下阅读源码的好工具」· C语言 代码 · 共 52 行

C
52
字号
/* Copyright (c) 1994 Sun Wu, Udi Manber, Burra Gopal.  All Rights Reserved. *//* copy of ./glimpse/index/memlook.c *//* ------------------------------------------------------------------------   this is a brute-force string matching function.   it's used for short text, or the matching place is expected to be   not far from the beginning.   the function returns the position where the pattern first matches.------------------------------------------------------------------------*/#include <stdio.h>#include <string.h>int tmemlook(pattern, text, length)unsigned char *pattern;unsigned char *text;int  length;{	unsigned char *text_end = text+length;	unsigned char *text_begin = text;	register unsigned char pat_0=pattern[0];	register unsigned char *px, *tx;	if(pat_0 == '\n') {		if(strncmp((char *)pattern+1, text, strlen((char *)pattern) -1) == 0) {			return(0);		}	}	/* this is a special case when the pattern is to begin of a line 	   while the text match the pattern right at the beginning, in	   which case, '\n' won't be matched.	*/	pattern++;	*text_end = pat_0 ;	while(text < text_end) {		while(*text++ != pat_0);		if(text < text_end) {			px = pattern; 			tx = text;			while(*px == *tx) {				px++; tx++;			};			if(*px == '\0') {				/*				printf("begin matched\n");				*/				return(text - text_begin);			}		}	}	return(-2);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?