Special case in make-array for strings
[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 (s)
17   (stringp s))
18
19 (defun make-string (n &key initial-element)
20   (make-array n :element-type 'character :initial-element initial-element))
21
22 ;; (defun char-to-string (x)
23 ;;   (make-string 1 :initial-element x))
24
25 (defun string (x)
26   (cond ((stringp x) x)
27         ((symbolp x) (symbol-name x))
28         (t (char-to-string x))))
29
30 (defun string= (s1 s2)
31   (let* ((s1 (string s1))
32          (s2 (string s2))
33          (n (length s1)))
34     (when (= (length s2) n)
35       (dotimes (i n t)
36         (unless (char= (char s1 i) (char s2 i))
37           (return-from string= nil))))))
38
39 (defun string< (s1 s2)
40   (let ((len-1 (length s1))
41         (len-2 (length s2)))
42     (cond ((= len-2 0) nil)
43           ((= len-1 0) 0)
44           (t (dotimes (i len-1 nil)
45                (when (char< (char s1 i) (char s2 i))
46                  (return-from string< i))
47                (when (and (= i (1- len-1)) (> len-2 len-1))
48                  (return-from string< (1+ i))))))))
49
50 (define-setf-expander char (string index)
51   (let ((g!string (gensym))
52         (g!index (gensym))
53         (g!value (gensym)))
54     (values (list g!string g!index)
55             (list string index)
56             (list g!value)
57             `(aset ,g!string ,g!index ,g!value)
58             `(char ,g!string ,g!index))))
59
60 (defun concatenate-two (string1 string2)
61   (let* ((len1 (length string1))
62          (len2 (length string2))
63          (string (make-array (+ len1 len2) :element-type 'character))
64          (i 0))
65     (dotimes (j len1)
66       (aset string i (char string1 j))
67       (incf i))
68     (dotimes (j len2)
69       (aset string i (char string2 j))
70       (incf i))
71     string))