From effcc8577862621a850c98cd6f9c2028c18c5de4 Mon Sep 17 00:00:00 2001 From: erg Date: Tue, 9 Jun 2009 15:51:38 +0000 Subject: [PATCH] Add rindex function --- cmd/gvpr/actions.c | 26 +++++++++++++++++++++++++- cmd/gvpr/actions.h | 1 + cmd/gvpr/compile.c | 3 +++ cmd/gvpr/gprdata | 1 + cmd/gvpr/gvpr.1 | 4 +++- 5 files changed, 33 insertions(+), 2 deletions(-) diff --git a/cmd/gvpr/actions.c b/cmd/gvpr/actions.c index c190561e7..a9b6ad856 100644 --- a/cmd/gvpr/actions.c +++ b/cmd/gvpr/actions.c @@ -54,7 +54,7 @@ Agraph_t *sameG(void *p1, void *p2, char *fn, char *msg) } /* indexOf: - * Return index of string s2 in string s1, or -1 + * Return index of leftmost string s2 in string s1, or -1 */ int indexOf(char *s1, char *s2) { @@ -76,6 +76,30 @@ int indexOf(char *s1, char *s2) return -1; } +/* rindexOf: + * Return index of rightmost string s2 in string s1, or -1 + */ +int rindexOf(char *s1, char *s2) +{ + char c1 = *s2; + char c; + char *p; + int len1 = strlen(s1); + int len2 = strlen(s2); + + if (c1 == '\0') + return (len1); + p = s1 + (len1 - len2); + while (p >= s1) { + c = *p; + if ((c == c1) && (strncmp(p+1, s2+1, len2-1) == 0)) + return (p - s1); + else + p--; + } + return -1; +} + /* match: * Return index of pattern pat in string str, or -1 */ diff --git a/cmd/gvpr/actions.h b/cmd/gvpr/actions.h index e2f07269e..388b36f1b 100644 --- a/cmd/gvpr/actions.h +++ b/cmd/gvpr/actions.h @@ -29,6 +29,7 @@ extern "C" { extern Agobj_t *copy(Agraph_t * g, Agobj_t * obj); extern int copyAttr(Agobj_t * obj, Agobj_t * obj1); extern int indexOf(char *s1, char *s2); + extern int rindexOf(char *s1, char *s2); extern int match(char *str, char *pat); extern int lockGraph(Agraph_t * g, int); extern Agraph_t *compOf(Agraph_t * g, Agnode_t * n); diff --git a/cmd/gvpr/compile.c b/cmd/gvpr/compile.c index 32b50c5ab..ccdc5c21a 100644 --- a/cmd/gvpr/compile.c +++ b/cmd/gvpr/compile.c @@ -1298,6 +1298,9 @@ getval(Expr_t * pgm, Exnode_t * node, Exid_t * sym, Exref_t * ref, case F_index: v.integer = indexOf(args[0].string, args[1].string); break; + case F_rindex: + v.integer = rindexOf(args[0].string, args[1].string); + break; case F_match: v.integer = match(args[0].string, args[1].string); break; diff --git a/cmd/gvpr/gprdata b/cmd/gvpr/gprdata index c50f12d86..e67572d87 100644 --- a/cmd/gvpr/gprdata +++ b/cmd/gvpr/gprdata @@ -59,6 +59,7 @@ F_nxtedgesg "nxtedge_sg" FUNCTION E|A(1,G)|A(2,E)|A(3,V) F_compof "compOf" FUNCTION G|A(1,G)|A(2,V) F_kindof "kindOf" FUNCTION S|A(1,O) F_index "index" FUNCTION I|A(1,S)|A(2,S) +F_rindex "rindex" FUNCTION I|A(1,S)|A(2,S) F_isedge "isEdge" FUNCTION E|A(1,V)|A(2,V)|A(3,S) F_isedgesg "isEdge_sg" FUNCTION E|A(1,G)|A(2,V)|A(3,V)|A(4,S) F_issubedge "isSubedge" FUNCTION I|A(1,G)|A(2,E) diff --git a/cmd/gvpr/gvpr.1 b/cmd/gvpr/gvpr.1 index 5800a2b1d..5d0cafa38 100644 --- a/cmd/gvpr/gvpr.1 +++ b/cmd/gvpr/gvpr.1 @@ -617,8 +617,10 @@ length of \fIstr\fP, a fatal error occurs. returns the length of the string \fIs\fP. .TP \fBindex\fP(\fIs\fP : \fBstring\fP, \fIt\fP : \fBstring\fP) : \fBint\fP +.TP +\fBrindex\fP(\fIs\fP : \fBstring\fP, \fIt\fP : \fBstring\fP) : \fBint\fP returns the index of the character in string \fIs\fP where the leftmost -copy of string \fIt\fP can be found, or \-1 if \fIt\fP is not a +(rightmost) copy of string \fIt\fP can be found, or \-1 if \fIt\fP is not a substring of \fIs\fP. .TP \fBmatch\fP(\fIs\fP : \fBstring\fP, \fIp\fP : \fBstring\fP) : \fBint\fP -- 2.40.0