Basic storage vectors operations and array construction
[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 stringp (x)
17 ;;   (and (vectorp x) (eq (array-element-type x) 'character)))
18
19 (defun string (x)
20   (cond ((stringp x) x)
21         ((symbolp x) (symbol-name x))
22         (t (char-to-string x))))
23
24 (defun string= (s1 s2)
25   (let* ((s1 (string s1))
26          (s2 (string s2))
27          (n (length s1)))
28     (when (= (length s2) n)
29       (dotimes (i n t)
30         (unless (char= (char s1 i) (char s2 i))
31           (return-from string= nil))))))
32
33 (defun string< (s1 s2)
34   (let ((len-1 (length s1))
35         (len-2 (length s2)))
36     (cond ((= len-2 0) nil)
37           ((= len-1 0) 0)
38           (t (dotimes (i len-1 nil)
39                (when (char< (char s1 i) (char s2 i))
40                  (return-from string< i))
41                (when (and (= i (1- len-1)) (> len-2 len-1))
42                  (return-from string< (1+ i))))))))
43
44 (defun stringp (s)
45   (stringp s))
46
47 (define-setf-expander char (string index)
48   (let ((g!string (gensym))
49         (g!index (gensym))
50         (g!value (gensym)))
51     (values (list g!string g!index)
52             (list string index)
53             (list g!value)
54             `(aset ,g!string ,g!index ,g!value)
55             `(char ,g!string ,g!index))))