MCPcopy Index your code
hub / github.com/apache/answer / findFirstMatchedWord

Function findFirstMatchedWord

pkg/htmltext/htmltext.go:117–138  ·  view source on GitHub ↗

findFirstMatchedWord returns the first matched word and its index

(text string, words []string)

Source from the content-addressed store, hash-verified

115
116// findFirstMatchedWord returns the first matched word and its index
117func findFirstMatchedWord(text string, words []string) (string, int) {
118 if len(text) == 0 || len(words) == 0 {
119 return "", 0
120 }
121
122 words = converter.UniqueArray(words)
123 firstWord := ""
124 firstIndex := len(text)
125
126 for _, word := range words {
127 if idx := strings.Index(text, word); idx != -1 && idx < firstIndex {
128 firstIndex = idx
129 firstWord = word
130 }
131 }
132
133 if firstIndex != len(text) {
134 return firstWord, firstIndex
135 }
136
137 return "", 0
138}
139
140// getRuneRange returns the valid begin and end indexes of the runeText
141func getRuneRange(runeText []rune, offset, limit int) (begin, end int) {

Callers 2

FetchMatchedExcerptFunction · 0.85
TestFindFirstMatchedWordFunction · 0.85

Calls 2

UniqueArrayFunction · 0.92
IndexMethod · 0.45

Tested by 1

TestFindFirstMatchedWordFunction · 0.68