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 == '\0') { 
 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 }

 

相关文章:

  • 2022-01-21
  • 2021-10-30
  • 2021-10-10
  • 2021-12-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-06
  • 2022-12-23
  • 2021-12-09
  • 2021-11-13
相关资源
相似解决方案