Skip to content

Commit 7f0331c

Browse files
Copilotjackfirth
andcommitted
Optimize to avoid duplicate regex computation
Co-authored-by: jackfirth <[email protected]>
1 parent ac58fb7 commit 7f0331c

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

default-recommendations/exception-suggestions.rkt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,18 @@ conventions."
4343
(error sym:expr message:str arg:id ...+)
4444

4545
#:do [(define message-str (syntax-e (attribute message)))
46-
(define args-list (attribute arg))]
47-
#:when (= (length (regexp-match* #rx"~a" message-str)) (length args-list))
46+
(define args-list (attribute arg))
47+
(define tilde-a-matches (regexp-match-positions* #rx"~a" message-str))]
48+
#:when (= (length tilde-a-matches) (length args-list))
4849
;; Check that all ~a occurrences are surrounded by spaces or at string boundaries
49-
#:when (let ([matches (regexp-match-positions* #rx"~a" message-str)])
50-
(for/and ([match (in-list matches)])
51-
(define start (car match))
52-
(define end (cdr match))
53-
(define before-ok? (or (= start 0)
54-
(char-whitespace? (string-ref message-str (- start 1)))))
55-
(define after-ok? (or (= end (string-length message-str))
56-
(char-whitespace? (string-ref message-str end))))
57-
(and before-ok? after-ok?)))
50+
#:when (for/and ([match (in-list tilde-a-matches)])
51+
(define start (car match))
52+
(define end (cdr match))
53+
(define before-ok? (or (= start 0)
54+
(char-whitespace? (string-ref message-str (- start 1)))))
55+
(define after-ok? (or (= end (string-length message-str))
56+
(char-whitespace? (string-ref message-str end))))
57+
(and before-ok? after-ok?))
5858
#:do [(define cleaned-message (string-replace message-str "~a" ""))
5959
;; Clean up extra spaces and trailing punctuation from placeholder removal
6060
(define cleaned-message-normalized

0 commit comments

Comments
 (0)