1 ;;;; string hacking functions that are stubs for things that might
2 ;;;; be microcoded someday
4 ;;;; This software is part of the SBCL system. See the README file for
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.
13 (in-package "SB!IMPL")
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))
23 (defun %sp-string-compare (string1 start1 end1 string2 start2 end2)
24 (declare (simple-string string1 string2))
25 (declare (fixnum start1 end1 start2 end2))
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
33 (let ((len1 (- end1 start1))
34 (len2 (- end2 start2)))
35 (declare (fixnum len1 len2))
38 (do ((index1 start1 (1+ index1))
39 (index2 start2 (1+ index2)))
41 (declare (fixnum index1 index2))
42 (if (char/= (schar string1 index1) (schar string2 index2))
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))
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))
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))
63 "like %SP-STRING-COMPARE, only backwards"
64 (let ((len1 (- end1 start1))
65 (len2 (- end2 start2)))
66 (declare (fixnum 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))
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))
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))
90 (defmacro maybe-sap-maybe-string ((var) &body body)
93 (macrolet ((byte-ref (index)
94 `(sap-ref-8 ,',var ,index))
96 `(code-char (byte-ref ,index))))
99 (macrolet ((char-ref (index)
100 `(schar ,',var ,index))
102 `(char-code (char-ref ,index))))
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))
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)))
118 (declare (fixnum index))
119 (unless (zerop (logand (aref table (byte-ref index)) mask))
122 (defun %sp-reverse-find-character-with-attribute (string start end table mask)
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))
135 (defun %sp-find-character (string start end character)
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
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)))
147 (declare (fixnum index))
148 (when (char= (char-ref index) character)
151 (defun %sp-reverse-find-character (string start end character)
152 (declare (type (or simple-string system-area-pointer) string)
154 (base-char character))
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
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)
168 (defun %sp-skip-character (string start end character)
169 (declare (type (or simple-string system-area-pointer) string)
171 (base-char character))
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)))
179 (declare (fixnum index))
180 (if (char/= (char-ref index) character)
183 (defun %sp-reverse-skip-character (string start end character)
184 (declare (type (or simple-string system-area-pointer) string)
186 (base-char character))
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)
199 (defun %sp-string-search (string1 start1 end1 string2 start2 end2)
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
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)))
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))