Example #1
0
// main entry point
func main() {

	flag.Parse()

	if len(flag.Args()) < 2 {
		usage()
	}
	word := flag.Args()[0]

	// open file and read it into string
	raw_content, err := ioutil.ReadFile(flag.Args()[1])
	if err != nil {
		log.Fatal("Failed to open and read file.")
	}

	content := sp.Preprocess_string(string(raw_content))
	matches := get_matches(string(raw_content), word)
	for _, match := range matches {

		match_index := len(content) - len(match)
		if colored_output {
			fmt.Print(content[match_index-num_context : match_index])
			fmt.Print("\033[1;31m",
				content[match_index:match_index+len(word)], "\033[0m")
			fmt.Println(content[match_index+len(word) : len(word)+match_index+num_context])
		} else {
			fmt.Println(content[match_index-num_context : len(word)+match_index+num_context])
		}
	}
}
Example #2
0
// lrs_h is a helper function for computing the longest repeated
// substring
func lrs_h(raw_content string) string {

	content := sp.Preprocess_string(raw_content)
	prefix_array := sp.Make_prefix_array(content)
	sp.String_quicksort(prefix_array)
	return longest_repeated_substring(prefix_array)
}
Example #3
0
// get_matches returns all matches for word filtered according
// to the provided command line flags
func get_matches(input, word string) []string {

	// process, create prefix array and sort
	content := sp.Preprocess_string(string(input))
	prefix_array := sp.Make_prefix_array(content)
	sp.String_quicksort(prefix_array)

	matches := make([]string, 0)
	length := len(content)
	i := sp.Binary_search(prefix_array, word)

	// found a match
	if i >= 0 {

		raw_matches := sp.Scan_array(prefix_array, word, i)
		if exact_match {
			for _, match := range raw_matches {
				match_index := length - len(match)

				// throw out matches starting in the middle of a word
				if match_index != 0 && content[match_index-1] != ' ' {
					continue
				} else if match_index <= length-2 &&
					content[match_index+len(word)] != ' ' {
					continue
				} else {
					matches = append(matches, match)
				}
			}
		} else {
			matches = raw_matches
		}
	}

	return matches
}