forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtf8.cs
More file actions
34 lines (29 loc) · 996 Bytes
/
Copy pathUtf8.cs
File metadata and controls
34 lines (29 loc) · 996 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
namespace System.Text.Unicode;
internal static class Utf8
{
public static bool TryWrite(Span<byte> destination, string value, out int bytesWritten)
{
var byteCount = Encoding.UTF8.GetByteCount(value);
if (byteCount > destination.Length)
{
bytesWritten = 0;
return false;
}
if (byteCount == value.Length)
{
for (var i = 0; i < value.Length; i++)
{
destination[i] = (byte)value[i];
}
bytesWritten = byteCount;
return true;
}
var bytes = Encoding.UTF8.GetBytes(value);
bytes.CopyTo(destination);
bytesWritten = byteCount;
return true;
}
}