4
|
1 ;;; tl-str.el --- Emacs Lisp Library module about string
|
|
2
|
70
|
3 ;; Copyright (C) 1995,1996 Free Software Foundation, Inc.
|
4
|
4
|
|
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
|
70
|
6 ;; Version:
|
82
|
7 ;; $Id: tl-str.el,v 1.2 1997/01/23 05:29:41 steve Exp $
|
4
|
8 ;; Keywords: string
|
|
9
|
|
10 ;; This file is part of tl (Tiny Library).
|
|
11
|
|
12 ;; This program is free software; you can redistribute it and/or
|
|
13 ;; modify it under the terms of the GNU General Public License as
|
|
14 ;; published by the Free Software Foundation; either version 2, or (at
|
|
15 ;; your option) any later version.
|
|
16
|
|
17 ;; This program is distributed in the hope that it will be useful, but
|
|
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
20 ;; General Public License for more details.
|
|
21
|
|
22 ;; You should have received a copy of the GNU General Public License
|
|
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
25 ;; Boston, MA 02111-1307, USA.
|
|
26
|
|
27 ;;; Code:
|
|
28
|
|
29 (require 'emu)
|
|
30 (require 'tl-list)
|
|
31
|
|
32
|
|
33 ;;; @ converter
|
|
34 ;;;
|
|
35
|
|
36 (defun expand-char-ranges (str)
|
|
37 (let ((i 0)
|
|
38 (len (length str))
|
|
39 chr pchr nchr
|
|
40 (dest ""))
|
|
41 (while (< i len)
|
|
42 (setq chr (elt str i))
|
|
43 (cond ((and pchr (eq chr ?-))
|
|
44 (setq pchr (1+ pchr))
|
|
45 (setq i (1+ i))
|
|
46 (setq nchr (elt str i))
|
|
47 (while (<= pchr nchr)
|
|
48 (setq dest (concat dest (char-to-string pchr)))
|
|
49 (setq pchr (1+ pchr))
|
|
50 )
|
|
51 )
|
|
52 (t
|
|
53 (setq dest (concat dest (char-to-string chr)))
|
|
54 ))
|
|
55 (setq pchr chr)
|
|
56 (setq i (1+ i))
|
|
57 )
|
|
58 dest))
|
|
59
|
|
60
|
|
61 ;;; @ space
|
|
62 ;;;
|
|
63
|
|
64 (defun eliminate-top-spaces (str)
|
|
65 "Eliminate top sequence of space or tab and return it. [tl-str.el]"
|
|
66 (if (string-match "^[ \t]+" str)
|
|
67 (substring str (match-end 0))
|
|
68 str))
|
|
69
|
|
70 (defun eliminate-last-spaces (str)
|
|
71 "Eliminate last sequence of space or tab and return it. [tl-str.el]"
|
|
72 (if (string-match "[ \t]+$" str)
|
|
73 (substring str 0 (match-beginning 0))
|
|
74 str))
|
|
75
|
|
76 (defun replace-space-with-underline (str)
|
|
77 (mapconcat (function
|
|
78 (lambda (arg)
|
|
79 (char-to-string
|
|
80 (if (eq arg ?\ )
|
|
81 ?_
|
|
82 arg)))) str "")
|
|
83 )
|
|
84
|
|
85
|
|
86 ;;; @ version
|
|
87 ;;;
|
|
88
|
|
89 (defun version-to-list (str)
|
|
90 (if (string-match "[0-9]+" str)
|
|
91 (let ((dest
|
|
92 (list
|
|
93 (string-to-number
|
|
94 (substring str (match-beginning 0)(match-end 0))
|
|
95 ))))
|
|
96 (setq str (substring str (match-end 0)))
|
|
97 (while (string-match "^\\.[0-9]+" str)
|
|
98 (setq dest
|
|
99 (cons
|
|
100 (string-to-number
|
|
101 (substring str (1+ (match-beginning 0))(match-end 0)))
|
|
102 dest))
|
|
103 (setq str (substring str (match-end 0)))
|
|
104 )
|
|
105 (nreverse dest)
|
|
106 )))
|
|
107
|
|
108 (defun version< (v1 v2)
|
|
109 (or (listp v1)
|
|
110 (setq v1 (version-to-list v1))
|
|
111 )
|
|
112 (or (listp v2)
|
|
113 (setq v2 (version-to-list v2))
|
|
114 )
|
|
115 (catch 'tag
|
|
116 (while (and v1 v2)
|
|
117 (cond ((< (car v1)(car v2))
|
|
118 (throw 'tag v2)
|
|
119 )
|
|
120 ((> (car v1)(car v2))
|
|
121 (throw 'tag nil)
|
|
122 ))
|
|
123 (setq v1 (cdr v1)
|
|
124 v2 (cdr v2))
|
|
125 )
|
|
126 v2))
|
|
127
|
|
128 (defun version<= (v1 v2)
|
|
129 (or (listp v1)
|
|
130 (setq v1 (version-to-list v1))
|
|
131 )
|
|
132 (or (listp v2)
|
|
133 (setq v2 (version-to-list v2))
|
|
134 )
|
|
135 (catch 'tag
|
|
136 (while (and v1 v2)
|
|
137 (cond ((< (car v1)(car v2))
|
|
138 (throw 'tag v2)
|
|
139 )
|
|
140 ((> (car v1)(car v2))
|
|
141 (throw 'tag nil)
|
|
142 ))
|
|
143 (setq v1 (cdr v1)
|
|
144 v2 (cdr v2))
|
|
145 )
|
|
146 (or v2 (and (null v1)(null v2)))
|
|
147 ))
|
|
148
|
|
149 (defun version> (v1 v2)
|
|
150 (or (listp v1)
|
|
151 (setq v1 (version-to-list v1))
|
|
152 )
|
|
153 (or (listp v2)
|
|
154 (setq v2 (version-to-list v2))
|
|
155 )
|
|
156 (catch 'tag
|
|
157 (while (and v1 v2)
|
|
158 (cond ((> (car v1)(car v2))
|
|
159 (throw 'tag v1)
|
|
160 )
|
|
161 ((< (car v1)(car v2))
|
|
162 (throw 'tag nil)
|
|
163 ))
|
|
164 (setq v1 (cdr v1)
|
|
165 v2 (cdr v2))
|
|
166 )
|
|
167 v1))
|
|
168
|
|
169 (defun version>= (v1 v2)
|
|
170 (or (listp v1)
|
|
171 (setq v1 (version-to-list v1))
|
|
172 )
|
|
173 (or (listp v2)
|
|
174 (setq v2 (version-to-list v2))
|
|
175 )
|
|
176 (catch 'tag
|
|
177 (while (and v1 v2)
|
|
178 (cond ((> (car v1)(car v2))
|
|
179 (throw 'tag v1)
|
|
180 )
|
|
181 ((< (car v1)(car v2))
|
|
182 (throw 'tag nil)
|
|
183 ))
|
|
184 (setq v1 (cdr v1)
|
|
185 v2 (cdr v2))
|
|
186 )
|
|
187 (or v1 (and (null v1)(null v2)))
|
|
188 ))
|
|
189
|
|
190
|
|
191 ;;; @ RCS version
|
|
192 ;;;
|
|
193
|
|
194 (defun get-version-string (id)
|
|
195 "Return a version-string from RCS ID. [tl-str.el]"
|
|
196 (and (string-match ",v \\([0-9][0-9.][0-9.]+\\)" id)
|
|
197 (substring id (match-beginning 1)(match-end 1))
|
|
198 ))
|
|
199
|
|
200
|
|
201 ;;; @ file name
|
|
202 ;;;
|
|
203
|
|
204 (defun file-name-non-extension (filename)
|
|
205 (if (string-match "\\.[^.]+$" filename)
|
|
206 (substring filename 0 (match-beginning 0))
|
|
207 filename))
|
|
208
|
82
|
209 (autoload 'replace-as-filename "filename"
|
|
210 "Return safety filename from STRING. [filename.el]")
|
4
|
211
|
|
212 ;;; @ symbol
|
|
213 ;;;
|
|
214
|
|
215 (defun symbol-concat (&rest args)
|
|
216 "Return a symbol whose name is concatenation of arguments ARGS
|
|
217 which are string or symbol. [tl-str.el]"
|
|
218 (intern (apply (function concat)
|
|
219 (mapcar (function
|
|
220 (lambda (s)
|
|
221 (cond ((symbolp s) (symbol-name s))
|
|
222 ((stringp s) s)
|
|
223 )
|
|
224 ))
|
|
225 args)))
|
|
226 )
|
|
227
|
|
228
|
|
229 ;;; @ matching
|
|
230 ;;;
|
|
231
|
|
232 (defun top-string-match (pat str)
|
|
233 "Return a list (MATCHED REST) if string PAT is top substring of
|
|
234 string STR. [tl-str.el]"
|
|
235 (if (string-match
|
|
236 (concat "^" (regexp-quote pat))
|
|
237 str)
|
|
238 (list pat (substring str (match-end 0)))
|
|
239 ))
|
|
240
|
|
241 (defun middle-string-match (pat str)
|
|
242 "Return a list (PREVIOUS MATCHED REST) if string PAT is found in
|
|
243 string STR. [tl-str.el]"
|
|
244 (if (equal pat str)
|
|
245 (list nil pat nil)
|
|
246 (if (string-match (regexp-quote pat) str)
|
|
247 (let ((b (match-beginning 0))
|
|
248 (e (match-end 0)) )
|
|
249 (list (if (not (= b 0))
|
|
250 (substring str 0 b)
|
|
251 )
|
|
252 pat
|
|
253 (if (> (length str) e)
|
|
254 (substring str e)
|
|
255 )
|
|
256 )))))
|
|
257
|
|
258 (defun re-top-string-match (pat str)
|
|
259 "Return a list (MATCHED REST) if regexp PAT is matched as top
|
|
260 substring of string STR. [tl-str.el]"
|
|
261 (if (string-match (concat "^" pat) str)
|
|
262 (let ((e (match-end 0)))
|
|
263 (list (substring str 0 e)(substring str e))
|
|
264 )))
|
|
265
|
|
266
|
|
267 ;;; @ compare
|
|
268 ;;;
|
|
269
|
|
270 (defun string-compare-from-top (str1 str2)
|
|
271 (let* ((len1 (length str1))
|
|
272 (len2 (length str2))
|
|
273 (len (min len1 len2))
|
|
274 (p 0)
|
|
275 c1 c2)
|
|
276 (while (and (< p len)
|
|
277 (progn
|
|
278 (setq c1 (sref str1 p)
|
|
279 c2 (sref str2 p))
|
|
280 (eq c1 c2)
|
|
281 ))
|
|
282 (setq p (+ p (char-length c1)))
|
|
283 )
|
|
284 (and (> p 0)
|
|
285 (let ((matched (substring str1 0 p))
|
|
286 (r1 (and (< p len1)(substring str1 p)))
|
|
287 (r2 (and (< p len2)(substring str2 p)))
|
|
288 )
|
|
289 (if (eq r1 r2)
|
|
290 matched
|
|
291 (list 'seq matched (list 'or r1 r2))
|
|
292 )))))
|
|
293
|
|
294
|
|
295 ;;; @ regexp
|
|
296 ;;;
|
|
297
|
|
298 (defun regexp-* (regexp)
|
|
299 (concat regexp "*"))
|
|
300
|
|
301 (defun regexp-or (&rest args)
|
|
302 (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
|
|
303
|
|
304
|
|
305 ;;; @ end
|
|
306 ;;;
|
|
307
|
|
308 (provide 'tl-str)
|
|
309
|
|
310 ;;; tl-str.el ends here
|