forked from StackExchange/StackExchange.Redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrameworkShims.Encoding.cs
More file actions
85 lines (79 loc) · 3.19 KB
/
Copy pathFrameworkShims.Encoding.cs
File metadata and controls
85 lines (79 loc) · 3.19 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System.Runtime.InteropServices;
#if !NET
// ReSharper disable once CheckNamespace
namespace System.Text
{
internal static class EncodingExtensions
{
public static unsafe int GetBytes(this Encoding encoding, ReadOnlySpan<char> source, Span<byte> destination)
{
if (source.IsEmpty) return 0;
fixed (byte* bPtr = &MemoryMarshal.GetReference(destination))
{
fixed (char* cPtr = source)
{
return encoding.GetBytes(cPtr, source.Length, bPtr, destination.Length);
}
}
}
public static unsafe int GetChars(this Encoding encoding, ReadOnlySpan<byte> source, Span<char> destination)
{
if (source.IsEmpty) return 0;
fixed (byte* bPtr = &MemoryMarshal.GetReference(source))
{
fixed (char* cPtr = &MemoryMarshal.GetReference(destination))
{
return encoding.GetChars(bPtr, source.Length, cPtr, destination.Length);
}
}
}
public static unsafe int GetCharCount(this Encoding encoding, ReadOnlySpan<byte> source)
{
if (source.IsEmpty) return 0;
fixed (byte* bPtr = &MemoryMarshal.GetReference(source))
{
return encoding.GetCharCount(bPtr, source.Length);
}
}
public static unsafe string GetString(this Encoding encoding, ReadOnlySpan<byte> source)
{
if (source.IsEmpty) return "";
fixed (byte* bPtr = &MemoryMarshal.GetReference(source))
{
return encoding.GetString(bPtr, source.Length);
}
}
public static unsafe int GetChars(this Decoder decoder, ReadOnlySpan<byte> bytes, Span<char> chars, bool flush)
{
// empty input cannot flush any held-over bytes (verified on netfx), so 0 is correct either way
if (bytes.IsEmpty) return 0;
fixed (byte* bPtr = &MemoryMarshal.GetReference(bytes))
{
fixed (char* cPtr = &MemoryMarshal.GetReference(chars))
{
return decoder.GetChars(bPtr, bytes.Length, cPtr, chars.Length, flush);
}
}
}
public static unsafe void Convert(this Decoder decoder, ReadOnlySpan<byte> bytes, Span<char> chars, bool flush, out int bytesUsed, out int charsUsed, out bool completed)
{
fixed (char* cPtr = &MemoryMarshal.GetReference(chars))
{
if (bytes.IsEmpty)
{
// a valid non-null pointer for the empty-input (flush-only) case
byte dummy = 0;
decoder.Convert(&dummy, 0, cPtr, chars.Length, flush, out bytesUsed, out charsUsed, out completed);
}
else
{
fixed (byte* bPtr = &MemoryMarshal.GetReference(bytes))
{
decoder.Convert(bPtr, bytes.Length, cPtr, chars.Length, flush, out bytesUsed, out charsUsed, out completed);
}
}
}
}
}
}
#endif