TEST(StrstrTestCase, InvaidInput) { const char* haystack = "hello world"; const char* needle = "world"; EXPECT_EQ(NULL, strStr(NULL, NULL)); EXPECT_EQ(NULL, strStr(NULL, needle)); EXPECT_EQ(haystack, strStr(haystack, NULL)); EXPECT_EQ(NULL, strStr("haha", "hahaha")); }
int handle_request(struct client *cli) { int ret; int hlen; char *idxptr = NULL; // RequestS req; // memset(&req, 0, sizeof (RequestS)); Logger(LOG_INFO, 0, "%s...\n", __FUNCTION__); ret = httpd_tcp_recv(cli, cli->req.buffer, sizeof (cli->req.buffer)); if ( ret <= 0 ) { Logger(LOG_ERR, 0, "http recv failed!\n"); return -1; } idxptr = strStr(cli->req.buffer, ret, "\r\n\r\n", 4); if (idxptr == NULL) { Logger(LOG_ERR, 0, "Get request header failed!\n"); return -1; } hlen = (int)(idxptr-cli->req.buffer); cli->req.entity = idxptr + 4; http_header_parse(cli, hlen); return 0; }
int main() { printf("%d\n", strStr("a", "")); system("pause"); return 0; }
/* returns true iff "s" is a substring of a member of the list */ int strListIsSubstr(const String * list, const char *s, char del) { assert(list && del); return strStr(*list, s) != 0; /* * Note: the original code with a loop is broken because it uses strstr() * instead of strnstr(). If 's' contains a 'del', strListIsSubstr() may * return true when it should not. If 's' does not contain a 'del', the * implementaion is equavalent to strstr()! Thus, we replace the loop with * strstr() above until strnstr() is available. */ #ifdef BROKEN_CODE const char *pos = NULL; const char *item; assert(list && s); while (strListGetItem(list, del, &item, NULL, &pos)) { if (strstr(item, s)) return 1; } return 0; #endif }
int main() { char *a = "hello world"; char *b = "llo"; printf("%s, %s, %d\n", a, b, strStr(a, b)); return 0; }
int main(){ char a[]="abdfa"; char b[]="df"; char* p = strStr(a,b); printf("ans: %s\n",p); return 0; }
/* returns true iff "s" is a substring of a member of the list, >1 if more than once */ int strIsSubstr(const String * list, const char *s) { const char *p; assert(list && s); p = strStr(*list, s); if (!p) return 0; if (strstr(p + 1, s) != NULL) return 2; return 1; }
void testCase_strStr() { string haystack = "hello world, i am Marin Young"; string s1 = "hello"; string s2 = "world"; string s3 = "i"; string s4 = "am"; string s5 = "Marin"; string s6 = "Young"; cout<<strStr(haystack, s1)<<endl; cout<<strStr(haystack, s2)<<endl; cout<<strStr(haystack, s3)<<endl; cout<<strStr(haystack, s4)<<endl; cout<<strStr(haystack, s5)<<endl; cout<<strStr(haystack, s6)<<endl; }
main(int argc, char *argv[]) { FILE *fp; int nl = 1; char line[512]; if((fp = fopen(argv[1], "r")) == NULL) { printf("Plik nie istnieje lub wystąpił problem z jego odczytem."); return(-1); } while(fgets(line, sizeof(line), fp) != NULL) { if(strStr(line, argv[2])) { printf("%d -> %s\n", nl, line); } nl++; } if(fp) { fclose(fp); } return(0); }
struct client* http_header_parse(struct client *cli, int len) { char *method = NULL; char *url = NULL; char *version = NULL; char *idxptr = NULL; char *headers = NULL; char *hdrname = NULL; char *hdrvalue = NULL; char *newhdr = NULL; struct request *req = &cli->req; Logger(LOG_INFO, 0, "%s...\n", __FUNCTION__); int hdrcount = 0; method = req->buffer; if ( (headers = strStr(req->buffer, len, "\r\n", 2)) != NULL) //start line terminal { Logger(LOG_INFO, 0, "Get start line\n"); *headers++ = 0; *headers++ = 0; //now headers point to HTTP Headers if ( NULL != (url = strchr(method, ' '))) { *url++ = 0; // now method has been terminal correct, and url point to begin of url } if ( NULL != (version = strchr(url, ' '))) { *version++ = 0; } // check method if (method && !strncmp(method, "GET", 3)) { req->method = HTTP_MSG_GET; } else if (method && !strncmp(method, "POST", 4)) { req->method = HTTP_MSG_POST; } else { req->method = HTTP_MSG_NOTSUPPORT; httpd_response(cli, 405, "Method Not Support"); return NULL; } Logger(LOG_INFO, 0, "Check URL...\n"); /* Check URL */ if (url && strlen(url)) { req->url = url; } else { httpd_response(cli, 400, "Bad Request"); } /* Check Version */ Logger(LOG_INFO, 0, "Check version...\n"); if (version && !strncmp(version, "HTTP/0.9", 8)) { req->version = HTTP_VERSION_0_9; } else if (version && !strncmp(version, "HTTP/1.0", 8)) { req->version = HTTP_VERSION_1_0; } else if (version && !strncmp(version, "HTTP/1.1", 8)) { req->version = HTTP_VERSION_1_1; } else { httpd_response(cli, 400, "Bad Request"); return NULL; } /* Header prase process */ Logger(LOG_INFO, 0, "Check Headers...\n"); newhdr = headers; while ( newhdr != req->entity ) { if ( hdrname && hdrvalue ) { if ( (hdrcount+1) > ARRAY_SIZE(req->hdrnames) ) { httpd_response(cli, 413, "Request Headers Too Large!"); return NULL; } req->hdrnames[hdrcount] = hdrname; req->hdrvalues[hdrcount] = hdrvalue; hdrcount++; hdrname = NULL; hdrvalue = NULL; } else if (hdrname) { hdrvalue = strchr(hdrname, ':'); *hdrvalue++ = 0; while (*hdrvalue == ' ') { hdrvalue++; } if ((newhdr = strchr(hdrvalue, '\r')) || (newhdr = strchr(hdrvalue, '\n'))) // error, while loop will cause newhdr == NULL at last { *newhdr++ = 0; } while (*newhdr == '\r' || *newhdr == '\n') { *newhdr++ = 0; } } else { hdrname = newhdr; } } } else { httpd_response(cli, 400, "Bad Request"); return NULL; } httpd_response(cli, 0, NULL); return cli; }
/* find: print lines that match pattern from 1st arg */ int main(int argc, char *argv[]) { char line[MAXLINE]; long lineno = 0; int c, except = 0, number = 0, found = 0; /* argv作为一个字符指针数组的数组名,则argv是数组首元素的地址,++argv * 是数组首元素的下一个元素的地址,*++argv相当于argv[1],而argv[1]是 * 一个指向第一个参数字符串的字符指针,(*++argv)[0]取指针指向的首元素 * 假如输入命令是 find -n -x "ould",则(*++argv)[0]正好等于'-'.下面的 * 第一个while循环正是通过这种方式来获取命令的选项参数,从而进行操作. */ while (--argc && (*++argv)[0] == '-') { /* 上面提到如果输入命令包含参数,则(*++argv)[0]正好指向参数的第一个 * 字符,按照惯例,该字符是'-';此时,argv[0]是一个指向第一个参数字符串 * 的字符指针,则++argv[0]指向第一个参数字符串的第二个字符的地址,则 * *++argv[0]指向该字符,如果输入命令是 find -n -x "ould",则该字符 * 是'n', switch 语句会对取出来的字符进行判断并处理.由于命令参数传 * 给main()函数时,是以字符串的形式传入,所以命令参数的末尾会有'\0', * 书中把下面的while循环写为 while (c = *++argv[0]), 这是利用了这 * 一点来作为终止条件.当然,写为下面的形式,会显得直观一点. */ while ((c = *++argv[0]) != '\0') switch (c) { /* precede each printed line by its line number */ case 'n': number = 1; break; /* print all the lines except those that match the pattern */ case 'x': except = 1; break; default: printf("error: illegal option %c\n", c); argc = 0; found = -1; break; } } /* 解析完参数选项后,应该还剩下一个参数,那就是要比较的模式字符串pattern, * 如果没有这个参数,则输入的命令不正确,下面会打印出提示字符串. */ if (argc != 1) printf("Usage: find -x -n pattern\n"); else while (getLine(line, MAXLINE) > 0) { ++lineno; /* 当匹配时,strStr(line, *argv) != NULL为1,若希望打印匹配的 * 字符串,则except为0,所以if的判断条件为真,打印出匹配的字符串; * 当不匹配时,strStr(line, *argv) != NULL为0,若希望打印不匹配 * 的字符串,则except为1,此时if的判断条件为真,打印不匹配的字符串 */ if ((strStr(line, *argv) != NULL) != except) { if (number) printf("%ld:", lineno); printf("%s", line); ++found; } } return found; }
int main() { char *s = "CKQYYHAHUYYCDBDFGPBXDJJKPBWLGCNGVAONMAFHRXASGEVEKQFKGQJOSTXN"; char *p = "GP"; printf("answer is %d\r\n", strStr(s, p)); }
int main(){ printf("%d\n", strStr("", "")); return 0; }
int main(){ char *s = "123456"; char *p = "456"; printf("%d",strStr(s,p)); return 0; }
void EnumFiles(VMenu2& Menu, const string& Str) { if(!Str.empty()) { string strStr(Str); size_t Pos = 0; if(std::count(ALL_CONST_RANGE(strStr), L'"') & 1) // odd quotes count { Pos = strStr.rfind(L'"'); } else { for(Pos=strStr.size()-1; Pos!=static_cast<size_t>(-1); Pos--) { if(strStr[Pos]==L'"') { Pos--; while(strStr[Pos]!=L'"' && Pos!=static_cast<size_t>(-1)) { Pos--; } } else if(strStr[Pos]==L' ') { Pos++; break; } } } if(Pos==static_cast<size_t>(-1)) { Pos=0; } bool StartQuote=false; if(Pos < strStr.size() && strStr[Pos]==L'"') { Pos++; StartQuote=true; } string strStart(strStr.data(),Pos); Unquote(strStr.erase(0, Pos)); if(!strStr.empty()) { string strExp = os::env::expand_strings(strStr); os::fs::enum_file Find(strExp+L"*"); bool Separator=false; std::for_each(CONST_RANGE(Find, i) { const wchar_t* FileName=PointToName(strStr); bool NameMatch=!StrCmpNI(FileName, i.strFileName.data(), StrLength(FileName)), AltNameMatch = NameMatch? false : !StrCmpNI(FileName, i.strAlternateFileName.data(), StrLength(FileName)); if(NameMatch || AltNameMatch) { strStr.resize(FileName-strStr.data()); string strAdd (strStr + (NameMatch ? i.strFileName : i.strAlternateFileName)); if (!StartQuote) QuoteSpace(strAdd); string strTmp(strStart+strAdd); if(StartQuote) strTmp += L'"'; if(!Separator) { if(Menu.GetItemCount()) { MenuItemEx Item; Item.strName = MSG(MCompletionFilesTitle); Item.Flags=LIF_SEPARATOR; Menu.AddItem(Item); } else { Menu.SetTitle(MSG(MCompletionFilesTitle)); } Separator=true; } Menu.AddItem(strTmp); } });