summaryrefslogtreecommitdiff
path: root/lisp/emacs-lisp
diff options
context:
space:
mode:
authorLars Ingebrigtsen <larsi@gnus.org>2020-12-23 07:45:19 +0100
committerLars Ingebrigtsen <larsi@gnus.org>2020-12-23 07:45:19 +0100
commit22c1f00d997d38ba0c453da5f5e9c526d0ac05b0 (patch)
tree9775eaaf9aefe1196cea7abd3aa9f586a5c99f4c /lisp/emacs-lisp
parent21097cdd32a29a14ba3b1af55cffb1a9180faf26 (diff)
downloademacs-22c1f00d997d38ba0c453da5f5e9c526d0ac05b0.tar.gz
emacs-22c1f00d997d38ba0c453da5f5e9c526d0ac05b0.tar.bz2
emacs-22c1f00d997d38ba0c453da5f5e9c526d0ac05b0.zip
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.
Diffstat (limited to 'lisp/emacs-lisp')
-rw-r--r--lisp/emacs-lisp/subr-x.el23
1 files changed, 10 insertions, 13 deletions
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.