| 1 | 1 | def grep(pattern: str, flags: str, files: list[str]) -> str: |
| 2 | | pass |
| 2 | """ |
| 3 | Search files for lines matching a search string and return all matching lines. |
| 4 | |
| 5 | Args: |
| 6 | pattern: The string to search for |
| 7 | flags: Space-separated string of flags (-n, -l, -i, -v, -x) |
| 8 | files: List of file paths to search in |
| 9 | |
| 10 | Returns: |
| 11 | String containing all matching lines, formatted according to flags |
| 12 | """ |
| 13 | # Parse flags |
| 14 | flag_list = flags.split() if flags else [] |
| 15 | line_numbers = '-n' in flag_list |
| 16 | files_only = '-l' in flag_list |
| 17 | case_insensitive = '-i' in flag_list |
| 18 | invert_match = '-v' in flag_list |
| 19 | exact_match = '-x' in flag_list |
| 20 | |
| 21 | # Edge Case: Empty pattern |
| 22 | if not pattern: |
| 23 | pattern_to_use = '' |
| 24 | else: |
| 25 | pattern_to_use = pattern.lower() if case_insensitive else pattern |
| 26 | |
| 27 | results = [] |
| 28 | matching_files = set() |
| 29 | |
| 30 | # Edge Case: No files provided |
| 31 | if not files: |
| 32 | return "" |
| 33 | |
| 34 | multiple_files = len(files) > 1 |
| 35 | |
| 36 | for file_path in files: |
| 37 | try: |
| 38 | with open(file_path, 'r', encoding='utf-8') as file: |
| 39 | lines = file.readlines() |
| 40 | |
| 41 | # Edge Case: File cannot be read |
| 42 | except IOError: |
| 43 | continue |
| 44 | |
| 45 | file_has_match = False |
| 46 | |
| 47 | for line_num, line in enumerate(lines, 1): |
| 48 | # Prepare line for comparison |
| 49 | line_to_compare = line.lower() if case_insensitive else line |
| 50 | |
| 51 | # Determine if line matches |
| 52 | if exact_match: |
| 53 | matches = pattern_to_use == line_to_compare.rstrip('\n') |
| 54 | else: |
| 55 | matches = pattern_to_use in line_to_compare |
| 56 | |
| 57 | # Apply invert flag |
| 58 | if invert_match: |
| 59 | matches = not matches |
| 60 | |
| 61 | if matches: |
| 62 | file_has_match = True |
| 63 | |
| 64 | # Edge Case: Only file names requested |
| 65 | if files_only: |
| 66 | matching_files.add(file_path) |
| 67 | break |
| 68 | |
| 69 | output_line = "" |
| 70 | |
| 71 | # Add file name prefix if multiple files |
| 72 | if multiple_files: |
| 73 | output_line += f"{file_path}:" |
| 74 | |
| 75 | # Add line number if requested |
| 76 | if line_numbers: |
| 77 | output_line += f"{line_num}:" |
| 78 | |
| 79 | output_line += line |
| 80 | results.append(output_line) |
| 81 | |
| 82 | # Edge Case: File has no matches but -l flag is used |
| 83 | if files_only and file_has_match: |
| 84 | matching_files.add(file_path) |
| 85 | |
| 86 | # Edge Case: -l flag with no matches |
| 87 | if files_only: |
| 88 | return "\n".join(sorted(matching_files)) + ("\n" if matching_files else "") |
| 89 | |
| 90 | # Edge Case: No matches found |
| 91 | return "".join(results) |
| 92 | |
| 93 | # Handled Edge Cases: Empty pattern, No files provided, File cannot be read, Only file names requested, File has no matches but -l flag is used, -l flag with no matches, No matches found |