str.stk 5.25 KB
Newer Older
1 2 3
;;;;
;;;; str.stk	-- string operations
;;;;
4
;;;; Copyright © 2011 Erick Gallesio - Polytech'Nice-Sophia <eg@unice.fr>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
;;;;
;;;;
;;;; This program is free software; you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation; either version 2 of the License, or
;;;; (at your option) any later version.
;;;;
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;; GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with this program; if not, write to the Free Software
;;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
;;;; USA.
;;;;
;;;;           Author: Erick Gallesio [eg@unice.fr]
;;;;    Creation date: 14-Jul-2011 16:42 (eg)
;;;; Last file update: 10-Aug-2011 16:17 (eg)
;;;;

;;; The functions defined here replace C primitives which are not UTF-8 aware.
;;; These functions still use their correspondant C primitive if the string
;;; argument doesn't use multi-byte characters


(when (%use-utf8?)
  (let ((%string-split		string-split)
	(%string-blit!		string-blit!)
	(%string-titlecase	string-titlecase)
	(%string-titlecase!	string-titlecase!)
	(error-bad-string	(lambda (str)
				  (error "bad string ~S" str))))
    ;;
    ;; string-split
    ;;
    (set! string-split
      (lambda (str :optional (delimiters  " \t\n"))
	(if (not (%string-use-utf8? str))
	    (%string-split str delimiters)
	    (begin
	      (unless (string? str)
		(error-bad-string str))
	      (unless (string? delimiters)
		(error-bad-string delimiters))

	      (let ((len   (string-length str))
		    (delim (string->list delimiters)))
		(let Loop ((start 0)
			   (current 0)
			   (res '()))
		  (cond
		    (( = current len)
		     (reverse! (if (> current start)
				   (cons (substring str start current) res)
				   res)))
		    ((memq (string-ref str current) delim)
		     (if (> current start)
			 (Loop (+ current 1)
			       (+ current 1)
			       (cons (substring str start current) res))
			 (Loop (+ current 1)
			       (+ current 1)
			       res)))
		    (else
		     (Loop start
			   (+ current 1)
			   res)))))))))
    ;;
    ;; string-blit!
    ;;
    (set! string-blit!
      (lambda (str1 str2 offset)
	(if (not (and (%string-use-utf8? str1) (%string-use-utf8? str2)))
	    (%string-blit! str1 str2 offset)
	    (begin
	      (unless (string? str1)
		(error-bad-string str1))
	      (unless (string? str2)
		(error-bad-string str2))
	      (unless (integer? offset)
		(error "bad offset ~S" offset))
	      (unless (string-mutable? str1)
		(error "changing the constant string ~S is not allowed"))

	      (let ((len1 (string-length str1))
		    (len2 (string-length str2)))
		(cond
		  ((and (zero? len1) (zero? offset))
		   str2)
		  ((< (+ offset len2) len1)
		   ;; str2 can be written in str1
		   (dotimes (i len2)
		     (string-set! str1 (+ offset i) (string-ref str2 i)))
		   str1)
		  (else
		   ;; Size of original string changes => allocate a new string
		   (let* ((newl (if (>= len1 (+ offset len2)) len1 (+ offset len2)))
			  (new  (make-string newl)))
		     (let Loop ((i 0)
				(j 0))
		       (cond
			 ((= i newl)
			  new)
			 ((and (>= i offset) (< i (+ offset len2)))
			  (string-set! new i (string-ref str2 j))
			  (Loop (+ i 1) (+ j 1)))
			 ((< i len1)
			  (string-set! new i (string-ref str1 i))
			  (Loop (+ i 1) j))
			 (else
			  (Loop (+ i 1) j))))))))))))

    ;;
    ;; string-titlecase
    ;;
    (set! string-titlecase
      (lambda (str :optional (start 0) (end -1))
	(if (not (%string-use-utf8? str))
	    (%string-titlecase str start end)
	    (begin
	      (unless (string? str)
		(error-bad-string  str))
	      (unless (integer? start)
		(error "bad starting index ~S" start))
	      (cond
		((not (integer? end))
		 (error "bad ending index ~S" end))
		((= end -1)
		 (set! end (string-length str))))

	      (let ((new (make-string (- end start))))
		(let Loop ((i start)
			   (j 0)
			   (prev-is-sep? #t))
		  (if (= i end)
		      new
		      (let* ((curr (string-ref str i))
			     (curr-is-sep? (not (char-alphabetic? curr))))
			(string-set! new j (cond
					     (curr-is-sep?  curr)
					     (prev-is-sep?  (char-upcase curr))
					     (else          (char-downcase curr))))
			(Loop (+ i 1) (+ j 1) curr-is-sep?)))))))))



    ;;
    ;; string-titlecase!
    ;;
    (set! string-titlecase!
      (lambda (str :optional (start 0) (end -1))
	(if (not (%string-use-utf8? str))
	    (%string-titlecase! str start end)
	    (begin
	      (unless (string? str)
		(error-bad-string str))
	      (unless (integer? start)
		(error "bad starting index ~S" start))
	      (cond
		((not (integer? end))
		 (error "bad ending index ~S" end))
		((= end -1)
		 (set! end (string-length str))))

	      (let Loop ((i start)
			 (prev-is-sep? #t))
		(if (= i end)
		    (void)
		    (let* ((curr (string-ref str i))
			   (curr-is-sep? (not (char-alphabetic? curr))))
		      (string-set! str i (cond
					   (curr-is-sep?  curr)
					   (prev-is-sep?  (char-upcase curr))
					   (else          (char-downcase curr))))
		      (Loop (+ i 1) curr-is-sep?))))))))
    ))