| 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 | # Parse flags - handle combined flags like "-ni" as separate flags |
| 14 | flag_list = [] |
| 15 | if flags: |
| 16 | # Remove leading dash and split into individual flag characters |
| 17 | clean_flags = flags.lstrip('-') |
| 18 | for flag_char in clean_flags: |
| 19 | flag_list.append(f'-{flag_char}') |
| 20 | |
| 21 | line_numbers = '-n' in flag_list |
| 22 | filenames_only = '-l' in flag_list |
| 23 | case_insensitive = '-i' in flag_list |
| 24 | invert_match = '-v' in flag_list |
| 25 | exact_match = '-x' in flag_list |
| 26 | |
| 27 | # Edge Case: Empty pattern |
| 28 | if not pattern: |
| 29 | pattern_to_use = '' |
| 30 | else: |
| 31 | pattern_to_use = pattern.lower() if case_insensitive else pattern |
| 32 | |
| 33 | results = [] |
| 34 | matching_files = set() |
| 35 | |
| 36 | # Edge Case: No files provided |
| 37 | if not files: |
| 38 | return "" |
| 39 | |
| 40 | # Edge Case: Empty files list |
| 41 | for file_path in files: |
| 42 | try: |
| 43 | with open(file_path, 'r', encoding='utf-8') as file: |
| 44 | lines = file.readlines() |
| 45 | except FileNotFoundError: |
| 46 | # Edge Case: File not found |
| 47 | continue |
| 48 | |
| 49 | file_has_match = False |
| 50 | |
| 51 | # Edge Case: Empty file |
| 52 | for line_num, line in enumerate(lines, start=1): |
| 53 | line_content = line.rstrip('\n') |
| 54 | |
| 55 | # Determine if line matches |
| 56 | if case_insensitive: |
| 57 | line_to_check = line_content.lower() |
| 58 | else: |
| 59 | line_to_check = line_content |
| 60 | |
| 61 | if exact_match: |
| 62 | is_match = (pattern_to_use == line_to_check) |
| 63 | else: |
| 64 | is_match = (pattern_to_use in line_to_check) |
| 65 | |
| 66 | if invert_match: |
| 67 | is_match = not is_match |
| 68 | |
| 69 | if is_match: |
| 70 | file_has_match = True |
| 71 | |
| 72 | if filenames_only: |
| 73 | # We only need to know the file has a match |
| 74 | matching_files.add(file_path) |
| 75 | break |
| 76 | else: |
| 77 | output_line = "" |
| 78 | # Add filename if multiple files |
| 79 | if len(files) > 1: |
| 80 | output_line += f"{file_path}:" |
| 81 | # Add line number if requested |
| 82 | if line_numbers: |
| 83 | output_line += f"{line_num}:" |
| 84 | # Add the actual line content |
| 85 | output_line += line_content |
| 86 | results.append(output_line) |
| 87 | |
| 88 | if filenames_only and file_has_match: |
| 89 | matching_files.add(file_path) |
| 90 | |
| 91 | # Edge Case: -l flag with matches |
| 92 | if filenames_only: |
| 93 | result = "\n".join(sorted(matching_files)) |
| 94 | return result + "\n" if result else "" |
| 95 | |
| 96 | # Edge Case: No matches found |
| 97 | result = "\n".join(results) |
| 98 | return result + "\n" if result else "" |
| 99 | |
| 100 | # Handled Edge Cases: Empty pattern, No files provided, Empty files list, File not found, Empty file, -l flag with matches, No matches found |