Subject: Re: bug#12051: 24.1; rcirc-send-message doesn't take multibyte into account.

From: Leo <[email-masked]>
Date: Wed, 15 Aug 2012 21:10:45 +0800

On 2012-08-15 10:57 +0800, Eli Zaretskii wrote:
> You can always stop at known characters, like whitespace.
>
> Anyway, another way is simply to assume the worst possible expansion
> ratio, and estimate the byte count before encoding.

I make a small optimisation that calls encode-coding-region char by
char. Comments?

=== modified file 'lisp/net/rcirc.el'
--- lisp/net/rcirc.el	2012-08-15 12:26:48 +0000
+++ lisp/net/rcirc.el	2012-08-15 13:06:15 +0000
@@ -797,22 +797,24 @@
 (defun rcirc-split-message (message)
   "Split MESSAGE into chunks within `rcirc-max-message-length'."
   ;; `rcirc-encode-coding-system' can have buffer-local value.
-  (let ((encoding rcirc-encode-coding-system))
+  (let ((encoding rcirc-encode-coding-system)
+	result oversize)
     (with-temp-buffer
       (insert message)
       (goto-char (point-min))
-      (let (result)
-	(while (not (eobp))
-	  (goto-char (or (byte-to-position rcirc-max-message-length)
-			 (point-max)))
-	  ;; max message length is 512 including CRLF
-	  (while (and (not (bobp))
-		      (> (length (encode-coding-region
-				  (point-min) (point) encoding t))
-			 rcirc-max-message-length))
-	    (forward-char -1))
-	  (push (delete-and-extract-region (point-min) (point)) result))
-	(nreverse result)))))
+      (while (not (eobp))
+	(goto-char (or (byte-to-position rcirc-max-message-length)
+		       (point-max)))
+	;; Max message length is 512 including CRLF
+	(setq oversize (- (length (encode-coding-region
+				   (point-min) (point) encoding t))
+			  rcirc-max-message-length))
+	(while (and (not (bobp)) (> oversize 0))
+	  (decf oversize (length (encode-coding-region
+				  (1- (point)) (point) encoding t)))
+	  (forward-char -1))
+	(push (delete-and-extract-region (point-min) (point)) result)))
+    (nreverse result)))
 
 (defun rcirc-send-message (process target message &optional noticep silent)
   "Send TARGET associated with PROCESS a privmsg with text MESSAGE.