| 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 | results = [] |
| 33 | matching_files = set() |
| 34 | |
| 35 | # Prepare pattern for case-insensitive matching |
| 36 | search_pattern = pattern.lower() if case_insensitive else pattern |
| 37 | |
| 38 | # Edge Case: Pattern is empty |
| 39 | if not pattern: |
| 40 | # For empty pattern, in exact match mode, only empty lines match |
| 41 | # In non-exact mode, all lines contain the empty string |
| 42 | pass |
| 43 | |
| 44 | for file_path in files: |
| 45 | try: |
| 46 | with open(file_path, 'r', encoding='utf-8') as file: |
| 47 | lines = file.readlines() |
| 48 | except FileNotFoundError: |
| 49 | # Edge Case: File not found - skip the file |
| 50 | continue |
| 51 | except Exception: |
| 52 | # Edge Case: Other file reading errors - skip the file |
| 53 | continue |
| 54 | |
| 55 | file_has_match = False |
| 56 | |
| 57 | for line_num, line in enumerate(lines, 1): |
| 58 | # Remove trailing newline for processing |
| 59 | line_content = line.rstrip('\n') |
| 60 | |
| 61 | # Determine if line matches |
| 62 | if case_insensitive: |
| 63 | line_to_check = line_content.lower() |
| 64 | else: |
| 65 | line_to_check = line_content |
| 66 | |
| 67 | if exact_match: |
| 68 | is_match = (search_pattern == line_to_check) |
| 69 | else: |
| 70 | is_match = (search_pattern in line_to_check) |
| 71 | |
| 72 | # Apply invert flag |
| 73 | if invert_match: |
| 74 | is_match = not is_match |
| 75 | |
| 76 | if is_match: |
| 77 | file_has_match = True |
| 78 | |
| 79 | # If we only need filenames, we can break early |
| 80 | if filenames_only: |
| 81 | matching_files.add(file_path) |
| 82 | break |
| 83 | |
| 84 | # Build output line |
| 85 | output_parts = [] |
| 86 | |
| 87 | # Add filename if multiple files |
| 88 | if len(files) > 1: |
| 89 | output_parts.append(file_path) |
| 90 | |
| 91 | # Add line number if requested |
| 92 | if line_numbers: |
| 93 | # If we have a filename, add colon, otherwise just the number |
| 94 | if output_parts: |
| 95 | output_parts.append(f":{line_num}") |
| 96 | else: |
| 97 | output_parts.append(str(line_num)) |
| 98 | |
| 99 | # Add colon separator if we have filename or line number |
| 100 | if output_parts: |
| 101 | output_parts.append(":") |
| 102 | |
| 103 | # Add the actual line content |
| 104 | output_parts.append(line_content) |
| 105 | |
| 106 | results.append(''.join(output_parts)) |
| 107 | |
| 108 | # If we're only looking for filenames and this file had a match |
| 109 | if filenames_only and file_has_match: |
| 110 | matching_files.add(file_path) |
| 111 | |
| 112 | # Edge Case: No matches found |
| 113 | if filenames_only: |
| 114 | return '\n'.join(sorted(matching_files)) |
| 115 | |
| 116 | # Edge Case: No results to return |
| 117 | if not results: |
| 118 | return "" |
| 119 | |
| 120 | return '\n'.join(results) |
| 121 | # Handled Edge Cases: No files provided, File not found, Other file reading errors, Pattern is empty, No matches found, No results to return |