| 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 (str): The string to search for. |
| 7 | flags (str): Space-separated flags that customize the search behavior. |
| 8 | files (list[str]): List of file paths to search in. |
| 9 | |
| 10 | Returns: |
| 11 | str: Matching lines, possibly with file names and line numbers prepended. |
| 12 | |
| 13 | Flags: |
| 14 | -n: Prepend line numbers. |
| 15 | -l: Output only file names. |
| 16 | -i: Case-insensitive matching. |
| 17 | -v: Invert matching. |
| 18 | -x: Match entire line only. |
| 19 | """ |
| 20 | # Parse flags |
| 21 | flag_list = flags.split() if flags else [] |
| 22 | line_numbers = '-n' in flag_list |
| 23 | filenames_only = '-l' in flag_list |
| 24 | case_insensitive = '-i' in flag_list |
| 25 | invert_match = '-v' in flag_list |
| 26 | exact_match = '-x' in flag_list |
| 27 | |
| 28 | # Edge Case: Empty pattern |
| 29 | if not pattern: |
| 30 | pattern = '' |
| 31 | |
| 32 | # Edge Case: No files provided |
| 33 | if not files: |
| 34 | return '' |
| 35 | |
| 36 | # Prepare pattern for comparison |
| 37 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 38 | |
| 39 | result_lines = [] |
| 40 | matching_files = set() |
| 41 | |
| 42 | multiple_files = len(files) > 1 |
| 43 | |
| 44 | for file_path in files: |
| 45 | try: |
| 46 | with open(file_path, 'r', encoding='utf-8') as file: |
| 47 | lines = file.readlines() |
| 48 | except FileNotFoundError: |
| 49 | # Edge Case: File not found - skip the file |
| 50 | continue |
| 51 | except Exception: |
| 52 | # Edge Case: Other file reading errors - skip the file |
| 53 | continue |
| 54 | |
| 55 | for line_num, line in enumerate(lines, start=1): |
| 56 | # Prepare line for comparison |
| 57 | compare_line = line.lower() if case_insensitive else line |
| 58 | |
| 59 | # Determine if line matches |
| 60 | if exact_match: |
| 61 | matches = search_pattern == compare_line.rstrip('\n') |
| 62 | else: |
| 63 | matches = search_pattern in compare_line |
| 64 | |
| 65 | # Apply invert flag |
| 66 | if invert_match: |
| 67 | matches = not matches |
| 68 | |
| 69 | if matches: |
| 70 | matching_files.add(file_path) |
| 71 | |
| 72 | # If we only need filenames, we can break early |
| 73 | if filenames_only: |
| 74 | break |
| 75 | |
| 76 | output_line = '' |
| 77 | |
| 78 | # Add filename prefix if multiple files |
| 79 | if multiple_files: |
| 80 | output_line += f'{file_path}:' |
| 81 | |
| 82 | # Add line number if requested |
| 83 | if line_numbers: |
| 84 | output_line += f'{line_num}:' |
| 85 | |
| 86 | # Add the actual line |
| 87 | output_line += line |
| 88 | |
| 89 | result_lines.append(output_line) |
| 90 | |
| 91 | # Edge Case: No matches found |
| 92 | if filenames_only: |
| 93 | return '\n'.join(sorted(matching_files)) + ('\n' if matching_files else '') |
| 94 | |
| 95 | # Edge Case: Empty result |
| 96 | return ''.join(result_lines) |
| 97 | |
| 98 | # Handled Edge Cases: Empty pattern, no files provided, file not found, other file reading errors, no matches found, empty result |