annotate lisp/pcl-cvs/string.el @ 128:13db4aa44a2e

Added tag xemacs-20-1p2 for changeset 7b236889ca3a
author cvs
date Mon, 13 Aug 2007 09:28:11 +0200
parents 131b0175ea99
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 0
diff changeset
1 ;;;; $Id: string.el,v 1.1.1.1 1996/12/18 22:42:58 steve Exp $
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 ;;;; This file contains some miscellaneous string functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 ;; Copyright (C) 1991-1995 Free Software Foundation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 ;; Author: Sebastian Kremer <sk@thp.Uni-Koeln.DE>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 ;; Per Cederqvist <ceder@lysator.liu.se>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 ;; Inge Wallin <inge@lysator.liu.se>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 ;; Maintainer: elib-maintainers@lysator.liu.se
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 ;; Created: before 9 May 1991
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 ;; Keywords: extensions, lisp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ;;;; This file is part of the GNU Emacs lisp library, Elib.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 ;;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 ;;;; GNU Elib is free software; you can redistribute it and/or modify
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 ;;;; it under the terms of the GNU General Public License as published by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 ;;;; the Free Software Foundation; either version 2, or (at your option)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 ;;;; any later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 ;;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 ;;;; GNU Elib is distributed in the hope that it will be useful,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 ;;;; GNU General Public License for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 ;;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 ;;;; You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 ;;;; along with GNU Elib; see the file COPYING. If not, write to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 ;;;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 ;;;; Boston, MA 02111-1307, USA
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 ;;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 ;;;; Author: Sebastian Kremer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 ;;;; sk@thp.Uni-Koeln.DE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 ;;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 ;;; Commentary:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 ;;; This file is part of the elisp library Elib.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 ;;; It implements simple generic string functions for use in other
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 ;;; elisp code: replace regexps in strings, split strings on regexps.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 ;;; Code:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 (provide 'string)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 ;; This function is a near-equivalent of the elisp function replace-match
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 ;; which work on strings instead of a buffer. The FIXEDCASE parameter
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 ;; of replace-match is not implemented.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 (defun string-replace-match (regexp string newtext &optional literal global)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 "Replace first match of REGEXP in STRING with NEWTEXT.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 If no match is found, nil is returned instead of the new string.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 Optional arg LITERAL non-nil means to take NEWTEXT literally. If LITERAL is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 nil, character `\\' is the start of one of the following sequences:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 \\\\ will be replaced by a single \\.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 \\& will be replaced by the text which matched the regexp.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 \\N where N is a number and 1 <= N <= 9, will be replaced
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 by the Nth subexpression in REGEXP. Subexpressions are grouped
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 inside \\( \\).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 Optional arg GLOBAL means to replace all matches instead of only the first."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 (let ((data (match-data)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 (unwind-protect
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 (if global
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 (let ((result "")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 (start 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 matchbeginning
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 matchend)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 (while (string-match regexp string start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 (setq matchbeginning (match-beginning 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 matchend (match-end 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 result (concat result
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 (substring string start matchbeginning)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 (if literal
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 newtext
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 (elib-string-expand-newtext)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 start matchend))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 (if matchbeginning ; matched at least once
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 (concat result (substring string start))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 ;; not GLOBAL
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 (if (not (string-match regexp string 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 (concat (substring string 0 (match-beginning 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 (if literal newtext (elib-string-expand-newtext))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 (substring string (match-end 0)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 (store-match-data data))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 (defun elib-string-expand-newtext ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 ;; Expand \& and \1..\9 (referring to STRING) in NEWTEXT.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 ;; Uses match data and fluid vars `newtext', `string'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 ;; Note that in Emacs 18 match data are clipped to current buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 ;; size...so the buffer should better not be smaller than STRING.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 (let ((pos 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 (len (length newtext))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 (expanded-newtext ""))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 (while (< pos len)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 (setq expanded-newtext
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 (concat expanded-newtext
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 (let ((c (aref newtext pos)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 (if (= ?\\ c)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 (cond ((= ?\& (setq c (aref newtext
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 (setq pos (1+ pos)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 (substring string
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 (match-beginning 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 (match-end 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 ((and (>= c ?1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 (<= c ?9))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 ;; return empty string if N'th
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 ;; sub-regexp did not match:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 (let ((n (- c ?0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 (if (match-beginning n)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 (substring string
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 (match-beginning n)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 (match-end n))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 "")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 (t (char-to-string c)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 (char-to-string c)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 (setq pos (1+ pos)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 expanded-newtext))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 (defun string-split (pattern string &optional limit)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 "Splitting on regexp PATTERN, turn string STRING into a list of substrings.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 Optional third arg LIMIT (>= 1) is a limit to the length of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 resulting list."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 (let ((data (match-data)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 (unwind-protect
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 (let* ((start (string-match pattern string))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 (result (list (substring string 0 start)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 (count 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 (end (if start (match-end 0))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 (if end ; else nothing left
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 (while (and (or (not (integerp limit))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 (< count limit))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 (string-match pattern string end))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 (setq start (match-beginning 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 count (1+ count)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 result (cons (substring string end start) result)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 end (match-end 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 start end)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 (if (and (or (not (integerp limit))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 (< count limit))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 end) ; else nothing left
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 (setq result
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 (cons (substring string end) result)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 (nreverse result))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 (store-match-data data))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 ;;; string.el ends here