From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Sam Halliday Newsgroups: gmane.emacs.help Subject: how to calculate the size of string in bytes? Date: Tue, 18 Aug 2015 02:11:54 -0700 (PDT) Message-ID: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1439889335 15574 80.91.229.3 (18 Aug 2015 09:15:35 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 18 Aug 2015 09:15:35 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Aug 18 11:15:31 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1ZRczS-0004l7-CD for geh-help-gnu-emacs@m.gmane.org; Tue, 18 Aug 2015 11:15:30 +0200 Original-Received: from localhost ([::1]:55324 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZRczR-0004fH-FB for geh-help-gnu-emacs@m.gmane.org; Tue, 18 Aug 2015 05:15:29 -0400 X-Received: by 10.107.164.215 with SMTP id d84mr5017986ioj.11.1439889114911; Tue, 18 Aug 2015 02:11:54 -0700 (PDT) X-Received: by 10.140.108.245 with SMTP id j108mr71023qgf.33.1439889114882; Tue, 18 Aug 2015 02:11:54 -0700 (PDT) Original-Path: usenet.stanford.edu!x6no1670712igd.0!news-out.google.com!78ni15527qge.1!nntp.google.com!69no1843817qgi.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=83.244.128.25; posting-account=kRukCAoAAAANs-vsVh9dFwo5kp5pwnPz Original-NNTP-Posting-Host: 83.244.128.25 User-Agent: G2/1.0 Injection-Date: Tue, 18 Aug 2015 09:11:54 +0000 Original-Xref: usenet.stanford.edu gnu.emacs.help:214380 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:106656 Archived-At: Hi all, We've had to change the ENSIME protocol to be more friendly to other editor= s and this has meant changing how we frame TCP messages. We used to have a 6 character hex number at the start of each message that = counted the number of multibyte characters, but we'd like to change it to b= e the number of bytes in the message. We're sending the string to `process-send-string' and `read'ing from the as= sociated network buffer. But when calculating the outgoing length of the st= ring that we want to send, we use `length' --- but we need this to be `leng= th-in-bytes' not the number of multibyte chars. Is there a built in functio= n to do this or am I going to have to iterate the string and count the byte= size of each character? A quick test shows that (length (encode-coding-string "EURO" 'raw-text)) seems to give the correct result (1 for ASCII, 2 for Pound Sterling, 3 for = Euro), but I am not 100% sure if this is correct. Similarly, when we read from the network, we want to ensure that we `read' = numbers of bytes, not multibyte chars. I *think* we are doing the right thi= ng here, but if somebody could check, that would be greatly appreciated. These are the relevant part of our Emacs code ;; https://github.com/ensime/ensime-emacs/blob/master/ensime-client.el#L507 (defun ensime-net-send (sexp proc) (let* ((msg (concat (ensime-prin1-to-string sexp) "\n")) (string (concat (ensime-net-encode-length (length msg)) msg)) (coding-system (cdr (process-coding-system proc)))) (when ensime--debug-messages (message "--> %s" sexp)) (ensime-log-event sexp) (process-send-string proc string))) ;; https://github.com/ensime/ensime-emacs/blob/master/ensime-client.el#L584 (defun ensime-net-read () "Read a message from the network buffer." (goto-char (point-min)) (let* ((length (ensime-net-decode-length)) (start (+ 6 (point))) (end (+ start length))) (assert (plusp length)) (goto-char (byte-to-position start)) (prog1 (read (current-buffer)) (delete-region (- (byte-to-position start) 6) (byte-to-position end)))))