| 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: All matching lines, formatted according to the flags. |
| 12 | |
| 13 | Flags: |
| 14 | -n: Prepend line numbers. |
| 15 | -l: Output only file names with matches. |
| 16 | -i: Case-insensitive matching. |
| 17 | -v: Invert match (find non-matching lines). |
| 18 | -x: Match entire line only. |
| 19 | """ |
| 20 | # Parse flags - handle both space-separated and combined flags |
| 21 | flag_list = [] |
| 22 | if flags: |
| 23 | # Handle combined flags like "-ni" as well as space-separated flags |
| 24 | i = 0 |
| 25 | while i < len(flags): |
| 26 | if flags[i] == '-': |
| 27 | i += 1 |
| 28 | while i < len(flags) and flags[i] != ' ': |
| 29 | if flags[i] in 'nlixv': |
| 30 | flag_list.append('-' + flags[i]) |
| 31 | i += 1 |
| 32 | else: |
| 33 | i += 1 |
| 34 | line_numbers = '-n' in flag_list |
| 35 | files_only = '-l' in flag_list |
| 36 | case_insensitive = '-i' in flag_list |
| 37 | invert_match = '-v' in flag_list |
| 38 | exact_match = '-x' in flag_list |
| 39 | |
| 40 | results = [] |
| 41 | matched_files = set() |
| 42 | |
| 43 | # Edge Case: No files provided |
| 44 | if not files: |
| 45 | return "" |
| 46 | |
| 47 | # Edge Case: Pattern is empty |
| 48 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 49 | |
| 50 | for file_path in files: |
| 51 | try: |
| 52 | with open(file_path, 'r', encoding='utf-8') as file: |
| 53 | lines = file.readlines() |
| 54 | except FileNotFoundError: |
| 55 | # Edge Case: File not found - skip the file |
| 56 | continue |
| 57 | except Exception: |
| 58 | # Edge Case: Other file reading errors - skip the file |
| 59 | continue |
| 60 | |
| 61 | file_has_match = False |
| 62 | |
| 63 | # Edge Case: Empty file |
| 64 | if not lines: |
| 65 | # For empty files, there are no lines to match, so we just continue |
| 66 | # If -v is specified, an empty file should technically return all its (zero) lines |
| 67 | # But since there are no lines, nothing is added to results |
| 68 | pass |
| 69 | |
| 70 | for i, line in enumerate(lines, 1): # 1-indexed line numbers |
| 71 | # Prepare line for comparison |
| 72 | compare_line = line.rstrip('\n') # Remove newline for matching |
| 73 | if case_insensitive: |
| 74 | compare_line = compare_line.lower() |
| 75 | |
| 76 | # Determine if line matches |
| 77 | if exact_match: |
| 78 | is_match = (compare_line == search_pattern) |
| 79 | else: |
| 80 | is_match = (search_pattern in compare_line) |
| 81 | |
| 82 | # Apply invert flag |
| 83 | if invert_match: |
| 84 | is_match = not is_match |
| 85 | |
| 86 | if is_match: |
| 87 | file_has_match = True |
| 88 | |
| 89 | # Edge Case: -l flag only needs to know if file has a match, not the actual lines |
| 90 | if files_only: |
| 91 | matched_files.add(file_path) |
| 92 | # We can break early since we only need to know the file matches |
| 93 | break |
| 94 | |
| 95 | # Build output line |
| 96 | output_line = "" |
| 97 | |
| 98 | # Add filename prefix if multiple files |
| 99 | if len(files) > 1: |
| 100 | output_line += f"{file_path}:" |
| 101 | |
| 102 | # Add line number if requested |
| 103 | if line_numbers: |
| 104 | output_line += f"{i}:" |
| 105 | |
| 106 | # Add the original line content |
| 107 | output_line += line # Keep original line with its newline |
| 108 | |
| 109 | results.append(output_line) |
| 110 | |
| 111 | # Edge Case: -l flag and this file had matches |
| 112 | if files_only and file_has_match: |
| 113 | matched_files.add(file_path) |
| 114 | |
| 115 | # Edge Case: -l flag - return only file names |
| 116 | if files_only: |
| 117 | # Return files in the order they were provided |
| 118 | output_files = [f for f in files if f in matched_files] |
| 119 | return "\n".join(output_files) + ("\n" if output_files else "") |
| 120 | |
| 121 | # Edge Case: No matches found |
| 122 | if not results: |
| 123 | return "" |
| 124 | |
| 125 | # Join results without removing trailing newlines |
| 126 | output = "".join(results) |
| 127 | |
| 128 | return output |
| 129 | |
| 130 | # Handled Edge Cases: No files provided, Pattern is empty, File not found, Other file reading errors, Empty file, -l flag only needs to know if file has a match, -l flag and this file had matches, No matches found, Remove trailing newline if it exists |