[67] | 1 | ;;; nt-string.el --- NT's string utilities |
---|
| 2 | ;; |
---|
[71] | 3 | ;; Copyright (C) 2005-2009 naoya_t. All Rights Reserved. |
---|
[67] | 4 | ;; |
---|
[71] | 5 | ;; Author: naoya_t <naoya.t@aqua.plala.or.jp> |
---|
| 6 | ;; Maintainer: naoya_t <naoya.t@aqua.plala.or.jp> |
---|
| 7 | ;; Primary distribution site: |
---|
| 8 | ;; http://lambdarepos.svnrepository.com/svn/share/lang/elisp/pdicv-mode/trunk |
---|
[67] | 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 | (when n |
---|
| 47 | (when (or (> n s1-length) (> n s2-length)) |
---|
[71] | 48 | (setq n nil strncmp-p nil))) |
---|
| 49 | (when (null n) |
---|
| 50 | (setq n (min s1-length s2-length))) |
---|
| 51 | |
---|
| 52 | (when (zerop n) (throw 'strcmp 0)) |
---|
[67] | 53 | |
---|
[71] | 54 | (while (< i n) |
---|
[67] | 55 | (let ((s1-i (aref s1 i)) |
---|
| 56 | (s2-i (aref s2 i))) |
---|
| 57 | (when (/= s1-i s2-i) (throw 'strcmp (- s1-i s2-i)))) |
---|
| 58 | (setq i (1+ i))) |
---|
[71] | 59 | |
---|
| 60 | ;nʸ����1=s2 |
---|
[67] | 61 | (if strncmp-p |
---|
| 62 | ;; strncmp() |
---|
| 63 | 0 |
---|
| 64 | ;; strcmp() |
---|
| 65 | (throw 'strcmp (- s1-length s2-length))) |
---|
| 66 | );let |
---|
| 67 | 0 |
---|
| 68 | );caught |
---|
| 69 | ) |
---|
| 70 | |
---|
| 71 | (defun nt:replace-in-string*DEPRECATED (str r n) |
---|
| 72 | "replace /r/ in str --> n" |
---|
| 73 | (catch 'replace-in-string |
---|
| 74 | (let* ((result "") |
---|
| 75 | (s-len (length str)) |
---|
| 76 | (r-len (length r)) |
---|
| 77 | (till (- s-len r-len)) |
---|
| 78 | (i 0) (at 0)) |
---|
| 79 | (while (<= i till) |
---|
| 80 | (setq at (string-match r str i)) |
---|
| 81 | (when (null at) (throw 'replace-in-string (concat result (substring str i)))) |
---|
| 82 | |
---|
[71] | 83 | (setq result (concat result (substring str i at) n)) |
---|
[67] | 84 | (setq i (+ at r-len))) |
---|
| 85 | result))) |
---|
[71] | 86 | |
---|
| 87 | (defun nt:replace-all (str regex subst) |
---|
[67] | 88 | "replace /regex/ in str --> subst" |
---|
| 89 | (let ((ofs 0) |
---|
| 90 | (last (length str)) |
---|
| 91 | (result "")) |
---|
| 92 | (catch 'while |
---|
| 93 | (while (< ofs last) |
---|
| 94 | (let ((found-at nil)) |
---|
| 95 | (if (setq found-at (string-match regex str ofs)) |
---|
| 96 | (progn |
---|
| 97 | (setq result (concat result (substring str ofs found-at) subst)) |
---|
| 98 | (setq ofs (match-end 0))) |
---|
| 99 | (progn |
---|
[71] | 100 | (setq result (concat result (substring str ofs last))) |
---|
[67] | 101 | (throw 'while nil)))))) |
---|
| 102 | result)) |
---|
[71] | 103 | |
---|
| 104 | (defun nt:rtrim (str) |
---|
[67] | 105 | "rtrim" |
---|
| 106 | (catch 'rtrim |
---|
| 107 | (let ((i (1- (length str)))) |
---|
| 108 | (while (> i 0) |
---|
| 109 | (when (> (aref str i) #x20) (throw 'rtrim (substring str 0 (1+ i)))) |
---|
| 110 | (-- i))))) |
---|
[71] | 111 | |
---|
| 112 | (defun nt:ltrim (str) |
---|
[67] | 113 | "ltrim" |
---|
| 114 | (catch 'ltrim |
---|
| 115 | (let ((len (length str)) (i 0)) |
---|
| 116 | (while (< i len) |
---|
| 117 | (when (> (aref str i) #x20) (throw 'ltrim (substring str i len))) |
---|
| 118 | (++ i))))) |
---|
[71] | 119 | |
---|
| 120 | ;(defun nt:trim (str) |
---|
[67] | 121 | ; "trim" |
---|
| 122 | ; (nt:ltrim (nt:rtrim str))) |
---|
| 123 | (defmacro nt:trim (str) |
---|
[71] | 124 | "trim" |
---|
[67] | 125 | `(nt:ltrim (nt:rtrim ,str))) |
---|
| 126 | |
---|
[71] | 127 | ;;; nt-string.el ends here |
---|