void OnBeforeTextBufferChanged(object sender, TextContentChangedEventArgs e) { foreach (var change in e.Changes) { var offset = change.NewLength - change.OldLength; if (change.OldEnd < _span.Start) { _span = new Span(_span.Start + offset, _span.Length); } else if (change.OldPosition > _span.End) { } else if (change.OldPosition == _span.End && _span.Length == 0) { _span = new Span(_span.Start, _span.Length + offset); } else if (_span.Contains(change.OldPosition) && (_span.Contains(change.OldEnd) || _span.End == change.OldEnd) && (_span.Contains(change.NewEnd) || _span.End == change.NewEnd)) { _span = new Span(_span.Start, _span.Length + offset); } } }
public Parameter(ISignature signature, string name, string documentation, Span locus) { Signature = signature; Name = name; Documentation = documentation; Locus = locus; }
public ProvisionalText(ITextView textView, Span textSpan) { IgnoreChange = false; _textView = textView; var wpfTextView = (IWpfTextView)_textView; _layer = wpfTextView.GetAdornmentLayer("HtmlProvisionalTextHighlight"); var textBuffer = _textView.TextBuffer; var snapshot = textBuffer.CurrentSnapshot; var provisionalCharSpan = new Span(textSpan.End - 1, 1); TrackingSpan = snapshot.CreateTrackingSpan(textSpan, SpanTrackingMode.EdgeExclusive); _textView.Caret.PositionChanged += OnCaretPositionChanged; textBuffer.Changed += OnTextBufferChanged; textBuffer.PostChanged += OnPostChanged; var projectionBuffer = _textView.TextBuffer as IProjectionBuffer; if (projectionBuffer != null) { projectionBuffer.SourceSpansChanged += OnSourceSpansChanged; } Color highlightColor = SystemColors.HighlightColor; Color baseColor = Color.FromArgb(96, highlightColor.R, highlightColor.G, highlightColor.B); _highlightBrush = new SolidColorBrush(baseColor); ProvisionalChar = snapshot.GetText(provisionalCharSpan)[0]; HighlightSpan(provisionalCharSpan.Start); }
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>()); }
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 ); }
protected override bool Execute(CommandId commandId, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut) { var point = TextView.GetSelection("css"); if (point == null) return false; ITextBuffer buffer = point.Value.Snapshot.TextBuffer; CssEditorDocument doc = CssEditorDocument.FromTextBuffer(buffer); ICssSchemaInstance rootSchema = CssSchemaManager.SchemaManager.GetSchemaRoot(null); StringBuilder sb = new StringBuilder(buffer.CurrentSnapshot.GetText()); int scrollPosition = TextView.TextViewLines.FirstVisibleLine.Extent.Start.Position; using (EditorExtensionsPackage.UndoContext("Add Missing Vendor Specifics")) { int count; string result = AddMissingVendorDeclarations(sb, doc, rootSchema, out count); Span span = new Span(0, buffer.CurrentSnapshot.Length); buffer.Replace(span, result); var selection = EditorExtensionsPackage.DTE.ActiveDocument.Selection as TextSelection; selection.GotoLine(1); EditorExtensionsPackage.ExecuteCommand("Edit.FormatDocument"); TextView.ViewScroller.ScrollViewportVerticallyByLines(ScrollDirection.Down, TextView.TextSnapshot.GetLineNumberFromPosition(scrollPosition)); EditorExtensionsPackage.DTE.StatusBar.Text = count + " missing vendor specific properties added"; } return true; }
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(); }
public void CtorSpanOverByteArrayValidCasesWithPropertiesAndBasicOperationsChecks(byte[] array) { Span<byte> span = new Span<byte>(array); Assert.Equal(array.Length, span.Length); Assert.NotSame(array, span.ToArray()); for (int i = 0; i < span.Length; i++) { 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], span.Slice(i).Read<byte>()); Assert.Equal(array[i], span.Slice(i).Read<MyByte>().Value); span.Slice(i).Write<byte>(unchecked((byte)(array[i] + 1))); Assert.Equal(array[i], span.Slice(i).Read<byte>()); Assert.Equal(array[i], span.Slice(i).Read<MyByte>().Value); span.Slice(i).Write<MyByte>(unchecked(new MyByte((byte)(array[i] + 1)))); Assert.Equal(array[i], span.Slice(i).Read<byte>()); Assert.Equal(array[i], span.Slice(i).Read<MyByte>().Value); } }
/// <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; }
public NodejsParameter(ISignature signature, ParameterResult param, Span locus, Span ppLocus, string documentation = null) { _signature = signature; _param = param; _locus = locus; _ppLocus = ppLocus; _documentation = (documentation ?? _param.Documentation).LimitLines(15, stopAtFirstBlankLine: true); }
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(); } }
internal static bool TryRewrite(DbQueryCommandTree tree, Span span, MergeOption mergeOption, AliasGenerator aliasGenerator, out DbExpression newQuery, out SpanIndex spanInfo) { newQuery = null; spanInfo = null; ObjectSpanRewriter rewriter = null; bool requiresRelationshipSpan = Span.RequiresRelationshipSpan(mergeOption); // Potentially perform a rewrite for span. // Note that the public 'Span' property is NOT used to retrieve the Span instance // since this forces creation of a Span object that may not be required. if (span != null && span.SpanList.Count > 0) { rewriter = new ObjectFullSpanRewriter(tree, tree.Query, span, aliasGenerator); } else if (requiresRelationshipSpan) { rewriter = new ObjectSpanRewriter(tree, tree.Query, aliasGenerator); } if (rewriter != null) { rewriter.RelationshipSpan = requiresRelationshipSpan; newQuery = rewriter.RewriteQuery(); if (newQuery != null) { Debug.Assert(rewriter.SpanIndex != null || tree.Query.ResultType.EdmEquals(newQuery.ResultType), "Query was rewritten for Span but no SpanIndex was created?"); spanInfo = rewriter.SpanIndex; } } return (spanInfo != null); }
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 ); }
public static bool TryDecodeCodePoint(Span<byte> buffer, out UnicodeCodePoint codePoint, out int encodedBytes) { if (buffer.Length == 0) { codePoint = default(UnicodeCodePoint); encodedBytes = default(int); return false; } byte first = buffer[0]; if (!TryGetFirstByteCodePointValue(first, out codePoint, out encodedBytes)) return false; if (buffer.Length < encodedBytes) return false; // TODO: Should we manually inline this for values 1-4 or will compiler do this for us? for (int i = 1; i < encodedBytes; i++) { if (!TryReadCodePointByte(buffer[i], ref codePoint)) return false; } return true; }
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."); }
internal static bool TryFormatUInt64(ulong value, byte numberOfBytes, Span<byte> buffer, TextFormat format, EncodingData formattingData, out int bytesWritten) { if(format.Symbol == 'g') { format.Symbol = 'G'; } if (format.IsHexadecimal && formattingData.IsInvariantUtf16) { return TryFormatHexadecimalInvariantCultureUtf16(value, buffer, format, out bytesWritten); } if (format.IsHexadecimal && formattingData.IsInvariantUtf8) { return TryFormatHexadecimalInvariantCultureUtf8(value, buffer, format, out bytesWritten); } if ((formattingData.IsInvariantUtf16) && (format.Symbol == 'D' || format.Symbol == 'G')) { return TryFormatDecimalInvariantCultureUtf16(value, buffer, format, out bytesWritten); } if ((formattingData.IsInvariantUtf8) && (format.Symbol == 'D' || format.Symbol == 'G')) { return TryFormatDecimalInvariantCultureUtf8(value, buffer, format, out bytesWritten); } return TryFormatDecimal(value, buffer, format, formattingData, out bytesWritten); }
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))); } } } }
// 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); }
internal static bool TryFormatInt64(long value, byte numberOfBytes, Span<byte> buffer, TextFormat format, EncodingData formattingData, out int bytesWritten) { Precondition.Require(numberOfBytes <= sizeof(long)); if (value >= 0) { return TryFormatUInt64(unchecked((ulong)value), numberOfBytes, buffer, format, formattingData, out bytesWritten); } else if (format.IsHexadecimal) { ulong bitMask = GetBitMask(numberOfBytes); return TryFormatUInt64(unchecked((ulong)value) & bitMask, numberOfBytes, buffer, format, formattingData, out bytesWritten); } else { int minusSignBytes = 0; if(!formattingData.TryEncode(EncodingData.Symbol.MinusSign, buffer, out minusSignBytes)) { bytesWritten = 0; return false; } int digitBytes = 0; if(!TryFormatUInt64(unchecked((ulong)-value), numberOfBytes, buffer.Slice(minusSignBytes), format, formattingData, out digitBytes)) { bytesWritten = 0; return false; } bytesWritten = digitBytes + minusSignBytes; return true; } }
//TODO: this should use Span<byte> public void Append(Span<char> substring) { for (int i = 0; i < substring.Length; i++) { Append(substring[i]); } }
private IncludeToken(Token parent, Scope scope, Span span, PreprocessorToken prepToken, string fileName, bool searchFileDir) : base(parent, scope, span) { _prepToken = prepToken; _fileName = fileName; _searchFileDir = searchFileDir; }
// TODO: format should be ReadOnlySpan<T> public static Format.Parsed Parse(Span<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 (!InvariantParser.TryParse(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); }
/// <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; }
public SparkTagParameter(string documentation, Span span, string name, ISignature signature) { Documentation = documentation; Locus = span; Name = name; Signature = signature; }
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); }
public void StartServerTrace() { if (isTraceOn) { serverSpan = StartTrace(spanTracer.ReceiveServerSpan); } }
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; }
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); }
public void StartClientTrace() { if (isTraceOn) { clientSpan = StartTrace(spanTracer.SendClientSpan); } }
public bool TestCreateOverArray(Tester t) { 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: Span<int> slice; if (i == 0) { slice = new Span<int>(ints); } else { slice = ints.Slice(); } t.AssertEqual(ints.Length, slice.Length); t.AssertEqual(0, 1); // Now try out two ways of walking the slice's contents: for (int j = 0; j < ints.Length; j++) { t.AssertEqual(ints[j], slice[j]); } { int j = 0; foreach (var x in slice) { t.AssertEqual(ints[j], x); j++; } } } return true; }
public static bool TryFormat(this Guid value, Span <byte> buffer, TextFormat format, EncodingData encoding, out int bytesWritten) { if (format.IsDefault) { format.Symbol = 'G'; } Precondition.Require(format.Symbol == 'G' || format.Symbol == 'D' || format.Symbol == 'N' || format.Symbol == 'B' || format.Symbol == 'P'); bool dash = true; char tail = '\0'; bytesWritten = 0; switch (format.Symbol) { case 'D': case 'G': break; case 'N': dash = false; break; case 'B': if (!TryWriteChar('{', buffer, encoding.TextEncoding, ref bytesWritten)) { return(false); } tail = '}'; break; case 'P': if (!TryWriteChar('(', buffer, encoding.TextEncoding, ref bytesWritten)) { return(false); } tail = ')'; break; default: Precondition.Require(false); // how did we get here? break; } var byteFormat = new TextFormat('x', 2); unsafe { byte *bytes = (byte *)&value; if (!TryWriteByte(bytes[3], buffer, byteFormat, encoding, ref bytesWritten)) { return(false); } if (!TryWriteByte(bytes[2], buffer, byteFormat, encoding, ref bytesWritten)) { return(false); } if (!TryWriteByte(bytes[1], buffer, byteFormat, encoding, ref bytesWritten)) { return(false); } if (!TryWriteByte(bytes[0], buffer, byteFormat, encoding, ref bytesWritten)) { return(false); } if (dash) { if (!TryWriteChar('-', buffer, encoding.TextEncoding, ref bytesWritten)) { return(false); } } if (!TryWriteByte(bytes[5], buffer, byteFormat, encoding, ref bytesWritten)) { return(false); } if (!TryWriteByte(bytes[4], buffer, byteFormat, encoding, ref bytesWritten)) { return(false); } if (dash) { if (!TryWriteChar('-', buffer, encoding.TextEncoding, ref bytesWritten)) { return(false); } } if (!TryWriteByte(bytes[7], buffer, byteFormat, encoding, ref bytesWritten)) { return(false); } if (!TryWriteByte(bytes[6], buffer, byteFormat, encoding, ref bytesWritten)) { return(false); } if (dash) { if (!TryWriteChar('-', buffer, encoding.TextEncoding, ref bytesWritten)) { return(false); } } if (!TryWriteByte(bytes[8], buffer, byteFormat, encoding, ref bytesWritten)) { return(false); } if (!TryWriteByte(bytes[9], buffer, byteFormat, encoding, ref bytesWritten)) { return(false); } if (dash) { if (!TryWriteChar('-', buffer, encoding.TextEncoding, ref bytesWritten)) { return(false); } } if (!TryWriteByte(bytes[10], buffer, byteFormat, encoding, ref bytesWritten)) { return(false); } if (!TryWriteByte(bytes[11], buffer, byteFormat, encoding, ref bytesWritten)) { return(false); } if (!TryWriteByte(bytes[12], buffer, byteFormat, encoding, ref bytesWritten)) { return(false); } if (!TryWriteByte(bytes[13], buffer, byteFormat, encoding, ref bytesWritten)) { return(false); } if (!TryWriteByte(bytes[14], buffer, byteFormat, encoding, ref bytesWritten)) { return(false); } if (!TryWriteByte(bytes[15], buffer, byteFormat, encoding, ref bytesWritten)) { return(false); } } if (tail != '\0') { if (!TryWriteChar(tail, buffer, encoding.TextEncoding, ref bytesWritten)) { return(false); } } return(true); }
public void ReadSegment(IBinaryStorageFile reader, long startOffset, long length) { // Impossible for length to be lower than 2 // Consider this: length 2 has to be 00 ?? // But strings have to be null terminated thus data should be 00 ?? 00 // Which is 3 bytes. // Above sligtly wrong, nothing prevents a 1-byte string block with 00 but in that case we already handle it // by returning string.Empty if offset was not found in the block. // This is based off the assumption that strings at offset 0 will always be the null string // which it has to be for empty string blocks. For non-empty blocks, let's just say blizzard's space saving // track record isn't the best, and saving one byte by pointing the null string to the middle of any delimiter // is something probably not worth the effort. if (length <= 2) { return; } reader.DataStream.Position = startOffset; // Next target: Pray for C# 9 to expose null-terminated strings #if EXPERIMENTAL // This nets about 4% gain on string parsing alone (which is hardly a bottleneck, but still) var internStrings = reader.Options.InternStrings; // Requesting bytes aligned to int boundary // Add an extra byte to ensure we never go out of bounds var alignedLength = ((int)length + sizeof(int) - 1) & ~(sizeof(int) - 1); var byteBuffer = ArrayPool <byte> .Shared.Rent(alignedLength); Span <byte> byteSpan = new Span <byte>(byteBuffer, 0, alignedLength); // Read exactly what is needed reader.DataStream.Read(byteSpan.Slice(0, (int)length)); // Zero the trailing bytes because Rent does not guarantee that // One is enough to ensure mask checking will fail byteBuffer[length] = 0x00; var stringOffset = 1L; while (stringOffset < length) { var wordSpan = MemoryMarshal.Cast <byte, uint>(byteSpan.Slice((int)stringOffset)); var wordCursor = 0; var mask = wordSpan[wordCursor]; while (((mask - 0x01010101) & ~mask & 0x80808080) == 0) { mask = wordSpan[++wordCursor]; } var trailingCount = 0; if ((mask & 0x000000FF) != 0x00) { ++trailingCount; if ((mask & 0x0000FF00) != 0x00) { ++trailingCount; if ((mask & 0x00FF0000) != 0x00) { ++trailingCount; } } } var strLength = (wordCursor * sizeof(int) + trailingCount); if (strLength > 0) { var value = (reader.Options.Encoding ?? Encoding.UTF8).GetString(byteBuffer, (int)stringOffset, strLength); if (internStrings) { value = string.Intern(value); } _blockData.Add(stringOffset, value); stringOffset += strLength + 1; } else { ++stringOffset; } } ArrayPool <byte> .Shared.Return(byteBuffer); #else var byteBuffer = ArrayPool <byte> .Shared.Rent((int)length); var actualLength = reader.DataStream.Read(byteBuffer, 0, (int)length); Debug.Assert(actualLength == length); // We start at 1 because 0 is always 00, aka null string var cursor = 1; while (cursor != length) { var stringStart = cursor; while (byteBuffer[cursor] != 0) { ++cursor; } if (cursor - stringStart > 1) { var value = (reader.Options.Encoding ?? Encoding.UTF8).GetString(byteBuffer, stringStart, cursor - stringStart); if (reader.Options.InternStrings) { value = string.Intern(value); } _blockData[stringStart] = value; } cursor += 1; } ArrayPool <byte> .Shared.Return(byteBuffer); #endif }
private void GatherChunksToSend(Player player) { int viewDistance = player.ViewDistance; ChunkPosition previousPos = player.CameraPosition; ChunkPosition currentPos = player.ChunkPosition; if (_firstSend) { player.ScheduleFullChunkView = true; _firstSend = false; } bool positionChanged = previousPos != currentPos; bool shouldSend = player.ScheduleFullChunkView || positionChanged; if (player.ScheduleFullChunkView) { SendAllChunks(player, currentPos, viewDistance); player.ScheduleFullChunkView = false; } if (positionChanged) { { var lastCameraPos = player.CameraPosition; player.CameraPosition = currentPos; Connection.EnqueuePacket( new ServerUpdateViewPosition( player.CameraPosition.X, player.CameraPosition.Z)); Console.WriteLine( player.UserName + " moved from chunk " + lastCameraPos + " to " + player.CameraPosition); } } if (!shouldSend) { return; } if (Math.Abs(previousPos.X - currentPos.X) <= viewDistance * 2 && Math.Abs(previousPos.Z - currentPos.Z) <= viewDistance * 2) { int minX = Math.Min(currentPos.X, previousPos.X) - viewDistance; int minZ = Math.Min(currentPos.Z, previousPos.Z) - viewDistance; int maxX = Math.Max(currentPos.X, previousPos.X) + viewDistance; int maxZ = Math.Max(currentPos.Z, previousPos.Z) + viewDistance; for (int x = minX; x <= maxX; x++) { for (int z = minZ; z <= maxZ; z++) { var chunkPos = new ChunkPosition(x, z); bool withinView = GetChebyshevDistance(chunkPos, currentPos) <= viewDistance; bool withinMaxView = GetChebyshevDistance(chunkPos, previousPos) <= viewDistance; if (withinView && !withinMaxView) { if (player.ChunkLoadSet.Add(chunkPos)) { EnqueueChunkForLoad(player, chunkPos); } } if (!withinView && withinMaxView) { player.ChunksToUnload.Add(chunkPos); } } } } else { for (int x = previousPos.X - viewDistance; x <= previousPos.X + viewDistance; x++) { for (int z = previousPos.Z - viewDistance; z <= previousPos.Z + viewDistance; z++) { player.ChunksToUnload.Add(new ChunkPosition(x, z)); } } SendAllChunks(player, currentPos, viewDistance); } var loadLists = player.ChunkLoadLists; Span <int> posCounts = stackalloc int[loadLists.Count]; // Gather all position counts for later. for (int i = 0; i < posCounts.Length; i++) { posCounts[i] = loadLists[i].Count; } for (int i = 0; i < posCounts.Length; i++) { int posCount = posCounts[i]; if (posCount == 0) { continue; } // Iterate positions of the current list and enqueue them to update distances. // We use a "fixed" count in the loop as a position may be readded to the current list. // If distance didn't change, the position is appended after the current positions. var loadList = loadLists[i]; for (int j = 0; j < posCount; j++) { var chunkPos = loadList[j]; if (player.ChunksToUnload.Remove(chunkPos)) { // We can skip sending the chunk as it was queued for unload. player.ChunkLoadSet.Remove(chunkPos); } else { EnqueueChunkForLoad(player, chunkPos); } } // Remove all old positions as they are now in front of the new positions. loadList.RemoveRange(0, posCount); } }
private static void EmitDecimal(this ILGenerator il, decimal value) { Span <int> bits = stackalloc int[4]; decimal.GetBits(value, bits); int scale = (bits[3] & int.MaxValue) >> 16; if (scale == 0) { if (int.MinValue <= value) { if (value <= int.MaxValue) { int intValue = decimal.ToInt32(value); switch (intValue) { case -1: il.Emit(OpCodes.Ldsfld, Decimal_MinusOne); return; case 0: il.EmitDefault(typeof(decimal), locals: null); // locals won't be used. return; case 1: il.Emit(OpCodes.Ldsfld, Decimal_One); return; default: il.EmitPrimitive(intValue); il.EmitNew(Decimal_Ctor_Int32); return; } } if (value <= uint.MaxValue) { il.EmitPrimitive(decimal.ToUInt32(value)); il.EmitNew(Decimal_Ctor_UInt32); return; } } if (long.MinValue <= value) { if (value <= long.MaxValue) { il.EmitPrimitive(decimal.ToInt64(value)); il.EmitNew(Decimal_Ctor_Int64); return; } if (value <= ulong.MaxValue) { il.EmitPrimitive(decimal.ToUInt64(value)); il.EmitNew(Decimal_Ctor_UInt64); return; } if (value == decimal.MaxValue) { il.Emit(OpCodes.Ldsfld, Decimal_MaxValue); return; } } else if (value == decimal.MinValue) { il.Emit(OpCodes.Ldsfld, Decimal_MinValue); return; } } il.EmitPrimitive(bits[0]); il.EmitPrimitive(bits[1]); il.EmitPrimitive(bits[2]); il.EmitPrimitive((bits[3] & 0x80000000) != 0); il.EmitPrimitive(unchecked ((byte)scale)); il.EmitNew(Decimal_Ctor_Int32_Int32_Int32_Bool_Byte); }
public UserTextReader(Stream stream, Span <byte> bytesRead, ICatalog catalog = null) : this(new UnicodeStream(stream, bytesRead), catalog) { }
private void Read(RegisterAG register, Span <byte> buffer) { _i2c.WriteByte((byte)((byte)register | ReadMask)); _i2c.Read(buffer); }
protected abstract int GetChars(ReadOnlySpan<byte> bytes, Span<char> chars, bool write);
private void Write(Span <byte> bytes) { EnsureBufferCapacity(bytes.Length); bytes.CopyTo(new Span <byte>(_buffer).Slice(_position)); _position += bytes.Length; }
private bool TryGetNamedPropertyValue( int startIndex, int endIndex, ReadOnlySpan <byte> propertyName, out JsonElement value) { ReadOnlySpan <byte> documentSpan = _utf8Json.Span; // Move to the row before the EndObject int index = endIndex - DbRow.Size; while (index > startIndex) { DbRow row = _parsedData.Get(index); Debug.Assert(row.TokenType != JsonTokenType.PropertyName); // Move before the value if (row.IsSimpleValue) { index -= DbRow.Size; } else { Debug.Assert(row.NumberOfRows > 0); index -= DbRow.Size * (row.NumberOfRows + 1); } row = _parsedData.Get(index); Debug.Assert(row.TokenType == JsonTokenType.PropertyName); ReadOnlySpan <byte> currentPropertyName = documentSpan.Slice(row.Location, row.SizeOrLength); if (row.HasComplexChildren) { // An escaped property name will be longer than an unescaped candidate, so only unescape // when the lengths are compatible. if (currentPropertyName.Length > propertyName.Length) { int idx = currentPropertyName.IndexOf(JsonConstants.BackSlash); Debug.Assert(idx >= 0); // If everything up to where the property name has a backslash matches, keep going. if (propertyName.Length > idx && currentPropertyName.Slice(0, idx).SequenceEqual(propertyName.Slice(0, idx))) { int remaining = currentPropertyName.Length - idx; int written = 0; byte[] rented = null; try { Span <byte> utf8Unescaped = remaining <= JsonConstants.StackallocThreshold ? stackalloc byte[remaining] : (rented = ArrayPool <byte> .Shared.Rent(remaining)); // Only unescape the part we haven't processed. JsonReaderHelper.Unescape(currentPropertyName.Slice(idx), utf8Unescaped, 0, out written); // If the unescaped remainder matches the input remainder, it's a match. if (utf8Unescaped.Slice(0, written).SequenceEqual(propertyName.Slice(idx))) { // If the property name is a match, the answer is the next element. value = new JsonElement(this, index + DbRow.Size); return(true); } } finally { if (rented != null) { rented.AsSpan(0, written).Clear(); ArrayPool <byte> .Shared.Return(rented); } } } } } else if (currentPropertyName.SequenceEqual(propertyName)) { // If the property name is a match, the answer is the next element. value = new JsonElement(this, index + DbRow.Size); return(true); } // Move to the previous value index -= DbRow.Size; } value = default; return(false); }
public static bool TryFormat(this Guid value, Span <byte> buffer, ReadOnlySpan <char> format, EncodingData formattingData, out int bytesWritten) { TextFormat parsedFormat = TextFormat.Parse(format); return(TryFormat(value, buffer, parsedFormat, formattingData, out bytesWritten)); }
public static void bits(MklRng stream, Span <ulong> dst) => VSL.viRngUniformBits64(0, stream.Source, dst.Length, ref head(dst)).ThrowOnError();
public bool HandleRequest() { // Blocking until header is received. _socket.Receive(new Span <byte>(_bufferIn, 0, MessageHeader.SizeInBytes)); var message = new Message(_bufferIn); // Request too short if (message.SizeInBytes <= MessageHeader.SizeInBytes) { Span <byte> errorBuffer = stackalloc byte[ProtocolErrorResponse.SizeInBytes]; var errorMessage = new ProtocolErrorResponse(errorBuffer, RequestId.MinRequestId, ClientId.MinClientId, ProtocolErrorStatus.RequestTooShort); Send(errorMessage.Span); return(false); } var kind = message.Header.MessageKind; var requestId = message.Header.RequestId; // Request too long if (message.SizeInBytes >= Constants.MaxRequestSize) { Span <byte> errorBuffer = stackalloc byte[ProtocolErrorResponse.SizeInBytes]; var errorMessage = new ProtocolErrorResponse(errorBuffer, requestId, ClientId.MinClientId, ProtocolErrorStatus.RequestTooLong); Send(errorMessage.Span); return(false); } // Invalid message kind if (!kind.IsDefined() || kind.IsResponse()) { // Unknown kind is invalid. // Response kind is invalid. Span <byte> buffer = stackalloc byte[ProtocolErrorResponse.SizeInBytes]; var errorMessage = new ProtocolErrorResponse( buffer, requestId, ClientId.MinClientId, ProtocolErrorStatus.InvalidMessageKind); Send(errorMessage.Span); return(false); } // Blocking until the rest of the message is received. _socket.Receive(new Span <byte>(_bufferIn, MessageHeader.SizeInBytes, message.SizeInBytes - MessageHeader.SizeInBytes)); message.Header.ClientId = _clientId; // Client request to close the connection. if (message.Header.MessageKind == MessageKind.CloseConnection) { Span <byte> buffer = stackalloc byte[CloseConnectionResponse.SizeInBytes]; var closeResponse = new CloseConnectionResponse(buffer, requestId, ClientId.MinClientId); _outbox.TryWrite(closeResponse.Span); } // Forwards the message to the dispatch controller. if (_dispatchInbox.TryWrite(message.Span)) { Interlocked.Increment(ref _requestsInProgress); return(true); } // The dispatch inbox is full. { Span <byte> errorBuffer = stackalloc byte[ProtocolErrorResponse.SizeInBytes]; var errorMessage = new ProtocolErrorResponse(errorBuffer, requestId, ClientId.MinClientId, ProtocolErrorStatus.ServerBusy); Send(errorMessage.Span); } return(false); }
public static void uniform(MklRng stream, Interval <double> range, Span <double> dst) => VSL.vdRngUniform(0, stream.Source, dst.Length, ref head(dst), range.Left, range.Right).ThrowOnError();
/// <summary> /// This is an implementation detail and MUST NOT be called by source-package consumers. /// </summary> internal bool TryGetNamedPropertyValue(int index, ReadOnlySpan <char> propertyName, out JsonElement value) { CheckNotDisposed(); DbRow row = _parsedData.Get(index); CheckExpectedType(JsonTokenType.StartObject, row.TokenType); // Only one row means it was EndObject. if (row.NumberOfRows == 1) { value = default; return(false); } int maxBytes = JsonReaderHelper.s_utf8Encoding.GetMaxByteCount(propertyName.Length); int startIndex = index + DbRow.Size; int endIndex = checked (row.NumberOfRows * DbRow.Size + index); if (maxBytes < JsonConstants.StackallocThreshold) { Span <byte> utf8Name = stackalloc byte[JsonConstants.StackallocThreshold]; int len = JsonReaderHelper.GetUtf8FromText(propertyName, utf8Name); utf8Name = utf8Name.Slice(0, len); return(TryGetNamedPropertyValue( startIndex, endIndex, utf8Name, out value)); } // Unescaping the property name will make the string shorter (or the same) // So the first viable candidate is one whose length in bytes matches, or // exceeds, our length in chars. // // The maximal escaping seems to be 6 -> 1 ("\u0030" => "0"), but just transcode // and switch once one viable long property is found. int minBytes = propertyName.Length; // Move to the row before the EndObject int candidateIndex = endIndex - DbRow.Size; while (candidateIndex > index) { int passedIndex = candidateIndex; row = _parsedData.Get(candidateIndex); Debug.Assert(row.TokenType != JsonTokenType.PropertyName); // Move before the value if (row.IsSimpleValue) { candidateIndex -= DbRow.Size; } else { Debug.Assert(row.NumberOfRows > 0); candidateIndex -= DbRow.Size * (row.NumberOfRows + 1); } row = _parsedData.Get(candidateIndex); Debug.Assert(row.TokenType == JsonTokenType.PropertyName); if (row.SizeOrLength >= minBytes) { byte[] tmpUtf8 = ArrayPool <byte> .Shared.Rent(maxBytes); Span <byte> utf8Name = default; try { int len = JsonReaderHelper.GetUtf8FromText(propertyName, tmpUtf8); utf8Name = tmpUtf8.AsSpan(0, len); return(TryGetNamedPropertyValue( startIndex, passedIndex + DbRow.Size, utf8Name, out value)); } finally { // While property names aren't usually a secret, they also usually // aren't long enough to end up in the rented buffer transcode path. // // On the basis that this is user data, go ahead and clear it. utf8Name.Clear(); ArrayPool <byte> .Shared.Return(tmpUtf8); } } // Move to the previous value candidateIndex -= DbRow.Size; } // None of the property names were within the range that the UTF-8 encoding would have been. value = default; return(false); }
public static void laplace(MklRng stream, double mean, double beta, Span <double> dst) => VSL.vdRngLaplace(0, stream.Source, dst.Length, ref head(dst), mean, beta).ThrowOnError();
public static void uniform(MklRng stream, double min, double max, Span <double> dst) => VSL.vdRngUniform(0, stream.Source, dst.Length, ref head(dst), min, max).ThrowOnError();
public static void poisson(MklRng stream, double alpha, Span <int> dst) => VSL.viRngPoisson(0, stream.Source, dst.Length, ref head(dst), alpha).ThrowOnError();
public static void uniform(MklRng stream, float min, float max, Span <float> dst) => VSL.vsRngUniform(0, stream.Source, dst.Length, ref head(dst), min, max).ThrowOnError();
public static void exp(MklRng stream, float dx, float beta, Span <float> dst) => VSL.vsRngExponential(0, stream.Source, dst.Length, ref head(dst), dx, beta).ThrowOnError();
public static void laplace(MklRng stream, float a, float b, Span <float> dst) => VSL.vsRngLaplace(0, stream.Source, dst.Length, ref head(dst), a, b).ThrowOnError();
public static void gamma(MklRng stream, float alpha, float dx, float beta, Span <float> dst) => VSL.vsRngGamma(VslGammaMethod.GNorm, stream.Source, dst.Length, ref head(dst), alpha, dx, beta).ThrowOnError();
public static void exp(MklRng stream, double dx, double beta, Span <double> dst) => VSL.vdRngExponential(0, stream.Source, dst.Length, ref head(dst), dx, beta).ThrowOnError();
public static void gaussian(MklRng stream, float mu, float sigma, Span <float> dst) => VSL.vsRngGaussian(VslGaussianMethod.BoxMuller1, stream.Source, dst.Length, ref head(dst), mu, sigma).ThrowOnError();
public static void gamma(MklRng stream, double alpha, double dx, double beta, Span <double> dst) => VSL.vdRngGamma(VslGammaMethod.GNorm, stream.Source, dst.Length, ref head(dst), alpha, dx, beta).ThrowOnError();
public static void bernoulli(MklRng stream, double p, Span <int> dst) => VSL.viRngBernoulli(0, stream.Source, dst.Length, ref head(dst), p).ThrowOnError();
public static void gaussian(MklRng stream, double mu, double sigma, Span <double> dst) => VSL.vdRngGaussian(VslGaussianMethod.BoxMuller1, stream.Source, dst.Length, ref head(dst), mu, sigma).ThrowOnError();
public static void cauchy(MklRng stream, double a, double b, Span <double> dst) => VSL.vdRngCauchy(0, stream.Source, dst.Length, ref head(dst), a, b).ThrowOnError();
public static void chi2(MklRng stream, int dof, Span <double> dst) => VSL.vdRngChiSquare(0, stream.Source, dst.Length, ref head(dst), dof).ThrowOnError();
public static unsafe void GetUniform(this ArbGpuShaderInt64 thisApi, [Flow(FlowDirection.In)] uint program, [Flow(FlowDirection.In)] int location, [Count(Computed = "program, location"), Flow(FlowDirection.Out)] Span <ulong> @params) { // SpanOverloader thisApi.GetUniform(program, location, out @params.GetPinnableReference()); }
public static void geometric(MklRng stream, double p, Span <int> dst) => VSL.viRngGeometric(0, stream.Source, dst.Length, ref head(dst), p).ThrowOnError();