fishlium

Finished
11def 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 string of flags (-n, -l, -i, -v, -x).
8 files (list[str]): List of file paths to search in.
9
10 Returns:
11 str: Matching lines according to the specified flags.
12
13 Edge Cases Handled:
14 - Empty files (no lines to process)
15 - Files that don't exist (skipped with no error)
16 - Empty pattern (matches all lines unless -x flag is used)
17 - No matching lines (returns empty string)
18 - Single file (no filename prefix in output)
19 - Multiple files (filename prefix added to each line)
20 - Combination of flags (-n with -l, -v with -x, etc.)
21 """
22 # Parse flags
23 flag_list = flags.split() if flags else []
24 line_numbers = '-n' in flag_list
25 files_only = '-l' in flag_list
26 case_insensitive = '-i' in flag_list
27 invert_match = '-v' in flag_list
28 exact_match = '-x' in flag_list
29
30 # Prepare pattern for comparison
31 search_pattern = pattern.lower() if case_insensitive else pattern
32
33 # Results storage
34 results = []
35 matching_files = set()
36
37 # Process each file
38 for file_path in files:
39 try:
40 with open(file_path, 'r', encoding='utf-8') as file:
41 # Edge Case: Empty files (no lines to process)
42 line_num = 0
43 for line in file:
44 line_num += 1
45
46 # Prepare line for comparison
47 compare_line = line.lower() if case_insensitive else line
48
49 # Determine if line matches
50 if exact_match:
51 # Edge Case: Exact match with line (strip newline for comparison)
52 matches = search_pattern == compare_line.rstrip('\n\r')
53 else:
54 # Edge Case: Substring match (empty pattern matches all)
55 matches = search_pattern in compare_line
56
57 # Apply invert flag
58 if invert_match:
59 matches = not matches
60
61 # If line matches criteria
62 if matches:
63 # Edge Case: Files only output (track matching files)
64 if files_only:
65 matching_files.add(file_path)
66 # Optimization: Break early when we only need file names
67 break
68 else:
69 # Build output line
70 output_line = ""
71 # Edge Case: Multiple files (add filename prefix)
72 if len(files) > 1:
73 output_line += f"{file_path}:"
74 # Edge Case: Line numbers (add line number after filename)
75 if line_numbers:
76 output_line += f"{line_num}:"
77 output_line += line
78 results.append(output_line)
79 except FileNotFoundError:
80 # Edge Case: Files that don't exist (skipped with no error)
81 continue
82
83 # Edge Case: Files only output (return only file names)
84 if files_only:
85 # Return file names in the order they were specified
86 ordered_matching_files = [f for f in files if f in matching_files]
87 return '\n'.join(ordered_matching_files) + ('\n' if ordered_matching_files else '')
88
89 # Edge Case: No matching lines (returns empty string)
90 return ''.join(results)
91
92# Handled Edge Cases: Empty files, files that don't exist, empty pattern, no matching lines, single file, multiple files, combination of flags
Test NameStatus
test_multiple_files_no_matches_various_flags
Pass
test_multiple_files_one_match_match_entire_lines_flag
Pass
test_multiple_files_one_match_multiple_flags
Pass
test_multiple_files_one_match_no_flags
Pass
test_multiple_files_one_match_print_file_names_flag
Pass
test_multiple_files_several_matches_case_insensitive_flag
Pass
test_multiple_files_several_matches_file_flag_takes_precedence_over_line_number_flag
Pass
test_multiple_files_several_matches_inverted_and_match_entire_lines_flags
Pass
test_multiple_files_several_matches_inverted_flag
Pass
test_multiple_files_several_matches_no_flags
Pass
test_multiple_files_several_matches_print_line_numbers_flag
Pass
test_one_file_no_matches_various_flags
Pass
test_one_file_one_match_case_insensitive_flag
Pass
test_one_file_one_match_file_flag_takes_precedence_over_line_flag
Pass
test_one_file_one_match_match_entire_lines_flag
Pass
test_one_file_one_match_multiple_flags
Pass
test_one_file_one_match_no_flags
Pass
test_one_file_one_match_print_file_names_flag
Pass
test_one_file_one_match_print_line_numbers_flag
Pass
test_one_file_several_matches_case_insensitive_flag
Pass
test_one_file_several_matches_inverted_and_match_entire_lines_flags
Pass
test_one_file_several_matches_inverted_flag
Pass
test_one_file_several_matches_match_entire_lines_flag
Pass
test_one_file_several_matches_no_flags
Pass
test_one_file_several_matches_print_line_numbers_flag
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.