on main help sign in
Merge pull request #351 from charmbracelet/regex-drop-compilation-in-runtime
9820890Raphael Amorim
internal/llm/provider/anthropic.go+4−2
@@ -21,6 +21,9 @@ import (
21 "github.com/charmbracelet/crush/internal/message"
21 "github.com/charmbracelet/crush/internal/message"
22)
22)
23
23
24+// Pre-compiled regex for parsing context limit errors.
25+var contextLimitRegex = regexp.MustCompile(`input length and ` + "`max_tokens`" + ` exceed context limit: (\d+) \+ (\d+) > (\d+)`)
26+
24type anthropicClient struct {
27type anthropicClient struct {
25 providerOptions providerClientOptions
28 providerOptions providerClientOptions
26 useBedrock bool
29 useBedrock bool
@@ -512,8 +515,7 @@ func (a *anthropicClient) handleContextLimitError(apiErr *anthropic.Error) (int,
512 // Parse error message like: "input length and max_tokens exceed context limit: 154978 + 50000 > 200000"
515 // Parse error message like: "input length and max_tokens exceed context limit: 154978 + 50000 > 200000"
513 errorMsg := apiErr.Error()
516 errorMsg := apiErr.Error()
514
517
515− re := regexp.MustCompile("input length and `max_tokens` exceed context limit: (\\d+) \\+ (\\d+) > (\\d+)")
516− matches := re.FindStringSubmatch(errorMsg)
518+ matches := contextLimitRegex.FindStringSubmatch(errorMsg)
517
519
518 if len(matches) != 4 {
520 if len(matches) != 4 {
519 return 0, false
521 return 0, false
internal/llm/tools/view.go+2−1
@@ -306,7 +306,8 @@ func readTextFile(filePath string, offset, limit int) (string, int, error) {
306 }
306 }
307 }
307 }
308
308
309+ // Pre-allocate slice with expected capacity
309− var lines []string
310+ lines := make([]string, 0, limit)
310 lineCount = offset
311 lineCount = offset
311
312
312 for scanner.Scan() && len(lines) < limit {
313 for scanner.Scan() && len(lines) < limit {
internal/tui/exp/list/filterable.go+8−3
@@ -15,6 +15,13 @@ import (
15 "github.com/sahilm/fuzzy"
15 "github.com/sahilm/fuzzy"
16)
16)
17
17
18+var (
19+ // Pre-compiled regex for checking if a string contains alphabetic characters.
20+ alphaRegex = regexp.MustCompile(`[a-zA-Z]`)
21+ // Pre-compiled regex for checking if a string is alphanumeric.
22+ alphanumericRegex = regexp.MustCompile(`^[a-zA-Z0-9]*$`)
23+)
24+
18type FilterableItem interface {
25type FilterableItem interface {
19 Item
26 Item
20 FilterValue() string
27 FilterValue() string
@@ -165,8 +172,6 @@ func (f *filterableList[T]) View() string {
165
172
166// removes bindings that are used for search
173// removes bindings that are used for search
167func (f *filterableList[T]) updateKeyMaps() {
174func (f *filterableList[T]) updateKeyMaps() {
168− alphanumeric := regexp.MustCompile("^[a-zA-Z0-9]*$")
169−
170 removeLettersAndNumbers := func(bindings []string) []string {
175 removeLettersAndNumbers := func(bindings []string) []string {
171 var keep []string
176 var keep []string
172 for _, b := range bindings {
177 for _, b := range bindings {
@@ -177,7 +182,7 @@ func (f *filterableList[T]) updateKeyMaps() {
177 if b == " " {
182 if b == " " {
178 continue
183 continue
179 }
184 }
180− m := alphanumeric.MatchString(b)
185+ m := alphanumericRegex.MatchString(b)
181 if !m {
186 if !m {
182 keep = append(keep, b)
187 keep = append(keep, b)
183 }
188 }
internal/tui/exp/list/filterable_group.go+5−3
@@ -15,6 +15,10 @@ import (
15 "github.com/sahilm/fuzzy"
15 "github.com/sahilm/fuzzy"
16)
16)
17
17
18+// Pre-compiled regex for checking if a string is alphanumeric.
19+// Note: This is duplicated from filterable.go to avoid circular dependencies.
20+var alphanumericRegexGroup = regexp.MustCompile(`^[a-zA-Z0-9]*$`)
21+
18type FilterableGroupList[T FilterableItem] interface {
22type FilterableGroupList[T FilterableItem] interface {
19 GroupedList[T]
23 GroupedList[T]
20 Cursor() *tea.Cursor
24 Cursor() *tea.Cursor
@@ -114,8 +118,6 @@ func (f *filterableGroupList[T]) View() string {
114
118
115// removes bindings that are used for search
119// removes bindings that are used for search
116func (f *filterableGroupList[T]) updateKeyMaps() {
120func (f *filterableGroupList[T]) updateKeyMaps() {
117− alphanumeric := regexp.MustCompile("^[a-zA-Z0-9]*$")
118−
119 removeLettersAndNumbers := func(bindings []string) []string {
121 removeLettersAndNumbers := func(bindings []string) []string {
120 var keep []string
122 var keep []string
121 for _, b := range bindings {
123 for _, b := range bindings {
@@ -126,7 +128,7 @@ func (f *filterableGroupList[T]) updateKeyMaps() {
126 if b == " " {
128 if b == " " {
127 continue
129 continue
128 }
130 }
129− m := alphanumeric.MatchString(b)
131+ m := alphanumericRegexGroup.MatchString(b)
130 if !m {
132 if !m {
131 keep = append(keep, b)
133 keep = append(keep, b)
132 }
134 }