コード例 #1
0
        public void Reset(int position)
        {
            // if position is within already read character range then just use what we have
            int relative = position - _basis;

            if (relative >= 0 && relative <= _characterWindowCount)
            {
                _offset = relative;
            }
            else
            {
                // we need to reread text buffer
                int amountToRead = Math.Min(_text.Length, position + _characterWindow.Length) - position;
                amountToRead = Math.Max(amountToRead, 0);
                if (amountToRead > 0)
                {
                    _text.CopyTo(position, _characterWindow, 0, amountToRead);
                }

                _lexemeStart          = 0;
                _offset               = 0;
                _basis                = position;
                _characterWindowCount = amountToRead;
            }
        }
コード例 #2
0
 public override int Read(char[] buffer, int index, int count)
 {
     count = Math.Min(count, text.Length - position);
     text.CopyTo(position, buffer, index, count);
     position += count;
     return(count);
 }
コード例 #3
0
                public void WriteText(SourceText text, CancellationToken cancellationToken)
                {
                    if (memoryMappedInfo != null)
                    {
                        throw new InvalidOperationException();
                    }

                    using (Logger.LogBlock(FeatureId.TemporaryStorage, FunctionId.Host_TemporaryStorageServiceFactory_WriteText, cancellationToken))
                    {
                        var size = Encoding.Unicode.GetMaxByteCount(text.Length);
                        memoryMappedInfo = service.memoryMappedFileManager.CreateViewInfo(size);

                        var buffer = SharedPools.CharArray.Allocate();
                        using (var stream = memoryMappedInfo.CreateWritableStream())
                        {
                            // PERF: Don't call text.Write(writer) directly since it can cause multiple large string
                            // allocations from String.Substring.  Instead use one of our pooled char[] buffers.
                            using (var writer = new StreamWriter(stream, Encoding.Unicode))
                            {
                                for (int index = 0; index < text.Length; index += buffer.Length)
                                {
                                    cancellationToken.ThrowIfCancellationRequested();
                                    var count = Math.Min(buffer.Length, text.Length - index);
                                    text.CopyTo(index, buffer, 0, count);
                                    writer.Write(buffer, 0, count);
                                }
                            }
                        }

                        SharedPools.CharArray.Free(buffer);
                    }
                }
コード例 #4
0
        public override int Read(char[] buffer, int index, int count)
        {
            var charsToCopy = Math.Min(count, _sourceText.Length - _position);

            _sourceText.CopyTo(_position, buffer, index, charsToCopy);
            _position += charsToCopy;
            return(charsToCopy);
        }
コード例 #5
0
        private string GetString(SourceText sourceText)
        {
            var sourceChars = new char[sourceText.Length];

            sourceText.CopyTo(0, sourceChars, 0, sourceText.Length);
            var sourceString = new string(sourceChars);

            return(sourceString);
        }
コード例 #6
0
        public static RootNode GetSyntaxTree(string path, SourceText text)
        {
            var chars = new char[text.Length];

            text.CopyTo(0, chars, 0, text.Length);
            var      resolvedEncoding = text.Encoding ?? Encoding.UTF8;
            var      inputFile        = resolvedEncoding.GetBytes(chars);
            var      nodes            = new List <Node>();
            var      lines            = text.Lines;
            RootNode ast = null;

            using (var inputSlab = new MemoryPoolSlab(inputFile))
                using (var outputSlab = new MemoryPoolSlab(new byte[1000000]))
                {
                    var parseStatus = generate(inputSlab.NativePointer, inputFile.Length, outputSlab.NativePointer, out var descriptorSize);
                    var byteArray   = new byte[descriptorSize];
                    Marshal.Copy(outputSlab.NativePointer, byteArray, 0, (int)descriptorSize);
                    var file = FileDescriptorProto.ParseFrom(byteArray);

                    foreach (var location in file.SourceCodeInfo.LocationList)
                    {
                        nodes.Add(Node.CreateNode(location, text));
                    }

                    foreach (var node in nodes.OrderByDescending(i => i.Content.Length))
                    {
                        if (ast == null)
                        {
                            ast = node as RootNode;
                        }
                        else
                        {
                            var targetNode = ast.GetNodeAt(node.StartLine, node.StartCol);
                            targetNode.Children.Add(node);
                            node.Parent = targetNode;
                        }
                    }
                }
            return(ast);
        }
コード例 #7
0
 public override void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)
 {
     _sourceText.CopyTo(sourceIndex, destination, destinationIndex, count);
 }