I wrote this method to change the string
public string Reverse(string s)
{
if(string.IsNullOrEmpty(s))
return s;
TextElementEnumerator enumerator =
StringInfo.GetTextElementEnumerator(s);
var elements = new List<char>();
while (enumerator.MoveNext())
{
var cs = enumerator.GetTextElement().ToCharArray();
if (cs.Length > 1)
{
elements.AddRange(cs.Reverse());
}
else
{
elements.AddRange(cs);
}
}
elements.Reverse();
return string.Concat(elements);
}
Now I do not want to start a discussion about how this code can be made more efficient or how there is one liner that I could use instead. I know that you can do Xors and all sorts of other things to potentially improve this code. If I want to reorganize the code later, I could do it easily, since I have unit tests.
Currently, this correctly changes BML strings (including strings with type accents "Les Misérables") and strings containing combined characters, such as "Les Mise\u0301rables".
My test containing surrogate pairs works if they are expressed as follows
Assert.AreEqual("𠈓", _stringOperations.Reverse("𠈓"));
But if I express surrogate pairs like this
Assert.AreEqual("\u10000", _stringOperations.Reverse("\u10000"));
. , ?
- , , , .