;;; 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.
;;; Spice Lisp is currently incomplete and under active development.
;;; 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.
;;;
;;; ****************************************************************
;;;

(eval-when (compile)

;;; %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)

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

)

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

(eval-when (compile)

(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 (%primitive header-ref
					 ,string %array-displacement-slot)))
	   (psetq ,string (%primitive header-ref ,string %array-data-slot)
		  start displacement
		  end (+ displacement
			 (%primitive header-ref
				     ,string %array-fill-pointer-slot))))
	 (setq end (length (the simple-string ,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)

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

)

(defun char (string index)
  "Given a string and a non-negative integer index less than the length of
  the string, returns the character object representing the character at
  that position in the string."
  (char string index))

(defun %charset (string index new-el)
  (setf (char string index) new-el))

(defun schar (string index)
  (schar string index))

(defun %scharset (string index new-el)
  (setf (schar string index) new-el))

(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
    (let ((slen1 (- end1 start1))
	  (slen2 (- end2 start2)))
      (declare (fixnum slen1 slen2))
      (if (or (minusp slen1) (minusp slen2))
	  ;;prevent endless looping later.
	  (error "Improper bounds for string comparison."))
      (if (= slen1 slen2)
	  ;;return () immediately if lengths aren't equal.
	  (do ((index1 start1 (1+ index1))
	       (index2 start2 (1+ index2)))
	      ((= index1 end1) t)
	    (declare (fixnum index1 index2))
	    (if (not (char-equal (schar string1 index1)
				 (schar string2 index2)))
		(return ()))))))) 

(defun string=* (string1 string2 start1 end1 start2 end2)
  (with-two-strings string1 string2 start1 end1 start2 end2
    (not (%sp-string-compare string1 start1 end1 string2 start2 end2))))


(defun string/=* (string1 string2 start1 end1 start2 end2)
  (with-two-strings string1 string2 start1 end1 start2 end2
    (%sp-string-compare string1 start1 end1 string2 start2 end2)))

;;; Lessp is true if the desired expansion is for string<* or string<=*.
;;; Equalp is true if the desired expansion is for string<=* or string>=*.
(defmacro string<>=*-body (lessp equalp)
  `(with-two-strings string1 string2 start1 end1 start2 end2
     (let ((index (%sp-string-compare string1 start1 end1 string2 start2 end2)))
       (if index
	   (cond ((= index ,(if lessp 'end1 'end2)) index)
		 ((= index ,(if lessp 'end2 'end1)) nil)
		 ((,(if lessp 'char< 'char>)
		   (schar string1 index)
		   (schar string2 (+ index (- start2 start1))))
		  index)
		 (t nil))
	   ,(if equalp '(- end1 start1) 'nil)))))

(defun string<* (string1 string2 start1 end1 start2 end2)
  (string<>=*-body t nil))

(defun string>* (string1 string2 start1 end1 start2 end2)
  (string<>=*-body nil nil))

(defun string<=* (string1 string2 start1 end1 start2 end2)
  (string<>=*-body t t))

(defun string>=* (string1 string2 start1 end1 start2 end2)
  (string<>=*-body nil t))



(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 ()."
  (string<* string1 string2 start1 end1 start2 end2))

(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 ()."
  (string>* string1 string2 start1 end1 start2 end2))


(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 ()."
  (string<=* string1 string2 start1 end1 start2 end2))

(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 ()."
  (string>=* string1 string2 start1 end1 start2 end2))


(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=)."
  (string=* string1 string2 start1 end1 start2 end2))

(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 ()."
  (string/=* string1 string2 start1 end1 start2 end2))

(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 ((slen1 (- end1 start1))
	  (slen2 (- end2 start2)))
      (declare (fixnum slen1 slen2))
      (if (or (minusp slen1) (minusp slen2))
	  ;;prevent endless looping later.
	  (error "Improper bounds for string comparison."))
      (do ((index1 start1 (1+ index1))
	   (index2 start2 (1+ index2))
	   (char1)
	   (char2))
	  ((or (= index1 end1) (= index2 end2))
	   ;;return index if string1 shorter, () if they're the same
	   (if (not (= slen1 slen2)) index1))
	(declare (fixnum index1 index2))
	(setq char1 (schar string1 index1))
	(setq char2 (schar string2 index2))
	(if (not (char-equal char1 char2))
	    (if (char-lessp char1 char2)
		(return index1)
		(return ())))))))

(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 ((slen1 (- end1 start1))
	  (slen2 (- end2 start2)))
      (declare (fixnum slen1 slen2))
      (if (or (minusp slen1) (minusp slen2))
	  ;;prevent endless looping later.
	  (error "Improper bounds for string comparison."))
      (do ((index1 start1 (1+ index1))
	   (index2 start2 (1+ index2))
	   (char1)
	   (char2))
	  ((or (= index1 end1) (= index2 end2))
	   ;;return index if string1 shorter, () if they're the same
	   (if (not (= slen1 slen2)) index1))
	(declare (fixnum index1 index2))
	(setq char1 (schar string1 index1))
	(setq char2 (schar string2 index2))
	(if (not (char-equal char1 char2))
	    (if (char-greaterp char1 char2)
		(return index1)
		(return ())))))))

(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 ((slen1 (- end1 start1))
	  (slen2 (- end2 start2)))
      (declare (fixnum slen1 slen2))
      (if (or (minusp slen1) (minusp slen2))
	  ;;prevent endless looping later.
	  (error "Improper bounds for string comparison."))
      (do ((index1 start1 (1+ index1))
	   (index2 start2 (1+ index2))
	   (char1)
	   (char2))
	  ((or (= index1 end1) (= index2 end2))
	   ;;return index if string1 shorter, () if they're the same
	   index1)
	(declare (fixnum index1 index2))
	(setq char1 (schar string1 index1))
	(setq char2 (schar string2 index2))
	(if (not (char-equal char1 char2))
	    (if (not (char-greaterp char1 char2))
		(return index1)
		(return ())))))))

(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 ((slen1 (- end1 start1))
	  (slen2 (- end2 start2)))
      (declare (fixnum slen1 slen2))
      (if (or (minusp slen1) (minusp slen2))
	  ;;prevent endless looping later.
	  (error "Improper bounds for string comparison."))
      (do ((index1 start1 (1+ index1))
	   (index2 start2 (1+ index2))
	   (char1)
	   (char2))
	  ((or (= index1 end1) (= index2 end2))
	   ;;return index if string1 shorter, () if they're the same
	   index1)
	(declare (fixnum index1 index2))
	(setq char1 (schar string1 index1))
	(setq char2 (schar string2 index2))
	(if (not (char-equal char1 char2))
	    (if (not (char-lessp char1 char2))
		(return index1)
		(return ())))))))

(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 ((slen1 (- end1 start1))
	  (slen2 (- end2 start2)))
      (declare (fixnum slen1 slen2))
      (if (or (minusp slen1) (minusp slen2))
	  ;;prevent endless looping later.
	  (error "Improper bounds for string comparison."))
      (cond ((or (minusp slen1) (or (minusp slen2)))
	     (error "Improper substring for comparison."))
	    ((= slen1 slen2)
	     (do ((index1 start1 (1+ index1))
		  (index2 start2 (1+ index2)))
		 ((= index1 end1) ())
	       (declare (fixnum index1 index2))
	       (if (not (char-equal (schar string1 index1)
				    (schar string2 index2)))
		   (return index1))))
	    ((< slen1 slen2)
	     (do ((index1 start1 (1+ index1))
		  (index2 start2 (1+ index2)))
		 ((or (= index1 end1)
		      (not (char-equal (schar string1 index1)
				       (schar string2 index2))))
		  index1)
	       (declare (fixnum index1 index2))))
	    (t
	     (do ((index1 start1 (1+ index1))
		  (index2 start2 (1+ index2)))
		 ((or (= index2 end2)
		      (not (char-equal (schar string1 index1)
				       (schar string2 index2))))
		  index1)
	       (declare (fixnum index1 index2))))))))

(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* ((slen (length string))
	   (newstring (make-string slen)))
      (declare (fixnum slen))
      (do ((index 0 (1+ index)))
	  ((= index start))
	(setf (schar newstring index) (schar string index)))
      (do ((index start (1+ index)))
	  ((= index end))
	(declare (fixnum index))
	(setf (schar newstring index)
	      (char-upcase (schar string index))))
      (do ((index end (1+ index)))
	  ((= index slen))
	(setf (schar newstring index) (schar string index)))
      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* ((slen (length string))
	   (newstring (make-string slen)))
      (declare (fixnum slen))
      (do ((index 0 (1+ index)))
	  ((= index start))
	(setf (schar newstring index) (schar string index)))
      (do ((index start (1+ index)))
	  ((= index end))
	(declare (fixnum index))
	(setf (schar newstring index)
	      (char-downcase (schar string index))))
      (do ((index end (1+ index)))
	  ((= index slen))
	(setf (schar newstring index) (schar string index)))
      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* ((slen (length string))
	   (newstring (make-string slen)))
      (declare (fixnum slen))
      (do ((index 0 (1+ index)))
	  ((= index start))
	(setf (schar newstring index) (schar string index)))
      (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
	       (setq char (char-upcase char))
	       (setq newword ()))
	      ;;char is case-modifiable, but not first
	      (t (setq char (char-downcase char))))
	(setf (schar newstring index) char))
      (do ((index end (1+ index)))
	  ((= index slen))
	(setf (schar newstring index) (schar string index)))
      newstring)))

(defun nstring-upcase (string &key (start 0) end)
  "Given a string, returns that string with all lower case alphabetic
  characters converted to uppercase."
  (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))))
    string))

(defun nstring-downcase (string &key (start 0) end)
  "Given a string, returns that string with all upper case alphabetic
  characters converted to lowercase."
  (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))))
    string))

(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."
  (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)))))
    string))

(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)))))