From 68cb45c9fd0bcc50644ed93e13bafca88515c34c Mon Sep 17 00:00:00 2001 From: PatR Date: Sat, 31 Oct 2015 17:13:26 -0700 Subject: [PATCH] str_end_is() Move this small utility routine to hacklib.c where other such things live and where it's feasible to find them if you need the functionality elsewhere. --- include/extern.h | 3 ++- src/hacklib.c | 38 +++++++++++++++++++++++++++++--------- src/options.c | 12 +----------- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/include/extern.h b/include/extern.h index 22df5471b..d3754352e 100644 --- a/include/extern.h +++ b/include/extern.h @@ -1,4 +1,4 @@ -/* NetHack 3.6 extern.h $NHDT-Date: 1445215014 2015/10/19 00:36:54 $ $NHDT-Branch: master $:$NHDT-Revision: 1.509 $ */ +/* NetHack 3.6 extern.h $NHDT-Date: 1446336781 2015/11/01 00:13:01 $ $NHDT-Branch: master $:$NHDT-Revision: 1.511 $ */ /* Copyright (c) Steve Creps, 1988. */ /* NetHack may be freely redistributed. See license for details. */ @@ -831,6 +831,7 @@ E char *FDECL(ucase, (char *)); E char *FDECL(upstart, (char *)); E char *FDECL(mungspaces, (char *)); E char *FDECL(eos, (char *)); +E boolean FDECL(str_end_is, (const char *, const char *)); E char *FDECL(strkitten, (char *, CHAR_P)); E void FDECL(copynchars, (char *, const char *, int)); E char FDECL(chrcasecpy, (int, int)); diff --git a/src/hacklib.c b/src/hacklib.c index 689266450..f7499decb 100644 --- a/src/hacklib.c +++ b/src/hacklib.c @@ -1,6 +1,6 @@ -/* NetHack 3.6 hacklib.c $NHDT-Date: 1432723746 2015/05/27 10:49:06 $ $NHDT-Branch: master $:$NHDT-Revision: 1.43 $ */ +/* NetHack 3.6 hacklib.c $NHDT-Date: 1446336792 2015/11/01 00:13:12 $ $NHDT-Branch: master $:$NHDT-Revision: 1.44 $ */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ -/* Copyright (c) Robert Patrick Rankin, 1991 */ +/* Copyright (c) Robert Patrick Rankin, 1991 */ /* NetHack may be freely redistributed. See license for details. */ #include "hack.h" /* for config.h+extern.h */ @@ -19,6 +19,7 @@ char * upstart (char *) char * mungspaces (char *) char * eos (char *) + boolean str_end_is (const char *, const char *) char * strkitten (char *,char) void copynchars (char *,const char *,int) char chrcasecpy (int,int) @@ -167,6 +168,18 @@ register char *s; return s; } +/* determine whether 'str' ends in 'chkstr' */ +boolean +str_end_is(str, chkstr) +const char *str, *chkstr; +{ + int clen = (int) strlen(chkstr); + + if ((int) strlen(str) >= clen) + return (boolean) (!strncmp(eos((char *) str) - clen, chkstr, clen)); + return FALSE; +} + /* append a character to a string (in place): strcat(s, {c,'\0'}); */ char * strkitten(s, c) @@ -203,8 +216,8 @@ chrcasecpy(oc, nc) int oc, nc; { #if 0 /* this will be necessary if we switch to */ - oc = (int)(unsigned char)oc; - nc = (int)(unsigned char)nc; + oc = (int) (unsigned char) oc; + nc = (int) (unsigned char) nc; #endif if ('a' <= oc && oc <= 'z') { /* old char is lower case; if new char is upper case, downcase it */ @@ -319,7 +332,9 @@ char *buf; return buf; } -boolean onlyspace(s) /* is a string entirely whitespace? */ +/* is a string entirely whitespace? */ +boolean +onlyspace(s) const char *s; { for (; *s; s++) @@ -328,7 +343,9 @@ const char *s; return TRUE; } -char *tabexpand(sbuf) /* expand tabs into proper number of spaces */ +/* expand tabs into proper number of spaces */ +char * +tabexpand(sbuf) char *sbuf; { char buf[BUFSZ]; @@ -351,7 +368,9 @@ char *sbuf; return strcpy(sbuf, buf); } -char *visctrl(c) /* make a displayable string from a character */ +/* make a displayable string from a character */ +char * +visctrl(c) char c; { Static char ccc[3]; @@ -510,9 +529,10 @@ int x0, y0, x1, y1; return (boolean) (!dy || !dx || dy == dx || dy == -dx); } -/* guts of pmatch(), pmatchi(), and pmatchz() */ +/* guts of pmatch(), pmatchi(), and pmatchz(); + match a string against a pattern */ static boolean -pmatch_internal(patrn, strng, ci, sk) /* match a string against a pattern */ +pmatch_internal(patrn, strng, ci, sk) const char *patrn, *strng; boolean ci; /* True => case-insensitive, False => case-sensitive */ const char *sk; /* set of characters to skip */ diff --git a/src/options.c b/src/options.c index 7a24137b3..7e1ac9420 100644 --- a/src/options.c +++ b/src/options.c @@ -1,4 +1,4 @@ -/* NetHack 3.6 options.c $NHDT-Date: 1445556879 2015/10/22 23:34:39 $ $NHDT-Branch: master $:$NHDT-Revision: 1.227 $ */ +/* NetHack 3.6 options.c $NHDT-Date: 1446336796 2015/11/01 00:13:16 $ $NHDT-Branch: master $:$NHDT-Revision: 1.234 $ */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /* NetHack may be freely redistributed. See license for details. */ @@ -5106,16 +5106,6 @@ const char *str; return; } -boolean -str_end_is(str, chkstr) -char *str, *chkstr; -{ - int clen = strlen(chkstr); - if (strlen(str) >= clen) - return !strncmp(eos(str) - clen, chkstr, clen); - return FALSE; -} - /* Returns the fid of the fruit type; if that type already exists, it * returns the fid of that one; if it does not exist, it adds a new fruit * type to the chain and returns the new one.