help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: how to calculate the size of string in bytes?


From: Eli Zaretskii
Subject: Re: how to calculate the size of string in bytes?
Date: Tue, 18 Aug 2015 17:34:58 +0300

> Date: Tue, 18 Aug 2015 02:11:54 -0700 (PDT)
> From: Sam Halliday <sam.halliday@gmail.com>
> 
> 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?

Emacs 25 has bufferpos-to-filepos, which I think does what you want.

> 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.

It will fail if the string includes some exotic characters or raw
bytes.



reply via email to

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