lastKeywordIndex returns the index of the last occurrence of kw in s that appears at a word boundary (not inside an identifier). Returns -1 if not found. Both s and kw must be uppercase.
(s, kw string)
| 195 | // that appears at a word boundary (not inside an identifier). Returns -1 |
| 196 | // if not found. Both s and kw must be uppercase. |
| 197 | func lastKeywordIndex(s, kw string) int { |
| 198 | search := s |
| 199 | for { |
| 200 | idx := strings.LastIndex(search, kw) |
| 201 | if idx < 0 { |
| 202 | return -1 |
| 203 | } |
| 204 | end := idx + len(kw) |
| 205 | startOk := idx == 0 || !isIdentRune(rune(search[idx-1])) |
| 206 | endOk := end >= len(search) || !isIdentRune(rune(search[end])) |
| 207 | if startOk && endOk { |
| 208 | return idx |
| 209 | } |
| 210 | // Shrink the search window and try again. |
| 211 | search = search[:idx] |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | func isIdentRune(r rune) bool { |
| 216 | return (r >= 'A' && r <= 'Z') || (r >= 'a' && r <= 'z') || |
no test coverage detected