-
Notifications
You must be signed in to change notification settings - Fork 5
/
lastpos.c
50 lines (39 loc) · 984 Bytes
/
lastpos.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
/*
* $Id: lastpos.c,v 1.4 2008/07/15 07:40:54 bnv Exp $
* $Log: lastpos.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"
/* ---------------- Llastpos --------------- */
long __CDECL
Llastpos(const PLstr needle, const PLstr haystack, long p) {
Lstr N;
Lstr H;
L2STR(needle);
L2STR(haystack);
if (p < 0 || p >= LLEN(*haystack)) p = LLEN(*haystack);
if (!LLEN(*needle)) return LNOTFOUND;
LINITSTR(N);
Lstrcpy(&N, needle);
LINITSTR(H);
Lstrcpy(&H, haystack);
Lreverse(&N);
Lreverse(&H);
if (p > 0) p = LLEN(H) - p + 1;
p = Lindex(&H, &N, p);
if (p != LNOTFOUND) p = LLEN(H) - (p + LLEN(N)) + 2;
LFREESTR(N);
LFREESTR(H);
return p;
} /* Llastpos */