From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1442) id B55F9385840C; Wed, 14 Feb 2024 15:58:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B55F9385840C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1707926320; bh=b9oZrotkONSmnoZldDRygV337DEq9fd8XZz4hF4rRsg=; h=From:To:Subject:Date:From; b=OFOBIp5aB3UE1VkAp1qf0YIzFm2hkVzXzVe/cytwa8VZ06cAIDN7xHlHxOP5S23uF WsQKb9B1k7QyTy3DZ3jEwa+IJRLDtNLq3S8Ex6Cl2u/SfDRSSLo31vwHxUANS5W0TO b1yNY81UTCXtnLjnKMtYzQSAs8Nhg8EwMlZnozmA= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jerry DeLisle To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-8983] Fortran: Implement read_x for UTF-8 encoded files. X-Act-Checkin: gcc X-Git-Author: Jerry DeLisle X-Git-Refname: refs/heads/master X-Git-Oldrev: eafbb05c49957096d5118dfa661c0efba774bd8b X-Git-Newrev: b79d3e6a9284703b70688122f7d4955e7c50804a Message-Id: <20240214155840.B55F9385840C@sourceware.org> Date: Wed, 14 Feb 2024 15:58:40 +0000 (GMT) List-Id: https://gcc.gnu.org/g:b79d3e6a9284703b70688122f7d4955e7c50804a commit r14-8983-gb79d3e6a9284703b70688122f7d4955e7c50804a Author: Jerry DeLisle Date: Tue Feb 13 14:32:21 2024 -0800 Fortran: Implement read_x for UTF-8 encoded files. PR fortran/99210 libgfortran/ChangeLog: * io/read.c (read_x): If UTF-8 encoding is enabled, use read_utf8 to move one character over in the read buffer. gcc/testsuite/ChangeLog: * gfortran.dg/pr99210.f90: New test. Diff: --- gcc/testsuite/gfortran.dg/pr99210.f90 | 29 +++++++++++++++++++++++++++++ libgfortran/io/read.c | 17 +++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/gcc/testsuite/gfortran.dg/pr99210.f90 b/gcc/testsuite/gfortran.dg/pr99210.f90 new file mode 100644 index 000000000000..9fd2fb468df0 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/pr99210.f90 @@ -0,0 +1,29 @@ +! { dg-do run } +! PR99210 X editing for reading file with encoding='utf-8' +program test_bug_format_x + use iso_fortran_env + integer, parameter :: u = selected_char_kind('ISO_10646') + + character(kind=u, len=1) a, b, a1, b1, b2 + + open(unit=10, file='test_bug_format_x.tmp', encoding='UTF-8') + + a = char(int(z'03B1'), u) + b = char(int(z'03B2'), u) + write(10, '(a1, a1)') a, b + + rewind(10) + read(10, '(a1, a1)') a1, b1 + + rewind(10) + read(10, '(1x, a1)') b2 + + close (10, status="delete") + if(a /= a1 .or. b /= b1) then + error stop 1 + end if + + if(b /= b2) then + error stop 2 + end if +end program test_bug_format_x diff --git a/libgfortran/io/read.c b/libgfortran/io/read.c index 0ffcf76fd387..e2d2f8be8065 100644 --- a/libgfortran/io/read.c +++ b/libgfortran/io/read.c @@ -1307,6 +1307,23 @@ read_x (st_parameter_dt *dtp, size_t n) if (n == 0) return; + + if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8) + { + gfc_char4_t c; + size_t nbytes, j; + + /* Proceed with decoding one character at a time. */ + for (j = 0; j < n; j++) + { + c = read_utf8 (dtp, &nbytes); + + /* Check for a short read and if so, break out. */ + if (nbytes == 0 || c == (gfc_char4_t)0) + break; + } + return; + } length = n;