Created
August 11, 2022 08:08
-
-
Save jasonm23/e6fc1d31d9abb0169bfa04190d4cd3dc to your computer and use it in GitHub Desktop.
Order a list of substring matches from s.el's s-matched-positions-all
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun order-substring-matches (raw-substring-matches) | |
"Order a set of RAW-SUBSTRING-MATCHES. | |
Ordered substrings can then be used to perform replacements | |
on the original source string. | |
The list is sorted last to first, so that string replacements | |
don't invalidate replacements using subsequent substring indexes. | |
Raw substring matches are in the form: | |
'((\"s1\" ((10 . 12) (30 . 32) ...))...) | |
(\"s2\" ((15 . 17) (20 . 22) ...))...) | |
This would become: | |
'((\"s1\" (30 . 33))) | |
(\"s2\" (20 . 22))) | |
(\"s2\" (15 . 17))) | |
(\"s1\" (10 . 13))) | |
" | |
(sort (cl-reduce (lambda (acc entry) | |
(let* ((indexes (cadr entry)) | |
(str (car entry)) | |
(mapped (mapcar (lambda (it) (list str it)) indexes))) | |
(seq-concatenate 'list acc mapped))) | |
raw-substring-matches | |
:initial-value '()) | |
(lambda (it other) (> (caadr it)) (caadr other)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment