开源字符处理类库:CharString类 拆分自自己研发的web服务器中的类库

Posted 17岁boy想当攻城狮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了开源字符处理类库:CharString类 拆分自自己研发的web服务器中的类库相关的知识,希望对你有一定的参考价值。

博主目前在做WEB服务器,众所周知WEB服务器最主要的是解析HTTP协议,其中最关键的是字符解析,也就是分割字符来确定每个字段所表达的意思,客户端的意愿!

此类是博主在编写WEB服务器所写出来的,专门用于处理字符串,当然也可以用在其它地方,比如用它来写一个解析json字符串的功能也是绰绰有余!

CharString类:  

//未公开的库,字符进制转换
#include <Shlwapi.h> 
#pragma comment(lib, "shlwapi.lib")
#include <malloc.h>
#pragma warning(disable:4996)	//降低编译器安全警告,使用标准C库一些库函数
//字符类
//宏定义
#define CHARSTRING_FRONT 0x00	//前
#define CHARSTRING_AFTER 0x01	//后
class CharString {
public:
	//构造
	CharString() :m_str(NULL), m_size(0) {}
	CharString(const char* str) {
		if (m_str != NULL || str == NULL) {
			Relete();
		}
		int strSize = strlen(str);
		m_str = (char*)malloc(strSize + 1);	//\\0
		memset(m_str, 0, strSize + 1);
		strcpy(m_str, str);
		GetStrSize();
	}
	CharString(char* str) {
		if (str == NULL){
			return;
		}
		if (m_str != NULL) {
			Relete();
		}
		int strSize = strlen(str);
		m_str = (char*)malloc(strSize + 1);	//\\0
		memset(m_str, 0, strSize + 1);
		strcpy(m_str, str);
		GetStrSize();

	}
	//返回索引指定数据
	char *GetNumStr(int Num) {
		if (Num > GetStrSize()){
			return NULL;
		}
		char *str = (char*)malloc(Num + 1);
		strncpy(str, m_str, Num);
		return str;
	}
	//返回索引数据
	char GetNumChar(int Num) {
		if (Num > GetStrSize()||Num<=0){
			return NULL;
		}
		return m_str[Num-1];
	}
	//return十六进制或十进制,看字符里是什么
	int StrInToHex() {
		int Hex = 0;
		StrToIntEx(m_str, STIF_SUPPORT_HEX, &Hex);
		return Hex;
	}
	//重载
	int StrInToHex(const char* Str) {
		int Hex = 0;
		StrToIntEx(Str, STIF_SUPPORT_HEX, &Hex);
		return Hex;
	}
	//重载
	int StrInToHex(char* Str) {
		int Hex = 0;
		StrToIntEx(Str, STIF_SUPPORT_HEX, &Hex);
		return Hex;
	}
	//字符到数字
	template <typename T>
	int ReturnIntToDouble(T Type){
		if (m_str == NULL){
			return 0;
		}
		//int
		if (typeid(Type) == typeid(int)) {
			return atoi(m_str);
		}
		//double
		if (typeid(Type) == typeid(double) || typeid(Type) == typeid(float)) {
			return atof(m_str);
		}
	}
	template <typename T>
	int ReturnIntToDouble(char* str,T Type){
		if (str == NULL){
			return 0;
		}
		//int
		if (typeid(Type) == typeid(int)) {
			return atoi(str);
		}
		//double
		if (typeid(Type) == typeid(double) || typeid(Type) == typeid(float)) {
			return atof(str);
		}
	}
	//删除指定字符后的字符
	void Delete_Appoint_Str_After(char str, int Method = CHARSTRING_FRONT/*从哪开始,0前,1后*/, BOOL Retain = FALSE/*是否保留原字符*/) {

		if (str == NULL) {
			return;
		}
		if (m_str == NULL) {
			return;
		}
		if (Method == CHARSTRING_FRONT) {
			for (int i = 0;; ++i) {

				if (str == m_str[i]/*指针偏移递增*/) {
					char *copy_str = NULL;
					int copy_num = 0;	//copy num
					if (Retain == TRUE) {
						copy_num = i;
						i = i + 2;
						copy_num = copy_num + 1;
					}
					else {
						copy_num = i;
						i = i + 1;
					}
					copy_str = (char*)malloc(i);//\\0和原字符
					memset(copy_str, 0, i);
					strncpy(copy_str, m_str, copy_num);
					Relete();
					m_str = (char*)malloc(i);
					memset(m_str, 0, i);
					strcpy(m_str, copy_str);
					m_strfree(copy_str);
					break;

				}
			}
		}
		if (Method == CHARSTRING_AFTER) {
			int i = GetStrSize();
			for (;; --i) {

				if (str == m_str[i]/*指针偏移递增*/) {
					char *copy_str = NULL;
					int copy_num = 0;	//copy num
					if (Retain == TRUE) {
						copy_num = i;
						i = i + 2;
						copy_num = copy_num + 1;
					}
					else {
						copy_num = i;
						i = i + 1;
					}
					copy_str = (char*)malloc(i);//\\0和原字符
					memset(copy_str, 0, i);
					strncpy(copy_str, m_str, copy_num);
					Relete();
					m_str = (char*)malloc(i);
					memset(m_str, 0, i);
					strcpy(m_str, copy_str);
					m_strfree(copy_str);
					break;


				}
			}
		}
		GetStrSize();

	}
	//删除指定字符前的字符
	void Delete_Appoint_Str_Front(char str, int Method = CHARSTRING_FRONT/*从哪开始,0前,1后*/, BOOL Retain = FALSE/*是否保留原字符*/) {

		if (str == NULL) {
			return;
		}
		if (m_str == NULL) {
			return;
		}
		if (Method == CHARSTRING_FRONT) {
			for (int i = 0;; ++i) {

				if (str == m_str[i]/*指针偏移递增*/) {
					char *copy_str = NULL;
					int copy_num = 0;	//copy num
					if (Retain == TRUE) {
						i = i + 2;
						copy_num = copy_num + 1;
					}
					else {
						i = i + 1;
					}
					copy_str = (char*)malloc(i);//\\0和原字符
					memset(copy_str, 0, i);
					strncpy(copy_str, m_str, i);
					Relete();
					m_str = (char*)malloc(i);
					memset(m_str, 0, i);
					strcpy(m_str, copy_str);
					m_strfree(copy_str);
					break;

				}
			}
		}
		if (Method == CHARSTRING_AFTER) {
			int i = GetStrSize();
			for (;; --i) {

				if (str == m_str[i]/*指针偏移递增*/) {
					char *copy_str = NULL;
					int copy_num = 0;	//copy num
					if (Retain == TRUE) {
						i = i + 2;
						copy_num = copy_num + 1;
					}
					else {
						i = i + 1;
					}
					copy_str = (char*)malloc(i);//\\0和原字符
					memset(copy_str, 0, i);
					strncpy(copy_str, m_str, i);
					Relete();
					m_str = (char*)malloc(i);
					memset(m_str, 0, i);
					strcpy(m_str, copy_str);
					m_strfree(copy_str);
					break;


				}
			}
		}
		GetStrSize();

	}
	void Relete() {
		if (m_str != NULL) {
			free(m_str);
			m_str = NULL;
			m_size = 0;
		}


	}
	//printf
	void Print_Cmd() {
		if (m_str == NULL) {
			return;
		}
		char str = 0;
		for (int i = 0; m_str[i] != '\\0'; ++i) {
			printf("%c", m_str[i]);
		}
	}
	//获取字符串大小
	signed long long int GetStrSize() {
		if (m_str == NULL) {
			return 0;
		}
		for (m_size = 0; m_str[m_size] != '\\0'; ++m_size) {}
		return m_size;
	}
	//获取指定char类型字符大小
	signed long long int GetCharSize(const char *str) {
		signed long long int i = 0;
		if (str == NULL) {
			return 0;
		}
		for (i = 0; str[i] != '\\0'; ++i) {}
		return i;
	}
	//压入字符
	void Input(const char *str) {
		if (m_str != NULL) {
			Relete();
		}
		//Relete();
		if (str == NULL) {
			return;
		}
		signed long long int i = 0;
		for (; str[i] != '\\0'; ++i) {}
		m_str = (char*)malloc(i + 1);
		memset(m_str, 0, i + 1);
		strcpy(m_str, str);
		GetStrSize();
	}
	//字符拼接
	void Splicing(const char *str) {
		if (str == NULL) {
			return;
		}
		if (m_str != NULL) {
			//保存原字符
			char *copy_str = NULL;
			GetStrSize();
			copy_str = (char*)malloc(m_size + 1);//\\0
			memset(copy_str, 0, m_size + 1);
			strcpy(copy_str, m_str);
			int L_Size = m_size;
			Relete();
			//获取字符串大小
			signed long long int i = GetCharSize(str);
			m_strmalloc(L_Size + i + 1);	//\\0
			memset(m_str, 0, L_Size + i + 1);
			strcpy(m_str, copy_str);
			//防止立即数copy之前将值复制一份
			char *copy_str_1 = (char*)malloc(i + 1);
			strcpy(copy_str_1, str);
			strcat(m_str, copy_str_1);
			GetStrSize();
			m_strfree(copy_str_1);
			m_strfree(copy_str);

		}

	}
	//返回data
	char *GetData() {
		if (m_str == NULL) {
			return NULL;
		}
		return m_str;
	}
	//判断指定的两个字符串是否相同
	//相同返回0,不相同返回非0
	int Char_Two_Identical(const char* str1, const char*str2) {
		int num1 = GetCharSize(str1);
		int num2 = GetCharSize(str2);
		if (num1 != num2) {
			return 1;
		}
		for (int i = 0; i <= num1; ++i) {
			if (str1[i] != str2[i]) {
				return 1;
			}
		}
		return 0;
	}
	//字符串裁剪
	int TailoringIndexToIndex(int A, int B) {
		if (m_str == NULL) {
			return -1;
		}
		if (strlen(m_str) > B) {
			return -2;
		}
		//定位A
		int size = 0;
		for (int i = 0; i <= B - 1; ++i) {
			if (i >= A) {
				size++;
			}

		}
		char *str1 = (char*)malloc(size + 1);
		memset(str1, 0, size + 1);
		int u = 0;
		for (int i = 0; i <= B - 1; ++i) {
			if (i >= A) {
				str1[u++] = m_str[i];
			}
		}
		//裁剪
		free(m_str);
		m_str = NULL;
		m_str = (char*)malloc(size + 1);
		memset(m_str, 0, size + 1);
		strcpy(m_str, str1);
		return 1;
	}
	//其他类型到字符
	template <typename T>
	void Format_Str(T Format) {
		//int
		if (typeid(Format) == typeid(int)) {
			Relete();
			m_strmalloc(sizeof(int));
			sprintf(m_str, "%d", Format);
			GetStrSize();
			return;
		}
		//double
		if (typeid(Format) == typeid(double)) {
			Relete();
			m_strmalloc(sizeof(double));
			sprintf(m_str, "%lf", Format);
			GetStrSize();
			return;
		}
		//float
		if (typeid(Format) == typeid(float)) {
			Relete();
			m_strmalloc(sizeof(float));
			sprintf(m_str, "%f", Format);
			GetStrSize();
			return;
		}
		//char
		if (typeid(Format) == typeid(char)) {
			Relete();
			m_strmalloc(sizeof(char)+1);	//\\0
			sprintf(m_str, "%c", Format);
			GetStrSize();
			return;
		}
	}
	//获取copy的文本可以做局部变量返回
	char *GetReturnStr() {
		if (m_str == NULL) {
			return NULL;
		}
		int Size = GetStrSize();
		char* ReturnStr = (char*)malloc(Size);
		if (ReturnStr == NULL) {
			return NULL;
		}
		memset(ReturnStr, 0, Size);
		strcpy(ReturnStr, m_str);
		return ReturnStr;

	}
	//获取指定文本索引,穷举法稳定
	int GetStrTxTIndx(const char* str){
			if (m_str == NULL || str == NULL){
				return -1;
			}
			int StrSize = strlen(str);
			if (StrSize <= 0){
				return -1;
			}
			int Str_m_str = GetStrSize();
			int m_str_index = 0;/*二次循环坐标*/
			int y = 0;
			for (int i = 0; i <= Str_m_str -1; i++/*加一是因为i从0开始,下方循环是size+1是为了将坐标移动到下个字符*/){
				y = 0;
				m_str_index = i;
				for (int j = 0; j <= StrSize - 1; ++j, ++m_str_index){
					//printf("%d", j);
					if (str[j] == m_str[m_str_index]){
						y += 1;
					}
					if (y == StrSize){
						return i+1;
					}
					if (str[j] != m_str[m_str_index]){
						break;
						
					}
				}
			}
			return -1;
		}
	//获取指定文本索引,穷举法稳定
	int GetStrTxTIndxRepeat(const char* str,int Repeat = 0/*重复次数*/){
		if (m_str == NULL || str == NULL){
			return -1;
		}
		int StrSize = strlen(str);
		if (StrSize <= 0){
			return -1;
		}
		int Str_m_str = GetStrSize();
		int m_str_index = 0;/*二次循环坐标*/
		int y = 0;
		int Repeat_i = 0;
		for (int i = 0; i <= Str_m_str - 1; i++/*加一是因为i从0开始,下方循环是size+1是为了将坐标移动到下个字符*/){
			y = 0;
			m_str_index = i;
			for (int j = 0; j <= StrSize - 1; ++j, ++m_str_index){
				//printf("%d", j);
				if (str[j] == m_str[m_str_index]){
					y += 1;
				}
				if (y == StrSize){
					if (Repeat_i == Repeat){
						return i + 1;
					}
					else{
						Repeat_i++;
					}
				}
				if (str[j] != m_str[m_str_index]){
					break;

				}
			}
		}
		return -1;
	}
	//判断文本是否存在
	BOOL JumpTxTExits(char *str){
		if (GetStrTxTIndx(str) == -1){
			return FALSE;
		}
		return TRUE;
	}
	//取一行文本
	char* GetLineTxT(int LineIndex = 1) {
		if (LineIndex == 0){
			return NULL;
		}
		if (m_str == NULL) {
			return NULL;
		}
		int l = 0;
		char *yun = NULL;
		int j = 0;
		int u = 0;
		for (int i = 0; i <= GetStrSize()+1/*包含\\0*/; ++i) {
			if (m_str[i] == '\\n') {
				++l;
				if (l != LineIndex)
					u = i + 1;//+1跳过\\n 这里递增1后面已经忽略0因为已经不是第一行坐标
			}
			if (l == LineIndex) {
				//判断此行多长用于分配内存
				for (j = u;; ++j) {
					if (m_str[j] == '\\n' || m_str[j] == '\\0') {
						int size = 0;
						if (u == 0){//防止从0下标导致计算实际长度少1,因为额外的1要用来\\0
							size = j + 1;	//+1是因为分配内存不是从0做下标所以需要判断是否为0
						}
						else{
							size = j;
						}
						yun = (char*)malloc(size + 1);	//多分配额外字节,\\0
						memset(yun, 0, size + 1);
						break;
					}
				}
				int in = 0;
				for (int n = u; n <= j; ++n, ++in) {
					yun[in] = m_str[n];
				}
				return yun;
			}
		}
	}
	//获取文本多少行
	int GetLineNum(){
		if (m_str == NULL){
			return NULL;
		}
		int Size = 0;
		for (int i = 0; i <= GetStrSize(); ++i){
			if (m_str[i] == '\\n'){
				++Size;
			}
			if (m_str[i] == '\\0'){
				if (m_str[i - 1] != '\\n'){
					Size++;
				}
				return Size;
			}
		}
	}
	//获取文本所在行
	int GetTxTLineNum(char *str){
		if (str == NULL || m_str == NULL){
			return NULL;
		}
		char *str1 = NULL;
		int i = 0;
		int u = GetLineNum();
		while (1){//查找这个文本格式在第几行里
			str1 = GetLineTxT(i);
			if (str1 == NULL){
				return NULL;
			}
			if (JumpTxTExits(str1) == TRUE){
				break;
			}
			++i;
			if (i > u){
				return 0;
			}
		}
		return i;
	}
	//索引到字符截取
	char* IndexToCharCutOut(int Index, char str){
		if (Index <= 0 || str == 0 || Index > GetStrSize()){
			return NULL;
		}
		int Is = GetStrSize();
		int Size = 1;
		for (int i = Index-1; i <= GetStrSize(); ++i){
			if (m_str[i] == str){
				char* r_str = (char*)malloc(Size+1);
				memset(r_str, 0, Size + 1);
				if (r_str == NULL){
					return NULL;
				}
				int s = Index - 1;
				for (int j = 0;j <= Size-1; ++j,++s){
					r_str[j] = m_str[s];
				}
				return r_str;
			}
			++Size;
		}
		return NULL;
	}
	//两点字符截取
	char* CharToCharCutOut(char str1, char str2){
		if (str1 <= 0 || str2 == 0){
			return NULL;
		}
		int Is = GetStrSize();
		int Size = 0;
		for (int i =  0; i <= GetStrSize(); ++i){
			if (m_str[i] == str1){
				for (int d = i; d <= GetStrSize(); ++d){//计算长度
					++Size;
					if (m_str[d] == str2){
						break;
					}
				}
				char* r_str = (char*)malloc(Size + 1);
				memset(r_str, 0, Size + 1);
				if (r_str == NULL){
					return NULL;
				}
				int s = i;
				for (int j = 0; j <= Size - 1; ++j, ++s){
					r_str[j] = m_str[s];
				}
				return r_str;
			}
		}
		return NULL;
	}
	//文本替换
	int TexTSubstiTution(char *str) {
		if (str == NULL || m_str == NULL) {
			return NULL;
		}
		//获取文本索引
		int i = GetStrTxTIndx(str);
		if (i == 0) {
			return 0;
		}
		//存储前面的字符
		char *str_q = (char*)malloc(i + 1);
		if (str_q == NULL) {
			return NULL;
		}
		memset(str_q, 0, i + 1);
		strncpy(str_q, m_str, i);
		//中间字符
		i = i + strlen(str) + 1;
		char* str_z = (char*)malloc(i);
		if (str_z == NULL) {
			return NULL;
		}
		memset(str_z, 0, i);
		sprintf(str_z, "%s%s", str_q, str);
		//存储后面的字符
		int h_size = 0;
		for (int j = GetStrTxTIndx(str) + strlen(str) + 1; m_str[j] != '\\0'; ++j) {
			++h_size;
		}
		char *str_h = (char*)malloc(h_size + 1);
		if (str_h == NULL) {
			return NULL;
		}
		memset(str_h, 0, h_size + 1);
		int u = 0;
		for (int j = GetStrTxTIndx(str) + strlen(str) + 1; m_str[j] != '\\0'; ++j, ++u) {
			str_h[u] = m_str[j];
		}
		strcat(str_z, str_h);
		free(m_str);
		m_str = NULL;
		m_str = (char*)malloc(strlen(str_z) + 1);
		if (m_str == NULL) {
			return NULL;
		}
		strcpy(m_str, str_h);
		return 1;
	}
	//文本截取
	int TxTCutOut(char *str) {
		if (str == NULL) {
			return 0;
		}
		//获取文本索引
		int i = GetStrTxTIndx(str);
		if (i == 0) {
			return 0;
		}
		//保留前文本
		char *str_q = (char*)malloc(i + 1);
		if (str_q == NULL) {
			return NULL;
		}
		memset(str_q, 0, i + 1);
		strncpy(str_q, m_str, i);
		//存储后面的字符
		int h_size = 0;
		for (int j = GetStrTxTIndx(str) + strlen(str) + 1; m_str[j] != '\\0'; ++j) {
			++h_size;
		}
		char *str_h = (char*)malloc(h_size + 1);
		if (str_h == NULL) {
			return NULL;
		}
		memset(str_h, 0, h_size + 1);
		int u = 0;
		for (int j = GetStrTxTIndx(str) + strlen(str) + 1; m_str[j] != '\\0'; ++j, ++u) {
			str_h[u] = m_str[j];
		}
		strcat(str_q, str_h);
		if (m_str != NULL) {
			free(m_str);
			m_str = NULL;
		}
		m_str = (char*)malloc((GetStrSize() - strlen(str)) + 1);
		strcpy(m_str, str_q);
		return 1;
	}
	//裁剪段落如sjcy会删除sjcy字符
	int CutOutChar(char *str) {
		if (str == NULL || m_str == NULL) {
			return 0;
		}
		char* Zw = (char*)malloc(strlen(str) + 1);
		if (Zw == NULL) {
			return NULL;
		}
		memset(Zw, 0, strlen(str) + 1);
		//解析字符数组
		for (int i = 0; i <= strlen(str) - 1; ++i) {
			sprintf(Zw, "%c", str[i]);
			TxTCutOut(Zw);
			memset(Zw, 0, strlen(Zw) + 1);
		}
		return 1;
	}
	//索引裁剪
	int CutOutIndex(int Index) {
		if (Index == 0) {
			return 0;
		}
		if (Index > GetStrSize()) {
			return 0;
		}
		if (m_str == NULL) {
			return 0;
		}
		m_str[Index] = '\\0';
		char *s = (char*)malloc(GetStrSize());
		if (s == NULL) {
			return 0;
		}
		for (int i = 0; i <= GetStrSize() - 1; ++i) {
			if (m_str[i] == '\\0') {
				continue;
			}
			s[i] = m_str[i];
		}
		free(m_str);
		m_str = s;
		return 1;
	}
	//其他类型到字符,从后加入
	template <typename T>
	void Format_Str_After(T Format) {
		if (m_str == NULL) {
			return;
		}
		//int
		if (typeid(Format) == typeid(int)) {
			GetStrSize();
			signed long long int i = m_size + 1;	//\\0
			//内容copy
			char *copy_str = (char*)malloc(i);
			memset(copy_str, 0, i);
			strcpy(copy_str, m_str);
			Relete();//释放
			//重新分配内存
			m_strmalloc(i + sizeof(int));
			sprintf(m_str, "%s%d", copy_str, Format);
			GetStrSize();
			return;
		}
		//double
		if (typeid(Format) == typeid(double)) {
			GetStrSize();
			signed long long int i = m_size + 1;	//\\0
			//内容copy
			char *copy_str = (char*)malloc(i);
			memset(copy_str, 0, i);
			strcpy(copy_str, m_str);
			Relete();//释放
			//重新分配内存
			m_strmalloc(i + sizeof(double));
			sprintf(m_str, "%s%lf", copy_str, Format);
			GetStrSize();
			return;
		}
		//float
		if (typeid(Format) == typeid(float)) {
			GetStrSize();
			signed long long int i = m_size + 1;	//\\0
			//内容copy
			char *copy_str = (char*)malloc(i);
			memset(copy_str, 0, i);
			strcpy(copy_str, m_str);
			Relete();//释放
			//重新分配内存
			m_strmalloc(i + sizeof(float));
			sprintf(m_str, "%s%f", copy_str, Format);
			GetStrSize();
			return;
		}
		//char
		if (typeid(Format) == typeid(char)) {
			GetStrSize();
			signed long long int i = m_size + 1;	//\\0
			//内容copy
			char *copy_str = (char*)malloc(i);
			memset(copy_str, 0, i);
			strcpy(copy_str, m_str);
			Relete();
			//重新分配内存
			m_strmalloc(i + sizeof(char));
			sprintf(m_str, "%s%c", copy_str, Format);
			GetStrSize();
			return;
		}
	}
	//取右边文本
	char* GetRightTxT(int Length = 0){
		if (Length == 0){
			return 0;
		}
		if (m_str == NULL){
			return 0;
		}
		char *str = (char*)malloc(Length + 1);
		memset(str, 0, Length + 1);
		int in = 0;
		for (int i = GetStrSize()-1/*去掉\\0*/; i >= (GetStrSize() - Length); --i,++in){
			str[in] = m_str[i];
		}
		return str;
	}
	//中英文浮点转换字符转换
	void CharInToCharAscii(char &cstr){
		switch (cstr){
		case ':':
			cstr = ':';
			break;
		case ',':
			cstr = ',';
			break;
		default:
			break;
		}
	}
	//三点解析取文本,使用方法:例如文本是 txt:1,2,3,4\\r\\n 使用方法:AnalysisTxTtoArr(':',',','\\r',x) 每次会根据x索引返回'1''2''3'
	char* ThreePoints_AnalysisTxTtoArr(char str1/*第一个char*/, char str2/*中间char*/, char str3/*结尾char*/, int Index/*索引*/){
		if (m_str == NULL){
			return 0;
		}
		if (str1 == str2 || str2 == str3){
			return NULL;
		}
		CharInToCharAscii(str1);
		CharInToCharAscii(str2);
		CharInToCharAscii(str3);
		int uIndex = 0;//索引
		int c_size = 0;//大小从str1开始计算得到双倍字符防止小
		int iis = 0;	//定位上一次str2的坐标 
		int yuno = 0;	//上一个结尾字符
		for (int i = 0; i <= GetStrSize(); ++i){
			if (m_str[i] == str1){
				
				for (int j = i;j<=GetStrSize(); ++j){
					if (m_str[j] == str2){
						yuno = j;
						++uIndex;
					}
					++c_size;
					if (uIndex == Index){
						if (Index != 1){
							iis = j - 1;
						}
						else{
							iis = i + 1;
						}
						char* str = (char*)malloc(c_size+1);
						memset(str, 0, c_size + 1);
						int y = 0;
						for (int u = iis; u <= GetStrSize(); ++u, ++y){
							if (m_str[u] == str2){
								char *i_str = NULL;
								i_str = (char*)malloc(strlen(str) + 1);
								memset(i_str, 0, strlen(str) + 1);
								strcpy(i_str, str);
								free(str);
								str = NULL;
								return i_str;
							}
							str[y] = m_str[u];
						}
					
					}
					else{
						if (Index == 1){
							iis = i + 1;
						}
					}
				}
			//	return NULL;
			}
			if (m_str[i] == str3){
				//先看看上一个分隔符是不是一样的
				int usize = 0;
				int y = 0;
				if (m_str[yuno] == str2){
					for (int i = yuno; i <= GetStrSize(); ++i){
						++usize;
					}
					char *i_str = NULL;
					i_str = (char*)malloc(usize + 1);
					memset(i_str, 0, usize + 1);
					for (int i = yuno+1; i <= GetStrSize(); ++i,++y){
						if (m_str[i] == str3){
							return i_str;
						}
						i_str[y] = m_str[i];
					}

					
				}
				return NULL;
			}
		}
	}
	//三点解析取文本,获取格式大小
	int ThreePoints_AnalysisGetTxTtoArrNum(char str1/*第一个char*/, char str2/*中间char*/, char str3/*结尾char*/){
		if (m_str == NULL){
			return 0;
		}
		if (str1 == str2 || str2 == str3){
			return NULL;
		}
		CharInToCharAscii(str1);
		CharInToCharAscii(str2);
		CharInToCharAscii(str3);
		int u = 0;
		for (int i = 0; i <= GetStrSize(); ++i){
			if (m_str[i] == str2){
				++u;
			}
			if (m_str[i] == str3){
				return u+1;
			}
		}
	}
	~CharString() {

		Relete();
	}
	char *m_str;
	signed long long int m_size;
	//运算符重载
	CharString & operator = (const char * str) {
		Input(str);
		return *this;
	}
	//运算符重载
	CharString & operator = (const char str) {
		Relete();
		m_strmalloc(sizeof(char)+1);	//\\0
		sprintf(m_str, "%c", str);
		return *this;
	}
	//+
	CharString & operator + (const char *str) {
		Splicing(str);
		return *this;
	}
	//-
	CharString & operator - (char *str) {
		TxTCutOut(str);
		return *this;
	}
	//[]
	char & operator [] (int i) {
		return m_str[i];
	}
	//%获取行
	char * operator % (int i) {
		return GetLineTxT(i);
	}
	//==
	BOOL operator == (const char *str) {
		if (0 == strcmp(m_str, str)) {
			return TRUE;
		}
		else {
			return FALSE;
		}
	}
	//()
	CharString & operator () (const char *str) {
		Relete();
		Input(str);
		return *this;
	}
	CharString & operator () (char *str) {
		Relete();
		Input(str);
		return *this;
	}
private:
	//内存分配
	void m_strmalloc(signed long long int size) {
		m_str = (char*)malloc(size);
		memset(m_str, 0, size);
	}
	//内存释放
	void m_strfree(char *str) {
		free(str);
		str = NULL;
	}
};

备注:此类可移植性高,代码不难,此类实现纯POIX C/C++实现 开发环境:vs2015,如要移植到其他开发平台请去掉#pragma warning(disable:4996)    //降低编译器安全警告,使用标准C库一些库函数 

以下是使用此类实现的解析一个http报文的功能(不包括权重分析)

解析HTTP结构体

//解析HTTP报文
struct Http_Format{
	char Method[256]; //请求方法
	char Data[1024]; // 请求数据
	char Browser_Kernel[256];//请求的浏览器内核
	int Browser_Kernel_Edition;	//请求浏览器内核版本
	char Support_File_Type[12][256];//请求方支持的文件类型
	char Support_File_Type_Num = 0;	//支持文件类型多少个
	char Support_Encoding_Format[12][256]; //编码格式
	char Support_Encoding_Format_Num = 0; //编码格式多少个
	char Language[256];	//支持的语言
	int Connection_Situation;//请求连接方法,0短连接,1长连接
	double Http_Edition; //http连接版本
	char Cookie_Data[256]; //Cookie数据
	int Windows_NT_Edition;//系统版本
	int Windows_NT_Bit;	//系统位数
	char HOST[256]; //请求的HOST
	char ConnectMode[256];//连接方式
	BOOL Statie = FALSE;	//解析状态
};

实现函数:

//解析http报文
Http_Format HttpFormat_Analysis(char* Http_Data){
	struct Http_Format ht;
	if (Http_Data == NULL){
		return ht;
	}
	char sd[1024] = { 0 };
	strcpy(sd, "GET /proct HTTP/1.1\\r\\nAccept:text/html,application/xhtml+html,image/jxr,*/*\\r\\nAccept-Language:zh-cn\\r\\nUser-Agent:Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1\\r\\n"\\
		"Accept-Encoding:gzip,deflate\\r\\nHOST:127.0.0.1:8080\\r\\nConnection:Keep Alive\\r\\n");
	//获取方法数据http版本
	if (1 != HttpFormatA(&ht, sd)){
		ht.Statie = FALSE;
		return ht;
	}
	//获取客户端支持的文本类型
	if (1 != HttpFormat_Accept(&ht, sd)){
		ht.Statie = FALSE;
		return ht;
	}
	//获取浏览器支持的语言
	if (1 != HttpFormat_Accept_Language(&ht, sd)){
		ht.Statie = FALSE;
		return ht;
	}
	//获取客户端信息
	if (1 != HttpFormat_User_Agent(&ht, sd)){
		ht.Statie = FALSE;
		return ht;
	}
	//获取压缩格式
	if (1 != HttpFormat_Accept_Encoding(&ht, sd)){
		ht.Statie = FALSE;
		return ht;
	}
	//获取HOST地址
	if (1 != HttpFormat_HOST(&ht, sd)){
		ht.Statie = FALSE;
		return ht;
	}
	//解析TCP检测机制
	if (1 != HttpFormat_Connection(&ht, sd)){
		ht.Statie = FALSE;
		return ht;
	}
	//解析Cookie
	if (1 != HttpFormat_Cookie(&ht, sd)){
		ht.Statie = FALSE;
		return ht;
	}
	return ht;
}
//http报文格式打印
void HttpFormat_print(Http_Format ht){
	/*打印解析的http报文*/
	printf("客户端HTTP报文解析内容:\\n");
	printf("方法:%s\\n数据:%s\\nHTTP版本:%0.1f\\n", ht.Method, ht.Data, ht.Http_Edition);
	if (ht.Http_Edition == 1.1f){
		printf("连接方式:长连接\\n");
	}
	else{
		printf("连接方式:短连接\\n");
	}
	printf("支持的文本类型:");
	for (int i = 0; i <= ht.Support_File_Type_Num - 1; ++i){
		if (i == ht.Support_File_Type_Num - 1){
			printf("%s", ht.Support_File_Type[i]);
			break;
		}
		printf("%s,", ht.Support_File_Type[i]);
	}
	printf("\\n");
	printf("支持的语言:%s\\n", ht.Language);
	printf("系统版本:Win%dx%d\\n", ht.Windows_NT_Edition, ht.Windows_NT_Bit);
	printf("浏览器内核:%s v%d\\n", ht.Browser_Kernel, ht.Browser_Kernel_Edition);
	printf("压缩格式:");
	for (int i = 0; i <= ht.Support_Encoding_Format_Num - 1; ++i){
		if (i == ht.Support_Encoding_Format_Num - 1){
			printf("%s", ht.Support_Encoding_Format[i]);
			break;
		}
		printf("%s,", ht.Support_Encoding_Format[i]);
	}
	printf("\\n");
	printf("HOST地址:%s\\n", ht.HOST);
	printf("检测机制:%s\\n", ht.ConnectMode);
	if (strlen(ht.Cookie_Data) < 1){
		printf("Cookie数据:空\\n");
	}
	else{
		printf("Cookie数据:%s\\n", ht.Cookie_Data);
	}
}

备注:解析函数里的参数被显示指定,如果需要接口调用请将更改参数代码

代码示例:

客户端发来的报文:

GET /proct HTTP/1.1\\r\\nAccept:text/html,application/xhtml+html,image/jxr,*/*\\r\\nAccept-Language:zh-cn\\r\\nUser-Agent:Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1\\r\\n"\\
        "Accept-Encoding:gzip,deflate\\r\\nHOST:127.0.0.1:8080\\r\\nConnection:Keep Alive\\r\\n

Http_Format hp = HttpFormat_Analysis("GET /proct HTTP/1.1\\r\\nAccept:text/html,application/xhtml+html,image/jxr,*/*\\r\\nAccept-Language:zh-cn\\r\\nUser-Agent:Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1\\r\\n"\\
		"Accept-Encoding:gzip,deflate\\r\\nHOST:127.0.0.1:8080\\r\\nConnection:Keep Alive\\r\\n");
	HttpFormat_print(hp);

运行结果:

以上是关于开源字符处理类库:CharString类 拆分自自己研发的web服务器中的类库的主要内容,如果未能解决你的问题,请参考以下文章

C# .NET 使用DotNetZip开源类库 处理 压缩/解压 Zip 处理乱码情况

开源代码:Http请求封装类库HttpLib介绍使用说明

.Net常用基本类库2.1字符串处理

别再重复造轮子了,推荐使用 Google Guava 开源工具类库,真心强大!

.NET平台开源项目速览(16)C#写PDF文件类库PDF File Writer介绍

别再重复造轮子了,推荐使用 Google Guava 开源工具类库,真心强大!