diff lisp/gnus/mm-util.el @ 90143:146c086df160

Revision: miles@gnu.org--gnu-2005/emacs--unicode--0--patch-37 Merge from emacs--cvs-trunk--0 Patches applied: * emacs--cvs-trunk--0 (patch 241-257) - Update from CVS - Merge from gnus--rel--5.10 * gnus--rel--5.10 (patch 59-65) - Update from CVS - Merge from emacs--cvs-trunk--0 - (mm-string-to-multibyte): Use Gnus trunk definition.
author Miles Bader <miles@gnu.org>
date Thu, 14 Apr 2005 05:03:52 +0000
parents 4da4a09e8b1b 6cf26dc690b2
children 01137c1fdbe9
line wrap: on
line diff
--- a/lisp/gnus/mm-util.el	Mon Apr 11 03:59:40 2005 +0000
+++ b/lisp/gnus/mm-util.el	Thu Apr 14 05:03:52 2005 +0000
@@ -57,9 +57,11 @@
 		    mm-mime-mule-charset-alist)
 	    nil t))))
      (subst-char-in-string
-      . (lambda (from to string) ;; stolen (and renamed) from nnheader.el
-	  "Replace characters in STRING from FROM to TO."
-	  (let ((string (substring string 0)) ;Copy string.
+      . (lambda (from to string &optional inplace)
+	  ;; stolen (and renamed) from nnheader.el
+	  "Replace characters in STRING from FROM to TO.
+	  Unless optional argument INPLACE is non-nil, return a new string."
+	  (let ((string (if inplace string (copy-sequence string)))
 		(len (length string))
 		(idx 0))
 	    ;; Replace all occurrences of FROM with TO.
@@ -70,7 +72,31 @@
 	    string)))
      (string-as-unibyte . identity)
      (string-make-unibyte . identity)
+     ;; string-as-multibyte often doesn't really do what you think it does.
+     ;; Example:
+     ;;    (aref (string-as-multibyte "\201") 0) -> 129 (aka ?\201)
+     ;;    (aref (string-as-multibyte "\300") 0) -> 192 (aka ?\300)
+     ;;    (aref (string-as-multibyte "\300\201") 0) -> 192 (aka ?\300)
+     ;;    (aref (string-as-multibyte "\300\201") 1) -> 129 (aka ?\201)
+     ;; but
+     ;;    (aref (string-as-multibyte "\201\300") 0) -> 2240
+     ;;    (aref (string-as-multibyte "\201\300") 1) -> <error>
+     ;; Better use string-to-multibyte or encode-coding-string.
+     ;; If you really need string-as-multibyte somewhere it's usually
+     ;; because you're using the internal emacs-mule representation (maybe
+     ;; because you're using string-as-unibyte somewhere), which is
+     ;; generally a problem in itself.
+     ;; Here is an approximate equivalence table to help think about it:
+     ;; (string-as-multibyte s)   ~= (decode-coding-string s 'emacs-mule)
+     ;; (string-to-multibyte s)   ~= (decode-coding-string s 'binary)
+     ;; (string-make-multibyte s) ~= (decode-coding-string s locale-coding-system)
      (string-as-multibyte . identity)
+     (string-to-multibyte
+      . (lambda (string)
+	  "Return a multibyte string with the same individual chars as string."
+	  (mapconcat
+	   (lambda (ch) (mm-string-as-multibyte (char-to-string ch)))
+	   string "")))
      (multibyte-string-p . ignore)
      ;; It is not a MIME function, but some MIME functions use it.
      (make-temp-file . (lambda (prefix &optional dir-flag)
@@ -153,7 +179,7 @@
 If CS is available, return CS itself in Emacs, and return a coding
 system object in XEmacs."
   (if (fboundp 'find-coding-system)
-      (find-coding-system cs)
+      (and cs (find-coding-system cs))
     (if (fboundp 'coding-system-p)
 	(when (coding-system-p cs)
 	  cs)
@@ -878,7 +904,7 @@
     (defun mm-detect-coding-region (start end)
       "Like `detect-coding-region' except returning the best one."
       (let ((coding-systems
-	     (detect-coding-region (point) (point-max))))
+	     (detect-coding-region start end)))
 	(or (car-safe coding-systems)
 	    coding-systems)))
   (defun mm-detect-coding-region (start end)
@@ -902,5 +928,5 @@
 
 (provide 'mm-util)
 
-;;; arch-tag: 94dc5388-825d-4fd1-bfa5-2100aa351238
+;; arch-tag: 94dc5388-825d-4fd1-bfa5-2100aa351238
 ;;; mm-util.el ends here