root/lang/elisp/pdicv-mode/trunk/nt-string.el @ 67

Revision 67, 3.5 kB (checked in by naoya_t, 16 years ago)
Line 
1;;; nt-string.el --- NT's string utilities
2
3;;
4;; Copyright (C) 2005 Naoya TOZUKA. All Rights Reserved.
5;;
6;; Author: Naoya TOZUKA <pdicviewer@gmail.com>
7;; Maintainer: Naoya TOZUKA <pdicviewer@gmail.com>
8;; Primary distribution site: http://pdicviewer.naochan.com/el/
9;;
10;; Created: 16 Feb 2005
11;; Last modified: 15 Dec 2005
12;; Version: 1.0
13;; Keywords: replace-in-string strcmp trim
14
15(provide 'nt-string)
16
17;;; Commentary:
18;
19; (nt:strcmp S1 S2)
20; (nt:strncmp S1 S2 N)
21;   - C��� strcmp(), strncmp() �ؿ���ߥ����Ȥ�����;
22; (nt:replace-all STR REGEXP SUBST)
23; (nt:replace-in-string*DEPRECATED STR REGEXP SUBST)
24;   - ʸ��TR ��Ρ����ɽ�� REGEXP �˰�����ʬ�򤹤٤�;     ��ʸ��UBST ��ִ����롣
25;
26; (nt:rtrim STR)
27; (nt:ltrim STR)
28; (nt:trim STR)
29;   - ʸ��TR �α�¦����¦������ξ��ζ���롣
30
31;;; Code:
32(require 'nt-macros)
33
34(defmacro nt:strncmp (s1 s2 n)
35  "strncmp()"
36  `(nt:strcmp ,s1 ,s2 ,n))
37
38(defun nt:strcmp (s1 s2 &optional n)
39  "strcmp()"
40  (catch 'strcmp
41    (let* ((s1-length (length s1))
42           (s2-length (length s2))
43           (strncmp-p n) ; (not (null n))) ; t/nil
44           (i 0))
45
46      (if n
47          (if (or (> n s1-length) (> n s2-length))
48              (setq n nil strncmp-p nil)))
49      (if (null n)
50          (setq n (min s1-length s2-length)))
51
52      (if (zerop n) (throw 'strcmp 0))
53
54
55      (while (< i n)
56        (let ((s1-i (aref s1 i))
57              (s2-i (aref s2 i)))
58          (if (/= s1-i s2-i) (throw 'strcmp (- s1-i s2-i)))
59          );let
60        (setq i (1+ i))
61        );wend
62
63      ;nʸ����1=s2
64      (if strncmp-p
65          ;; strncmp()
66          0
67        ;; strcmp()
68        (throw 'strcmp (- s1-length s2-length)))
69      );let
70    0
71    );caught
72  )
73
74(defun nt:replace-in-string*DEPRECATED (str r n)
75  "replace /r/ in str --> n"
76  (catch 'replace-in-string
77    (let* ((result "")
78           (s-len (length str))
79           (r-len (length r))
80           (till (- s-len r-len))
81           (i 0) (at 0))
82      (while (<= i till)
83        (setq at (string-match r str i))
84        (if (null at) (throw 'replace-in-string (concat result (substring str i))))
85
86        (setq result (concat result (substring str i at) n))
87        (setq i (+ at r-len))
88        ); wend
89      result
90      ); let
91    ); caught
92  )
93
94(defun nt:replace-all (str regex subst)
95  "replace /regex/ in str --> subst"
96  (let ((ofs 0)
97        (last (length str))
98        (result ""))
99    (catch 'while
100      (while (< ofs last)
101        (let ((found-at nil))
102          (if (setq found-at (string-match regex str ofs))
103              (progn
104                (setq result (concat result (substring str ofs found-at) subst))
105                (setq ofs (match-end 0))
106                )
107            (progn
108              (setq result (concat result (substring str ofs last)))
109              (throw 'while nil)
110              ))
111          );let
112        );wend
113      );caught
114    result
115    );let
116  )
117
118(defun nt:rtrim (str)
119  "rtrim"
120  (catch 'rtrim
121    (let ((i (1- (length str))))
122      (while (> i 0)
123        (if (> (aref str i) #x20) (throw 'rtrim (substring str 0 (1+ i))))
124        (-- i)
125        );wend
126      );let
127    );caught
128  )
129
130(defun nt:ltrim (str)
131  "ltrim"
132  (catch 'ltrim
133    (let ((len (length str)) (i 0))
134      (while (< i len)
135        (if (> (aref str i) #x20) (throw 'ltrim (substring str i len)))
136        (++ i)
137        );wend
138      );let
139    );caught
140  )
141
142;(defun nt:trim (str)
143;  "trim"
144;  (nt:ltrim (nt:rtrim str))
145;  )
146(defmacro nt:trim (str)
147  "trim"
148  `(nt:ltrim (nt:rtrim ,str))
149  )
150
151;;; nt-string.el ends here
Note: See TracBrowser for help on using the browser.