From 67dcd04d033749a13f3ff07169b827fdfa74992c Mon Sep 17 00:00:00 2001 From: Alex Shinn Date: Wed, 29 Jul 2020 11:01:56 +0900 Subject: [PATCH] speedup snow search --- lib/chibi/snow/commands.scm | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/chibi/snow/commands.scm b/lib/chibi/snow/commands.scm index 2a9b4159..c8e61f85 100644 --- a/lib/chibi/snow/commands.scm +++ b/lib/chibi/snow/commands.scm @@ -1046,13 +1046,18 @@ (map car lib-names+pkgs) (map cdr lib-names+pkgs))) -;; faster than (length (regexp-extract re str)) -(define (regexp-count re str) - (regexp-fold re (lambda (from md str acc) (+ acc 1)) 0 str)) +(define (string-count-word str word) + (let lp ((sc (string-cursor-start str)) (count 0)) + (let ((sc2 (string-contains str word sc))) + (if sc2 + (lp (string-cursor-next str sc2) (+ count 1)) + count)))) (define (count-in-sexp x keywords) - (regexp-count `(word (w/nocase (or ,@keywords))) - (write-to-string x))) + (let ((s (string-downcase (write-to-string x)))) + (fold (lambda (k sum) (+ sum (string-count-word s k))) + 0 + (map string-downcase keywords)))) (define (extract-matching-libraries cfg repo keywords) (define (library-score lib)