Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions flang/lib/Parser/characters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,21 +158,24 @@ DecodedCharacter DecodeRawCharacter<Encoding::UTF_8>(
const char *cp, std::size_t bytes) {
auto p{reinterpret_cast<const std::uint8_t *>(cp)};
char32_t ch{*p};
if (ch <= 0x7f) {
// Valid UTF-8 encodings must be minimal.
if (ch <= 0x7f) { // 1 byte: 7 bits of payload
return {ch, 1};
} else if ((ch & 0xf8) == 0xf0 && bytes >= 4 && ch > 0xf0 &&
((p[1] | p[2] | p[3]) & 0xc0) == 0x80) {
} else if ((ch & 0xf8) == 0xf0 && bytes >= 4 &&
((p[1] | p[2] | p[3]) & 0xc0) == 0x80 && (ch > 0xf0 || p[1] > 0x8f)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do 0x8f and 0x9f come from?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second and later bytes of a UTF-8 encoding sequence are always values in the range 0x80 to 0xbf. Their least significant six bits carry their 6-bit payloads. A four-byte UTF-8 sequence has 21 bits of payload: three bits in the first byte, and six bits in each of the next three bytes. Since a 3-byte UTF-8 sequence has 16 bits of payload, a minimal four-byte UTF-8 sequence must have at least one of its most significant five (21-16) payload bits set. These five most significant payload bits are encoded as the three least significant bits of the first byte and as the 0x20 and 0x10 bits of the second byte. So either the first byte (in the range 0xf0-0xf7) has to be greater than 0xf0, or the second byte (in the range 0x80-0xbf) has to be greater than 0x8f.

// 4 bytes: 3+6+6+6=21 bits of payload
ch = ((ch & 7) << 6) | (p[1] & 0x3f);
ch = (ch << 6) | (p[2] & 0x3f);
ch = (ch << 6) | (p[3] & 0x3f);
return {ch, 4};
} else if ((ch & 0xf0) == 0xe0 && bytes >= 3 && ch > 0xe0 &&
((p[1] | p[2]) & 0xc0) == 0x80) {
} else if ((ch & 0xf0) == 0xe0 && bytes >= 3 &&
((p[1] | p[2]) & 0xc0) == 0x80 && (ch > 0xe0 || p[1] > 0x9f)) {
// 3 bytes: 4+6+6=16 bits of payload
ch = ((ch & 0xf) << 6) | (p[1] & 0x3f);
ch = (ch << 6) | (p[2] & 0x3f);
return {ch, 3};
} else if ((ch & 0xe0) == 0xc0 && bytes >= 2 && ch > 0xc0 &&
(p[1] & 0xc0) == 0x80) {
(p[1] & 0xc0) == 0x80) { // 2 bytes: 5+6=11 bits of payload
ch = ((ch & 0x1f) << 6) | (p[1] & 0x3f);
return {ch, 2};
} else {
Expand Down
15 changes: 15 additions & 0 deletions flang/test/Parser/utf8-01.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
!RUN: %flang_fc1 -fdebug-unparse %s 2>&1 | FileCheck %s

character(kind=4), parameter :: c(2) = [character(kind=4) :: &
4_'🍌', 4_'水' ]
print *, '🍌'
print *, 4_'🍌'
print *, '水'
print *, 4_'水'
end

!CHECK: CHARACTER(KIND=4_4), PARAMETER :: c(2_4) = [CHARACTER(KIND=4,LEN=1)::4_"\360\237\215\214",4_"\346\260\264"]
!CHECK: PRINT *, "\360\237\215\214"
!CHECK: PRINT *, 4_"\360\237\215\214"
!CHECK: PRINT *, "\346\260\264"
!CHECK: PRINT *, 4_"\346\260\264"
Loading