Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
strnrstrn.c
Go to the documentation of this file.
00001 #pragma ident "@(#)92/gen/strnrstrn.c   92.1    06/02/99 16:43:34"
00002 
00003 /*
00004 
00005   Copyright (C) 2000, 2001 Silicon Graphics, Inc.  All Rights Reserved.
00006 
00007   This program is free software; you can redistribute it and/or modify it
00008   under the terms of version 2.1 of the GNU Lesser General Public License 
00009   as published by the Free Software Foundation.
00010 
00011   This program is distributed in the hope that it would be useful, but
00012   WITHOUT ANY WARRANTY; without even the implied warranty of
00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00014 
00015   Further, this software is distributed without any warranty that it is
00016   free of the rightful claim of any third person regarding infringement 
00017   or the like.  Any license provided herein, whether implied or 
00018   otherwise, applies only to this software file.  Patent licenses, if
00019   any, provided herein do not apply to combinations of this program with 
00020   other software, or any other product whatsoever.  
00021 
00022   You should have received a copy of the GNU Lesser General Public 
00023   License along with this program; if not, write the Free Software 
00024   Foundation, Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, 
00025   USA.
00026 
00027   Contact information:  Silicon Graphics, Inc., 1600 Amphitheatre Pky,
00028   Mountain View, CA 94043, or:
00029 
00030   http://www.sgi.com
00031 
00032   For further information regarding this notice, see:
00033 
00034   http://oss.sgi.com/projects/GenInfo/NoticeExplan
00035 
00036 */
00037 
00038 
00039 #include <string.h>
00040 
00041 #define MAXCHRS 256     /* Size of character set */
00042 
00043 /*
00044  *      strnrstrn       Search for the last occurance of a substring
00045  *                      in another string.  The length of the strings
00046  *                      are arguments and the strings are not necessarily
00047  *                      null-terminated.
00048  *
00049  *      Algorithm:  This routine uses the Boyer-Moore algorithm as
00050  *              described in _Algorithms_ by Robert Sedgewick,
00051  *              Addison-Wesley, 2d Ed., 1988.  pp. 277-289.
00052  *
00053  *      Performance:  If M is the length of the substring and N is
00054  *              the length of the string; this algorithm's worst case
00055  *              is M+N character comparisons, and ``average-case'' is
00056  *              N/M comparisons.  The ``brute-force'' algorithm's
00057  *              worst case is about N*M comparisons.
00058  */
00059 
00060 char *
00061 strnrstrn(const char *string, size_t lenstr, const char *substr, size_t lensub)
00062 {
00063         register short  i;
00064         register unsigned short j;
00065         unsigned short  skip[MAXCHRS];
00066 
00067         if (lensub == 0)
00068                 return( (char *) string + lenstr);
00069 
00070         /* Initialize skip array */
00071 
00072         for (i = 0; i < MAXCHRS; i++)   /* Should vectorize */
00073                 skip[i] = lensub;
00074 
00075         for (i = lensub - 1; i >= 0; i--)
00076                 skip[(int)substr[i]]    = i;
00077 
00078         i       = lenstr - lensub;
00079         j       = 0;
00080 
00081         do {
00082 
00083                 if (string[i] == substr[j]) {
00084                         i       = i + 1;
00085                         j       = j + 1;
00086                 }
00087                 else {
00088                         if (j > skip[(int)string[i]])
00089                                 i       = i - (j + 1);
00090                         else
00091                                 i       = i - skip[(int)string[i]];
00092 
00093                         j       = 0;
00094                 }
00095 
00096         } while (j < (unsigned) lensub && i >= 0);
00097 
00098         if (i < 0)
00099                 return(NULL);
00100         else
00101                 return( (char *) string + i - lensub);
00102 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines