コード例 #1
0
        private Encoding DetectEncoding(byte[] sampleBytes)
        {
            if (AlwaysUseEncoding != null)
            {
                return(AlwaysUseEncoding);
            }

            return(EncodingDetector.Detect(sampleBytes, defaultEncoding: DefaultEncodingIfNotDetected));
        }
コード例 #2
0
        private FindResultItem FindInFile(string filePath)
        {
            var resultItem = new FindResultItem();

            resultItem.IsSuccess = true;
            resultItem.IncludeFilesWithoutMatches = IncludeFilesWithoutMatches;

            resultItem.FileName         = Path.GetFileName(filePath);
            resultItem.FilePath         = filePath;
            resultItem.FileRelativePath = "." + filePath.Substring(Dir.Length);

            byte[] sampleBytes;

            StopWatch.Start("ReadSampleFileContent");

            //Check if can read first
            try
            {
                sampleBytes = Utils.ReadFileContentSample(filePath);
            }
            catch (Exception exception)
            {
                StopWatch.Stop("ReadSampleFileContent");

                resultItem.IsSuccess    = false;
                resultItem.FailedToOpen = true;
                resultItem.ErrorMessage = exception.Message;
                return(resultItem);
            }

            StopWatch.Stop("ReadSampleFileContent");


            if (!SkipBinaryFileDetection)
            {
                StopWatch.Start("IsBinaryFile");

                if (resultItem.IsSuccess)
                {
                    // check for /0/0/0/0
                    if (Utils.IsBinaryFile(sampleBytes))
                    {
                        StopWatch.Stop("IsBinaryFile");

                        resultItem.IsSuccess    = false;
                        resultItem.IsBinaryFile = true;
                        return(resultItem);
                    }
                }

                StopWatch.Stop("IsBinaryFile");
            }

            Encoding encoding = EncodingDetector.Detect(sampleBytes, defaultEncoding: Encoding.UTF8);

            resultItem.FileEncoding = encoding;

            StopWatch.Start("ReadFullFileContent");

            string fileContent;

            using (var sr = new StreamReader(filePath, encoding))
            {
                fileContent = sr.ReadToEnd();
            }

            StopWatch.Stop("ReadFullFileContent");

            StopWatch.Start("FindMatches");
            RegexOptions regexOptions = Utils.GetRegExOptions(IsCaseSensitive);

            resultItem.Matches = Utils.FindMatches(fileContent, FindText, FindTextHasRegEx, regexOptions);

            StopWatch.Stop("FindMatches");

            resultItem.NumMatches = resultItem.Matches.Count;
            return(resultItem);
        }