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")
18 ;(defun %sp-byte-blt (src-string src-start dst-string dst-start dst-end)
19 ; "Moves bytes from Src-String into Dst-String between Dst-Start (inclusive)
20 ;and Dst-End (exclusive) (Dst-Start - Dst-End bytes are moved). Overlap of the
21 ;strings does not affect the result. This would be done on the Vax
22 ;with MOVC3. The arguments do not need to be strings: 8-bit U-Vectors
23 ;are also acceptable."
24 ; (%primitive byte-blt src-string src-start dst-string dst-start dst-end))
26 (defun %sp-string-compare (string1 start1 end1 string2 start2 end2)
27 (declare (simple-string string1 string2))
28 (declare (fixnum start1 end1 start2 end2))
30 "Compares the substrings specified by String1 and String2 and returns
31 NIL if the strings are String=, or the lowest index of String1 in
32 which the two differ. If one string is longer than the other and the
33 shorter is a prefix of the longer, the length of the shorter + start1 is
34 returned. This would be done on the Vax with CMPC3. The arguments must
36 (let ((len1 (- end1 start1))
37 (len2 (- end2 start2)))
38 (declare (fixnum len1 len2))
41 (do ((index1 start1 (1+ index1))
42 (index2 start2 (1+ index2)))
44 (declare (fixnum index1 index2))
45 (if (char/= (schar string1 index1) (schar string2 index2))
48 (do ((index1 start1 (1+ index1))
49 (index2 start2 (1+ index2)))
50 ((= index2 end2) index1)
51 (declare (fixnum index1 index2))
52 (if (char/= (schar string1 index1) (schar string2 index2))
55 (do ((index1 start1 (1+ index1))
56 (index2 start2 (1+ index2)))
57 ((= index1 end1) index1)
58 (declare (fixnum index1 index2))
59 (if (char/= (schar string1 index1) (schar string2 index2))
62 (defun %sp-reverse-string-compare (string1 start1 end1 string2 start2 end2)
63 (declare (simple-string string1 string2))
64 (declare (fixnum start1 end1 start2 end2))
66 "like %SP-STRING-COMPARE, only backwards"
67 (let ((len1 (- end1 start1))
68 (len2 (- end2 start2)))
69 (declare (fixnum len1 len2))
72 (do ((index1 (1- end1) (1- index1))
73 (index2 (1- end2) (1- index2)))
74 ((< index1 start1) nil)
75 (declare (fixnum index1 index2))
76 (if (char/= (schar string1 index1) (schar string2 index2))
79 (do ((index1 (1- end1) (1- index1))
80 (index2 (1- end2) (1- index2)))
81 ((< index2 start2) index1)
82 (declare (fixnum index1 index2))
83 (if (char/= (schar string1 index1) (schar string2 index2))
86 (do ((index1 (1- end1) (1- index1))
87 (index2 (1- end2) (1- index2)))
88 ((< index1 start1) index1)
89 (declare (fixnum index1 index2))
90 (if (char/= (schar string1 index1) (schar string2 index2))
93 (defmacro maybe-sap-maybe-string ((var) &body body)
96 (macrolet ((byte-ref (index)
97 `(sap-ref-8 ,',var ,index))
99 `(code-char (byte-ref ,index))))
102 (macrolet ((char-ref (index)
103 `(schar ,',var ,index))
105 `(char-code (char-ref ,index))))
108 (defun %sp-find-character-with-attribute (string start end table mask)
109 (declare (type (simple-array (unsigned-byte 8) (256)) table)
110 (type (or simple-string system-area-pointer) string)
111 (fixnum start end mask))
113 "%SP-Find-Character-With-Attribute String, Start, End, Table, Mask
114 The codes of the characters of String from Start to End are used as indices
115 into the Table, which is a U-Vector of 8-bit bytes. When the number picked
116 up from the table bitwise ANDed with Mask is non-zero, the current
117 index into the String is returned. The corresponds to SCANC on the Vax."
118 (maybe-sap-maybe-string (string)
119 (do ((index start (1+ index)))
121 (declare (fixnum index))
122 (unless (zerop (logand (aref table (byte-ref index)) mask))
125 (defun %sp-reverse-find-character-with-attribute (string start end table mask)
127 "Like %SP-Find-Character-With-Attribute, only sdrawkcaB."
128 (declare (type (or simple-string system-area-pointer) string)
129 (fixnum start end mask)
130 (type (array (unsigned-byte 8) (256)) table))
131 (maybe-sap-maybe-string (string)
132 (do ((index (1- end) (1- index)))
133 ((< index start) nil)
134 (declare (fixnum index))
135 (unless (zerop (logand (aref table (byte-ref index)) mask))
138 (defun %sp-find-character (string start end character)
140 "%SP-Find-Character String, Start, End, Character
141 Searches String for the Character from Start to End. If the character is
142 found, the corresponding index into String is returned, otherwise NIL is
144 (declare (fixnum start end)
145 (type (or simple-string system-area-pointer) string)
146 (base-char character))
147 (maybe-sap-maybe-string (string)
148 (do ((index start (1+ index)))
150 (declare (fixnum index))
151 (when (char= (char-ref index) character)
154 (defun %sp-reverse-find-character (string start end character)
155 (declare (type (or simple-string system-area-pointer) string)
157 (base-char character))
159 "%SP-Reverse-Find-Character String, Start, End, Character
160 Searches String for Character from End to Start. If the character is
161 found, the corresponding index into String is returned, otherwise NIL is
163 (maybe-sap-maybe-string (string)
164 (do ((index (1- end) (1- index))
165 (terminus (1- start)))
166 ((= index terminus) nil)
167 (declare (fixnum terminus index))
168 (if (char= (char-ref index) character)
171 (defun %sp-skip-character (string start end character)
172 (declare (type (or simple-string system-area-pointer) string)
174 (base-char character))
176 "%SP-Skip-Character String, Start, End, Character
177 Returns the index of the first character between Start and End which
178 is not Char= to Character, or NIL if there is no such character."
179 (maybe-sap-maybe-string (string)
180 (do ((index start (1+ index)))
182 (declare (fixnum index))
183 (if (char/= (char-ref index) character)
186 (defun %sp-reverse-skip-character (string start end character)
187 (declare (type (or simple-string system-area-pointer) string)
189 (base-char character))
191 "%SP-Skip-Character String, Start, End, Character
192 Returns the index of the last character between Start and End which
193 is not Char= to Character, or NIL if there is no such character."
194 (maybe-sap-maybe-string (string)
195 (do ((index (1- end) (1- index))
196 (terminus (1- start)))
197 ((= index terminus) nil)
198 (declare (fixnum terminus index))
199 (if (char/= (char-ref index) character)
202 (defun %sp-string-search (string1 start1 end1 string2 start2 end2)
204 "%SP-String-Search String1, Start1, End1, String2, Start2, End2
205 Searches for the substring of String1 specified in String2.
206 Returns an index into String2 or NIL if the substring wasn't
208 (declare (simple-string string1 string2))
209 (do ((index2 start2 (1+ index2)))
210 ((= index2 end2) nil)
211 (declare (fixnum index2))
212 (when (do ((index1 start1 (1+ index1))
213 (index2 index2 (1+ index2)))
215 (declare (fixnum index1 index2))
216 (when (= index2 end2)
217 (return-from %sp-string-search nil))
218 (when (char/= (char string1 index1) (char string2 index2))