forked from adesutherland/CMS-370-BREXX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.c
59 lines (53 loc) · 1.64 KB
/
index.c
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* $Id: index.c,v 1.4 2008/07/15 07:40:54 bnv Exp $
* $Log: index.c,v $
* Revision 1.4 2008/07/15 07:40:54 bnv
* #include changed from <> to ""
*
* Revision 1.3 2002/06/11 12:37:15 bnv
* Added: CDECL
*
* Revision 1.2 2001/06/25 18:49:48 bnv
* Header changed to Id
*
* Revision 1.1 1998/07/02 17:18:00 bnv
* Initial Version
*
*/
#include "lstring.h"
/* ----------------- Lindex ---------------------- */
/* haystack - Lstr where to search *
* needle - Lstr to search *
* p - starting position [1,haystack len] *
* if p < 1 then p = 1 *
* returns 0 (NOTFOUND) is needle is not found *
* else returns position [1,haystack len] *
* ----------------------------------------------- */
long __CDECL
Lindex(const PLstr haystack, const PLstr needle, long p) {
long n, lp;
p--; /* for C string offset = 0, Rexx=1 */
if (p < 0) p = 0;
L2STR(haystack);
L2STR(needle);
if (!LLEN(*needle)) return LNOTFOUND;
if (LLEN(*needle) > LLEN(*haystack)) return LNOTFOUND;
lp = p - 1;
do {
n = 0;
p = lp + 1;
if (p >= LLEN(*haystack)) return LNOTFOUND;
while (LSTR(*haystack)[p] != LSTR(*needle)[0]) {
p++;
if (p >= LLEN(*haystack)) return LNOTFOUND;
}
lp = p;
while ((LSTR(*haystack)[p] == LSTR(*needle)[n]) &&
(n < LLEN(*needle))) {
if ((++n) >= LLEN(*needle)) return lp + 1;
p++;
if (p >= LLEN(*haystack)) return LNOTFOUND;
}
} while (n < LLEN(*needle));
return lp + 1;
} /* Lindex */