FileSearchServiceBase.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using LAPS_XMLQC_Service.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5. using System.Threading.Tasks;
  6. using MyMatch = LAPS_XMLQC_Service.Models.Matches;
  7. using SystemRegexMatch = System.Text.RegularExpressions.Match;
  8. namespace LAPS_XMLQC_Service.Services
  9. {
  10. public class FileSearchServiceBase
  11. {
  12. public Task<List<PatternResult>> SearchContent(string content, string? searchTerm, bool caseInsensitive, bool singleLine, bool multiLine, bool ignoreWhitespace, bool explicitCapture)
  13. {
  14. if (string.IsNullOrEmpty(content))
  15. {
  16. return Task.FromResult(new List<PatternResult>()); // Return empty if content or search term is null/empty
  17. }
  18. //if (string.IsNullOrEmpty(content) || string.IsNullOrEmpty(searchTerm))
  19. //{
  20. // return Task.FromResult(new List<PatternResult>()); // Return empty if content or search term is null/empty
  21. //}
  22. var results = new List<PatternResult>();
  23. Regex? regex = null;
  24. if (!string.IsNullOrEmpty(searchTerm))
  25. {
  26. var regexOptions = RegexOptions.None;
  27. if (caseInsensitive)
  28. regexOptions |= RegexOptions.IgnoreCase;
  29. if (singleLine)
  30. regexOptions |= RegexOptions.Singleline;
  31. if (multiLine)
  32. regexOptions |= RegexOptions.Multiline;
  33. if (ignoreWhitespace)
  34. regexOptions |= RegexOptions.IgnorePatternWhitespace;
  35. if (explicitCapture)
  36. regexOptions |= RegexOptions.ExplicitCapture;
  37. regex = new Regex(searchTerm, regexOptions);
  38. }
  39. var matches = new List<MyMatch>();
  40. if (regex != null)
  41. {
  42. foreach (SystemRegexMatch match in regex!.Matches(content))
  43. {
  44. matches.Add(new MyMatch
  45. {
  46. Index = match.Index,
  47. Content = match.Value,
  48. LineNumber = CalculateLineNumber(content, match.Index)
  49. });
  50. }
  51. }
  52. if (matches != null && matches.Count > 0)
  53. {
  54. var patternResult = new PatternResult { Pattern = searchTerm, Matches = matches };
  55. results.Add(patternResult);
  56. }
  57. return Task.FromResult(results);
  58. }
  59. public int CalculateLineNumber(string content, int index)
  60. {
  61. if (content == null) throw new ArgumentNullException(nameof(content));
  62. if (index < 0) throw new ArgumentOutOfRangeException(nameof(index), "Index must be non-negative.");
  63. if (index > content.Length) throw new ArgumentOutOfRangeException(nameof(index), "Index must not exceed the length of the content.");
  64. // Count newlines up to the specified index
  65. int lineCount = 0;
  66. for (int i = 0; i < index; i++)
  67. {
  68. if (content[i] == '\n') lineCount++;
  69. }
  70. return lineCount + 1; // Return the 1-based line number
  71. }
  72. }
  73. }