Ruby  2.0.0p247(2013-06-27revision41674)
missing/strstr.c
Go to the documentation of this file.
00001 /* public domain rewrite of strstr(3) */
00002 
00003 #include "ruby/missing.h"
00004 
00005 size_t strlen(const char*);
00006 
00007 char *
00008 strstr(const char *haystack, const char *needle)
00009 {
00010     const char *hend;
00011     const char *a, *b;
00012 
00013     if (*needle == 0) return (char *)haystack;
00014     hend = haystack + strlen(haystack) - strlen(needle) + 1;
00015     while (haystack < hend) {
00016         if (*haystack == *needle) {
00017             a = haystack;
00018             b = needle;
00019             for (;;) {
00020                 if (*b == 0) return (char *)haystack;
00021                 if (*a++ != *b++) {
00022                     break;
00023                 }
00024             }
00025         }
00026         haystack++;
00027     }
00028     return 0;
00029 }
00030