From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Newsgroups: gmane.emacs.bugs Subject: bug#70007: [PATCH] native JSON encoder Date: Tue, 2 Apr 2024 16:13:58 +0200 Message-ID: <4C93E902-282D-4408-B449-C8E771B2B3E4@gmail.com> References: <1BF559D1-DB9F-4FEB-90ED-72E0EFD76424@gmail.com> <86wmpphrg7.fsf@gnu.org> <4589243D-C11A-45C1-AF3E-6F4A5BADEB54@gmail.com> <864jcrindg.fsf@gnu.org> <291DD5F1-85B8-4647-A40A-EBBD4C51E253@gmail.com> <8634sbijfx.fsf@gnu.org> <2CF47DA5-A65B-47C4-A28A-6FEE1469BD13@gmail.com> <86cyrdfuai.fsf@gnu.org> <3139C8FE-5C67-4FE3-B940-F449DA73E76C@gmail.com> <86le5zdfbi.fsf@gnu.org> Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.15\)) Content-Type: multipart/mixed; boundary="Apple-Mail=_02BFADEC-F9DF-4C2E-8704-AF3AF789CAD1" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="36435"; mail-complaints-to="usenet@ciao.gmane.io" Cc: casouri@gmail.com, 70007@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Apr 02 16:15:41 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@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 1rrevN-0009Je-19 for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 02 Apr 2024 16:15:41 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rrev6-000668-Bg; Tue, 02 Apr 2024 10:15:24 -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 1rreuh-00063F-UE for bug-gnu-emacs@gnu.org; Tue, 02 Apr 2024 10:15:01 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rreug-0008U5-HF for bug-gnu-emacs@gnu.org; Tue, 02 Apr 2024 10:14:58 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rreuk-0007Lz-4K for bug-gnu-emacs@gnu.org; Tue, 02 Apr 2024 10:15:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 02 Apr 2024 14:15:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 70007 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 70007-submit@debbugs.gnu.org id=B70007.171206725428086 (code B ref 70007); Tue, 02 Apr 2024 14:15:02 +0000 Original-Received: (at 70007) by debbugs.gnu.org; 2 Apr 2024 14:14:14 +0000 Original-Received: from localhost ([127.0.0.1]:54855 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rretx-0007Iu-JL for submit@debbugs.gnu.org; Tue, 02 Apr 2024 10:14:14 -0400 Original-Received: from mail-lf1-x130.google.com ([2a00:1450:4864:20::130]:59702) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rretv-0007I1-C9 for 70007@debbugs.gnu.org; Tue, 02 Apr 2024 10:14:12 -0400 Original-Received: by mail-lf1-x130.google.com with SMTP id 2adb3069b0e04-516b80252c5so854827e87.3 for <70007@debbugs.gnu.org>; Tue, 02 Apr 2024 07:14:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712067241; x=1712672041; darn=debbugs.gnu.org; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:sender:from:to:cc:subject:date:message-id:reply-to; bh=8gyVk4MN1Zd5mZnqH3NH0cDrb7AxYffHP1IZSp9XBFo=; b=hl+o7cxWdIIDxaUD83IwxBPvJmQR+uVjSpPG4RdEIjSMZoP2lS6Nw7KmPM7ZCYAGIX wH7Ri/xXPEfcA67L64eHSKweoBydeo44/Pq46hN+p66nwzpok1IXRuS5TWQ6RW6E0F6y 70mkx3udL1yQWwJQ2POOOF1wf8lNlq12gSfjO7H4EEORQQdKkuDsniYRC+UC0shEv55u F5T2odUYY21qlwv9cpVbzwcUpky6UOkpkio6wlpDcF2ZBlwkL1e+s8ePv9mKQmg1zfct dKi6QoWeOoSQRiE0LNoSdPeTpHnCegHhICoONmx37yib+qnhd2AfUFAAW2xX0g/ab7X7 wPBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712067241; x=1712672041; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:sender:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=8gyVk4MN1Zd5mZnqH3NH0cDrb7AxYffHP1IZSp9XBFo=; b=ZSbE0BcGy63wuEXmsy3T7/XVBjmYwNsf+iFmxNLKxMKADvVHNXBtudA/1FxF8QwC00 x0Dg3Ni1mheIAECSHEJhqHSvS2Yww17XqhNYagwCXAFPHy63i2KcSWTqBrQg4JOBiA0/ wGgBcyyMe1lGx3Jp4HijES8NQU5gOC3onOqe2eYD82oQS1UGPd8HYICjzoiGerqDDnNd e6uQ8fCY3eGqkPxPOjuDG7mtQhWIvHiM0W0wzNu1U6iC4STNXsMghIVPSe+CbSsKivQR Yz/ZsFOxCG1Z+fehV+/GikICT+EqmeSMJYF0ePC7ZS8dc80nsygY1kZvkb/DDdpT3zSQ PVvQ== X-Forwarded-Encrypted: i=1; AJvYcCX7zjtXuOMLpni+N6BJM8BUfOHMLX1EYNApSZyxXytcSAY1gh2dVr4ZjUPOZMEwGKtMEWMBw4vj+I5w0vi/yszWLW8JF7M= X-Gm-Message-State: AOJu0YwP2Cu4EtyJBE9K+m7VSpMA5GZTRKIKC0uIEc0qYfvANhoe93NB ocr+1tXoLRPDKBVU3yv4Nl3tQo+1TNUE2rxonKdvHeILiIK6/HaT X-Google-Smtp-Source: AGHT+IGrlhQZ+mPtyJyqcMWuHId2NUpkYnTCwKoS2OAX3xO5R0Eroh22cUe99XwxSfJUVZO1Ue3f9w== X-Received: by 2002:ac2:424b:0:b0:515:9ee7:ce45 with SMTP id m11-20020ac2424b000000b005159ee7ce45mr9207827lfl.49.1712067241078; Tue, 02 Apr 2024 07:14:01 -0700 (PDT) Original-Received: from smtpclient.apple (c80-217-1-132.bredband.tele2.se. [80.217.1.132]) by smtp.gmail.com with ESMTPSA id c15-20020a19760f000000b00515ac3af704sm1739962lff.79.2024.04.02.07.13.59 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 02 Apr 2024 07:14:00 -0700 (PDT) In-Reply-To: X-Mailer: Apple Mail (2.3654.120.0.1.15) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:282511 Archived-At: --Apple-Mail=_02BFADEC-F9DF-4C2E-8704-AF3AF789CAD1 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Looks like the new serialiser inherited a bug from the old code: = `json-insert` in a unibyte buffer does not move point correctly. = Example: (with-temp-buffer (set-buffer-multibyte nil) (json-insert "=C3=A9") (list (buffer-string) (point))) =3D> ("\"\303\251\"" 4) The string is correct but the position should be 5, not 4. This made me look at the Fjson_insert logic a bit. I'm probably = betraying my lack of knowledge about buffer subtleties here, but since = the serialiser always produces (correct) UTF-8, shouldn't it be enough = to copy the bytes, don't bother with any decoding, and perform the = buffer insertion ceremonies? Proposed patch attached. (There will also be a test, of course.) --Apple-Mail=_02BFADEC-F9DF-4C2E-8704-AF3AF789CAD1 Content-Disposition: attachment; filename=json-insert.diff Content-Type: application/octet-stream; x-unix-mode=0644; name="json-insert.diff" Content-Transfer-Encoding: 7bit diff --git a/src/json.c b/src/json.c index c3244ad04d2..7c62d63c6fd 100644 --- a/src/json.c +++ b/src/json.c @@ -656,39 +656,14 @@ DEFUN ("json-insert", Fjson_insert, Sjson_insert, 1, MANY, /* No need to keep allocation beyond this point. */ unbind_to (count, Qnil); - ptrdiff_t inserted = 0; + bool ub_buffer = NILP (BVAR (current_buffer, enable_multibyte_characters)); ptrdiff_t inserted_bytes = jo.size; + ptrdiff_t inserted = ub_buffer ? jo.size : jo.size - jo.chars_delta; - /* If required, decode the stuff we've read into the gap. */ - struct coding_system coding; - /* JSON strings are UTF-8 encoded strings. */ - setup_coding_system (Qutf_8_unix, &coding); - coding.dst_multibyte = !NILP (BVAR (current_buffer, - enable_multibyte_characters)); - if (CODING_MAY_REQUIRE_DECODING (&coding)) - { - /* Now we have all the new bytes at the beginning of the gap, - but `decode_coding_gap` needs them at the end of the gap, so - we need to move them. */ - memmove (GAP_END_ADDR - inserted_bytes, GPT_ADDR, inserted_bytes); - decode_coding_gap (&coding, inserted_bytes); - inserted = coding.produced_char; - } - else - { - /* Make the inserted text part of the buffer, as unibyte text. */ - eassert (NILP (BVAR (current_buffer, enable_multibyte_characters))); - insert_from_gap_1 (inserted_bytes, inserted_bytes, false); - - /* The target buffer is unibyte, so we don't need to decode. */ - invalidate_buffer_caches (current_buffer, - PT, PT + inserted_bytes); - adjust_after_insert (PT, PT_BYTE, - PT + inserted_bytes, - PT_BYTE + inserted_bytes, - inserted_bytes); - inserted = inserted_bytes; - } + insert_from_gap_1 (inserted, inserted_bytes, false); + invalidate_buffer_caches (current_buffer, PT, PT + inserted); + adjust_after_insert (PT, PT_BYTE, PT + inserted, PT_BYTE + inserted_bytes, + inserted); /* Call after-change hooks. */ signal_after_change (PT, 0, inserted); --Apple-Mail=_02BFADEC-F9DF-4C2E-8704-AF3AF789CAD1--