{"record":{"id":"29b79a46933fbd69","repo":"TheAlgorithms/Go","slug":"pattern-was-not-found-in-the-input-string","errorCode":null,"errorMessage":"pattern was not found in the input string","messagePattern":"pattern was not found in the input string","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"strings/horspool/horspool.go","lineNumber":8,"sourceCode":"// Implementation of the\n// [Boyer–Moore–Horspool algorithm](https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore%E2%80%93Horspool_algorithm)\n\npackage horspool\n\nimport \"errors\"\n\nvar ErrNotFound = errors.New(\"pattern was not found in the input string\")\n\nfunc Horspool(t, p string) (int, error) {\n\t// in order to handle multy-byte character properly\n\t// the input is converted into rune arrays\n\treturn horspool([]rune(t), []rune(p))\n}\n\nfunc horspool(t, p []rune) (int, error) {\n\tshiftMap := computeShiftMap(t, p)\n\tpos := 0\n\tfor pos <= len(t)-len(p) {\n\t\tif isMatch(pos, t, p) {\n\t\t\treturn pos, nil\n\t\t}\n\t\tif pos+len(p) >= len(t) {\n\t\t\t// because the remaining length of the input string\n\t\t\t// is the same as the length of the pattern\n\t\t\t// and it does not match the pattern","sourceCodeStart":1,"sourceCodeEnd":26,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/strings/horspool/horspool.go#L1-L26","documentation":"horspool.ErrNotFound is the sentinel error returned by the Boyer–Moore–Horspool search (Horspool) when the pattern does not occur in the input string. It is also reused by other search helpers (e.g. math/kthnumber.go) as a generic 'not found' sentinel when an index is out of range or no result exists.","triggerScenarios":"Calling horspool.Horspool(t, p) where p never appears in t; calling kthNumber(nums, k) with k < 0 or k >= len(nums), or when the search space is exhausted without finding the kth value.","commonSituations":"Searching user-provided text for a pattern that may legitimately be absent; off-by-one k values (k is zero-based) when selecting the kth smallest/largest number; empty input slices or strings.","solutions":["Treat the returned error as an expected outcome: check errors.Is(err, horspool.ErrNotFound) and handle the miss case","Validate k against len(nums) (0 <= k < len) before calling kthNumber","Verify the pattern and haystack are non-empty and correct before searching"],"exampleFix":"// before\nidx, err := horspool.Horspool(text, pattern) // unhandled\n// after\nidx, err := horspool.Horspool(text, pattern)\nif errors.Is(err, horspool.ErrNotFound) { /* pattern absent — handle */ }","handlingStrategy":"try-catch","validationCode":"if k < 0 || k >= len(nums) {\n    // out-of-range: would yield ErrNotFound\n}\nif pattern == \"\" || text == \"\" {\n    // empty inputs: pattern cannot be found\n}","typeGuard":null,"tryCatchPattern":"idx, err := horspool.Horspool(text, pattern)\nif errors.Is(err, horspool.ErrNotFound) {\n    // not-found is an expected result, not a failure\n    return -1 // or sentinel of your own\n} else if err != nil {\n    return err\n}","preventionTips":["Always use errors.Is against the sentinel, never string comparison","Clamp k to [0, len(nums)-1] before kthNumber","Check for empty pattern/haystack before searching"],"tags":["string-search","sentinel-error","not-found"],"backgroundTag":"pattern-not-found","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}