| 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 | # Prepare pattern for comparison |
| 29 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 30 | |
| 31 | # Results storage |
| 32 | results = [] |
| 33 | matching_files = set() |
| 34 | |
| 35 | # Process each file |
| 36 | for filename in files: |
| 37 | try: |
| 38 | with open(filename, 'r', encoding='utf-8') as f: |
| 39 | lines = f.readlines() |
| 40 | except FileNotFoundError: |
| 41 | # Edge Case: File not found - skip the file |
| 42 | continue |
| 43 | except Exception: |
| 44 | # Edge Case: Other file reading errors - skip the file |
| 45 | continue |
| 46 | |
| 47 | # Process each line in the file |
| 48 | for line_num, line in enumerate(lines, 1): |
| 49 | # Prepare line for comparison |
| 50 | compare_line = line.lower() if case_insensitive else line |
| 51 | |
| 52 | # Determine if line matches |
| 53 | if exact_match: |
| 54 | matches = search_pattern == compare_line.rstrip('\n') |
| 55 | else: |
| 56 | matches = search_pattern in compare_line |
| 57 | |
| 58 | # Apply invert flag |
| 59 | if invert_match: |
| 60 | matches = not matches |
| 61 | |
| 62 | # If line matches, collect results |
| 63 | if matches: |
| 64 | matching_files.add(filename) |
| 65 | |
| 66 | # If we only need filenames, we can break early from line processing |
| 67 | if filenames_only: |
| 68 | break |
| 69 | |
| 70 | # Build output line |
| 71 | output_line = "" |
| 72 | |
| 73 | # Add filename prefix if multiple files |
| 74 | if len(files) > 1: |
| 75 | output_line += f"{filename}:" |
| 76 | |
| 77 | # Add line number if requested |
| 78 | if line_numbers: |
| 79 | output_line += f"{line_num}:" |
| 80 | |
| 81 | # Add the actual line content (without trailing newline) |
| 82 | output_line += line.rstrip('\n') |
| 83 | |
| 84 | results.append(output_line) |
| 85 | |
| 86 | # If only filenames requested, return them |
| 87 | if filenames_only: |
| 88 | # Edge Case: No matching files - return empty string |
| 89 | # Return files in the order they were processed, not sorted |
| 90 | ordered_files = [f for f in files if f in matching_files] |
| 91 | return "\n".join(ordered_files) if ordered_files else "" |
| 92 | |
| 93 | # Edge Case: No matches found - return empty string |
| 94 | return "\n".join(results) if results else "" |
| 95 | |
| 96 | # Handled Edge Cases: File not found, other file reading errors, no matching files, no matches found |