summaryrefslogtreecommitdiff
path: root/jni/ruby/missing/strstr.c
blob: e6613c5d2fb8fc3acf29a55f1460c37735e97d0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* public domain rewrite of strstr(3) */

#include "ruby/missing.h"

size_t strlen(const char*);

char *
strstr(const char *haystack, const char *needle)
{
    const char *hend;
    const char *a, *b;

    if (*needle == 0return (char *)haystack;
    hend = haystack + strlen(haystack) - strlen(needle) + 1;
    while (haystack < hend) {
        if (*haystack == *needle) {
            a = haystack;
            b = needle;
            for (;;) {
                if (*b == 0return (char *)haystack;
                if (*a++ != *b++) {
                    break;
                }
            }
        }
        haystack++;
    }
    return 0;
}