⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 textsearch.c

📁 非常快的字符搜索算法。注:上传时
💻 C
字号:
/*

	Super fast linear text search algorithms:
	searchi = search ignore case
	search = search case sensitive
	searchiw = search ignore case words only (e.g. words delimited by whitespace only,
				not words within words)
	searchw() = search case sensitive words only
	
	All functions return the number of matches for keyword in buffer, or -1 on error.

	by James Buchanan
	No license ristrictions on this code.
	
	Email: jamesb@northnet.com.au

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "textsearch.h"

int searchi(const char *buffer, const char *keyword)
{
	int k_len, b_len, ch_matches, found, i, j;
	b_len = strlen(buffer);
	k_len = strlen(keyword);
	if (!b_len || !k_len)
		return -1;
	ch_matches = found = 0;
	for (i=0; i<b_len-k_len; i++) {
		ch_matches = 0;
		for (j=0; j<k_len; j++) {
			if (tolower(buffer[i+j]) == tolower(keyword[j])) {
				ch_matches++;
				if (ch_matches == k_len) {
					found++;	
					i += k_len;
				}
			}		
		}	
	}
	return found; 	
}

int search(const char *buffer, const char *keyword)
{
	int k_len, b_len, ch_matches, found, i, j;
	b_len = strlen(buffer);
	k_len = strlen(keyword);
	if (!b_len || !k_len)
		return -1;
	ch_matches = found = 0;
	for (i=0; i<b_len-k_len; i++) {
		ch_matches = 0;
		for (j=0; j<k_len; j++) {
			if (buffer[i+j] == keyword[j]) {
				ch_matches++;
				if (ch_matches == k_len) {
					found++;	
					i += k_len;
				}
			}		
		}	
	}
	return found;	
}

int searchiw(const char *buffer, const char *keyword)
{
	int k_len, b_len, ch_matches, found, i, j;
	char *temp_keyword;
	b_len = strlen(buffer);
	k_len = strlen(keyword);
	if (b_len < 2 || k_len < 2)		/* Useless, for words only */
		return -1;
	if (keyword[0] != ' ' && keyword[k_len-1] != ' ') {
		temp_keyword = (char *)calloc(k_len+3, sizeof(char));
		if (!temp_keyword)
			return -1;
		temp_keyword[0] = ' ';
		strcat(temp_keyword, keyword);
		strcat(temp_keyword, " ");	
	}
	else if (keyword[0] == ' ' && keyword[k_len-1] != ' ') {
		temp_keyword = (char *)calloc(k_len+2, sizeof(char));
		if (!temp_keyword)
			return -1;
		strcat(temp_keyword, keyword);
		strcat(temp_keyword, " ");
	}
	else if (keyword[0] != ' ' && keyword[k_len-1] == ' ') {
		temp_keyword = (char *)calloc(k_len+2, sizeof(char));
		if (!temp_keyword)
			return -1;
		temp_keyword[0] = ' ';
		strcat(temp_keyword, keyword);
	}	
	else {
		/* If we get to here and no if statement has executed, keyword already has whitespaces
			surrounding it */
		temp_keyword = (char *)calloc(k_len+1, sizeof(char));
		if (!temp_keyword)
			return -1;
		strcpy(temp_keyword, keyword);
	}
	ch_matches = found = 0;
	k_len = strlen(temp_keyword);	/* Calculate new string length */
	for (i=0; i<b_len-k_len; i++) {
		ch_matches = 0;
		for (j=0; j<k_len; j++) {
			if (buffer[i+j] == temp_keyword[j]) {
				ch_matches++;
				if (ch_matches == k_len) {
					found++;	
					i += k_len;
				}
			}		
		}	
	}
	if (temp_keyword != NULL)
		free(temp_keyword);
	return found;	
}

int searchw(const char *buffer, const char *keyword)
{
	int k_len, b_len, ch_matches, found, i, j;
	char *temp_keyword;
	b_len = strlen(buffer);
	k_len = strlen(keyword);
	if (b_len < 2 || k_len < 2)		/* Useless, for words only */
		return -1;
	if (keyword[0] != ' ' && keyword[k_len-1] != ' ') {
		temp_keyword = (char *)calloc(k_len+3, sizeof(char));
		if (!temp_keyword)
			return -1;
		temp_keyword[0] = ' ';
		strcat(temp_keyword, keyword);
		strcat(temp_keyword, " ");	
	}
	else if (keyword[0] == ' ' && keyword[k_len-1] != ' ') {
		temp_keyword = (char *)calloc(k_len+2, sizeof(char));
		if (!temp_keyword)
			return -1;
		strcat(temp_keyword, keyword);
		strcat(temp_keyword, " ");
	}
	else if (keyword[0] != ' ' && keyword[k_len-1] == ' ') {
		temp_keyword = (char *)calloc(k_len+2, sizeof(char));
		if (!temp_keyword)
			return -1;
		temp_keyword[0] = ' ';
		strcat(temp_keyword, keyword);
	}	
	else {
		/* If we get to here and no if statement has executed, keyword already has whitespaces
			surrounding it */
		temp_keyword = (char *)calloc(k_len+1, sizeof(char));
		if (!temp_keyword)
			return -1;
		strcpy(temp_keyword, keyword);
	}
	ch_matches = found = 0;
	k_len = strlen(temp_keyword);	/* Calculate new string length */
	for (i=0; i<b_len-k_len; i++) {
		ch_matches = 0;
		for (j=0; j<k_len; j++) {
			if (buffer[i+j] == temp_keyword[j]) {
				ch_matches++;
				if (ch_matches == k_len) {
					found++;	
					i += k_len;
				}
			}		
		}	
	}
	if (temp_keyword != NULL)
		free(temp_keyword);
	return found;	
}



⌨️ 快捷键说明

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