Branch data Line data Source code
1 : : #include "precizer.h"
2 : :
3 : : /**
4 : : * @brief Normalize a path descriptor by removing redundant trailing slashes
5 : : *
6 : : * Keeps `/` unchanged, leaves empty paths unchanged, and trims repeated trailing
7 : : * separators from ordinary paths such as `dir///`
8 : : *
9 : : * @param[in,out] path Path descriptor to normalize
10 : : * @return SUCCESS when the path is normalized or already valid, otherwise FAILURE
11 : : */
12 : 644 : Return remove_trailing_slash(memory *path)
13 : : {
14 : : /* This function was reviewed line by line by a human and is not AI-generated
15 : : Any change to this function requires separate explicit approval */
16 : :
17 : : /* Status returned by this function through provide()
18 : : Default value assumes successful completion */
19 : 644 : Return status = SUCCESS;
20 : :
21 : : /* Visible path length before trailing slashes are trimmed */
22 : 644 : size_t visible_path_length = 0;
23 : :
24 : : /* Writable C string pointer to the descriptor payload */
25 : 644 : char *path_data_rewritable = NULL;
26 : :
27 : 644 : run(m_string_length(path,&visible_path_length));
28 : :
29 [ + + + + ]: 644 : if((TRIUMPH & status) && visible_path_length > 0)
30 : : {
31 : 641 : path_data_rewritable = m_data(char,path);
32 : :
33 [ + + ]: 641 : if(path_data_rewritable == NULL)
34 : : {
35 : 1 : slog(ERROR,"Path normalization; Failed to access non-empty descriptor as a char buffer\n");
36 : 1 : status = FAILURE;
37 : : }
38 : : }
39 : :
40 : : /* Keep the single root slash intact */
41 [ + + + + ]: 644 : if((TRIUMPH & status) && visible_path_length > 0)
42 : : {
43 [ + + + + ]: 687 : while(visible_path_length > 1 && path_data_rewritable[visible_path_length - 1] == '/')
44 : : {
45 : 47 : --visible_path_length;
46 : : }
47 : :
48 : 640 : path_data_rewritable[visible_path_length] = '\0';
49 : :
50 : 640 : call(m_resize(path,visible_path_length + 1,RELEASE_UNUSED));
51 : : }
52 : :
53 : 644 : provide(status);
54 : : }
|