From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.devel Subject: Re: How to get buffer byte length (not number of characters)? Date: Tue, 20 Aug 2024 14:15:39 +0300 Message-ID: <86o75nwilg.fsf@gnu.org> References: <87wmkbekjp.fsf@ushin.org> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="24300"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: Joseph Turner Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Tue Aug 20 13:17:09 2024 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1sgMrN-00069r-3L for ged-emacs-devel@m.gmane-mx.org; Tue, 20 Aug 2024 13:17:09 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sgMqU-0000Sk-1r; Tue, 20 Aug 2024 07:16:15 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sgMqI-0000Sa-R0 for emacs-devel@gnu.org; Tue, 20 Aug 2024 07:16:03 -0400 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sgMqH-0003IU-U6; Tue, 20 Aug 2024 07:16:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-version:References:Subject:In-Reply-To:To:From: Date; bh=KsSiB8AR2eDqyXGTH8b4X5wZOAKearslXkj9nmPb1XU=; b=l7HGZ6myTboqgsxYS/1K VGEBOXsfk5m7t4Nk/S/w8lnkuuW61tM0NLHeLuUdtT9TEi1N1FTu9+mH0MIHy0F0MAZICZKv8O+Ip O2uoMKOcROEe8WRqZg9VVoAIphwklmGO5dRMS0glGdVkstRkSEMKaqL5EitejUR1tQtXrQpsdjFlN QgEK22iBjsp0Sj0P1pk5ktMmIajZP12p4V8uAdIyZsetzPt4YirQuzRiumLqDxBHDUZItcavW/0Z/ wbV5nhnVIIEVuhKW2QSr3nWnnVSdMv8pS3j4okd7zWF6i13ms5SvC2Ev6568q7Mc/JXJwnektJEVI CICst/TGibY0Fw==; In-Reply-To: <87wmkbekjp.fsf@ushin.org> (message from Joseph Turner on Tue, 20 Aug 2024 00:10:50 -0700) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:322955 Archived-At: > From: Joseph Turner > Date: Tue, 20 Aug 2024 00:10:50 -0700 > > Hello! > > `buffer-size' returns the number of characters in a buffer: > > (with-temp-buffer > (insert "你好") > (buffer-size)) ;; 2 > > However, the buffer's byte length may be larger: > > (let* ((filename (make-temp-file "buffer-size-test-")) > (file (with-temp-file filename (insert "你好")))) > (file-attribute-size (file-attributes filename))) ;; 6 > > How can I get a buffer's byte length without writing to a file? This depends on why do you need the byte length of the buffer. If I interpret your question literally, then this is the answer: (position-bytes (point-max)) perhaps preceded by a call to 'widen'. But that returns the number of bytes that the buffer's characters take when represented in the internal Emacs representation of characters, which is not necessarily useful to Lisp programs. For example, if you need to know how many bytes will Emacs write to a file if you save the buffer, or to a network connection or a sub-process if you send the buffer there, then you need to consider the encoding process: Emacs always encodes the buffer text on output to the external world. If this is what you want, then you need to use bufferpos-to-filepos, and make sure you pass the correct coding-system argument to it. If you need this for something else, please tell the details.