mirrored from https://www.bouncycastle.org/repositories/bc-csharp
-
Notifications
You must be signed in to change notification settings - Fork 602
Expand file tree
/
Copy pathStrings.cs
More file actions
166 lines (140 loc) · 5.33 KB
/
Copy pathStrings.cs
File metadata and controls
166 lines (140 loc) · 5.33 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using System.Diagnostics;
using System.Text;
namespace Org.BouncyCastle.Utilities
{
/// <summary> General string utilities.</summary>
public static class Strings
{
private static readonly UTF8Encoding StrictUtf8 = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false,
throwOnInvalidBytes: true);
/// <summary>
/// Use instead of <see cref="System.Text.Encoding.UTF8"/> to enable validation.
/// </summary>
public static Encoding UTF8 => StrictUtf8;
internal static void AppendFromByteArray(StringBuilder sb, byte[] buf, int off, int len)
{
sb.EnsureCapacity(sb.Length + len);
for (int i = 0; i < len; ++i)
{
sb.Append(Convert.ToChar(buf[off + i]));
}
}
internal static bool IsOneOf(string s, params string[] candidates)
{
foreach (string candidate in candidates)
{
if (s == candidate)
return true;
}
return false;
}
public static string FromByteArray(byte[] bs)
{
if (bs == null)
throw new ArgumentNullException(nameof(bs));
int len = bs.Length;
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
return string.Create(len, bs, (chars, bytes) =>
{
for (int i = 0; i < chars.Length; ++i)
{
chars[i] = Convert.ToChar(bytes[i]);
}
});
#else
char[] cs = new char[len];
for (int i = 0; i < len; ++i)
{
cs[i] = Convert.ToChar(bs[i]);
}
return new string(cs);
#endif
}
public static string FromByteArray(byte[] buf, int off, int len)
{
Arrays.ValidateSegment(buf, off, len);
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
return string.Create(len, buf.AsMemory(off, len), (chars, bytes) =>
{
var span = bytes.Span;
for (int i = 0; i < chars.Length; ++i)
{
chars[i] = Convert.ToChar(span[i]);
}
});
#else
char[] cs = new char[len];
for (int i = 0; i < len; ++i)
{
cs[i] = Convert.ToChar(buf[off + i]);
}
return new string(cs);
#endif
}
public static byte[] ToByteArray(char[] cs)
{
byte[] bs = new byte[cs.Length];
for (int i = 0; i < bs.Length; ++i)
{
bs[i] = Convert.ToByte(cs[i]);
}
return bs;
}
public static byte[] ToByteArray(string s)
{
byte[] bs = new byte[s.Length];
for (int i = 0; i < bs.Length; ++i)
{
bs[i] = Convert.ToByte(s[i]);
}
return bs;
}
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
public static byte[] ToByteArray(ReadOnlySpan<char> cs)
{
byte[] bs = new byte[cs.Length];
for (int i = 0; i < bs.Length; ++i)
{
bs[i] = Convert.ToByte(cs[i]);
}
return bs;
}
#endif
public static string FromAsciiByteArray(byte[] bytes) => Encoding.ASCII.GetString(bytes);
public static string FromAsciiByteArray(byte[] bytes, int index, int count) =>
Encoding.ASCII.GetString(bytes, index, count);
public static byte[] ToAsciiByteArray(char[] cs) => Encoding.ASCII.GetBytes(cs);
public static byte[] ToAsciiByteArray(char[] chars, int index, int count) =>
Encoding.ASCII.GetBytes(chars, index, count);
public static byte[] ToAsciiByteArray(string s) => Encoding.ASCII.GetBytes(s);
public static string FromUtf8ByteArray(byte[] bytes) => StrictUtf8.GetString(bytes);
public static string FromUtf8ByteArray(byte[] bytes, int index, int count) =>
StrictUtf8.GetString(bytes, index, count);
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
public static string FromUtf8ByteArray(ReadOnlySpan<byte> bytes) => StrictUtf8.GetString(bytes);
#endif
public static byte[] ToUtf8ByteArray(char[] cs) => StrictUtf8.GetBytes(cs);
public static byte[] ToUtf8ByteArray(char[] chars, int index, int count) =>
StrictUtf8.GetBytes(chars, index, count);
public static byte[] ToUtf8ByteArray(string s) => StrictUtf8.GetBytes(s);
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
public static byte[] ToUtf8ByteArray(ReadOnlySpan<char> cs)
{
int count = StrictUtf8.GetByteCount(cs);
byte[] bytes = new byte[count];
StrictUtf8.GetBytes(cs, bytes);
return bytes;
}
#endif
public static byte[] ToUtf8ByteArray(string s, int preAlloc, int postAlloc)
{
int byteCount = StrictUtf8.GetByteCount(s);
byte[] array = new byte[preAlloc + byteCount + postAlloc];
int bytes = StrictUtf8.GetBytes(s, 0, s.Length, array, preAlloc);
Debug.Assert(bytes == byteCount);
return array;
}
public static string[] Split(string input, char delimiter) => input.Split(delimiter);
}
}