From 59a7ff95b3e75149da74413071c653e9c730c44e Mon Sep 17 00:00:00 2001 From: BDisp Date: Mon, 22 May 2023 07:14:12 +0100 Subject: [PATCH] Fixes #2655. Should be avoid cast to char on a Rune ctor which always fallback for a UTF16 char. (#2656) --- UICatalog/Scenarios/CharacterMap.cs | 4 ++-- UnitTests/Text/RuneTests.cs | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/UICatalog/Scenarios/CharacterMap.cs b/UICatalog/Scenarios/CharacterMap.cs index 295ce7d09..bb8c8a4d1 100644 --- a/UICatalog/Scenarios/CharacterMap.cs +++ b/UICatalog/Scenarios/CharacterMap.cs @@ -332,7 +332,7 @@ class CharMap : ScrollView { } private void CopyCodePoint () => Clipboard.Contents = $"U+{SelectedCodePoint:x5}"; - private void CopyGlyph () => Clipboard.Contents = $"{new Rune ((char)SelectedCodePoint)}"; + private void CopyGlyph () => Clipboard.Contents = $"{new Rune (SelectedCodePoint)}"; public override void OnDrawContent (Rect contentArea) { @@ -553,7 +553,7 @@ class CharMap : ScrollView { //} } - var title = $"{ToCamelCase (name)} - {new Rune ((char)SelectedCodePoint)} U+{SelectedCodePoint:x4}"; + var title = $"{ToCamelCase (name)} - {new Rune (SelectedCodePoint)} U+{SelectedCodePoint:x4}"; switch (MessageBox.Query (title, decResponse, "Copy _Glyph", "Copy Code _Point", "Cancel")) { case 0: CopyGlyph (); diff --git a/UnitTests/Text/RuneTests.cs b/UnitTests/Text/RuneTests.cs index c4df9a413..9c0d686bf 100644 --- a/UnitTests/Text/RuneTests.cs +++ b/UnitTests/Text/RuneTests.cs @@ -711,4 +711,13 @@ public class RuneTests { Assert.Equal (utf16Length, r.Utf16SequenceLength); Assert.Equal (utf8Length, r.Utf8SequenceLength); } + + [Fact] + public void Cast_To_Char_Durrogate_Pair_Return_UTF16 () + { + Assert.NotEqual ("𝔹", $"{new Rune (unchecked((char)0x1d539))}"); + Assert.Equal ("픹", $"{new Rune (unchecked((char)0x1d539))}"); + Assert.Equal ("픹", $"{new Rune (0xd539)}"); + Assert.Equal ("𝔹", $"{new Rune (0x1d539)}"); + } }