findFirstMatchedWord returns the first matched word and its index
(text string, words []string)
| 115 | |
| 116 | // findFirstMatchedWord returns the first matched word and its index |
| 117 | func 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 |
| 141 | func getRuneRange(runeText []rune, offset, limit int) (begin, end int) { |