1 /*-------------- Telecommunications & Signal Processing Lab ---------------
2                              McGill University
3 
4 Routine:
5   char *STstrstrNM (const char Si[], const char Ti[], int N, int M)
6 
7 Purpose:
8   Search for the first occurrence of string Ti in string Si
9 
10 Description:
11   This routine searches for the first occurrence of string Ti in the input
12   string Si.  The string Si is of length N and the string Ti is of length M.
13   Null characters in either string are treated as ordinary characters.
14 
15 Parameters:
16    -> char Si[]
17       Input character string of length N
18    -> char Ti[]
19       Input character string of length M
20    -> int N
21       Number of characters in Si
22    -> int M
23       Number of characters in Ti
24 
25 Author / revision:
26   P. Kabal  Copyright (C) 2003
27   $Revision: 1.13 $  $Date: 2003/05/09 03:06:42 $
28 
29 -------------------------------------------------------------------------*/
30 
31 #include <libtsp/nucleus.h>
32 
33 
34 char *
STstrstrNM(const char Si[],const char Ti[],int N,int M)35 STstrstrNM (const char Si[], const char Ti[], int N, int M)
36 
37 {
38   const char *ss;
39   const char *tt;
40   int i;
41   int k;
42   int NM;
43 
44 /* Null search string matches the first character */
45   if (M == 0)
46     return (char *) Si;
47 
48 /* Outer loop searches for a match to the first character of Ti */
49   NM = N - M;
50   for (i = 0; i <= NM; ++i, ++Si) {
51 
52     if (*Si == *Ti) {
53       /* Inner loop checks the remaining characters in Ti */
54       ss = Si;
55       tt = Ti;
56       for (k = 1; k < M; ++k) {
57 	if (*++ss != *++tt)
58 	  break;
59       }
60       if (k == M)
61 	return (char *) Si;
62     }
63 
64   }
65   return NULL;
66 }
67