Google
 

Trailing-Edge - PDP-10 Archives - clisp - clisp/upsala/string.clisp
There are no other files named string.clisp in the archive.
;;; This is a -*-Lisp-*- file.
;;;
;;; **********************************************************************
;;; This code was written as part of the Spice Lisp project at
;;; Carnegie-Mellon University, and has been placed in the public domain.
;;; If you want to use this code or any part of Spice Lisp, please contact
;;; Scott Fahlman (FAHLMAN@CMUC). 
;;; **********************************************************************
;;;
;;; Functions to implement strings for Spice Lisp
;;; Written by David Dill
;;; Rewritten and currently maintained by Skef Wholey
;;;
;;; Runs in the standard Spice Lisp environment.
;;;
;;; ****************************************************************
;;;

(in-package 'lisp)

(export '(string string= string-equal string< string> string<= string>=
	  string/= string-lessp string-greaterp string-not-lessp
	  string-not-greaterp string-not-equal make-string string-trim
	  string-left-trim string-right-trim string-upcase string-downcase
	  string-capitalize nstring-upcase nstring-downcase
	  nstring-capitalize))

(eval-when (compile eval)

;;; %String returns its arg if it is a string, otherwise calls String.
;;;
(defmacro %string (thing)
  `(if (stringp ,thing) ,thing (string ,thing)))
)

(defun string (X)
  "Coerces X into a string.  If X is a string, X is returned.  If X is a
   symbol, X's pname is returned.  If X is a character then a one element
   string containing that character is returned.  If X cannot be coerced
   into a string, an error occurs."
  (cond ((stringp x) x)
	((symbolp x) (symbol-name x))
	((characterp x)
	 (let ((res (make-string 1)))
	   (setf (schar res 0) x) res))
	(t
	 (error "~S cannot be coerced to a string." x))))


;;; With-One-String is used to set up some string hacking things.  The keywords
;;; are parsed, and the string is hacked into a simple-string.

(eval-when (compile eval)

(defmacro with-one-string (string start end &rest forms)
  `(let ((displace 0)
	 slen)
     (if (symbolp ,string) (setq ,string (symbol-name ,string)))
     (if (slisp-array-p ,string)
	 (progn
	  (setq displace (header-ref ,string %array-displacement-slot))
	  (psetq ,string (header-ref ,string %array-data-slot)
		 slen (header-ref ,string %array-fill-pointer-slot)
		 ,start (+ ,start displace)
		 ,end (if ,end
			  (+ ,end displace)
			  (+ (header-ref ,string %array-fill-pointer-slot)
			     displace))))
	 (progn
	  (setq slen (%sp-get-vector-length ,string))
	  (if (not ,end) (setq ,end (%sp-get-vector-length ,string)))))
     ,@forms))

)

;;; With-String is like With-One-String, but doesn't parse keywords.

(eval-when (compile eval)

(defmacro with-string (string &rest forms)
  `(let ((start 0)
	 (end))
     (if (symbolp ,string) (setq ,string (symbol-name ,string)))
     (if (slisp-array-p ,string)
	 (let ((displacement (header-ref ,string %array-displacement-slot)))
	   (psetq ,string (header-ref ,string %array-data-slot)
		  start displacement
		  end (+ displacement
			 (header-ref ,string %array-fill-pointer-slot))))
	 (setq end (%sp-get-vector-length ,string)))
     ,@forms))

)

;;; With-Two-Strings is used to set up string comparison operations.  The
;;; keywords are parsed, and the strings are hacked into simple-strings.

(eval-when (compile eval)

(defmacro with-two-strings (string1 string2 start1 end1 start2 end2
				    &rest forms)
  `(let ((displace1 0))
     (if (symbolp ,string1) (setq ,string1 (symbol-name ,string1)))
     (if (symbolp ,string2) (setq ,string2 (symbol-name ,string2)))
     (if (slisp-array-p ,string1)
	 (progn
	   (setq displace1 (header-ref ,string1 %array-displacement-slot))
	   (psetq ,string1 (header-ref ,string1 %array-data-slot)
		  ,start1 (+ ,start1 displace1)
		  ,end1 (if ,end1
			    (+ ,end1 displace1)
			    (+ (header-ref ,string1 %array-fill-pointer-slot)
			       displace1))))
	 (if (not ,end1) (setq ,end1 (%sp-get-vector-length ,string1))))
     (if (slisp-array-p ,string2)
	 (let ((displacement (header-ref ,string2 %array-displacement-slot)))
	   (psetq ,string2 (header-ref ,string2 %array-data-slot)
		  ,start2 (+ ,start2 displacement)
		  ,end2 (if ,end2
			    (+ ,end2 displacement)
			    (+ (header-ref ,string2 %array-fill-pointer-slot)
			       displacement))))
	 (if (not ,end2) (setq ,end2 (%sp-get-vector-length ,string2))))
     ,@forms))

)

;20; remove char, etc., which are in kernel
(defun string= (string1 string2 &key (start1 0) end1 (start2 0) end2)
  "Given two strings (string1 and string2), and optional integers start1,
  start2, end1 and end2, compares characters in string1 to characters in
  string2 (using char=)."
  (with-two-strings string1 string2 start1 end1 start2 end2
    (not (%sp-string-compare string1 start1 end1 string2 start2 end2))))

(defun string-equal (string1 string2 &key (start1 0) end1 (start2 0) end2)
  "Given two strings (string1 and string2), and optional integers start1,
  start2, end1 and end2, compares characters in string1 to characters in
  string2 (using char-equal)."
  (with-two-strings string1 string2 start1 end1 start2 end2
    (not (%sp-string-compare-ignore string1 start1 end1 string2 start2 end2))))

(defun string< (string1 string2 &key (start1 0) end1 (start2 0) end2)
  "Given two strings, if the first string is lexicographically less than
  the second string, returns the longest common prefix (using char=)
  of the two strings. Otherwise, returns ()."
  (with-two-strings string1 string2 start1 end1 start2 end2
    (let ((index (%sp-string-compare string1 start1 end1 string2 start2 end2)))
	 (cond ((not index) nil) ;equal
	       ((eq index end1) (- index displace1))  ;first string ran out
	       ((eq (- index start1) (- end2 start2)) nil) ;second ran out
	       ((char< (schar string1 index)
			(schar string2 (+ index (- start2 start1))))
	        (- index displace1))))))

(defun string> (string1 string2 &key (start1 0) end1 (start2 0) end2)
  "Given two strings, if the first string is lexicographically greater than
  the second string, returns the longest common prefix (using char=)
  of the two strings. Otherwise, returns ()."
  (with-two-strings string1 string2 start1 end1 start2 end2
    (let ((index (%sp-string-compare string1 start1 end1 string2 start2 end2)))
	 (cond ((not index) nil) ;equal
	       ((eq index end1) nil)  ;first string ran out
	       ((eq (- index start1) (- end2 start2))
		(- index displace1)) ;second ran out
	       ((char> (schar string1 index)
			(schar string2 (+ index (- start2 start1))))
	        (- index displace1))))))
(defun string<= (string1 string2 &key (start1 0) end1 (start2 0) end2)
  "Given two strings, if the first string is lexicographically less than
  or equal to the second string, returns the longest common prefix
  (using char=) of the two strings. Otherwise, returns ()."
  (with-two-strings string1 string2 start1 end1 start2 end2
    (let ((index (%sp-string-compare string1 start1 end1 string2 start2 end2)))
	 (cond ((not index) (- end1 displace1)) ;equal
	       ((eq index end1) (- index displace1))  ;first string ran out
	       ((eq (- index start1) (- end2 start2)) nil) ;second ran out
	       ((char<= (schar string1 index)
			(schar string2 (+ index (- start2 start1))))
	        (- index displace1))))))

(defun string>= (string1 string2 &key (start1 0) end1 (start2 0) end2)
  "Given two strings, if the first string is lexicographically greater
  than or equal to the second string, returns the longest common prefix
  (using char=) of the two strings. Otherwise, returns ()."
  (with-two-strings string1 string2 start1 end1 start2 end2
    (let ((index (%sp-string-compare string1 start1 end1 string2 start2 end2)))
	 (cond ((not index) (- end1 displace1)) ;equal
	       ((eq index end1) nil)  ;first string ran out
	       ((eq (- index start1) (- end2 start2))
		(- index displace1)) ;second ran out
	       ((char>= (schar string1 index)
			(schar string2 (+ index (- start2 start1))))
	        (- index displace1))))))
(defun string/= (string1 string2 &key (start1 0) end1 (start2 0) end2)
  "Given two strings, if the first string is not lexicographically equal
  to the second string, returns the longest common prefix (using char=)
  of the two strings. Otherwise, returns ()."
  (with-two-strings string1 string2 start1 end1 start2 end2
    (let ((index (%sp-string-compare string1 start1 end1 string2 start2 end2)))
         (cond ((not index) nil)
	       (t (- index displace1))))))

(defun string-lessp (string1 string2 &key (start1 0) end1 (start2 0) end2)
  "Given two strings, if the first string is lexicographically less than
  the second string, returns the longest common prefix (using char-equal)
  of the two strings. Otherwise, returns ()."
  (with-two-strings string1 string2 start1 end1 start2 end2
    (let ((index (%sp-string-compare-ignore string1 start1 end1 
					    string2 start2 end2)))
	 (cond ((not index) nil) ;equal
	       ((eq index end1) (- index displace1))  ;first string ran out
	       ((eq (- index start1) (- end2 start2)) nil) ;second ran out
	       ((char-lessp (schar string1 index)
			    (schar string2 (+ index (- start2 start1))))
	        (- index displace1))))))
(defun string-greaterp (string1 string2 &key (start1 0) end1 (start2 0) end2)
  "Given two strings, if the first string is lexicographically greater than
  the second string, returns the longest common prefix (using char-equal)
  of the two strings. Otherwise, returns ()."
  (with-two-strings string1 string2 start1 end1 start2 end2
    (let ((index (%sp-string-compare-ignore string1 start1 end1
					    string2 start2 end2)))
	 (cond ((not index) nil) ;equal
	       ((eq index end1) nil)  ;first string ran out
	       ((eq (- index start1) (- end2 start2)) 
		(- index displace1)) ;second ran out
	       ((char-greaterp (schar string1 index)
			       (schar string2 (+ index (- start2 start1))))
	        (- index displace1))))))

(defun string-not-greaterp (string1 string2 &key (start1 0) end1
				    (start2 0) end2)
  "Given two strings, if the first string is lexicographically less than
  or equal to the second string, returns the longest common prefix
  (using char-equal) of the two strings. Otherwise, returns ()."
  (with-two-strings string1 string2 start1 end1 start2 end2
    (let ((index (%sp-string-compare-ignore string1 start1 end1
					    string2 start2 end2)))
	 (cond ((not index) (- end1 displace1)) ;equal
	       ((eq index end1) (- index displace1))  ;first string ran out
	       ((eq (- index start1) (- end2 start2)) nil) ;second ran out
	       ((char-not-greaterp (schar string1 index)
				   (schar string2 (+ index (- start2 start1))))
	        (- index displace1))))))
(defun string-not-lessp (string1 string2 &key (start1 0) end1 (start2 0) end2)
  "Given two strings, if the first string is lexicographically greater
  than or equal to the second string, returns the longest common prefix
  (using char-equal) of the two strings. Otherwise, returns ()."
  (with-two-strings string1 string2 start1 end1 start2 end2
    (let ((index (%sp-string-compare-ignore string1 start1 end1
					    string2 start2 end2)))
	 (cond ((not index) (- end1 displace1)) ;equal
	       ((eq index end1) nil)  ;first string ran out
	       ((eq (- index start1) (- end2 start2)) 
		(- index displace1)) ;second ran out
	       ((char-not-lessp (schar string1 index)
				(schar string2 (+ index (- start2 start1))))
	        (- index displace1))))))


(defun string-not-equal (string1 string2 &key (start1 0) end1 (start2 0) end2)
  "Given two strings, if the first string is not lexicographically equal
  to the second string, returns the longest common prefix (using char-equal)
  of the two strings. Otherwise, returns ()."
  (with-two-strings string1 string2 start1 end1 start2 end2
    (let ((index (%sp-string-compare-ignore string1 start1 end1
					    string2 start2 end2)))
         (cond ((not index) nil)
	       (t (- index displace1))))))
(defun make-string (count &key ((:initial-element fill-char)))
  "Given a character count and an optional fill character, makes and returns
  a new string Count long filled with the fill character."
  (if fill-char
      (do ((i 0 (1+ i))
	   (string (%sp-alloc-string count)))
	  ((= i count) string)
	(declare (fixnum i))
	(setf (schar string i) fill-char))
      (%sp-alloc-string count)))

(defun string-upcase (string &key (start 0) end)
  "Given a string, returns a new string that is a copy of it with
  all lower case alphabetic characters converted to uppercase."
  (with-one-string string start end
    (let* ((newstring (make-string slen)))
      (declare (fixnum slen))
      (do ((index1 displace (1+ index1))
	   (index2 0 (1+ index2)))
	  ((= index1 start))
	(setf (schar newstring index2) (schar string index1)))
      (do ((index1 start (1+ index1))
	   (index2 (- start displace) (1+ index2)))
	  ((= index1 end))
	(declare (fixnum index1 index2))
	(setf (schar newstring index2)
	      (char-upcase (schar string index1))))
      (do ((index1 end (1+ index1))
	   (index2 (- end displace) (1+ index2)))
	  ((= index2 slen))
	(setf (schar newstring index2) (schar string index1)))
      newstring)))

(defun string-downcase (string &key (start 0) end)
  "Given a string, returns a new string that is a copy of it with
  all upper case alphabetic characters converted to lowercase."
  (with-one-string string start end
    (let* ((newstring (make-string slen)))
      (declare (fixnum slen))
      (do ((index1 displace (1+ index1))
	   (index2 0 (1+ index2)))
	  ((= index1 start))
	(setf (schar newstring index2) (schar string index1)))
      (do ((index1 start (1+ index1))
	   (index2 (- start displace) (1+ index2)))
	  ((= index1 end))
	(declare (fixnum index1 index2))
	(setf (schar newstring index2)
	      (char-downcase (schar string index1))))
      (do ((index1 end (1+ index1))
	   (index2 (- end displace) (1+ index2)))
	  ((= index2 slen))
	(setf (schar newstring index2) (schar string index1)))
      newstring)))
(defun string-capitalize (string &key (start 0) end)
  "Given a string, returns a copy of the string with the first
  character of each ``word'' converted to upper-case, and remaining
  chars in the word converted to lower case. A ``word'' is defined
  to be a string of case-modifiable characters delimited by
  non-case-modifiable chars."
  (with-one-string string start end
    (let* ((newstring (make-string slen)))
      (declare (fixnum slen))
      (do ((index1 displace (1+ index1))
	   (index2 0 (1+ index2)))
	  ((= index1 start))
	(setf (schar newstring index2) (schar string index1)))
      (do ((index1 start (1+ index1))
	   (index2 (- start displace) (1+ index2))
	   (newword t)
	   (char ()))
	  ((= index1 end))
	(declare (fixnum index1 index2))
	(setq char (schar string index1))
	(cond ((not (alphanumericp char))
	       (setq newword t))
	      (newword
	       ;;char is first case-modifiable after non-case-modifiable
	       (setq char (char-upcase char))
	       (setq newword ()))
	      ;;char is case-modifiable, but not first
	      (t (setq char (char-downcase char))))
	(setf (schar newstring index2) char))
      (do ((index1 end (1+ index1))
	   (index2 (- end displace) (1+ index2)))
	  ((= index2 slen))
	(setf (schar newstring index2) (schar string index1)))
      newstring)))
(defun nstring-upcase (string &key (start 0) end)
  "Given a string, returns that string with all lower case alphabetic
  characters converted to uppercase."
 (let ((original string))
  (with-one-string string start end
    (do ((index start (1+ index)))
	((= index end))
      (declare (fixnum index))
      (setf (schar string index) (char-upcase (schar string index))))
    original)))

(defun nstring-downcase (string &key (start 0) end)
  "Given a string, returns that string with all upper case alphabetic
  characters converted to lowercase."
 (let ((original string))
  (with-one-string string start end
    (do ((index start (1+ index)))
	((= index end))
      (declare (fixnum index))
      (setf (schar string index) (char-downcase (schar string index))))
    original)))

(defun nstring-capitalize (string &key (start 0) end)
  "Given a string, returns that string with the first
  character of each ``word'' converted to upper-case, and remaining
  chars in the word converted to lower case. A ``word'' is defined
  to be a string of case-modifiable characters delimited by
  non-case-modifiable chars."
 (let ((original string))
  (with-one-string string start end
    (do ((index start (1+ index))
	 (newword t)
	 (char ()))
	((= index end))
      (declare (fixnum index))
      (setq char (schar string index))
      (cond ((not (alphanumericp char))
	     (setq newword t))
	    (newword
	     ;;char is first case-modifiable after non-case-modifiable
	     (setf (schar string index) (char-upcase char))
	     (setq newword ()))
	    (t
	     (setf (schar string index) (char-downcase char)))))
    original)))
(defun string-left-trim (char-bag string)
  "Given a set of characters (a list or string) and a string, returns
  a copy of the string with the characters in the set removed from the
  left end."
  (with-string string
    (do ((index start (1+ index)))
	((or (= index end) (not (find (schar string index) char-bag)))
	 (subseq (the simple-string string) index end))
      (declare (fixnum index)))))

(defun string-right-trim (char-bag string)
  "Given a set of characters (a list or string) and a string, returns
  a copy of the string with the characters in the set removed from the
  right end."
  (with-string string
    (do ((index (1- end) (1- index)))
	((or (< index start) (not (find (schar string index) char-bag)))
	 (subseq (the simple-string string) start (1+ index)))
      (declare (fixnum index)))))

(defun string-trim (char-bag string)
  "Given a set of characters (a list or string) and a string, returns a
  copy of the string with the characters in the set removed from both
  ends."
  (with-string string
    (let ((left-end)
	  (right-end))
      (declare (fixnum left-end right-end))
      (do ((index start (1+ index)))
	  ((or (= index end) (not (find (schar string index) char-bag)))
	   (setq left-end index))
	(declare (fixnum index)))
      (do ((index (1- end) (1- index)))
	  ((or (< index left-end)
	       (not (find (schar string index) char-bag)))
	   (setq right-end index))
	(declare (fixnum index)))
      (subseq (the simple-string string) left-end (1+ right-end)))))