             (ar/bazel-qualified-package-path closest-build-file)
             (completing-read "build rule: " (ar/bazel-rule-names-in-build-file-path closest-build-file)))))
 
-(defun ar/bazel-build-rule-names (str)
-  "Return build rule names in STR."
-  (let ((start-pos))
-    ;; match all: name = "rulename"
-    (cl-loop for match-pos = (string-match "name *= *\"\\(.*\\)\"" str start-pos)
-             while match-pos
-             collect (match-string 1 str)
-             do (setf start-pos (1+ match-pos)))))
-
 (defun ar/bazel-rule-names-in-build-file-path (file-path)
   "Get rule names in build FILE-PATH."
-  (ar/bazel-build-rule-names (with-temp-buffer
-                               (insert-file-contents file-path)
-                               (buffer-string))))
+  (let ((rule-names))
+    (with-temp-buffer
+      (insert-file-contents file-path)
+      (goto-char 1)
+      ;; match all: name = "rulename"
+      (while (search-forward-regexp "name *= *\"\\(.*\\)\"" nil t 1)
+        (push (match-string-no-properties 1) rule-names)))
+    rule-names))
 
 (defun ar/bazel-qualified-rule-names-in-build-file-path (file-path)
   "Get qualified rule names in build FILE-PATH."


(defun ar/bazel-qualified-package-path (path)
  "Convert PATH to workspace-qualified package: /some/path/workspace/package/BUILD => //package."
  (replace-regexp-in-string (ar/bazel-workspace-path) "//"
                            (string-remove-suffix "/" (file-name-directory (expand-file-name path)))))

(defun ar/bazel-linked-dpath (name)
  "Append NAME to bazel root path and return it."

  "Get all workspace qualified rules.  If FRESH-READ, skip cache."
  (if fresh-read
      (let* ((counter 0)
             (build-files (ar/bazel-workspace-build-files))
             (length (length build-files)))
        (seq-mapcat (lambda (build-file)
                      (setq counter (1+ counter))
                      (message "(%d/%d) %s" counter length
                               (ar/bazel-qualified-package-path build-file))
                      (ar/bazel-qualified-rule-names-in-build-file-path build-file))
                    build-files))
    (ar/bazel--read-rules-cache)))
