fix: increase termination efficiency

This commit is contained in:
Anna 2022-06-03 19:56:24 -04:00
parent 65fb1f5cce
commit a5f93fd194
1 changed files with 3 additions and 3 deletions

View File

@ -4,9 +4,9 @@ namespace ChatTwo.Util;
internal static class StringUtil {
internal static byte[] ToTerminatedBytes(this string s) {
var unterminated = Encoding.UTF8.GetBytes(s);
var bytes = new byte[unterminated.Length + 1];
Array.Copy(unterminated, bytes, unterminated.Length);
var utf8 = Encoding.UTF8;
var bytes = new byte[utf8.GetByteCount(s) + 1];
utf8.GetBytes(s, 0, s.Length, bytes, 0);
bytes[^1] = 0;
return bytes;
}