字符串操作函数

1.实现在一块内存中查找子串位置:该函数主要是为了在二进制文本中进行查找操作。

 1  23 //获取字符流中的指定子串的位置
 2  24 char* memstr(char* full_data, int full_data_len, char* substr) 
 3  25 { 
 4  26     if (full_data == NULL || full_data_len <= 0 || substr == NULL) { 
 5  27         return NULL; 
 6  28     } 
 7  29 
 8  30     if (*substr == '') { 
 9  31         return NULL; 
10  32     } 
11  33 
12  34     int sublen = strlen(substr); 
13  35 
14  36     int i; 
15  37     char* cur = full_data; 
16  38     int last_possible = full_data_len - sublen + 1; 
17  39     for (i = 0; i < last_possible; i++) { 
18  40         if (*cur == *substr) { 
19  41             //assert(full_data_len - i >= sublen);  
20  42             if (memcmp(cur, substr, sublen) == 0) { 
21  43                 //found  
22  44                 return cur; 
23  45             } 
24  46         } 
25  47         cur++; 
26  48     }                                                                                                                                                    
27  49 
28  50     return NULL;
29  51 }
原文地址:https://www.cnblogs.com/yyx1-1/p/6180144.html