| 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 modify 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: No files provided |
| 29 | if not files: |
| 30 | return "" |
| 31 | |
| 32 | results = [] |
| 33 | matching_files = set() |
| 34 | |
| 35 | # Edge Case: Pattern is empty |
| 36 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 37 | |
| 38 | for file_path in files: |
| 39 | try: |
| 40 | with open(file_path, 'r', encoding='utf-8') as file: |
| 41 | lines = file.readlines() |
| 42 | |
| 43 | # Edge Case: File is empty or cannot be read |
| 44 | if not lines and filenames_only and file_path not in matching_files: |
| 45 | continue |
| 46 | |
| 47 | for line_num, line in enumerate(lines, 1): |
| 48 | # Prepare line for comparison |
| 49 | compare_line = line.rstrip('\n') |
| 50 | |
| 51 | # Determine if line matches |
| 52 | if exact_match: |
| 53 | if case_insensitive: |
| 54 | matches = (compare_line.lower() == search_pattern) |
| 55 | else: |
| 56 | matches = (compare_line == search_pattern) |
| 57 | else: |
| 58 | if case_insensitive: |
| 59 | matches = (search_pattern in compare_line.lower()) |
| 60 | else: |
| 61 | matches = (search_pattern in compare_line) |
| 62 | |
| 63 | # Apply invert flag |
| 64 | if invert_match: |
| 65 | matches = not matches |
| 66 | |
| 67 | # Edge Case: Line matches pattern |
| 68 | if matches: |
| 69 | if filenames_only: |
| 70 | matching_files.add(file_path) |
| 71 | # No need to check other lines in this file |
| 72 | break |
| 73 | else: |
| 74 | output_line = "" |
| 75 | # Add filename if multiple files |
| 76 | if len(files) > 1: |
| 77 | output_line += f"{file_path}:" |
| 78 | # Add line number if requested |
| 79 | if line_numbers: |
| 80 | output_line += f"{line_num}:" |
| 81 | # Add the actual line |
| 82 | output_line += line.rstrip('\n') + '\n' |
| 83 | results.append(output_line) |
| 84 | except FileNotFoundError: |
| 85 | # Edge Case: File not found |
| 86 | if not filenames_only: |
| 87 | raise |
| 88 | except IOError: |
| 89 | # Edge Case: Other I/O error |
| 90 | if not filenames_only: |
| 91 | raise |
| 92 | |
| 93 | # Edge Case: Only filenames requested |
| 94 | if filenames_only: |
| 95 | # Return files in the order they were provided |
| 96 | output_files = [f for f in files if f in matching_files] |
| 97 | return "\n".join(output_files) + ("\n" if output_files else "") |
| 98 | |
| 99 | # Edge Case: No matches found |
| 100 | if not results: |
| 101 | return "" |
| 102 | |
| 103 | # Join all results. Each line already includes its newline |
| 104 | return "".join(results) |
| 105 | |
| 106 | # Handled Edge Cases: No files provided, empty pattern, file is empty, file not found, other I/O errors, line matches pattern, only filenames requested, no matches found |