Ejemplo n.º 1
0
        private void TestFile(IFindInFilesItem item, string relativeRoot)
        {
            string displayPath = Path.Combine(relativeRoot, item.GetFileName());
            const string Prefix = @".\";
            if (displayPath.StartsWith(Prefix))
            {
                displayPath = displayPath.Substring(Prefix.Length);
            }

            try
            {
                using (Stream stream = item.Open())
                {
                    using (TextReader reader = new StreamReader(stream, true/*detectEncoding*/))
                    {
                        int lineNumber = 0;
                        string line;
                        while ((line = reader.ReadLine()) != null)
                        {
                            lineNumber++;
                            int i = -1;
                            do
                            {
                                i = line.IndexOf(pattern, i + 1, caseSensitive ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase);
                                bool skip = false;
                                if (matchWholeWords && (i >= 0))
                                {
                                    if (Char.IsLetterOrDigit(pattern[0]))
                                    {
                                        if ((i - 1 >= 0) && Char.IsLetterOrDigit(line[i - 1]))
                                        {
                                            skip = true;
                                        }
                                    }
                                    if (Char.IsLetterOrDigit(pattern[pattern.Length - 1]))
                                    {
                                        if ((i + pattern.Length < line.Length)
                                            && Char.IsLetterOrDigit(line[i + pattern.Length]))
                                        {
                                            skip = true;
                                        }
                                    }
                                }
                                if ((i >= 0) && !skip)
                                {
                                    SendResult(new FindInFilesEntry(item, displayPath, line, lineNumber, i, i + pattern.Length));
                                }
                            } while (i >= 0);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                SendResult(new FindInFilesEntry(item, displayPath, String.Format("Unable to open: {0}", exception.Message), 0, 0, 0));
            }

            if (lastFlush.AddMilliseconds(250) < DateTime.UtcNow)
            {
                Flush();
            }
        }