summaryrefslogtreecommitdiff
path: root/s.el
diff options
context:
space:
mode:
authorMatus Goljer <dota.keys@gmail.com>2014-07-11 19:55:40 +0200
committerMatus Goljer <dota.keys@gmail.com>2014-07-11 19:59:08 +0200
commitdc8a787c8472b734547414014d687ba765876e53 (patch)
tree3946b20ffb3619681a5ce4b90f8b6dcfcdf55ff1 /s.el
parente7bd10599ad5884a86fa777cc58ca646b94f05c9 (diff)
Add s-split-up-to
Diffstat (limited to 's.el')
-rw-r--r--s.el26
1 files changed, 26 insertions, 0 deletions
diff --git a/s.el b/s.el
index c0a4154..5b4d191 100644
--- a/s.el
+++ b/s.el
@@ -54,6 +54,32 @@ If OMIT-NULLS is non-nil, zero-length substrings are omitted.
This is a simple wrapper around the built-in `split-string'."
(split-string s separator omit-nulls))
+(defun s-split-up-to (separator s n &optional omit-nulls)
+ "Split S up to N times into substrings bounded by matches for regexp SEPARATOR.
+
+If OMIT-NULLS is non-nil, zero-length substrings are omitted.
+
+See also `s-split'."
+ (save-match-data
+ (let ((op 0)
+ (r nil))
+ (with-temp-buffer
+ (insert s)
+ (setq op (goto-char (point-min)))
+ (while (and (re-search-forward separator nil t)
+ (< 0 n))
+ (let ((sub (buffer-substring-no-properties op (match-beginning 0))))
+ (unless (and omit-nulls
+ (equal sub ""))
+ (push sub r)))
+ (setq op (goto-char (match-end 0)))
+ (setq n (1- n)))
+ (if (/= (point) (point-max))
+ (push (buffer-substring-no-properties op (point-max)) r)
+ (unless omit-nulls
+ (push "" r))))
+ (nreverse r))))
+
(defun s-lines (s)
"Splits S into a list of strings on newline characters."
(s-split "\\(\r\n\\|[\n\r]\\)" s))