xas

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 formatted according to the flags
12 """
13 # Parse flags
14 flag_list = flags.split() if flags else []
15 line_numbers = '-n' in flag_list
16 files_only = '-l' in flag_list
17 case_insensitive = '-i' in flag_list
18 invert_match = '-v' in flag_list
19 exact_match = '-x' in flag_list
20
21 results = []
22 matched_files = set()
23
24 # Edge Case: Empty pattern should match empty lines or be handled consistently
25 # Edge Case: No files provided should return empty result
26 if not files:
27 return ""
28
29 for file_path in files:
30 try:
31 with open(file_path, 'r', encoding='utf-8') as file:
32 lines = file.readlines()
33 except FileNotFoundError:
34 # Edge Case: File not found - skip the file
35 continue
36
37 # Edge Case: File cannot be read - handled by try/except
38
39 file_has_match = False
40
41 for i, line in enumerate(lines, 1):
42 # Remove newline for comparison but keep it for output
43 line_content = line.rstrip('\n')
44
45 # Apply case insensitivity if needed
46 search_pattern = pattern.lower() if case_insensitive else pattern
47 search_line = line_content.lower() if case_insensitive else line_content
48
49 # Determine if line matches
50 if exact_match:
51 is_match = search_pattern == search_line
52 else:
53 is_match = search_pattern in search_line
54
55 # Apply invert flag
56 if invert_match:
57 is_match = not is_match
58
59 if is_match:
60 file_has_match = True
61
62 # Edge Case: When -l flag is used, we only need to know which files match
63 if files_only:
64 matched_files.add(file_path)
65 break # No need to check more lines in this file
66
67 # Build output line
68 output_line = ""
69
70 # Add filename prefix if multiple files
71 # Edge Case: Single file should not have filename prefix
72 if len(files) > 1:
73 output_line += f"{file_path}:"
74
75 # Add line number if requested
76 if line_numbers:
77 output_line += f"{i}:"
78
79 output_line += line
80 results.append(output_line)
81
82 # Edge Case: Handle -l flag after processing each file
83 if files_only and file_has_match:
84 matched_files.add(file_path)
85
86 # Edge Case: When -l flag is used, return only file names
87 if files_only:
88 return "\n".join(sorted(list(matched_files))) + ("\n" if matched_files else "")
89
90 return "".join(results)
91
92# Handled Edge Cases: Empty pattern, no files provided, file not found, file cannot be read, single file (no filename prefix), -l flag usage
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.