本文整理汇总了C#中ReadOnlySpan类的典型用法代码示例。如果您正苦于以下问题:C# ReadOnlySpan类的具体用法?C# ReadOnlySpan怎么用?C# ReadOnlySpan使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ReadOnlySpan类属于命名空间,在下文中一共展示了ReadOnlySpan类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Append
//TODO: this should use ByteSpan
public void Append(ReadOnlySpan<char> substring)
{
for (int i = 0; i < substring.Length; i++)
{
Append(substring[i]);
}
}
示例2: DangerousGetPinnableReferenceArray
public static void DangerousGetPinnableReferenceArray()
{
int[] a = { 91, 92, 93, 94, 95 };
ReadOnlySpan<int> span = new ReadOnlySpan<int>(a, 1, 3);
ref int pinnableReference = ref span.DangerousGetPinnableReference();
Assert.True(Unsafe.AreSame<int>(ref a[1], ref pinnableReference));
}
示例3: TryFormatInt64
// TODO: format should be ReadOnlySpan<char>
internal static bool TryFormatInt64(long value, byte numberOfBytes, Span<byte> buffer, ReadOnlySpan<char> format, EncodingData formattingData, out int bytesWritten)
{
Precondition.Require(numberOfBytes <= sizeof(long));
TextFormat parsedFormat = TextFormat.Parse(format);
return TryFormatInt64(value, numberOfBytes, buffer, parsedFormat, formattingData, out bytesWritten);
}
示例4: TwoReadOnlySpansCreatedOverSameIntArrayAreEqual
public void TwoReadOnlySpansCreatedOverSameIntArrayAreEqual()
{
for (int i = 0; i < 2; i++)
{
var ints = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// Try out two ways of creating a slice:
ReadOnlySpan<int> slice;
if (i == 0)
{
slice = new ReadOnlySpan<int>(ints);
}
else
{
slice = ints.Slice();
}
Assert.Equal(ints.Length, slice.Length);
// Now try out two ways of walking the slice's contents:
for (int j = 0; j < ints.Length; j++)
{
Assert.Equal(ints[j], slice[j]);
}
{
int j = 0;
foreach (var x in slice)
{
Assert.Equal(ints[j], x);
j++;
}
}
}
}
示例5: IsFalse
private static bool IsFalse(ReadOnlySpan<char> utf16Chars)
{
if (utf16Chars.Length < 5)
return false;
char firstChar = utf16Chars[0];
if (firstChar != 'f' && firstChar != 'F')
return false;
char secondChar = utf16Chars[1];
if (secondChar != 'a' && secondChar != 'A')
return false;
char thirdChar = utf16Chars[2];
if (thirdChar != 'l' && thirdChar != 'L')
return false;
char fourthChar = utf16Chars[3];
if (fourthChar != 's' && fourthChar != 'S')
return false;
char fifthChar = utf16Chars[4];
if (fifthChar != 'e' && fifthChar != 'E')
return false;
return true;
}
示例6: ByteReadOnlySpanEqualsTestsTwoDifferentInstancesOfBuffersWithOneValueDifferent
public unsafe void ByteReadOnlySpanEqualsTestsTwoDifferentInstancesOfBuffersWithOneValueDifferent()
{
const int bufferLength = 128;
byte[] buffer1 = new byte[bufferLength];
byte[] buffer2 = new byte[bufferLength];
for (int i = 0; i < bufferLength; i++)
{
buffer1[i] = (byte)(bufferLength + 1 - i);
buffer2[i] = (byte)(bufferLength + 1 - i);
}
fixed (byte* buffer1pinned = buffer1)
fixed (byte* buffer2pinned = buffer2)
{
ReadOnlySpan<byte> b1 = new ReadOnlySpan<byte>(buffer1pinned, bufferLength);
ReadOnlySpan<byte> b2 = new ReadOnlySpan<byte>(buffer2pinned, bufferLength);
for (int i = 0; i < bufferLength; i++)
{
for (int diffPosition = i; diffPosition < bufferLength; diffPosition++)
{
buffer1[diffPosition] = unchecked((byte)(buffer1[diffPosition] + 1));
Assert.False(b1.Slice(i).SequenceEqual(b2.Slice(i)));
}
}
}
}
示例7: Encode
/// <summary>
///
/// </summary>
/// <param name="source"></param>
/// <param name="destination"></param>
/// <returns>Number of bytes written to the destination.</returns>
public static int Encode(ReadOnlySpan<byte> source, Span<byte> destination)
{
int di = 0;
int si = 0;
byte b0, b1, b2, b3;
for (; si<source.Length - 2;) {
var result = Encode(source.Slice(si));
si += 3;
destination.Slice(di).Write(result);
di += 4;
}
if (si == source.Length - 1) {
Encode(source[si], 0, 0, out b0, out b1, out b2, out b3);
destination[di++] = b0;
destination[di++] = b1;
destination[di++] = s_encodingMap[64];
destination[di++] = s_encodingMap[64];
}
else if(si == source.Length - 2) {
Encode(source[si++], source[si], 0, out b0, out b1, out b2, out b3);
destination[di++] = b0;
destination[di++] = b1;
destination[di++] = b2;
destination[di++] = s_encodingMap[64];
}
return di;
}
示例8: CtorArrayIntStartEqualsLength
public static void CtorArrayIntStartEqualsLength()
{
// Valid for start to equal the array length. This returns an empty span that starts "just past the array."
int[] a = { 91, 92, 93 };
ReadOnlySpan<int> span = new ReadOnlySpan<int>(a, 3);
span.Validate<int>();
}
示例9: JsonObject
internal JsonObject(ReadOnlySpan<byte> values, ReadOnlySpan<byte> db, BufferPool pool = null, OwnedMemory<byte> dbMemory = null)
{
_db = db;
_values = values;
_pool = pool;
_dbMemory = dbMemory;
}
示例10: Parse
// TODO: format should be ReadOnlySpan<T>
public static Format.Parsed Parse(ReadOnlySpan<char> format)
{
if (format.Length == 0)
{
return default(Format.Parsed);
}
uint precision = NoPrecision;
if (format.Length > 1)
{
var span = format.Slice(1, format.Length - 1);
if (!PrimitiveParser.TryParseUInt32(span, out precision))
{
throw new NotImplementedException("UnableToParsePrecision");
}
if (precision > Parsed.MaxPrecision)
{
// TODO: this is a contract violation
throw new Exception("PrecisionValueOutOfRange");
}
}
// TODO: this is duplicated from above. It needs to be refactored
var specifier = format[0];
return new Parsed(specifier, (byte)precision);
}
示例11: SliceIntIntPastEnd
public static void SliceIntIntPastEnd()
{
int[] a = { 90, 91, 92, 93, 94, 95, 96, 97, 98, 99 };
ReadOnlySpan<int> span = new ReadOnlySpan<int>(a).Slice(a.Length, 0);
Assert.Equal(0, span.Length);
Assert.True(Unsafe.AreSame<int>(ref a[a.Length - 1], ref Unsafe.Subtract<int>(ref span.DangerousGetPinnableReference(), 1)));
}
示例12: SliceIntIntUpToEnd
public static void SliceIntIntUpToEnd()
{
int[] a = { 90, 91, 92, 93, 94, 95, 96, 97, 98, 99 };
ReadOnlySpan<int> span = new ReadOnlySpan<int>(a).Slice(4, 6);
Assert.Equal(6, span.Length);
Assert.True(Unsafe.AreSame<int>(ref a[4], ref span.DangerousGetPinnableReference()));
}
示例13: CtorReadOnlySpanOverByteArrayValidCasesWithPropertiesAndBasicOperationsChecks
public void CtorReadOnlySpanOverByteArrayValidCasesWithPropertiesAndBasicOperationsChecks(byte[] array)
{
ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(array);
Assert.Equal(array.Length, span.Length);
Assert.NotSame(array, span.CreateArray());
Assert.False(span.Equals(array));
ReadOnlySpan<byte>.Enumerator it = span.GetEnumerator();
for (int i = 0; i < span.Length; i++)
{
Assert.True(it.MoveNext());
Assert.Equal(array[i], it.Current);
Assert.Equal(array[i], span.Slice(i).Read<byte>());
Assert.Equal(array[i], span.Slice(i).Read<MyByte>().Value);
array[i] = unchecked((byte)(array[i] + 1));
Assert.Equal(array[i], it.Current);
Assert.Equal(array[i], span.Slice(i).Read<byte>());
Assert.Equal(array[i], span.Slice(i).Read<MyByte>().Value);
}
Assert.False(it.MoveNext());
it.Reset();
for (int i = 0; i < span.Length; i++)
{
Assert.True(it.MoveNext());
Assert.Equal(array[i], it.Current);
}
Assert.False(it.MoveNext());
}
示例14: Contains
// Helper methods similar to System.ArrayExtension:
// String helper methods, offering methods like String on Slice<char>:
// TODO(joe): culture-sensitive comparisons.
// TODO: should these move to string/text related assembly
public static bool Contains(this ReadOnlySpan<char> str, ReadOnlySpan<char> value)
{
if (value.Length > str.Length)
{
return false;
}
return str.IndexOf(value) >= 0;
}
示例15: GetByteCount
public static int GetByteCount(ReadOnlySpan buffer)
{
var count = 0;
foreach (var x in buffer)
if ((x & 0b1100_0000) != 0b1000_0000)
count++;
return count;
}