| 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: String containing zero or more 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 - handle both separate and combined flags |
| 14 | line_numbers = '-n' in flags or 'n' in flags.replace('-', '') |
| 15 | files_only = '-l' in flags or 'l' in flags.replace('-', '') |
| 16 | case_insensitive = '-i' in flags or 'i' in flags.replace('-', '') |
| 17 | invert_match = '-v' in flags or 'v' in flags.replace('-', '') |
| 18 | exact_match = '-x' in flags or 'x' in flags.replace('-', '') |
| 19 | |
| 20 | |
| 21 | |
| 22 | # Edge Case: Empty pattern |
| 23 | if not pattern: |
| 24 | pattern_to_match = '' |
| 25 | else: |
| 26 | pattern_to_match = pattern.lower() if case_insensitive else pattern |
| 27 | |
| 28 | result_lines = [] |
| 29 | matching_files = set() |
| 30 | |
| 31 | # Edge Case: No files provided |
| 32 | if not files: |
| 33 | return "" |
| 34 | |
| 35 | multiple_files = len(files) > 1 |
| 36 | |
| 37 | for file_path in files: |
| 38 | try: |
| 39 | # Edge Case: File cannot be opened/read |
| 40 | with open(file_path, 'r', encoding='utf-8') as file: |
| 41 | lines = file.readlines() |
| 42 | except FileNotFoundError: |
| 43 | # Edge Case: File not found - skip silently as per typical grep behavior |
| 44 | continue |
| 45 | except IOError: |
| 46 | # Edge Case: Other I/O error - skip silently |
| 47 | continue |
| 48 | |
| 49 | file_has_match = False |
| 50 | |
| 51 | for line_num, line in enumerate(lines, 1): |
| 52 | # Prepare line for comparison |
| 53 | line_to_check = line |
| 54 | if case_insensitive: |
| 55 | line_to_check = line.lower() |
| 56 | |
| 57 | |
| 58 | # Determine if line matches |
| 59 | if exact_match: |
| 60 | matches = pattern_to_match == line_to_check.rstrip('\n\r') |
| 61 | else: |
| 62 | matches = pattern_to_match in line_to_check |
| 63 | |
| 64 | # Apply invert flag |
| 65 | if invert_match: |
| 66 | matches = not matches |
| 67 | |
| 68 | if matches: |
| 69 | file_has_match = True |
| 70 | |
| 71 | # For -l flag, we don't add lines to result_lines, but we track that this file has a match |
| 72 | if files_only: |
| 73 | file_has_match = True |
| 74 | # Don't break - continue checking all lines to properly handle -v flag |
| 75 | else: |
| 76 | # Build output line |
| 77 | output_line = "" |
| 78 | |
| 79 | # Add filename prefix if multiple files |
| 80 | if multiple_files: |
| 81 | output_line += f"{file_path}:" |
| 82 | |
| 83 | # Add line number if requested |
| 84 | if line_numbers: |
| 85 | output_line += f"{line_num}:" |
| 86 | |
| 87 | # Add the actual line content |
| 88 | output_line += line |
| 89 | |
| 90 | result_lines.append(output_line) |
| 91 | |
| 92 | # Edge Case: Files only mode |
| 93 | if files_only: |
| 94 | # With -v flag, we want files that DON'T contain the pattern |
| 95 | # Without -v flag, we want files that DO contain the pattern |
| 96 | if invert_match: |
| 97 | # With -v flag, add file if it has NO matches |
| 98 | if not file_has_match: |
| 99 | matching_files.add(file_path) |
| 100 | else: |
| 101 | # Without -v flag, add file if it has matches |
| 102 | if file_has_match: |
| 103 | matching_files.add(file_path) |
| 104 | |
| 105 | # Edge Case: Files only mode - return file names |
| 106 | if files_only: |
| 107 | return "\n".join(sorted(list(matching_files))) + ("\n" if matching_files else "") |
| 108 | |
| 109 | # Edge Case: No matches found |
| 110 | if not result_lines: |
| 111 | return "" |
| 112 | |
| 113 | return "".join(result_lines) |
| 114 | |
| 115 | # Handled Edge Cases: Empty pattern, No files provided, File cannot be opened/read, File not found, Other I/O error, Only file names requested, Files only mode, No matches found |