本文整理汇总了C#中Span类的典型用法代码示例。如果您正苦于以下问题:C# Span类的具体用法?C# Span怎么用?C# Span使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Span类属于命名空间,在下文中一共展示了Span类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BufferReaderRead
public void BufferReaderRead()
{
Assert.True(BitConverter.IsLittleEndian);
ulong value = 0x8877665544332211; // [11 22 33 44 55 66 77 88]
Span<byte> span;
unsafe {
span = new Span<byte>(&value, 8);
}
Assert.Equal<byte>(0x11, span.ReadBigEndian<byte>());
Assert.Equal<byte>(0x11, span.ReadLittleEndian<byte>());
Assert.Equal<sbyte>(0x11, span.ReadBigEndian<sbyte>());
Assert.Equal<sbyte>(0x11, span.ReadLittleEndian<sbyte>());
Assert.Equal<ushort>(0x1122, span.ReadBigEndian<ushort>());
Assert.Equal<ushort>(0x2211, span.ReadLittleEndian<ushort>());
Assert.Equal<short>(0x1122, span.ReadBigEndian<short>());
Assert.Equal<short>(0x2211, span.ReadLittleEndian<short>());
Assert.Equal<uint>(0x11223344, span.ReadBigEndian<uint>());
Assert.Equal<uint>(0x44332211, span.ReadLittleEndian<uint>());
Assert.Equal<int>(0x11223344, span.ReadBigEndian<int>());
Assert.Equal<int>(0x44332211, span.ReadLittleEndian<int>());
Assert.Equal<ulong>(0x1122334455667788, span.ReadBigEndian<ulong>());
Assert.Equal<ulong>(0x8877665544332211, span.ReadLittleEndian<ulong>());
Assert.Equal<long>(0x1122334455667788, span.ReadBigEndian<long>());
Assert.Equal<long>(unchecked((long)0x8877665544332211), span.ReadLittleEndian<long>());
}
示例2: FindTabSpan
private static Span FindTabSpan(Span zenSpan, bool isReverse, string text, Regex regex)
{
MatchCollection matches = regex.Matches(text);
if (!isReverse)
{
foreach (Match match in matches)
{
Group group = match.Groups[2];
if (group.Index >= zenSpan.Start)
{
return new Span(group.Index, group.Length);
}
}
}
else
{
for (int i = matches.Count - 1; i >= 0; i--)
{
Group group = matches[i].Groups[2];
if (group.Index < zenSpan.End)
{
return new Span(group.Index, group.Length);
}
}
}
return new Span();
}
示例3: EditingTests
public EditingTests()
{
m_promptSpan = new Span( m_prompt );
m_promptWrapSpan = new Span( m_promptWrap );
m_promptOutputSpan = new Span( m_promptOutput );
m_promptOutputWrapSpan = new Span( m_promptOutputWrap );
}
示例4: TryFormat
public static bool TryFormat(this DateTime value, Span<byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
{
if (format.IsDefault)
{
format.Symbol = 'G';
}
Precondition.Require(format.Symbol == 'R' || format.Symbol == 'O' || format.Symbol == 'G');
switch (format.Symbol)
{
case 'R':
var utc = value.ToUniversalTime();
if (formattingData.IsUtf16)
{
return TryFormatDateTimeRfc1123(utc, buffer, FormattingData.InvariantUtf16, out bytesWritten);
}
else
{
return TryFormatDateTimeRfc1123(utc, buffer, FormattingData.InvariantUtf8, out bytesWritten);
}
case 'O':
if (formattingData.IsUtf16)
{
return TryFormatDateTimeFormatO(value, true, buffer, FormattingData.InvariantUtf16, out bytesWritten);
}
else
{
return TryFormatDateTimeFormatO(value, true, buffer, FormattingData.InvariantUtf8, out bytesWritten);
}
case 'G':
return TryFormatDateTimeFormagG(value, buffer, formattingData, out bytesWritten);
default:
throw new NotImplementedException();
}
}
示例5: 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;
}
示例6: ParseSpanAnnotation
public static BratAnnotation ParseSpanAnnotation(Span[] tokens, string line) {
if (tokens.Length > 4) {
string type = tokens[TYPE_OFFSET].GetCoveredText(line);
int endOffset = -1;
int firstTextTokenIndex = -1;
for (int i = END_OFFSET; i < tokens.Length; i++) {
if (!tokens[i].GetCoveredText(line).Contains(";")) {
endOffset = ParseInt(tokens[i].GetCoveredText(line));
firstTextTokenIndex = i + 1;
break;
}
}
var id = tokens[ID_OFFSET].GetCoveredText(line);
var coveredText = line.Substring(tokens[firstTextTokenIndex].Start, tokens[endOffset].End);
return new SpanAnnotation(
id,
type,
new Span(ParseInt(tokens[BEGIN_OFFSET].GetCoveredText(line)), endOffset, type),
coveredText);
}
throw new InvalidFormatException("Line must have at least 5 fields.");
}
示例7: WriteOutputTests
public WriteOutputTests()
{
m_promptSpan = new Span( m_prompt );
m_promptWrapSpan = new Span( m_promptWrap );
m_promptOutputSpan = new Span( m_promptOutput );
m_promptOutputWrapSpan = new Span( m_promptOutputWrap );
}
示例8: Intersection
/// <summary>
/// Provides the intersection of two spans
/// </summary>
/// <param name="span1"></param>
/// <param name="span2"></param>
/// <returns></returns>
public static Span Intersection(Span span1, Span span2)
{
Span retVal;
if (span1.LowBound > span2.LowBound)
{
Span tmp = span1;
span1 = span2;
span2 = tmp;
}
// span1.LowBound <= span2.LowBound
if (span1.HighBound < span2.LowBound)
{
retVal = null;
}
else
{
// span1.LowBound < span2.LowBound and span1.HighBound >= span2.LowBound
int lowBound = span2.LowBound;
int highBound;
if (span1.HighBound > span2.HighBound)
{
highBound = span2.HighBound;
}
else
{
highBound = span1.HighBound;
}
retVal = new Span(lowBound, highBound);
}
return retVal;
}
示例9: DisplayRecognitionError
public override void DisplayRecognitionError(string[] tokenNames, RecognitionException e)
{
var outputWindow = OutputWindowService.TryGetPane(PredefinedOutputWindowPanes.TvlIntellisense);
if (outputWindow != null)
{
string header = GetErrorHeader(e);
string message = GetErrorMessage(e, tokenNames);
Span span = new Span();
if (e.Token != null)
span = Span.FromBounds(e.Token.StartIndex, e.Token.StopIndex + 1);
if (message.Length > 100)
message = message.Substring(0, 100) + " ...";
ITextDocument document;
if (TextBuffer.Properties.TryGetProperty(typeof(ITextDocument), out document) && document != null)
{
string fileName = document.FilePath;
var line = Snapshot.GetLineFromPosition(span.Start);
message = string.Format("{0}({1},{2}): {3}: {4}", fileName, line.LineNumber + 1, span.Start - line.Start.Position + 1, GetType().Name, message);
}
outputWindow.WriteLine(message);
}
base.DisplayRecognitionError(tokenNames, e);
}
示例10: SparkTagParameter
public SparkTagParameter(string documentation, Span span, string name, ISignature signature)
{
Documentation = documentation;
Locus = span;
Name = name;
Signature = signature;
}
示例11: Classify
private static List<Tuple<string, string>> Classify(string markdown, Span? subSpan = null)
{
if (subSpan == null)
{
var spanStart = markdown.IndexOf("(<");
if (spanStart >= 0)
{
markdown = markdown.Remove(spanStart, 2);
var spanEnd = markdown.IndexOf(">)", spanStart);
if (spanEnd < 0)
throw new ArgumentException("Markdown (<...>) span indicator must be well-formed", "markdown");
markdown = markdown.Remove(spanEnd, 2);
subSpan = Span.FromBounds(spanStart, spanEnd);
}
}
var crlf = newline.Replace(markdown, "\r\n");
var lf = newline.Replace(markdown, "\n");
var cr = newline.Replace(markdown, "\r");
// Test newline equivalence on the full source
// string. We cannot pass the subspan because
// newline replacements will move its indices.
// Also, this part is only to test the parser,
// which doesn't get subspans anyway.
var fullResult = RunParseCase(crlf);
RunParseCase(lf).Should().Equal(fullResult, "LF should be the same as CRLF");
RunParseCase(cr).Should().Equal(fullResult, "CR should be the same as CRLF");
return RunParseCase(markdown, subSpan);
}
示例12: InvokeZenCoding
private bool InvokeZenCoding()
{
Span zenSpan = GetText();
if (zenSpan.Length == 0 || TextView.Selection.SelectedSpans[0].Length > 0 || !IsValidTextBuffer())
return false;
string zenSyntax = TextView.TextBuffer.CurrentSnapshot.GetText(zenSpan);
Parser parser = new Parser();
string result = parser.Parse(zenSyntax, ZenType.HTML);
if (!string.IsNullOrEmpty(result))
{
Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
{
using (EditorExtensionsPackage.UndoContext("ZenCoding"))
{
ITextSelection selection = UpdateTextBuffer(zenSpan, result);
EditorExtensionsPackage.ExecuteCommand("Edit.FormatSelection");
Span newSpan = new Span(zenSpan.Start, selection.SelectedSpans[0].Length);
selection.Clear();
SetCaret(newSpan, false);
}
}), DispatcherPriority.ApplicationIdle, null);
return true;
}
return false;
}
示例13: StartServerTrace
public void StartServerTrace()
{
if (isTraceOn)
{
serverSpan = StartTrace(spanTracer.ReceiveServerSpan);
}
}
示例14: StartClientTrace
public void StartClientTrace()
{
if (isTraceOn)
{
clientSpan = StartTrace(spanTracer.SendClientSpan);
}
}
示例15: ByteSpanEqualsTestsTwoDifferentInstancesOfBuffersWithOneValueDifferent
public unsafe void ByteSpanEqualsTestsTwoDifferentInstancesOfBuffersWithOneValueDifferent()
{
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)
{
Span<byte> b1 = new Span<byte>(buffer1pinned, bufferLength);
Span<byte> b2 = new Span<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)));
}
}
}
}