From e176ea749c00312fd76e5c6cc7004c8f2aa2ca34 Mon Sep 17 00:00:00 2001 From: stefnotch Date: Fri, 19 Feb 2021 20:31:30 +0100 Subject: [PATCH] Technically, reversing a unicode string involves unicode stuff Like this --- Source/Engine/Utilities/StringUtils.cs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/Source/Engine/Utilities/StringUtils.cs b/Source/Engine/Utilities/StringUtils.cs index 4dd28e705..e443f8f11 100644 --- a/Source/Engine/Utilities/StringUtils.cs +++ b/Source/Engine/Utilities/StringUtils.cs @@ -232,16 +232,26 @@ namespace FlaxEngine return result; } + private static IEnumerable GraphemeClusters(this string s) + { + var enumerator = System.Globalization.StringInfo.GetTextElementEnumerator(s); + while (enumerator.MoveNext()) + { + yield return (string)enumerator.Current; + } + } + /// /// Reverses the specified input string. /// + /// Correctly handles all UTF-16 strings /// The string to reverse. /// The reversed string. public static string Reverse(this string s) { - char[] charArray = s.ToCharArray(); - Array.Reverse(charArray); - return new string(charArray); + string[] graphemes = s.GraphemeClusters().ToArray(); + Array.Reverse(graphemes); + return string.Concat(graphemes); } private static readonly Regex IncNameRegex1 = new Regex("(\\d+)$");