4
|
1 ;;; tl-str.el --- Emacs Lisp Library module about string
|
|
2
|
|
3 ;; Copyright (C) 1995,1996 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
|
|
6 ;; Version:
|
|
7 ;; $Id: tl-str.el,v 1.1.1.1 1996/12/18 03:55:31 steve Exp $
|
|
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
|
|
209 (defvar filename-special-char-range
|
|
210 (nconc '((0 . 31))
|
|
211 (string-to-int-list "!\"$")
|
|
212 (list (cons (char-int ?&) (char-int ?*)))
|
|
213 (string-to-int-list "/;<>?")
|
|
214 (list (cons (char-int ?\[) (char-int ?^)))
|
|
215 (string-to-int-list "`")
|
|
216 (list (cons (char-int ?{) (char-int ?})))
|
|
217 '((127 . 159)))
|
|
218 "*Range of characters which is not available in file name. [tl-str.el]")
|
|
219
|
|
220 (defvar filename-space-char-range '(9 32 160)
|
|
221 "*Range of characters which indicates space. These characters
|
|
222 are replaced to `_' by function `replace-as-filename' [tl-str.el]")
|
|
223
|
|
224 (defun replace-as-filename (str)
|
|
225 "Return safety filename from STR. [tl-str.el]"
|
|
226 (let (sf)
|
|
227 (mapconcat (function
|
|
228 (lambda (chr)
|
|
229 (cond ((member-of-range chr filename-space-char-range)
|
|
230 (if sf
|
|
231 ""
|
|
232 (setq sf t)
|
|
233 "_"))
|
|
234 ((member-of-range chr filename-special-char-range)
|
|
235 "")
|
|
236 (t
|
|
237 (setq sf nil)
|
|
238 (char-to-string chr)
|
|
239 ))
|
|
240 ))
|
|
241 (string-to-char-list str)
|
|
242 "")))
|
|
243
|
|
244
|
|
245 ;;; @ symbol
|
|
246 ;;;
|
|
247
|
|
248 (defun symbol-concat (&rest args)
|
|
249 "Return a symbol whose name is concatenation of arguments ARGS
|
|
250 which are string or symbol. [tl-str.el]"
|
|
251 (intern (apply (function concat)
|
|
252 (mapcar (function
|
|
253 (lambda (s)
|
|
254 (cond ((symbolp s) (symbol-name s))
|
|
255 ((stringp s) s)
|
|
256 )
|
|
257 ))
|
|
258 args)))
|
|
259 )
|
|
260
|
|
261
|
|
262 ;;; @ matching
|
|
263 ;;;
|
|
264
|
|
265 (defun top-string-match (pat str)
|
|
266 "Return a list (MATCHED REST) if string PAT is top substring of
|
|
267 string STR. [tl-str.el]"
|
|
268 (if (string-match
|
|
269 (concat "^" (regexp-quote pat))
|
|
270 str)
|
|
271 (list pat (substring str (match-end 0)))
|
|
272 ))
|
|
273
|
|
274 (defun middle-string-match (pat str)
|
|
275 "Return a list (PREVIOUS MATCHED REST) if string PAT is found in
|
|
276 string STR. [tl-str.el]"
|
|
277 (if (equal pat str)
|
|
278 (list nil pat nil)
|
|
279 (if (string-match (regexp-quote pat) str)
|
|
280 (let ((b (match-beginning 0))
|
|
281 (e (match-end 0)) )
|
|
282 (list (if (not (= b 0))
|
|
283 (substring str 0 b)
|
|
284 )
|
|
285 pat
|
|
286 (if (> (length str) e)
|
|
287 (substring str e)
|
|
288 )
|
|
289 )))))
|
|
290
|
|
291 (defun re-top-string-match (pat str)
|
|
292 "Return a list (MATCHED REST) if regexp PAT is matched as top
|
|
293 substring of string STR. [tl-str.el]"
|
|
294 (if (string-match (concat "^" pat) str)
|
|
295 (let ((e (match-end 0)))
|
|
296 (list (substring str 0 e)(substring str e))
|
|
297 )))
|
|
298
|
|
299
|
|
300 ;;; @ compare
|
|
301 ;;;
|
|
302
|
|
303 (defun string-compare-from-top (str1 str2)
|
|
304 (let* ((len1 (length str1))
|
|
305 (len2 (length str2))
|
|
306 (len (min len1 len2))
|
|
307 (p 0)
|
|
308 c1 c2)
|
|
309 (while (and (< p len)
|
|
310 (progn
|
|
311 (setq c1 (sref str1 p)
|
|
312 c2 (sref str2 p))
|
|
313 (eq c1 c2)
|
|
314 ))
|
|
315 (setq p (+ p (char-length c1)))
|
|
316 )
|
|
317 (and (> p 0)
|
|
318 (let ((matched (substring str1 0 p))
|
|
319 (r1 (and (< p len1)(substring str1 p)))
|
|
320 (r2 (and (< p len2)(substring str2 p)))
|
|
321 )
|
|
322 (if (eq r1 r2)
|
|
323 matched
|
|
324 (list 'seq matched (list 'or r1 r2))
|
|
325 )))))
|
|
326
|
|
327
|
|
328 ;;; @ regexp
|
|
329 ;;;
|
|
330
|
|
331 (defun regexp-* (regexp)
|
|
332 (concat regexp "*"))
|
|
333
|
|
334 (defun regexp-or (&rest args)
|
|
335 (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
|
|
336
|
|
337
|
|
338 ;;; @ end
|
|
339 ;;;
|
|
340
|
|
341 (provide 'tl-str)
|
|
342
|
|
343 ;;; tl-str.el ends here
|