| 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 | # Edge Case: Pattern is empty |
| 33 | if not pattern: |
| 34 | case_insensitive = False # No need for case insensitivity with empty pattern |
| 35 | |
| 36 | # Prepare pattern for comparison |
| 37 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 38 | |
| 39 | results = [] |
| 40 | matching_files = set() |
| 41 | |
| 42 | # Process each file |
| 43 | for filename in files: |
| 44 | try: |
| 45 | with open(filename, 'r', encoding='utf-8') as file: |
| 46 | lines = file.readlines() |
| 47 | |
| 48 | # Edge Case: File is empty or unreadable |
| 49 | if not lines: |
| 50 | # For empty files, we still need to check if an empty line would match |
| 51 | if pattern == "" and not invert_match: |
| 52 | if filenames_only: |
| 53 | matching_files.add(filename) |
| 54 | else: |
| 55 | if len(files) > 1: |
| 56 | results.append(f"{filename}:") |
| 57 | else: |
| 58 | results.append("") |
| 59 | continue |
| 60 | |
| 61 | # Process each line |
| 62 | for line_num, line in enumerate(lines, 1): |
| 63 | # Remove newline for processing but keep for output |
| 64 | line_content = line.rstrip('\n') |
| 65 | |
| 66 | # Determine if line matches |
| 67 | if case_insensitive: |
| 68 | line_to_check = line_content.lower() |
| 69 | else: |
| 70 | line_to_check = line_content |
| 71 | |
| 72 | if exact_match: |
| 73 | is_match = (line_to_check == search_pattern) |
| 74 | else: |
| 75 | is_match = (search_pattern in line_to_check) |
| 76 | |
| 77 | # Apply invert flag |
| 78 | if invert_match: |
| 79 | is_match = not is_match |
| 80 | |
| 81 | # Edge Case: Handle empty pattern with -x flag |
| 82 | if pattern == "" and exact_match and not invert_match: |
| 83 | is_match = (line_content == "") |
| 84 | |
| 85 | if is_match: |
| 86 | if filenames_only: |
| 87 | matching_files.add(filename) |
| 88 | break # No need to check more lines in this file |
| 89 | else: |
| 90 | output_line = "" |
| 91 | if len(files) > 1: |
| 92 | output_line += f"{filename}:" |
| 93 | if line_numbers: |
| 94 | output_line += f"{line_num}:" |
| 95 | output_line += line_content |
| 96 | results.append(output_line) |
| 97 | except FileNotFoundError: |
| 98 | # Edge Case: File not found - skip silently as per typical grep behavior |
| 99 | continue |
| 100 | except PermissionError: |
| 101 | # Edge Case: Permission denied - skip silently as per typical grep behavior |
| 102 | continue |
| 103 | except Exception: |
| 104 | # Edge Case: Other file reading errors - skip silently |
| 105 | continue |
| 106 | |
| 107 | # Edge Case: -l flag requires only filenames |
| 108 | if filenames_only: |
| 109 | # Maintain order of files as they appeared in the input |
| 110 | ordered_results = [f for f in files if f in matching_files] |
| 111 | return "\n".join(ordered_results) |
| 112 | |
| 113 | return "\n".join(results) |
| 114 | |
| 115 | # Handled Edge Cases: No files provided, empty pattern, empty files, file not found, permission denied, other file reading errors, empty pattern with -x flag |