unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* how to calculate the size of string in bytes?
@ 2015-08-18  9:11 Sam Halliday
  2015-08-18 10:13 ` tomas
                   ` (2 more replies)
  0 siblings, 3 replies; 19+ messages in thread
From: Sam Halliday @ 2015-08-18  9:11 UTC (permalink / raw)
  To: help-gnu-emacs

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

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



^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2015-08-19 19:47 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-08-18  9:11 how to calculate the size of string in bytes? Sam Halliday
2015-08-18 10:13 ` tomas
2015-08-18 14:37   ` Eli Zaretskii
2015-08-18 14:45     ` tomas
2015-08-18 15:00       ` Eli Zaretskii
2015-08-18 16:01         ` tomas
2015-08-18 16:35           ` Eli Zaretskii
2015-08-18 19:30             ` tomas
2015-08-18 19:49               ` Eli Zaretskii
2015-08-18 20:11                 ` tomas
2015-08-18 21:47   ` Stefan Monnier
2015-08-19  5:43     ` tomas
     [not found]   ` <mailman.8577.1439934462.904.help-gnu-emacs@gnu.org>
2015-08-19  8:57     ` Sam Halliday
2015-08-19  9:22       ` Sam Halliday
2015-08-19 19:47       ` Stefan Monnier
     [not found] ` <mailman.8504.1439892841.904.help-gnu-emacs@gnu.org>
2015-08-18 10:43   ` Sam Halliday
2015-08-18 11:47     ` tomas
     [not found]     ` <mailman.8510.1439898432.904.help-gnu-emacs@gnu.org>
2015-08-18 12:06       ` Sam Halliday
2015-08-18 14:34 ` Eli Zaretskii

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).