comparison lisp/subr.el @ 1425:74cb069b8417

[xemacs-hg @ 2003-04-23 15:42:44 by stephent] stale match data <87fzo99rje.fsf@tleepslib.sk.tsukuba.ac.jp> new split-string <87d6jd9qis.fsf@tleepslib.sk.tsukuba.ac.jp> support (info "(file)node") <87adeh9qa7.fsf@tleepslib.sk.tsukuba.ac.jp>
author stephent
date Wed, 23 Apr 2003 15:42:52 +0000
parents c9b6a2fec10d
children c3cf7db99b98
comparison
equal deleted inserted replaced
1424:c35e2ad2f97d 1425:74cb069b8417
605 (set-text-properties 0 (length result) nil result) 605 (set-text-properties 0 (length result) nil result)
606 result) 606 result)
607 (buffer-substring-no-properties (match-beginning num) 607 (buffer-substring-no-properties (match-beginning num)
608 (match-end num))))) 608 (match-end num)))))
609 609
610 (defun split-string (string &optional separators) 610 (defconst split-string-default-separators "[ \f\t\n\r\v]+"
611 "Splits STRING into substrings where there are matches for SEPARATORS. 611 "The default value of separators for `split-string'.
612 Each match for SEPARATORS is a splitting point. 612
613 The substrings between the splitting points are made into a list 613 A regexp matching strings of whitespace. May be locale-dependent
614 \(as yet unimplemented). Should not match non-breaking spaces.
615
616 Warning: binding this to a different value and using it as default is
617 likely to have undesired semantics.")
618
619 ;; specification for `split-string' agreed with rms 2003-04-23
620 ;; xemacs design <87vfx5vor0.fsf@tleepslib.sk.tsukuba.ac.jp>
621
622 (defun split-string (string &optional separators omit-nulls)
623 "Splits STRING into substrings bounded by matches for SEPARATORS.
624
625 The beginning and end of STRING, and each match for SEPARATORS, are
626 splitting points. The substrings matching SEPARATORS are removed, and
627 the substrings between the splitting points are collected as a list,
614 which is returned. 628 which is returned.
615 If SEPARATORS is absent, it defaults to \"[ \\f\\t\\n\\r\\v]+\". 629
616 630 If SEPARATORS is nil, it defaults to the value of
617 If there is match for SEPARATORS at the beginning of STRING, we do not 631 `split-string-default-separators', normally \"[ \\f\\t\\n\\r\\v]+\".
618 include a null substring for that. Likewise, if there is a match 632
619 at the end of STRING, we don't include a null substring for that. 633 If OMIT-NULLs is t, zero-length substrings are omitted from the list \(so
634 that for the default value of SEPARATORS leading and trailing whitespace
635 are effectively trimmed). If nil, all zero-length substrings are retained,
636 which correctly parses CSV format, for example.
637
638 As a special case, if both SEPARATORS and OMIT-NULLS are nil, white-space
639 will be trimmed (ie, the effect of `(split-string STRING)' is the same as
640 `(split-string STRING split-string-default-separators t)'). In the very
641 rare case that you need to retain zero-length substrings when splitting on
642 the default separators, use
643 `(split-string STRING split-string-default-separators)'.
620 644
621 Modifies the match data; use `save-match-data' if necessary." 645 Modifies the match data; use `save-match-data' if necessary."
622 (let ((rexp (or separators "[ \f\t\n\r\v]+")) 646
647 (let ((keep-nulls (if separators (not omit-nulls) nil))
648 (rexp (or separators split-string-default-separators))
623 (start 0) 649 (start 0)
624 notfirst 650 notfirst
625 (list nil)) 651 (list nil))
626 (while (and (string-match rexp string 652 (while (and (string-match rexp string
627 (if (and notfirst 653 (if (and notfirst
628 (= start (match-beginning 0)) 654 (= start (match-beginning 0))
629 (< start (length string))) 655 (< start (length string)))
630 (1+ start) start)) 656 (1+ start) start))
631 (< (match-beginning 0) (length string))) 657 (< start (length string)))
632 (setq notfirst t) 658 (setq notfirst t)
633 (or (eq (match-beginning 0) 0) 659 (if (or keep-nulls (< start (match-beginning 0)))
634 (and (eq (match-beginning 0) (match-end 0))
635 (eq (match-beginning 0) start))
636 (setq list 660 (setq list
637 (cons (substring string start (match-beginning 0)) 661 (cons (substring string start (match-beginning 0))
638 list))) 662 list)))
639 (setq start (match-end 0))) 663 (setq start (match-end 0)))
640 (or (eq start (length string)) 664 (if (or keep-nulls (< start (length string)))
641 (setq list 665 (setq list
642 (cons (substring string start) 666 (cons (substring string start)
643 list))) 667 list)))
644 (nreverse list))) 668 (nreverse list)))
645 669