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

📄 string.c

📁 这是一个简单的C编译器的词法分析和语法分析。
💻 C
字号:
#include "define.h"

#define STR_HASH_SIZE 3907
#define MAX_STRING_LEN 1000
#define MAX_STRING_BLOCK_SIZE 1024*1024*2

struct string{
	char* str;
	int len;
	struct string* next;
}* slot[STR_HASH_SIZE];

static char str_block[MAX_STRING_BLOCK_SIZE];
static char* str_curr = str_block;
static char* str_last = str_block + MAX_STRING_BLOCK_SIZE;


static unsigned long hash_code(char *arKey, unsigned int nKeyLength) 
{ 
	unsigned long h = 0, g; 
	char *arEnd=arKey+nKeyLength; 
	while (arKey < arEnd) { 
		h = (h << 4) + *arKey++; 
		if ((g = (h & 0xF0000000))) { 
			h = h ^ (g >> 24); 
			h = h ^ g; 
		} 
	} 
	return h % STR_HASH_SIZE; 
} 

char* in_str_len( char *str, int len)
{
	int i;
	struct string *p;
	unsigned int hashcode;
	if ( len > MAX_STRING_LEN ){
		error("the string len expired")	;
		exit(-1);
	}
	hashcode = hash_code( str, len );
	p = slot[ hashcode ];
	while ( p )
	{
		if ( len == p->len )
		{
			int i = 0;
			for ( i = 0 ; (i < len ) && ( str[i] == p->str[i] ); i++ ) ;
			if ( i >= len )
				return p->str;
		}
	}
	NEW(p,1);
	if (str_last - str_curr < len)
	{
		error("the string buffer expired\n")	;
		exit(-1);
	}
	p->str = str_curr;
	for ( i = 0; i < len ; i++)
	{	
		*(str_curr++) = str[i];
	}
	p->next = slot[hashcode];
	slot[hashcode] = p;
	return p->str;	
}


char* in_str(char *str)				//This fucntion used to add a string end up with '\0' in tho the system string table
{
	int n=0;
	while( n <= MAX_STRING_LEN && str[n] ) n++;
	if ( n <= MAX_STRING_LEN ){
		return 	in_str_len( str, n );
	}else{
		error("the string len expired")	;
		exit(-1);
	}
	return NULL;
}






⌨️ 快捷键说明

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