8bead9b5573383120cf35282e09b0d0041052152
[jscl.git] / src / string.lisp
1 ;;; string.lisp
2
3 ;; JSCL is free software: you can redistribute it and/or
4 ;; modify it under the terms of the GNU General Public License as
5 ;; published by the Free Software Foundation, either version 3 of the
6 ;; License, or (at your option) any later version.
7 ;;
8 ;; JSCL is distributed in the hope that it will be useful, but
9 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 ;; General Public License for more details.
12 ;;
13 ;; You should have received a copy of the GNU General Public License
14 ;; along with JSCL.  If not, see <http://www.gnu.org/licenses/>.
15
16 (defun string (x)
17   (cond ((stringp x) x)
18         ((symbolp x) (symbol-name x))
19         (t (char-to-string x))))
20
21 (defun string= (s1 s2)
22   (let* ((s1 (string s1))
23          (s2 (string s2))
24          (n (length s1)))
25     (when (= (length s2) n)
26       (dotimes (i n t)
27         (unless (char= (char s1 i) (char s2 i))
28           (return-from string= nil))))))
29
30 (defun string< (s1 s2)
31   (let ((len-1 (length s1))
32         (len-2 (length s2)))
33     (cond ((= len-2 0) nil)
34           ((= len-1 0) 0)
35           (t (dotimes (i len-1 nil)
36                (when (char< (char s1 i) (char s2 i))
37                  (return-from string< i))
38                (when (and (= i (1- len-1)) (> len-2 len-1))
39                  (return-from string< (1+ i))))))))
40
41 (defun stringp (s)
42   (stringp s))
43
44 (define-setf-expander char (string index)
45   (let ((g!string (gensym))
46         (g!index (gensym))
47         (g!value (gensym)))
48     (values (list g!string g!index)
49             (list string index)
50             (list g!value)
51             `(aset ,g!string ,g!index ,g!value)
52             `(char ,g!string ,g!index))))