diff options
| author | Richard M. Stallman | 2013-07-19 08:18:16 -0400 |
|---|---|---|
| committer | Richard M. Stallman | 2013-07-19 08:18:16 -0400 |
| commit | 77c92cb94d91ecad4040c8f14c8413f23aaddd9a (patch) | |
| tree | 424a155e40f8e8b769015bffafcbc75aaf1770e2 /lisp | |
| parent | 621dd9ac0cf31453dc6e40436eae2aebd27d1517 (diff) | |
| download | emacs-77c92cb94d91ecad4040c8f14c8413f23aaddd9a.tar.gz emacs-77c92cb94d91ecad4040c8f14c8413f23aaddd9a.zip | |
split-string takes a new arg TRIM that's a regexp
saying what to trim from the start and end of each substring.
* subr.el (split-string): New arg TRIM.
Diffstat (limited to 'lisp')
| -rw-r--r-- | lisp/ChangeLog | 4 | ||||
| -rw-r--r-- | lisp/subr.el | 63 |
2 files changed, 52 insertions, 15 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 86715d6d695..fd9416acdc9 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog | |||
| @@ -1,3 +1,7 @@ | |||
| 1 | 2013-07-19 Richard Stallman <rms@gnu.org> | ||
| 2 | |||
| 3 | * subr.el (split-string): New arg TRIM. | ||
| 4 | |||
| 1 | 2013-07-18 Juanma Barranquero <lekktu@gmail.com> | 5 | 2013-07-18 Juanma Barranquero <lekktu@gmail.com> |
| 2 | 6 | ||
| 3 | * frame.el (blink-cursor-timer-function, blink-cursor-suspend): | 7 | * frame.el (blink-cursor-timer-function, blink-cursor-suspend): |
diff --git a/lisp/subr.el b/lisp/subr.el index b6ee96f879e..75c6b3a0620 100644 --- a/lisp/subr.el +++ b/lisp/subr.el | |||
| @@ -3529,7 +3529,7 @@ likely to have undesired semantics.") | |||
| 3529 | ;; defaulted, OMIT-NULLS should be treated as t. Simplifying the logical | 3529 | ;; defaulted, OMIT-NULLS should be treated as t. Simplifying the logical |
| 3530 | ;; expression leads to the equivalent implementation that if SEPARATORS | 3530 | ;; expression leads to the equivalent implementation that if SEPARATORS |
| 3531 | ;; is defaulted, OMIT-NULLS is treated as t. | 3531 | ;; is defaulted, OMIT-NULLS is treated as t. |
| 3532 | (defun split-string (string &optional separators omit-nulls) | 3532 | (defun split-string (string &optional separators omit-nulls trim) |
| 3533 | "Split STRING into substrings bounded by matches for SEPARATORS. | 3533 | "Split STRING into substrings bounded by matches for SEPARATORS. |
| 3534 | 3534 | ||
| 3535 | The beginning and end of STRING, and each match for SEPARATORS, are | 3535 | The beginning and end of STRING, and each match for SEPARATORS, are |
| @@ -3547,17 +3547,50 @@ that for the default value of SEPARATORS leading and trailing whitespace | |||
| 3547 | are effectively trimmed). If nil, all zero-length substrings are retained, | 3547 | are effectively trimmed). If nil, all zero-length substrings are retained, |
| 3548 | which correctly parses CSV format, for example. | 3548 | which correctly parses CSV format, for example. |
| 3549 | 3549 | ||
| 3550 | If TRIM is non-nil, it should be a regular expression to match | ||
| 3551 | text to trim from the beginning and end of each substring. If trimming | ||
| 3552 | makes the substring empty, it is treated as null. | ||
| 3553 | |||
| 3554 | If you want to trim whitespace from the substrings, the reliably correct | ||
| 3555 | way is using TRIM. Making SEPARATORS match that whitespace gives incorrect | ||
| 3556 | results when there is whitespace at the start or end of STRING. If you | ||
| 3557 | see such calls to `split-string', please fix them. | ||
| 3558 | |||
| 3550 | Note that the effect of `(split-string STRING)' is the same as | 3559 | Note that the effect of `(split-string STRING)' is the same as |
| 3551 | `(split-string STRING split-string-default-separators t)'. In the rare | 3560 | `(split-string STRING split-string-default-separators t)'. In the rare |
| 3552 | case that you wish to retain zero-length substrings when splitting on | 3561 | case that you wish to retain zero-length substrings when splitting on |
| 3553 | whitespace, use `(split-string STRING split-string-default-separators)'. | 3562 | whitespace, use `(split-string STRING split-string-default-separators)'. |
| 3554 | 3563 | ||
| 3555 | Modifies the match data; use `save-match-data' if necessary." | 3564 | Modifies the match data; use `save-match-data' if necessary." |
| 3556 | (let ((keep-nulls (not (if separators omit-nulls t))) | 3565 | (let* ((keep-nulls (not (if separators omit-nulls t))) |
| 3557 | (rexp (or separators split-string-default-separators)) | 3566 | (rexp (or separators split-string-default-separators)) |
| 3558 | (start 0) | 3567 | (start 0) |
| 3559 | notfirst | 3568 | this-start this-end |
| 3560 | (list nil)) | 3569 | notfirst |
| 3570 | (list nil) | ||
| 3571 | (push-one | ||
| 3572 | ;; Push the substring in range THIS-START to THIS-END | ||
| 3573 | ;; onto LIST, trimming it and perhaps discarding it. | ||
| 3574 | (lambda () | ||
| 3575 | (when trim | ||
| 3576 | ;; Discard the trim from start of this substring. | ||
| 3577 | (let ((tem (string-match trim string this-start))) | ||
| 3578 | (and (eq tem this-start) | ||
| 3579 | (setq this-start (match-end 0))))) | ||
| 3580 | |||
| 3581 | (when (or keep-nulls (< this-start this-end)) | ||
| 3582 | (let ((this (substring string this-start this-end))) | ||
| 3583 | |||
| 3584 | ;; Discard the trim from end of this substring. | ||
| 3585 | (when trim | ||
| 3586 | (let ((tem (string-match (concat trim "\\'") this 0))) | ||
| 3587 | (and tem (< tem (length this)) | ||
| 3588 | (setq this (substring this 0 tem))))) | ||
| 3589 | |||
| 3590 | ;; Trimming could make it empty; check again. | ||
| 3591 | (when (or keep-nulls (> (length this) 0)) | ||
| 3592 | (push this list))))))) | ||
| 3593 | |||
| 3561 | (while (and (string-match rexp string | 3594 | (while (and (string-match rexp string |
| 3562 | (if (and notfirst | 3595 | (if (and notfirst |
| 3563 | (= start (match-beginning 0)) | 3596 | (= start (match-beginning 0)) |
| @@ -3565,15 +3598,15 @@ Modifies the match data; use `save-match-data' if necessary." | |||
| 3565 | (1+ start) start)) | 3598 | (1+ start) start)) |
| 3566 | (< start (length string))) | 3599 | (< start (length string))) |
| 3567 | (setq notfirst t) | 3600 | (setq notfirst t) |
| 3568 | (if (or keep-nulls (< start (match-beginning 0))) | 3601 | (setq this-start start this-end (match-beginning 0) |
| 3569 | (setq list | 3602 | start (match-end 0)) |
| 3570 | (cons (substring string start (match-beginning 0)) | 3603 | |
| 3571 | list))) | 3604 | (funcall push-one)) |
| 3572 | (setq start (match-end 0))) | 3605 | |
| 3573 | (if (or keep-nulls (< start (length string))) | 3606 | ;; Handle the substring at the end of STRING. |
| 3574 | (setq list | 3607 | (setq this-start start this-end (length string)) |
| 3575 | (cons (substring string start) | 3608 | (funcall push-one) |
| 3576 | list))) | 3609 | |
| 3577 | (nreverse list))) | 3610 | (nreverse list))) |
| 3578 | 3611 | ||
| 3579 | (defun combine-and-quote-strings (strings &optional separator) | 3612 | (defun combine-and-quote-strings (strings &optional separator) |