Apply suggested optimisations

This commit is contained in:
smoogipoo 2019-12-25 12:04:28 +09:00
parent 36dd0e6998
commit 1a7937bcf7
1 changed files with 11 additions and 5 deletions

View File

@ -100,17 +100,23 @@ private void updateStatus()
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)
return new string(str);
return str;
ReadOnlyMemory<char> strMem = str.AsMemory();
do
{
str = str[..^1];
} while (Encoding.UTF8.GetByteCount(str) + ellipsis_length > 128);
strMem = strMem[..^1];
} 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)