| 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 for customizing 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. |
| 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 - flags can be combined like "-ni" or space-separated like "-n -i" |
| 21 | flag_list = [] |
| 22 | if flags: |
| 23 | # Handle both space-separated and combined flags |
| 24 | if ' ' in flags: |
| 25 | flag_list = flags.split() |
| 26 | else: |
| 27 | # Split combined flags like "-ni" into ["-n", "-i"] |
| 28 | flag_list = ['-' + char for char in flags if char != '-'] |
| 29 | |
| 30 | line_numbers = '-n' in flag_list |
| 31 | filenames_only = '-l' in flag_list |
| 32 | case_insensitive = '-i' in flag_list |
| 33 | invert_match = '-v' in flag_list |
| 34 | exact_match = '-x' in flag_list |
| 35 | |
| 36 | # Edge Case: No files provided |
| 37 | if not files: |
| 38 | return "" |
| 39 | |
| 40 | results = [] |
| 41 | matched_files = set() |
| 42 | |
| 43 | # Prepare pattern for case-insensitive matching |
| 44 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 45 | |
| 46 | # Edge Case: Pattern is empty |
| 47 | if not pattern: |
| 48 | # For empty pattern, with -x flag, only empty lines match |
| 49 | # Without -x flag, all lines contain empty pattern |
| 50 | pass |
| 51 | |
| 52 | for file_path in files: |
| 53 | try: |
| 54 | with open(file_path, 'r', encoding='utf-8') as f: |
| 55 | lines = f.readlines() |
| 56 | except FileNotFoundError: |
| 57 | # Edge Case: File not found - skip the file |
| 58 | continue |
| 59 | except Exception: |
| 60 | # Edge Case: Other file reading errors - skip the file |
| 61 | continue |
| 62 | |
| 63 | file_has_match = False |
| 64 | |
| 65 | for i, line in enumerate(lines, start=1): |
| 66 | # Prepare line for comparison |
| 67 | compare_line = line.lower() if case_insensitive else line |
| 68 | |
| 69 | # Determine if line matches |
| 70 | if exact_match: |
| 71 | matches = (compare_line.rstrip('\n') == search_pattern) |
| 72 | else: |
| 73 | matches = (search_pattern in compare_line) |
| 74 | |
| 75 | # Apply invert flag |
| 76 | if invert_match: |
| 77 | matches = not matches |
| 78 | |
| 79 | if matches: |
| 80 | file_has_match = True |
| 81 | |
| 82 | # Edge Case: Empty pattern with -x flag - only truly empty lines match |
| 83 | if not pattern and exact_match and line.strip() != '': |
| 84 | file_has_match = False |
| 85 | continue |
| 86 | |
| 87 | if filenames_only: |
| 88 | # For -l flag, we only need to know the file has a match |
| 89 | break |
| 90 | |
| 91 | output_line = line |
| 92 | |
| 93 | # Build output with prefixes |
| 94 | if len(files) > 1: |
| 95 | if line_numbers: |
| 96 | output_line = f"{file_path}:{i}:{output_line.rstrip('\n')}" |
| 97 | else: |
| 98 | output_line = f"{file_path}:{output_line.rstrip('\n')}" |
| 99 | else: |
| 100 | if line_numbers: |
| 101 | output_line = f"{i}:{output_line.rstrip('\n')}" |
| 102 | else: |
| 103 | output_line = output_line.rstrip('\n') |
| 104 | |
| 105 | results.append(output_line.rstrip('\n')) |
| 106 | |
| 107 | if file_has_match and filenames_only: |
| 108 | results.append(file_path) |
| 109 | |
| 110 | # Edge Case: No matches found |
| 111 | return "\n".join(results) + ("\n" if results else "") |
| 112 | |
| 113 | # Handled Edge Cases: No files provided, File not found, Other file reading errors, Empty pattern, Empty pattern with -x flag" |