Technically, reversing a unicode string involves unicode stuff

Like this
This commit is contained in:
stefnotch
2021-02-19 20:31:30 +01:00
parent 3964fc7795
commit e176ea749c

View File

@@ -232,16 +232,26 @@ namespace FlaxEngine
return result;
}
private static IEnumerable<string> GraphemeClusters(this string s)
{
var enumerator = System.Globalization.StringInfo.GetTextElementEnumerator(s);
while (enumerator.MoveNext())
{
yield return (string)enumerator.Current;
}
}
/// <summary>
/// Reverses the specified input string.
/// </summary>
/// <remarks>Correctly handles all UTF-16 strings</remarks>
/// <param name="s">The string to reverse.</param>
/// <returns>The reversed string.</returns>
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+)$");