0.6.7.22: removed CVS dollar-Header-dollar tags from sources
[sbcl.git] / src / code / mipsstrops.lisp
1 ;;;; string hacking functions that are stubs for things that might
2 ;;;; be microcoded someday
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
12
13 (in-package "SB!IMPL")
14
15 ;(defun %sp-byte-blt (src-string src-start dst-string dst-start dst-end)
16 ;  "Moves bytes from Src-String into Dst-String between Dst-Start (inclusive)
17 ;and Dst-End (exclusive) (Dst-Start - Dst-End bytes are moved). Overlap of the
18 ;strings does not affect the result. This would be done on the Vax
19 ;with MOVC3. The arguments do not need to be strings: 8-bit U-Vectors
20 ;are also acceptable."
21 ;  (%primitive byte-blt src-string src-start dst-string dst-start dst-end))
22
23 (defun %sp-string-compare (string1 start1 end1 string2 start2 end2)
24   (declare (simple-string string1 string2))
25   (declare (fixnum start1 end1 start2 end2))
26   #!+sb-doc
27   "Compares the substrings specified by String1 and String2 and returns
28 NIL if the strings are String=, or the lowest index of String1 in
29 which the two differ. If one string is longer than the other and the
30 shorter is a prefix of the longer, the length of the shorter + start1 is
31 returned. This would be done on the Vax with CMPC3. The arguments must
32 be simple strings."
33   (let ((len1 (- end1 start1))
34         (len2 (- end2 start2)))
35     (declare (fixnum len1 len2))
36     (cond
37      ((= len1 len2)
38       (do ((index1 start1 (1+ index1))
39            (index2 start2 (1+ index2)))
40           ((= index1 end1) nil)
41         (declare (fixnum index1 index2))
42         (if (char/= (schar string1 index1) (schar string2 index2))
43             (return index1))))
44      ((> len1 len2)
45       (do ((index1 start1 (1+ index1))
46            (index2 start2 (1+ index2)))
47           ((= index2 end2) index1)
48         (declare (fixnum index1 index2))
49         (if (char/= (schar string1 index1) (schar string2 index2))
50             (return index1))))
51      (t
52       (do ((index1 start1 (1+ index1))
53            (index2 start2 (1+ index2)))
54           ((= index1 end1) index1)
55         (declare (fixnum index1 index2))
56         (if (char/= (schar string1 index1) (schar string2 index2))
57             (return index1)))))))
58
59 (defun %sp-reverse-string-compare (string1 start1 end1 string2 start2 end2)
60   (declare (simple-string string1 string2))
61   (declare (fixnum start1 end1 start2 end2))
62   #!+sb-doc
63   "like %SP-STRING-COMPARE, only backwards"
64   (let ((len1 (- end1 start1))
65         (len2 (- end2 start2)))
66     (declare (fixnum len1 len2))
67     (cond
68      ((= len1 len2)
69       (do ((index1 (1- end1) (1- index1))
70            (index2 (1- end2) (1- index2)))
71           ((< index1 start1) nil)
72         (declare (fixnum index1 index2))
73         (if (char/= (schar string1 index1) (schar string2 index2))
74             (return index1))))
75      ((> len1 len2)
76       (do ((index1 (1- end1) (1- index1))
77            (index2 (1- end2) (1- index2)))
78           ((< index2 start2) index1)
79         (declare (fixnum index1 index2))
80         (if (char/= (schar string1 index1) (schar string2 index2))
81             (return index1))))
82      (t
83       (do ((index1 (1- end1) (1- index1))
84            (index2 (1- end2) (1- index2)))
85           ((< index1 start1) index1)
86         (declare (fixnum index1 index2))
87         (if (char/= (schar string1 index1) (schar string2 index2))
88             (return index1)))))))
89
90 (defmacro maybe-sap-maybe-string ((var) &body body)
91   `(etypecase ,var
92      (system-area-pointer
93       (macrolet ((byte-ref (index)
94                    `(sap-ref-8 ,',var ,index))
95                  (char-ref (index)
96                    `(code-char (byte-ref ,index))))
97         ,@body))
98      (simple-string
99       (macrolet ((char-ref (index)
100                    `(schar ,',var ,index))
101                  (byte-ref (index)
102                    `(char-code (char-ref ,index))))
103         ,@body))))
104
105 (defun %sp-find-character-with-attribute (string start end table mask)
106   (declare (type (simple-array (unsigned-byte 8) (256)) table)
107            (type (or simple-string system-area-pointer) string)
108            (fixnum start end mask))
109   #!+sb-doc
110   "%SP-Find-Character-With-Attribute  String, Start, End, Table, Mask
111   The codes of the characters of String from Start to End are used as indices
112   into the Table, which is a U-Vector of 8-bit bytes. When the number picked
113   up from the table bitwise ANDed with Mask is non-zero, the current
114   index into the String is returned. The corresponds to SCANC on the Vax."
115   (maybe-sap-maybe-string (string)
116     (do ((index start (1+ index)))
117         ((>= index end) nil)
118       (declare (fixnum index))
119       (unless (zerop (logand (aref table (byte-ref index)) mask))
120         (return index)))))
121
122 (defun %sp-reverse-find-character-with-attribute (string start end table mask)
123   #!+sb-doc
124   "Like %SP-Find-Character-With-Attribute, only sdrawkcaB."
125   (declare (type (or simple-string system-area-pointer) string)
126            (fixnum start end mask)
127            (type (array (unsigned-byte 8) (256)) table))
128   (maybe-sap-maybe-string (string)
129     (do ((index (1- end) (1- index)))
130         ((< index start) nil)
131       (declare (fixnum index))
132       (unless (zerop (logand (aref table (byte-ref index)) mask))
133         (return index)))))
134
135 (defun %sp-find-character (string start end character)
136   #!+sb-doc
137   "%SP-Find-Character  String, Start, End, Character
138   Searches String for the Character from Start to End. If the character is
139   found, the corresponding index into String is returned, otherwise NIL is
140   returned."
141   (declare (fixnum start end)
142            (type (or simple-string system-area-pointer) string)
143            (base-char character))
144   (maybe-sap-maybe-string (string)
145     (do ((index start (1+ index)))
146         ((>= index end) nil)
147       (declare (fixnum index))
148       (when (char= (char-ref index) character)
149         (return index)))))
150
151 (defun %sp-reverse-find-character (string start end character)
152   (declare (type (or simple-string system-area-pointer) string)
153            (fixnum start end)
154            (base-char character))
155   #!+sb-doc
156   "%SP-Reverse-Find-Character  String, Start, End, Character
157   Searches String for Character from End to Start. If the character is
158   found, the corresponding index into String is returned, otherwise NIL is
159   returned."
160   (maybe-sap-maybe-string (string)
161     (do ((index (1- end) (1- index))
162          (terminus (1- start)))
163         ((= index terminus) nil)
164       (declare (fixnum terminus index))
165       (if (char= (char-ref index) character)
166           (return index)))))
167
168 (defun %sp-skip-character (string start end character)
169   (declare (type (or simple-string system-area-pointer) string)
170            (fixnum start end)
171            (base-char character))
172   #!+sb-doc
173   "%SP-Skip-Character  String, Start, End, Character
174   Returns the index of the first character between Start and End which
175   is not Char=  to Character, or NIL if there is no such character."
176   (maybe-sap-maybe-string (string)
177     (do ((index start (1+ index)))
178         ((= index end) nil)
179       (declare (fixnum index))
180       (if (char/= (char-ref index) character)
181           (return index)))))
182
183 (defun %sp-reverse-skip-character (string start end character)
184   (declare (type (or simple-string system-area-pointer) string)
185            (fixnum start end)
186            (base-char character))
187   #!+sb-doc
188   "%SP-Skip-Character  String, Start, End, Character
189   Returns the index of the last character between Start and End which
190   is not Char=  to Character, or NIL if there is no such character."
191   (maybe-sap-maybe-string (string)
192     (do ((index (1- end) (1- index))
193          (terminus (1- start)))
194         ((= index terminus) nil)
195       (declare (fixnum terminus index))
196       (if (char/= (char-ref index) character)
197           (return index)))))
198
199 (defun %sp-string-search (string1 start1 end1 string2 start2 end2)
200   #!+sb-doc
201   "%SP-String-Search  String1, Start1, End1, String2, Start2, End2
202    Searches for the substring of String1 specified in String2.
203    Returns an index into String2 or NIL if the substring wasn't
204    found."
205   (declare (simple-string string1 string2))
206   (do ((index2 start2 (1+ index2)))
207       ((= index2 end2) nil)
208     (declare (fixnum index2))
209     (when (do ((index1 start1 (1+ index1))
210                (index2 index2 (1+ index2)))
211               ((= index1 end1) t)
212             (declare (fixnum index1 index2))
213             (when (= index2 end2)
214               (return-from %sp-string-search nil))
215             (when (char/= (char string1 index1) (char string2 index2))
216               (return nil)))
217       (return index2))))