/* +++Date last modified: 05-Jul-1997 */

/*
**        A Pratt-Boyer-Moore string search, written by Jerry Coffin
**  sometime or other in 1991.  Removed from original program, and
**  (incorrectly) rewritten for separate, generic use in early 1992.
**  Corrected with help from Thad Smith, late March and early
**  April 1992...hopefully it's correct this time. Revised by Bob Stout.
**
**  This is hereby placed in the Public Domain by its author.
**
**  10/21/93 rdg  Fixed bug found by Jeff Dunlop
*/

#include <stddef.h>
#include <string.h>
#include <limits.h>

#include "cmsis_os.h"
#include "ImC/imc_extension.h"

/*
**  Call this with the string to locate to initialize the table
*/

#include <stdio.h>

void vStringSearch()
{
      char *here;
      char *find_strings[] = {"abb",  "you", "not", "it", "dad", "yoo", "hoo",
                              "oo", "oh", "xx", "xx", "x", "x", "field", "new", "row",
			      "regime", "boom", "that", "impact", "and", "zoom", "texture",
			      "magnet", "doom", "loom", "freq", "current", "phase",
			      "images", 
			      "appears", "phase", "conductor", "wavez", 
			      "normal", "free", "termed",
			      "provide", "for", "and", "struct", "about", "have",
			      "proper",
			      "involve",
			      "describedly",
			      "thats",
			      "spaces",
			      "circumstance",
			      "the",
			      "member",
			      "such",
			      "guide",
			      "regard",
			      "officers",
			      "implement",
			      "principalities",			      
			      NULL};
      char *search_strings[] = {"cabbie", "your", "It isn't here",
                                "But it is here", "hodad", "yoohoo", "yoohoo",
                                "yoohoo", "yoohoo", "yoohoo", "xx", "x", ".", 
				"In recent years, the field of photonic ",
				"crystals has found new",
				"applications in the RF and microwave",
				"regime. A new type of metallic",
				"electromagnetic crystal has been", 
				"developed that is having a",
				"significant impact on the field of", 
				"antennas. It consists of a",
				"conductive surface, covered with a",
				"special texture which alters its",
				"electromagnetic properties. Made of solid",
				"metal, the structure",
				"conducts DC currents, but over a",
				"particular frequency range it does",
				"not conduct AC currents. It does not",
				"reverse the phase of reflected",
				"waves, and the effective image currents",

				"appear in-phase, rather than",
				"out-of-phase as they are on normal",
				"conductors. Furthermore, surface",
				"waves do not propagate, and instead",
				"radiate efficiently into free",
				"space. This new material, termed a",
				"high-impedance surface, provides",
				"a useful new ground plane for novel",
				"low-profile antennas and other",
				"electromagnetic structures.",
				"The recent protests about the Michigamua",
				"student organization have raised an",
				"important question as to the proper nature",
				"and scope of University involvement",
				"with student organizations. Accordingly",
				"the panel described in my Statement of",
				"February 25, 2000 that is considering the",
				"question of privileged space also will",
				"consider under what circumstances and in", 
				"what ways the University, its",
				"administrators and faculty members should",
				"be associated with such organizations",
				"and it will recommend guiding principles",
				"in this regard. The University's",
				"Executive Officers and I will then decide",
				"whether and how to implement such",
				"principles."			       
};
      int i;
	  int table[UCHAR_MAX + 1];
	  size_t len;
	  char *findme;

    //   while(1) {
	  
	//   int IMC_REPEAT = 5;
	  int IMC_REPEAT = imcBENCH_REPEAT_COUNT;
	  IMC_UNROLL(3)
	  for(int imc_repeat=0; imc_repeat < IMC_REPEAT; imc_repeat++) {

      for (i = 0; find_strings[i]; i++)
      {
            // init_search(find_strings[i], table, &len, &findme);
			size_t j;
			char *string = find_strings[i];
			len = strlen(string);
			for(j=0; j<=UCHAR_MAX; j++) table[j] = len;
			for(j=0; j<len; j++) table[(unsigned char)string[j]] = len - j - 1;
			findme = (char *)string;

            // here = strsearch(search_strings[i], table, len, findme);
			string = search_strings[i];
			register size_t shift;
			register size_t pos = len - 1;
			char *here_local;
			size_t limit=strlen(string);
			int found = 0;

			while(!found && pos < limit) {
				while(pos < limit && (shift = table[(unsigned char)string[pos]]) > 0) {
					pos += shift;
				}
				if(0 == shift) {
					if(0 == strncmp(findme, here_local = (char *)&string[pos-len+1], len)) {
						found = 1;
						here = here_local;
						break;
					}
					else {
						pos++;
					}
				}
			}
			if(!found) here = NULL;

			if(imc_repeat == IMC_REPEAT -1 && find_strings[i+1] == NULL) {
				portDISABLE_INTERRUPTS();
				printf("(OUT) ");
				printf("\"%s\" is%s in \"%s\"", find_strings[i],
					here ? "" : " not", search_strings[i]);
				if (here)
					printf(" [\"%s\"]", here);
				putchar('\r'); putchar('\n');
				portENABLE_INTERRUPTS();
			}
			// for(int j=0; j<1000000; j++) {__asm__(" nop");}
      }

	  } // imc_repeat

    //   }

}

