Apply suggested optimisations

This commit is contained in:
smoogipoo 2019-12-25 12:04:28 +09:00
parent 36dd0e6998
commit 1a7937bcf7

View File

@ -100,17 +100,23 @@ namespace osu.Desktop
private static readonly int ellipsis_length = Encoding.UTF8.GetByteCount(new[] { '…' }); private static readonly int ellipsis_length = Encoding.UTF8.GetByteCount(new[] { '…' });
private string truncate(ReadOnlySpan<char> str) private string truncate(string str)
{ {
if (Encoding.UTF8.GetByteCount(str) <= 128) if (Encoding.UTF8.GetByteCount(str) <= 128)
return new string(str); return str;
ReadOnlyMemory<char> strMem = str.AsMemory();
do do
{ {
str = str[..^1]; strMem = strMem[..^1];
} while (Encoding.UTF8.GetByteCount(str) + ellipsis_length > 128); } while (Encoding.UTF8.GetByteCount(strMem.Span) + ellipsis_length > 128);
return new string(str) + '…'; return string.Create(strMem.Length + 1, strMem, (span, mem) =>
{
mem.Span.CopyTo(span);
span[^1] = '…';
});
} }
private string getDetails(UserActivity activity) private string getDetails(UserActivity activity)