From 22c1f00d997d38ba0c453da5f5e9c526d0ac05b0 Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Wed, 23 Dec 2020 07:45:19 +0100 Subject: Allow string-slice to take zero-length matches * lisp/emacs-lisp/subr-x.el (string-slice): Allow zero-length matches. Code adapted from s.el by Magnar Sveen. --- lisp/emacs-lisp/subr-x.el | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) (limited to 'lisp/emacs-lisp') diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el index 09c4649817a..8a9424cbb3d 100644 --- a/lisp/emacs-lisp/subr-x.el +++ b/lisp/emacs-lisp/subr-x.el @@ -308,19 +308,16 @@ If OMIT-NULLS, empty lines will be removed from the results." (defun string-slice (string regexp) "Split STRING at REGEXP boundaries and return a list of slices. -The boundaries that match REGEXP are included in the result." - (let ((start-substring 0) - (start-search 0) - (result nil)) - (save-match-data - (while (string-match regexp string start-search) - (if (zerop (match-beginning 0)) - (setq start-search (match-end 0)) - (push (substring string start-substring (match-beginning 0)) result) - (setq start-substring (match-beginning 0) - start-search (match-end 0)))) - (push (substring string start-substring) result) - (nreverse result)))) +The boundaries that match REGEXP are included in the result. + +Also see `split-string'." + (if (zerop (length string)) + (list "") + (let ((i (string-match-p regexp string 1))) + (if i + (cons (substring string 0 i) + (string-slice (substring string i) regexp)) + (list string))))) (defun string-pad (string length &optional padding start) "Pad STRING to LENGTH using PADDING. -- cgit v1.2.3