yahoo

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 flags that customize the 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 file_names_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 # Prepare pattern for comparison
29 search_pattern = pattern.lower() if case_insensitive else pattern
30
31 # Results storage
32 results = []
33 matching_files = set()
34
35 # Process each file
36 for file_path in files:
37 try:
38 with open(file_path, 'r', encoding='utf-8') as file:
39 lines = file.readlines()
40 except FileNotFoundError:
41 # Edge Case: File not found - skip the file
42 continue
43 except IOError:
44 # Edge Case: File cannot be read - skip the file
45 continue
46
47 # Process each line in the file
48 for line_num, line in enumerate(lines, start=1):
49 # Prepare line for comparison
50 compare_line = line.lower() if case_insensitive else line
51
52 # Determine if line matches
53 if exact_match:
54 # Edge Case: Exact match mode - match entire line (excluding newline)
55 line_content = compare_line.rstrip('\n\r')
56 is_match = (line_content == search_pattern)
57 else:
58 # Edge Case: Substring match mode - check if pattern is in line
59 is_match = (search_pattern in compare_line)
60
61 # Apply invert flag
62 if invert_match:
63 # Edge Case: Invert match - lines that do NOT match are selected
64 is_match = not is_match
65
66 # If line matches criteria
67 if is_match:
68 # Edge Case: File names only mode - collect file names but not lines
69 if file_names_only:
70 matching_files.add(file_path)
71 # Break early since we only need to know the file matches
72 break
73 else:
74 # Build output line
75 output_line = ""
76
77 # Add file name if multiple files
78 # Edge Case: Multiple files - prepend file name to each line
79 if len(files) > 1:
80 output_line += f"{file_path}:"
81
82 # Add line number if requested
83 # Edge Case: Line numbers requested - prepend line number
84 if line_numbers:
85 output_line += f"{line_num}:"
86
87 # Add the actual line content
88 output_line += line
89
90 results.append(output_line)
91
92 # Return appropriate result based on flags
93 if file_names_only:
94 # Edge Case: File names only mode - return sorted file names
95 return "\n".join(sorted(matching_files)) + ("\n" if matching_files else "")
96 else:
97 # Edge Case: Normal mode - return matching lines
98 return "".join(results)
99
100# Handled Edge Cases: File not found, File cannot be read, Exact match mode, Substring match mode, Invert match, File names only mode, Multiple files, Line numbers requested, Normal mode
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.