[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

how to calculate the size of string in bytes?

From: Sam Halliday
Subject: how to calculate the size of string in bytes?
Date: Tue, 18 Aug 2015 02:11:54 -0700 (PDT)
User-agent: G2/1.0

Hi all,

We've had to change the ENSIME protocol to be more friendly to other editors 
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 be 
the number of bytes in the message.

We're sending the string to `process-send-string' and `read'ing from the 
associated network buffer. But when calculating the outgoing length of the 
string that we want to send, we use `length' --- but we need this to be 
`length-in-bytes' not the number of multibyte chars. Is there a built in 
function 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 thing 
here, but if somebody could check, that would be greatly appreciated.

These are the relevant part of our Emacs code

(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)))

(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)))))

reply via email to

[Prev in Thread] Current Thread [Next in Thread]